예제 #1
0
        override public int Execute(AddUserOptions opts)
        {
            if (opts.Claims.Any())
            {
                ClaimsHelper.Check(opts.Claims);
            }


            IdentityUser user = new IdentityUser {
                UserName = opts.UserName
            };

            _userManager.CreateAsync(user, opts.UserPassword).GetAwaiter().GetResult()
            .Check();

            foreach (var r in opts.Roles)
            {
                if (_roleManager.FindByNameAsync(r).Result == null)
                {
                    _roleManager.CreateAsync(new IdentityRole {
                        Name = r
                    }).GetAwaiter().GetResult();
                }
                _userManager.AddToRoleAsync(user, r).GetAwaiter().GetResult();
            }

            if (opts.Claims != null && opts.Claims.Any())
            {
                _userManager.AddClaimsAsync(user, ClaimsHelper.Parse(opts.Claims))
                .GetAwaiter().GetResult()
                .Check();
            }

            if (opts.UserEMail != null)
            {
                user.Email = opts.UserEMail;
            }

            if (opts.UserPhone != null)
            {
                user.PhoneNumber = opts.UserPhone;
            }

            _userManager.UpdateAsync(user).Result
            .Check();

            _consoleWriter.WriteUsers(new[] { user });

            return(0);
        }
예제 #2
0
        override public int Execute(UpdateUserOptions opts)
        {
            if (opts.Claims.Any())
            {
                ClaimsHelper.Check(opts.Claims);
            }

            IdentityUser user = _userManager.FindByNameAsync(opts.UserName).Result;

            if (user == null)
            {
                throw new Exception("User not found");
            }

            if (opts.NewUserName != null)
            {
                user.UserName = opts.NewUserName;
                _userManager.UpdateAsync(user).Result
                .Check();
            }

            if (opts.UserEmail != null)
            {
                user.Email = opts.UserEmail;
                _userManager.UpdateAsync(user).Result
                .Check();
            }

            if (opts.UserPhone != null)
            {
                user.Email = opts.UserPhone;
                _userManager.UpdateAsync(user).Result
                .Check();
            }

            if (opts.UserPassword != null)
            {
                var token = _userManager.GeneratePasswordResetTokenAsync(user).Result;
                _userManager.ResetPasswordAsync(user, token, opts.UserPassword).Result
                .Check();
            }

            if (opts.Roles != null && opts.Roles.Any())
            {
                _userManager.RemoveFromRolesAsync(user, _userManager.GetRolesAsync(user).Result).Result
                .Check();

                foreach (var r in opts.Roles)
                {
                    if (_roleManager.FindByNameAsync(r).Result == null)
                    {
                        _roleManager.CreateAsync(new IdentityRole {
                            Name = r
                        }).GetAwaiter().GetResult()
                        .Check();
                    }
                    _userManager.AddToRoleAsync(user, r).GetAwaiter().GetResult()
                    .Check();
                }
            }

            if (opts.Claims != null && opts.Claims.Any())
            {
                _userManager.RemoveClaimsAsync(user, _userManager.GetClaimsAsync(user).Result)
                .Result
                .Check();

                _userManager.AddClaimsAsync(user, ClaimsHelper.Parse(opts.Claims))
                .GetAwaiter().GetResult()
                .Check();
            }


            _consoleWriter.WriteUsers(new[] { user });

            return(0);
        }