3

在我的WPF 桌面示例书店应用程序中,我想管理用户和角色。有多个用户我想达到以下几点

1) 应用程序应该有多个用户
2) 用户有 3 个类别 a) 管理员 b) 经理 c) 员工
3) 应用程序可以有多个角色,例如添加书籍、销售书籍、更新库存、生成采购订单等
4) 用户应该能够分配和删除层次结构中较低的其他用户的角色。理想的用户层次结构就像:-

a) 管理员 - TOP 拥有完全权限
b) 经理 - 由管理员添加和删除角色
c) 员工 - 由经理/管理员添加和删除角色。

我需要方法来实现它。方法应该灵活,以便将来角色和用户添加/删除将很容易;无需更改数据库结构和额外的代码行。高级经理可以轻松地将角色分配给单个员工。

4

2 回答 2

1

首先,我将您的“类别”称为“角色”,将您当前的“角色”称为“特权”,那么您将需要下表用户、角色、特权、用户角色和用户特权。然后围绕链接表中的记录构建所有应用程序逻辑。那里有一些非常有用的会员提供程序(取决于您的数据库),您可以实施它们而不是从头开始。然后你可能只需要添加权限表和过程。

提供程序示例视频 WPF

于 2010-07-23T16:42:53.690 回答
0

假设您正在开发一个 asp.net 应用程序,如果您想为用户分配角色并创建角色。您必须在角色控制器上实现以下代码。

 //[Authorize(Roles = "Admin")]

    public class RolesController : Controller
    {
        RolesBusiness rb = new RolesBusiness();
        ApplicationDbContext con = new ApplicationDbContext();


        // GET: Roles
        public ActionResult Index()
        {
            return View(rb.AllRoles());
        }

        // Is Admin
        public int IsAdmin(string Id)
        {
            ViewBag.Users = new SelectList(con.Users, "Id", "FullName");

            Session["UserId"] = Id;

            List<RolesView> roleslist = rb.RolesForThisUser(Id);

            if (roleslist != null)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }

        [HttpGet]
        public ActionResult AddRole()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddRole(string name)
        {

            if (name == "")
            {
                ViewBag.Result = "Please enter Role Name.";
            }

            else
            {
                bool found = rb.RoleExists(name);

                if (found == true)
                {
                    ViewBag.Result = "Role name " + name + " already exists.";
                }

                else
                {
                    rb.CreateRole(name);

                    ViewBag.Result = "Role created successfully.";
                    RedirectToAction("UsersInRole");

                    //  return Json(new { success = true, message = "Saved Successfully" }, JsonRequestBehavior.AllowGet);
                }
            }

            return View();
        }



        [HttpGet]
        public ActionResult UsersInRole()
        {
            ViewBag.Roles = new SelectList(con.appRoles, "Name", "Name");

            try
            {
                ViewBag.Feed = Session["feedack"].ToString();
            }

            catch (Exception x)
            {

            }
            return View();
        }

        [HttpPost]
        public ActionResult UsersInRole(string Id)
        {
            ViewBag.Roles = new SelectList(con.appRoles, "Name", "Name");

            List<UsersView> list = new List<UsersView>();

            if (Id == "")
            {
                ViewBag.Result = "Please select a role.";
                return View();
            }

            list = rb.UsersInRole(Id);


            if (list.Count == 0)
            {
                ViewBag.Result = "No users in this role.";
                return View();
            }

            ViewBag.Count = "[" + list.Count + "] Users found.";

            Session["RoleId"] = Id;
            Session["feedack"] = "";

            return View(list);
        }

        public ActionResult UnassignUsersInRole(string userId)
        {
            string roleId = Session["RoleId"].ToString();

            string feed = rb.UnassignFromRole(userId, roleId);

            Session["feedack"] = feed;

            return RedirectToAction("UsersInRole");
        }



        [HttpGet]
        public ActionResult AddUserToRole()
        {
            ViewBag.Users = new SelectList(con.Users, "Id", "Email");
            ViewBag.Roles = new SelectList(con.appRoles, "Name", "Name");

            return View();
        }

        [HttpPost]
        public ActionResult AddUserToRole(string Id, string Name)
        {
            ViewBag.Users = new SelectList(con.Users, "Id", "Email");
            ViewBag.Roles = new SelectList(con.appRoles, "Name", "Name");

            if (Id != "" && Name != null)
            {
                if (rb.IsUserInRole(Id, Name) == false)
                {
                    rb.AddUserToRole(Id, Name);
                    ViewBag.Result = "User successfully assigned a role!";
                }

                else
                {
                    ViewBag.Result = "User is already in selected Role!";
                }
            }

            else
            {
                ViewBag.Result = "Please select Username and Rolename!";
            }

            return View();
        }



        [HttpGet]
        public ActionResult RolesForThisUser()
        {
            ViewBag.Users = new SelectList(con.Users, "Id", "FullName");

            try
            {
                ViewBag.Feed = Session["feed"].ToString();
            }

            catch (Exception c)
            {

            }

            return View();
        }

        [HttpPost]
        public ActionResult RolesForThisUser(string Id)
        {
            ViewBag.Users = new SelectList(con.Users, "Id", "FullName");

            Session["UserId"] = Id;

            List<RolesView> roleslist = rb.RolesForThisUser(Id);

            if (roleslist == null)
            {
                ViewBag.Result = "This User isn't assigned any Role!";
                return View();
            }

            ViewBag.Count = "[" + roleslist.Count + "] Role(s) found!";

            return View(roleslist);
        }



        public ActionResult RemoveFromRole(string id)
        {
            string userid = Session["UserId"].ToString();

            string feed = "";

            try
            {
                if (userid != null && id != null)
                {
                    feed = rb.UnassignFromRole(userid, id);
                }
            }

            catch (Exception x)
            {
                ViewBag.Result = "Please select User.";
            }

            Session["feed"] = feed;


            return RedirectToAction("RolesForThisUser");
        }
    }
于 2019-09-15T14:17:01.883 回答