示例#1
0
 public IHttpActionResult AddRole(MvcRole role)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     try
     {
         RoleManagerBso.AddRole(role);
         return(Ok(role.Id));
     }
     catch (DbUpdateException dbex)
     {
         var internalEx = dbex.InnerException?.InnerException;
         if (internalEx != null)
         {
             if (internalEx is SqlException)
             {
                 var sqlEx = internalEx as SqlException;
                 if (sqlEx.Number == 2601)
                 {
                     throw new System.Exception(string.Format("{0} already exists. Please use another name.", role.Name));
                 }
             }
         }
         throw new System.Exception("Add new role failed.Please try again.");
     }
 }
        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            var       roleManagerBso = new RoleManagerBso();
            MvcAction action         = new MvcAction
            {
                ActionName     = actionContext.ActionDescriptor.ActionName,
                ControllerName = actionContext.ControllerContext.Controller.ToString(),
                ParameterTypes = actionContext.ActionDescriptor.GetParameters().Select(p => p.ParameterType.ToString()),
                ReturnType     = actionContext.ActionDescriptor.ReturnType.ToString()
            };
            //Get id of roles that are assigned to this action
            var dbRoles = roleManagerBso.GetRolesByAction(action)?.Select(r => r.Name);

            //if no role assigned to this action, it means all roles can have access to this action
            if (dbRoles == null)
            {
                return(true);
            }
            var identity = (actionContext.RequestContext.Principal.Identity as ClaimsIdentity);
            //Get the role claims that are attached to current identity
            var claimRoles = identity.Claims.Where(c => c.Type == identity.RoleClaimType).Select(r => r.Value);

            //Check if two sets of roles have something in common.
            return(dbRoles.Intersect <string>(claimRoles).Count() > 0);
        }
示例#3
0
 public IHttpActionResult AddRolesToAction(MvcAction mvcAction)
 {
     try
     {
         RoleManagerBso.AddRolesToAction(mvcAction);
         return(Ok());
     }
     catch (Exception e)
     {
         return(InternalServerError(e));
     }
 }
示例#4
0
 public IHttpActionResult AddActionsToRole(MvcRole role)
 {
     try
     {
         RoleManagerBso.AddActionsToRole(role);
         return(Ok());
     }
     catch (Exception e)
     {
         return(InternalServerError(e));
     }
 }
示例#5
0
 public IHttpActionResult UpdateRole(MvcRole role)
 {
     if (ModelState.IsValid)
     {
         RoleManagerBso.UpdateRole(role);
         return(Ok());
     }
     else
     {
         return(BadRequest(ModelState));
     }
 }
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var roleManagerBso = new RoleManagerBso();

            //Get id of roles that are assigned to this action
            var dbRoles = roleManagerBso.GetRolesByAction(action)?.Select(r => r.Name);

            //if no role assigned to this action, it means all roles can have access to this action
            if (dbRoles == null)
            {
                return(true);
            }
            var identity = (httpContext.User.Identity as ClaimsIdentity);
            //Get the role claims that are attached to current identity
            var claimRoles = identity.Claims.Where(c => c.Type == identity.RoleClaimType).Select(r => r.Value);

            //Check if two sets of roles have something in common.
            return(dbRoles.Intersect <string>(claimRoles).Count() > 0);
        }
示例#7
0
 public IHttpActionResult DeleteRole(MvcRole role)
 {
     RoleManagerBso.DeleteRole(role);
     return(Ok());
 }
示例#8
0
 public List <MvcController> GetControllers()
 {
     return(RoleManagerBso.GetControllers());
 }
示例#9
0
 public List <Models.Action> GetActionsByRole(string id)
 {
     return(RoleManagerBso.GetActionsByRole(id));
 }
示例#10
0
 public List <MvcRole> GetRolesByAction(MvcAction mvcAction)
 {
     return(RoleManagerBso.GetRolesByAction(mvcAction));
 }
示例#11
0
 public List <ApplicationRole> GetRoles()
 {
     return(RoleManagerBso.GetRoles());
 }