コード例 #1
0
        public async Task <IHttpActionResult> RemoveFromRole(string userId, string roleId)
        {
            var appUser = await AppUserManager.FindByIdAsync(userId);

            if (appUser == null)
            {
                ModelState.AddModelError("", "User does not exist");
                return(BadRequest(ModelState));
            }

            var role = await AppRoleManager.FindByIdAsync(roleId);

            if (role == null)
            {
                ModelState.AddModelError("", "Role does not exist");
                return(BadRequest(ModelState));
            }

            var result = await AppUserManager.RemoveFromRoleAsync(userId, role.Name);

            if (!result.Succeeded)
            {
                GetIdentityErrorResult(result);
                return(BadRequest(ModelState));
            }

            return(Ok());
        }
コード例 #2
0
        public static MvcHtmlString GetRoleName(this HtmlHelper html, string id)
        {
            AppRoleManager mgr
                = HttpContext.Current.GetOwinContext().GetUserManager <AppRoleManager>();

            return(new MvcHtmlString(mgr.FindByIdAsync(id).Result.Name));
        }
コード例 #3
0
        public async Task <ActionResult> Edit(string id)
        {
            AppRole role = await RoleManager.FindByIdAsync(id);

            string[] memberIDs               = role.Users.Select(x => x.UserId).ToArray();
            IEnumerable <AppUser> members    = _userManager.Users.Where(x => memberIDs.Any(y => y == x.Id));
            IEnumerable <AppUser> nonMembers = _userManager.Users.Except(members);
            RoleEditViewModel     model      = new RoleEditViewModel
            {
                Role       = role,
                Members    = members,
                NonMembers = nonMembers
            };

            return(View(model));
        }
コード例 #4
0
        public async Task <IActionResult> Edit(Role model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            Role role = await _roleManager.FindByIdAsync(model.Id.ToString());

            role.Name = Regex.Replace(model.Name, @"\s", "").Trim();

            role.NormalizedName = Regex.Replace(model.Name, @"\s", "").Trim().ToUpper();
            _dbContext.Roles.Update(role);

            await _dbContext.SaveChangesAsync();

            return(RedirectToRoute("GetRoles"));
        }
コード例 #5
0
        public async Task <ActionResult> Delete(string id)
        {
            var role = await _roleManager.FindByIdAsync(id);

            if (role == null)
            {
                return(View("Error", new[] { "Role Not Found" }));
            }

            var result = await _roleManager.DeleteAsync(role);

            if (result.Succeeded)
            {
                return(RedirectToAction("Index"));
            }

            return(View("Error", result.Errors));
        }
コード例 #6
0
        public async Task <IHttpActionResult> ManageUsersInRole(UsersInRoleModel model)
        {
            var role = await AppRoleManager.FindByIdAsync(model.Id);

            if (role == null)
            {
                ModelState.AddModelError("", "Role does not exist");
                return(BadRequest(ModelState));
            }

            foreach (string user in model.EnrolledUsers)
            {
                var appUser = await AppUserManager.FindByIdAsync(user);

                if (appUser == null)
                {
                    ModelState.AddModelError("", String.Format("User: {0} does not exists", user));
                    continue;
                }

                if (!AppUserManager.IsInRole(user, role.Name))
                {
                    IdentityResult result = await AppUserManager.AddToRoleAsync(user, role.Name);

                    if (!result.Succeeded)
                    {
                        ModelState.AddModelError("", String.Format("User: {0} could not be added to role", user));
                    }
                }
            }

            foreach (string user in model.RemovedUsers)
            {
                var appUser = await AppUserManager.FindByIdAsync(user);

                if (appUser == null)
                {
                    ModelState.AddModelError("", String.Format("User: {0} does not exists", user));
                    continue;
                }

                IdentityResult result = await AppUserManager.RemoveFromRoleAsync(user, role.Name);

                if (!result.Succeeded)
                {
                    ModelState.AddModelError("", String.Format("User: {0} could not be removed from role", user));
                }
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            return(Ok());
        }
コード例 #7
0
        public async Task <IHttpActionResult> GetRole(string Id)
        {
            var role = await AppRoleManager.FindByIdAsync(Id);

            if (role != null)
            {
                return(Ok(TheModelFactory.Create(role)));
            }
            return(NotFound());
        }
コード例 #8
0
        public async Task <IHttpActionResult> GetRole(string Id)
        {
            var role = await AppRoleManager.FindByIdAsync(Id);

            if (role != null)
            {
                return(Ok(role.ToDto()));
            }

            return(NotFound());
        }
コード例 #9
0
        public async Task <IHttpActionResult> GetRole(string Id)
        {
            var role = await AppRoleManager.FindByIdAsync(Id);

            if (role != null)
            {
                return(Ok(_mapper.Map <RoleViewModel>(role)));
            }

            return(NotFound());
        }
コード例 #10
0
ファイル: RolesController.cs プロジェクト: MrRevalis/Library
        public async Task <ActionResult> EditRole(string id)
        {
            AppRole appRole = await RoleManager.FindByIdAsync(id);

            if (appRole == null)
            {
                ViewBag.Error = $"Error occurred while looking for role with ID = {id}";
                return(View("Error"));
            }
            else
            {
                string[] membersID                      = appRole.Users.Select(x => x.UserId).ToArray();
                IEnumerable <AppUser> members           = UserManager.Users.Where(x => membersID.Any(y => y == x.Id));
                EditRoleViewModel     editRoleViewModel = new EditRoleViewModel
                {
                    Role    = appRole,
                    Members = members
                };
                return(View(editRoleViewModel));
            }
        }
        public async Task <IActionResult> Remove(string roleId)
        {
            var role = await _roleManager.FindByIdAsync(roleId);

            if (role == null)
            {
                return(NotFound());
            }

            _dbContext.Roles.Remove(role);

            await _dbContext.SaveChangesAsync();

            return(RedirectToRoute("GetRoles"));
        }
コード例 #12
0
        public async Task <IHttpActionResult> DeleteRole(string Id)
        {
            var role = await AppRoleManager.FindByIdAsync(Id);

            if (role != null)
            {
                IdentityResult result = await AppRoleManager.DeleteAsync(role);

                if (!result.Succeeded)
                {
                    GetErrorResult(result);
                }
                return(Ok());
            }

            return(NotFound());
        }
コード例 #13
0
        public async Task <IHttpActionResult> AddUserToRole([FromBody] RoleBindingModel userData)
        {
            var role = await AppRoleManager.FindByIdAsync(userData.RoleId);

            if (role != null)
            {
                IdentityResult result = await AppUserManager.AddToRoleAsync(userData.UserId, role.Name);

                if (!result.Succeeded)
                {
                    return(GetErrorResult(result));
                }

                return(Ok());
            }

            return(NotFound());
        }
コード例 #14
0
        public async Task <HttpResponseMessage> Delete(HttpRequestMessage request, string id)
        {
            var role = await AppRoleManager.FindByIdAsync(id);

            var listPermission = _permissionService.GetByRoleId(id);

            if (listPermission.Count > 0)
            {
                _permissionService.Delete(id);
            }
            var result = await AppRoleManager.DeleteAsync(role);

            if (result.Succeeded)
            {
                return(request.CreateResponse(HttpStatusCode.OK, "Xóa thành công" + nameof(id)));
            }
            else
            {
                return(request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Join(",", result.Errors)));
            }
        }
