3

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.

Cloud Spider
  • 195
  • 8

2 Answers2

1

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

jumpdart
  • 1,702
  • 16
  • 35
0

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");
        }
    }
MaartenDev
  • 5,631
  • 5
  • 21
  • 33