Пример #1
0
        /// <summary>
        /// 设置用户权限
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        private async Task <OperationResponse> SetUserRoles(User user, Guid?[] roleIds, bool isAdd = true)
        {
            IList <string> existRoleNames = await _userManager.GetRolesAsync(user);

            try
            {
                IdentityResult result = await _userManager.RemoveFromRolesAsync(user, existRoleNames);

                if (!result.Succeeded)
                {
                    return(result.ToOperationResponse());
                }
                var roles = await _roleManager.Roles.Where(m => roleIds.Contains(m.Id)).ToListAsync();

                IList <string> roleNames = roles.Select(m => m.Name).ToList();

                result = await _userManager.AddToRolesAsync(user, roleNames);

                if (!result.Succeeded)
                {
                    return(result.ToOperationResponse());
                }
                await _userManager.UpdateSecurityStampAsync(user);

                await _bus?.PublishAsync(new UserRoleCacheAddOrUpdateEvent()
                {
                    User = user, Roles = roles, EventState = isAdd ? Events.EventState.Add : Events.EventState.Update
                });;
            }
            catch (InvalidOperationException ex)
            {
                return(new OperationResponse(ex.Message, OperationResponseType.Error));
            }
            return(new OperationResponse("添加用户角色成功", OperationResponseType.Success));
        }
Пример #2
0
        /// <summary>
        /// 设置用户权限
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        private async Task <OperationResponse> SetUserRoles(User user, Guid[] roleIds)
        {
            IList <string> existRoleNames = await _userManager.GetRolesAsync(user);

            try
            {
                IdentityResult result = await _userManager.RemoveFromRolesAsync(user, existRoleNames);

                if (!result.Succeeded)
                {
                    return(result.ToOperationResponse());
                }
                IList <string> roleNames = await _roleManager.Roles.Where(m => roleIds.Contains(m.Id)).Select(m => m.Name).ToListAsync();


                result = await _userManager.AddToRolesAsync(user, roleNames);

                if (!result.Succeeded)
                {
                    return(result.ToOperationResponse());
                }
                await _userManager.UpdateSecurityStampAsync(user);
            }
            catch (InvalidOperationException ex)
            {
                return(new OperationResponse(ex.Message, OperationResponseType.Error));
            }
            return(new OperationResponse("添加用户角色成功", OperationResponseType.Success));
        }
Пример #3
0
        private async Task <OperationResponse> DeleteUserRoleAsync(User user)
        {
            IList <string> existRoleNames = await _userManager.GetRolesAsync(user);

            IdentityResult result = await _userManager.RemoveFromRolesAsync(user, existRoleNames);

            if (!result.Succeeded)
            {
                return(result.ToOperationResponse());
            }
            return(result.ToOperationResponse());
        }
Пример #4
0
        private async Task <OperationResponse> DeleteUserRoleAsync(User user)
        {
            IList <string> existRoleNames = await _userManager.GetRolesAsync(user);

            IdentityResult result = await _userManager.RemoveFromRolesAsync(user, existRoleNames);

            if (!result.Succeeded)
            {
                return(result.ToOperationResponse());
            }
            //_bus?.PublishAsync(new UserRoleCacheDeleteEvent() { UserId = user.Id, EventState = Events.EventState.Remove });
            return(result.ToOperationResponse());
        }