コード例 #15
0
        public async Task <IHttpActionResult> UpdateUsersInRoles([FromBody] IEnumerable <UserInRoleModel> usersInRole)
        {
            if (usersInRole != null && usersInRole.Count() > 0)
            {
                IdentityRole role = null;
                foreach (var userInRole in usersInRole)
                {
                    if (role == null)
                    {
                        role = await AppRoleManager.FindByIdAsync(userInRole.RoleId);
                    }
                    if (role != null)
                    {
                        if (userInRole.IsInRole)
                        {
                            if (!AppUserManager.GetRoles(userInRole.UserId).Contains(role.Name))
                            {
                                await AppUserManager.AddToRoleAsync(userInRole.UserId, role.Name);
                            }
                        }
                        else
                        {
                            if (AppUserManager.GetRoles(userInRole.UserId).Contains(role.Name))
                            {
                                await AppUserManager.RemoveFromRoleAsync(userInRole.UserId, role.Name);
                            }
                        }
                    }
                    else
                    {
                        return(BadRequest("Role not found."));
                    }
                }
                return(Ok());
            }

            return(NotFound());
        }
コード例 #16
0
        public async Task <IHttpActionResult> ManageUsersInRole(UsersInRoleModel model)
        {
            var role = await AppRoleManager.FindByIdAsync(model.Id);

            if (role == null)
            {
                ModelState.AddModelError("", "Role dos not exist");
                return(BadRequest(ModelState));
            }

            foreach (var user in model.EnrolledUsers)
            {
                var appUser = await AppUserManager.FindByIdAsync(user);

                if (appUser == null)
                {
                    ModelState.AddModelError("", $"User dos not exist {user}");
                    continue;
                }

                if (!AppUserManager.IsInRole(user, role.Name))
                {
                    IdentityResult result = await AppUserManager.AddToRoleAsync(user, role.Name);

                    if (!result.Succeeded)
                    {
                        ModelState.AddModelError("User", $" User can't not added to role {user}");
                    }
                }
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            return(Ok());
        }
コード例 #17
0
        public async Task <RepositoryResult <RoleInterfaceModel> > GetRoleByIdForInterfaceAsync(int roleId)
        {
            try
            {
                if (roleId <= 0)
                {
                    return(new RepositoryResult <RoleInterfaceModel>(HttpStatusCode.BadRequest));
                }

                var role = await AppRoleManager.FindByIdAsync(roleId);

                if (role == null)
                {
                    return(new RepositoryResult <RoleInterfaceModel>(HttpStatusCode.NotFound));
                }

                return(new RepositoryResult <RoleInterfaceModel>(HttpStatusCode.OK, Mapper.Map <RoleInterfaceModel>(role)));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #18
0
 public async Task <ApplicationRole> FindByIdAsync(int roleId)
 {
     return(await _roleManager.FindByIdAsync(roleId));
 }