时间:2010-07-23 16:32:18

标签: security roles security-roles role-based

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

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

 //[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");
        }
    }