Exemplo n.º 1
0
        public IdentityResult UpdateMember(Member oldMember, string oldPassword = null, string newPassword = null)
        {
            //不是最佳做法,没有事务回滚
            //更新
            IdentityResult result;

            if (!string.IsNullOrEmpty(oldPassword) && !string.IsNullOrEmpty(newPassword))
            {
                result = _userManager.ChangePassword(oldMember.Id, oldPassword, newPassword);
                if (!result.Succeeded)
                {
                    return(result);
                }
            }

            result = _userManager.Update(oldMember.GetUser(_userManager));
            if (!result.Succeeded)
            {
                return(result);
            }

            //更新扩展数据
            _currencyService.Update(oldMember.GetMemberExtension());

            Logger.Operation($"修改会员-{oldMember.UserName}:{oldMember.Id}", MemberBaseModule.Instance, SecurityLevel.Warning);
            //更新缓存
            _signals.Trigger(_userContainer.UserChangedSignalName);
            _signals.Trigger($"member_{oldMember.Id}_changed");

            return(result);
        }
Exemplo n.º 2
0
        public IdentityResult UpdateAdminUser(User oldUser, string oldPassword, string newPassword, List <string> roles, bool modifyRoles)
        {
            //不是最佳做法,没有事务回滚
            //更新
            oldUser.AllRoles.Clear();

            var result = _userManager.Update(oldUser);

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

            if (!string.IsNullOrEmpty(oldPassword) && !string.IsNullOrEmpty(newPassword))
            {
                result = _userManager.ChangePassword(oldUser.Id, oldPassword, newPassword);
                if (!result.Succeeded)
                {
                    return(result);
                }
            }

            if (modifyRoles)
            {
                var allAdminRoles = _roleManager.Roles.Select(r => r.Name).ToArray();
                foreach (var roleName in allAdminRoles)
                {
                    if (_userManager.IsInRole(oldUser.Id, roleName))
                    {
                        result = _userManager.RemoveFromRole(oldUser.Id, roleName);
                        if (!result.Succeeded)
                        {
                            return(result);
                        }
                    }
                }

                if (roles == null)
                {
                    roles = new List <string>();
                }

                roles.Add("admin");
                foreach (var roleName in roles)
                {
                    if (!_userManager.IsInRole(oldUser.Id, roleName))
                    {
                        result = _userManager.AddToRole(oldUser.Id, roleName);
                        if (!result.Succeeded)
                        {
                            return(result);
                        }
                    }
                }
            }

            Logger.Operation($"修改后台用户-{oldUser.UserName}:{oldUser.Id}", SystemUsersModule.Instance, SecurityLevel.Warning);
            //更新缓存
            _signals.Trigger(_userContainer.UserChangedSignalName);

            return(result);
        }