Пример #1
0
        /// <summary>
        ///     Create a new User
        /// </summary>
        private async Task <IActionResult> CreateUser(User newUser)
        {
            try
            {
                newUser.PasswordHash = newUser.TempPassword;
                if (newUser.RegistrationMethod.Type != CoreConst.RegistrationTypes.Application)
                {
                    newUser.PasswordHash = CoreFunc.PasswordGenerator(20, 5, 5, 5, 5);
                }
                ModelState.Clear();
                /// if model validation failed
                if (!TryValidateModel(newUser))
                {
                    AppFunc.ExtractErrors(ModelState, ref ErrorsList);
                    /// return bad request with all the errors
                    return(UnprocessableEntity(ErrorsList));
                }
                /// check the database to see if a user with the same email exists
                if (_DbContext.Users.Any(d => d.Email == newUser.Email))
                {
                    /// extract the errors and return bad request containing the errors
                    AppFunc.Error(ref ErrorsList, "Email already exists.");
                    return(StatusCode(412, ErrorsList));
                }
                /// Create the new user
                IdentityResult newUserResult = await _UserManager.CreateAsync(newUser, newUser.PasswordHash)
                                               .ConfigureAwait(false);

                /// If result failed
                if (!newUserResult.Succeeded)
                {
                    /// Add the error below to the error list and return bad request
                    foreach (var error in newUserResult.Errors)
                    {
                        AppFunc.Error(ref ErrorsList, error.Description, error.Code);
                    }
                    return(StatusCode(417, ErrorsList));
                }
                /// else result is successful the try to add the access claim for the user
                IdentityResult addedClaimResult = await _UserManager.AddClaimAsync(
                    newUser,
                    new Claim(AppConst.AccessClaims.Type, newUser.Role.AccessClaim)
                    ).ConfigureAwait(false);

                /// if claim failed to be created
                if (!addedClaimResult.Succeeded)
                {
                    /// remove the user account and return appropriate error
                    _DbContext.Users.Remove(newUser);
                    await _DbContext.SaveChangesAsync().ConfigureAwait(false);

                    AppFunc.Error(ref ErrorsList, AppConst.CommonErrors.ServerError);
                    return(StatusCode(417, ErrorsList));
                }

                isUserCreated = true;
                /// return 201 created status with the new object
                /// and success message
                return(Created("Success", newUser));
            }
            catch (Exception ee) // DbUpdateException, DbUpdateConcurrencyException
            {
                /// Add the error below to the error list and return bad request
                AppFunc.Error(ref ErrorsList, AppConst.CommonErrors.ServerError);
                return(StatusCode(417, ErrorsList));
            }
        }