Exemplo n.º 1
0
        public IdentityResult CreateAdminUser(User user, string password, List <string> roles)
        {
            //不是最佳做法,没有事务回滚
            user.UserType = UserType.Admin;
            var result = _userManager.Create(user, password);

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

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

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

            return(result);
        }
Exemplo n.º 2
0
        public ActionResult RolesPost(UserManagerRolesViewModel model, string id)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction(nameof(HomeController.Index), "Home"));
            }

            if (string.IsNullOrEmpty(id))
            {
                return(RedirectToAction(nameof(HomeController.Index), "Home"));
            }
            else
            {
                if (model.Admin == true)
                {
                    DefaultUserManager.AddToRole(id, Admin);
                }
                else
                {
                    DefaultUserManager.RemoveFromRole(id, Admin);
                }

                if (model.ProjectManager == true)
                {
                    DefaultUserManager.AddToRole(id, ProjectManager);
                }
                else
                {
                    DefaultUserManager.RemoveFromRole(id, ProjectManager);
                }

                if (model.Developer == true)
                {
                    DefaultUserManager.AddToRole(id, Developer);
                }
                else
                {
                    DefaultUserManager.RemoveFromRole(id, Developer);
                }

                if (model.Submitter == true)
                {
                    DefaultUserManager.AddToRole(id, Submitter);
                }
                else
                {
                    DefaultUserManager.RemoveFromRole(id, Submitter);
                }

                return(RedirectToAction(nameof(UsersController.UserManager), "Users"));
            }
        }
Exemplo n.º 3
0
        public IdentityResult CreateMember(Member member, string password)
        {
            //不是最佳做法,没有事务回滚
            member.UserType     = UserType.Member;
            member.DynamicToken = KeyGenerator.GetGuidKey().ToString();
            var user   = member.GetUser();
            var result = _userManager.Create(user, password);

            if (result.Succeeded)
            {
                member.Id = user.Id;
                //插入扩展数据
                _currencyService.Create(member.GetMemberExtension());

                result = _userManager.AddToRole(member.Id, "member");
                Logger.Operation($"添加会员-{member.UserName}:{member.Id}", MemberBaseModule.Instance, SecurityLevel.Warning);
                //更新缓存
                _signals.Trigger(_userContainer.UserChangedSignalName);
                _signals.Trigger($"member_{member.Id}_changed");
            }

            return(result);
        }