Beispiel #1
0
        public async Task <ActionResult> UsersAdd_Partial(AddModel model)
        {
            if (!ModelState.IsValid)
            {
                return(PartialView(model));
            }

            using (UserDefinitionDataProvider dataProvider = new UserDefinitionDataProvider()) {
                LoginConfigData config = await LoginConfigDataProvider.GetConfigAsync();

                switch (config.RegistrationType)
                {
                default:
                case RegistrationTypeEnum.NameAndEmail: {     // Email == model.Email
                    List <DataProviderFilterInfo> filters = DataProviderFilterInfo.Join(null, new DataProviderFilterInfo {
                            Field = nameof(UserDefinition.Email), Operator = "==", Value = model.Email,
                        });
                    UserDefinition userExists = await dataProvider.GetItemAsync(filters);

                    if (userExists != null)
                    {
                        ModelState.AddModelError(nameof(model.Email), this.__ResStr("emailUsed", "An account using email address {0} already exists.", model.Email));
                        return(PartialView(model));
                    }
                    break;
                }

                case RegistrationTypeEnum.EmailOnly:
                    model.UserName = model.Email;
                    break;

                case RegistrationTypeEnum.NameOnly:
                    model.UserName = model.Email;
                    break;
                }

                UserDefinition user = model.GetData();

                string hashedNewPassword;
#if MVC6
                IPasswordHasher <UserDefinition> passwordHasher = (IPasswordHasher <UserDefinition>)YetaWFManager.ServiceProvider.GetService(typeof(IPasswordHasher <UserDefinition>));
                hashedNewPassword = passwordHasher.HashPassword(user, model.Password);
#else
                UserManager <UserDefinition> userManager = Managers.GetUserManager();
                hashedNewPassword = userManager.PasswordHasher.HashPassword(model.Password);
#endif
                user.PasswordPlainText = config.SavePlainTextPassword ? model.Password : null;
                user.PasswordHash      = hashedNewPassword;

                if (!await dataProvider.AddItemAsync(user))
                {
                    throw new Error(this.__ResStr("alreadyExists", "A user named \"{0}\" already exists."), model.UserName);
                }
                return(FormProcessed(model, this.__ResStr("okSaved", "New user saved"), OnPopupClose: OnPopupCloseEnum.ReloadModule));
            }
        }
Beispiel #2
0
        //~UserStore() { Dispose(false); }

        // IUserStore
        // IUserStore
        // IUserStore

        public async Task <IdentityResult> CreateAsync(UserDefinition user, CancellationToken cancellationToken)
        {
            using (UserDefinitionDataProvider dataProvider = new UserDefinitionDataProvider(this.CurrentSiteIdentity)) {
                bool status = await dataProvider.AddItemAsync(user);

                if (!status)
                {
                    throw new Error(this.__ResStr("userExists", "User {0} already exists.", user.UserName));
                }
                return(IdentityResult.Success);
            }
        }