管理用户和角色

发布于 2024-09-11 07:40:09 字数 428 浏览 9 评论 0原文

在我的 WPF Desktop 示例书店应用程序中,我想管理用户和角色。对于多个用户,我想实现以下几点

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

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

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

In my WPF Desktop sample Book Store application I want to manage Users and Roles. With multiple Users I want to achieve below points

1) Application Should have multiple user
2) User has 3 categories a) Admin b) Manager c) Employee
3) Application can have multiple roles like, add books, sale books, Update Stocks, Generate Purchase Order etc
4) User should be able to assign and remove roles of other user those are lower in herarchy. Ideal User herarchy is like :-

a) Admin - TOP having full Rights
b) Manager - Having roles added and removed by Admin
c) Employee - Having roles added and remover by Manager / Admin.

I need approach to implenet it. Approach should be flexible that In future Roles and User addition / Removal will be easy; without change of Database structure and extra line of codes. Higher manager can easily assign roles to individual employee.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-09-18 07:40:09

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

提供程序示例视频 WPF

First, I would refer to your "categories" as "roles" and your current "roles" as "privileges" then you would need the following tables user, roles, privileges, userroles, and userprivileges. Then build all your app logic around records in the link tables. There are some pretty useful membership providers out there(depending on your DB) which you can implement instead of doing it all from scratch. Then you would likely just need to add the privilege tables and procs.

Provider Example video WPF

情绪操控生活 2024-09-18 07:40:09

假设您正在开发一个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");
        }
    }

Assuming that you are developing an asp.net application, if you want to assign roles and create roles for users. You will have to implement the following code on your Roles controller.

 //[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");
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文