public async Task ResetUsername(SystemUserResetUsernameModel model)
        {
            var systemUser = await GetItem(new Guid(model.Id));

            if (systemUser == null)
            {
                throw new UserNotFoundException();
            }

            if (string.Compare(_hashingService.DecryptString(systemUser.Password), model.ConfirmPassword, false) != 0)
            {
                throw new PasswordsDoNotMatchException();
            }
            if (await UsernameAlreadyExists(model.ResetUsername))
            {
                throw new UsernameAlreadyExistsException(model.ResetUsername);
            }

            //  TODO: All or nothing!
            //  NoSQL databases do not support updating the partition key value of an existing item. Need to delete entire document and create new.
            //  Transactions using the same partition key (but this process uses 2 different partition keys) ~ https://devblogs.microsoft.com/cosmosdb/introducing-transactionalbatch-in-the-net-sdk/
            await _systemUsersManager.DeleteItemAsync(systemUser);

            systemUser.Username = model.ResetUsername;

            var systemUserModel = new SystemUserAuthenticateModel(systemUser);

            await CreateItem(systemUserModel);
        }
        public async Task CreateItem(SystemUserAuthenticateModel model)
        {
            model = await Validate(model);

            var systemUser = new SystemAuthenticateUser(model);

            systemUser.Password = _hashingService.EncryptString(model.Password);
            await _systemUsersManager.CreateItemAsync(systemUser);
        }
Exemple #3
0
        public async Task <IActionResult> Create(SystemUserAuthenticateModel model)
        {
            try
            {
                await _systemUserService.CreateItem(model);

                response = new ApiResponse(HttpStatusCode.OK, "System user created successfully.", null);
                return(Ok(new { response }));
            }
            catch (Exception exception)
            {
                return(BadRequest("System user create failed. Error: " + exception.Message));
            }
        }
        private async Task <SystemUserAuthenticateModel> Validate(SystemUserAuthenticateModel model)
        {
            if (await UsernameAlreadyExists(model.Username))
            {
                throw new UsernameAlreadyExistsException(model.Username);
            }

            model.NameFirst = Helpers.ToTitleCase(model.NameFirst);
            model.NameLast  = Helpers.ToTitleCase(model.NameLast);

            model.EmailAddresses = await _systemEmailAddressService.Validate(model.EmailAddresses);

            model.PhoneNumbers = await _systemPhoneNumberService.Validate(model.PhoneNumbers);

            return(model);
        }
 public SystemAuthenticateUser(SystemUserAuthenticateModel model)
 {
     Id              = string.IsNullOrEmpty(model.Id) ? Guid.NewGuid().ToString() : model.Id;
     NamePrefix      = model.NamePrefix;
     NameFirst       = model.NameFirst;
     NameLast        = model.NameLast;
     NameSuffix      = model.NameSuffix;
     Username        = model.Username;
     Password        = model.Password;
     DisplayAs       = model.DisplayAs;
     ProfileImageUrl = model.ProfileImageUrl;
     MustChangePasswordAtNextLogin = model.MustChangePasswordAtNextLogin;
     PasswordExpirationDateTime    = string.IsNullOrEmpty(model.PasswordExpirationDateTime) ? DateTime.Now.AddMonths(3).ToShortDateString() : model.PasswordExpirationDateTime;
     CloneToAdminDatabase          = model.CloneToAdminDatabase;
     Enabled        = model.Enabled;
     EmailAddresses = SystemEmailAddress.Construct(model.EmailAddresses);
     PhoneNumbers   = SystemPhoneNumber.Construct(model.PhoneNumbers);
     Roles          = model.Roles;
 }