Exemplo n.º 1
0
        public async Task <(Result Result, string UserId)> CreateUserAsync(string userToken,
                                                                           string userName,
                                                                           string password)
        {
            logger.LogInformation("Start creating {userName} user with token {userToken}.", userName, userToken);

            //The identifier is not verified on the ASP.NET Identity side
            (Result findResult, ApplicationUser user) = await FindUserByTokenAsync(userToken);

            if (findResult.Succeeded)
            {
                Result result = Result.ValidationError(new InnerError(ErrorTarget.DuplicateUserToken, "This user token is already taken."));

                logger.LogError("Error creating user with token {userToken}. Errors: {@message}", userToken, result.Errors);

                return(result, user.Id);
            }

            user = new ApplicationUser(userToken)
            {
                UserName = userName
            };

            IdentityResult identityResult = await userManager.CreateAsync(user, password);

            if (!identityResult.Succeeded)
            {
                Result result = identityResult.ToApplicationResult();

                logger.LogError("Error creating user with token {userToken}. Errors: {@message}", userToken, result.Errors);

                return(result, user.Id);
            }

            logger.LogInformation("Successfully created {userName} user with token {userToken}.", userName, userToken);

            var userRoleName = Roles.User.ToString("G");

            identityResult = await userManager.AddToRoleAsync(user, userRoleName);

            if (!identityResult.Succeeded)
            {
                Result result = identityResult.ToApplicationResult();

                logger.LogError("Error on assignment role {userRoleName} for user with token {userToken}. Errors: {@message}",
                                userRoleName,
                                userToken,
                                result.Errors);

                return(result, user.Id);
            }

            logger.LogInformation("Successfully assigned role {userRoleName} for {userName} user with token {userToken}.",
                                  userRoleName,
                                  userName,
                                  userToken);

            return(Result.Success(), user.Id);
        }
Exemplo n.º 2
0
        public async Task <Result> DeleteUserAsync(string userToken)
        {
            logger.LogInformation("Start deleting of user with token {userToken}.", userToken);

            (Result result, ApplicationUser user) = await FindUserByTokenAsync(userToken);

            if (!result.Succeeded)
            {
                logger.LogError("Error deleting user with token {userToken}. Errors: {@message}", userToken, result.Errors);

                return(result);
            }

            IdentityResult deleteResult = await userManager.DeleteAsync(user);

            if (!deleteResult.Succeeded)
            {
                result = deleteResult.ToApplicationResult();

                logger.LogError("Error deleting user with token {userToken}. Errors: {@message}", userToken, result.Errors);

                return(result);
            }

            logger.LogInformation("Successfully deleted {userName} user with token {userToken}.", user.UserName, userToken);

            return(Result.Success());
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        public async Task <(Result result, string userId, string code)> CreateUserAsync(string email, string userName, string password)
        {
            var user = new ApplicationUser
            {
                Email    = email,
                UserName = userName
            };

            var isExist = await _userManager.FindByEmailAsync(email);

            if (isExist != null)
            {
                return(null, null, null);
            }

            IdentityResult result = await _userManager.CreateAsync(user, password);

            var code = string.Empty;

            if (result.Succeeded)
            {
                await _userManager.AddToRoleAsync(user, "User");

                code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
            }

            return(result.ToApplicationResult(), user.Id, code);
        }
Exemplo n.º 4
0
        public async Task <(Result Result, string UserId)> AddUserToRolesAsync(string userId, List <string> role)
        {
            ApplicationUser user = await GetUserByIdAsync(userId);

            IdentityResult result = await _userManager.AddToRolesAsync(user, role);

            return(result.ToApplicationResult(), user.Id);
        }
Exemplo n.º 5
0
        public async Task <(Result Result, string UserId)> CreateUserAsync(UserDto userDto)
        {
            ApplicationUser user = _mapper.Map <ApplicationUser>(userDto);

            IdentityResult result = await _userManager.CreateAsync(user, user.PasswordHash);

            return(result.ToApplicationResult(), user.Id);
        }
Exemplo n.º 6
0
        public async Task <(Result Result, string UserId)> AddClaimToUser(string userId, Claim claim)
        {
            ApplicationUser user = await GetUserByIdAsync(userId);

            IdentityResult result = await _userManager.AddClaimAsync(user, claim);

            return(result.ToApplicationResult(), user.Id);
        }
Exemplo n.º 7
0
        public async Task <(Result Result, string UserId)> ChangeUserPhoneNumberAsync(string userId, string oldPhoneNumber, string newPhoneNumber)
        {
            ApplicationUser user = await GetUserByIdAsync(userId);

            IdentityResult result = await _userManager.ChangePhoneNumberAsync(user, oldPhoneNumber, newPhoneNumber);

            return(result.ToApplicationResult(), user.Id);
        }
Exemplo n.º 8
0
        public async Task <(Result Result, string UserId)> AddUserPasswordAsync(string userId)
        {
            ApplicationUser user = await GetUserByIdAsync(userId);

            IdentityResult result = await _userManager.AddPasswordAsync(user, user.PasswordHash);

            return(result.ToApplicationResult(), user.Id);
        }
Exemplo n.º 9
0
        public async Task <(Result Result, string UserId)> ResetUserAccessFailedCountAsync(string userId)
        {
            ApplicationUser user = await GetUserByIdAsync(userId);

            IdentityResult result = await _userManager.ResetAccessFailedCountAsync(user);

            return(result.ToApplicationResult(), user.Id);
        }
Exemplo n.º 10
0
        public async Task <(Result Result, string UserId)> SetUserTwoFactorLoginEnabledAsync(string userId, bool isEnable)
        {
            ApplicationUser user = await GetUserByIdAsync(userId);

            IdentityResult result = await _userManager.SetTwoFactorEnabledAsync(user, isEnable);

            return(result.ToApplicationResult(), user.Id);
        }
Exemplo n.º 11
0
        public async Task <(Result Result, string UserId)> RemoveClaimsFromUser(string userId, Claim oldClaim, Claim newClaim)
        {
            ApplicationUser user = await GetUserByIdAsync(userId);

            IdentityResult result = await _userManager.ReplaceClaimAsync(user, oldClaim, newClaim);

            return(result.ToApplicationResult(), user.Id);
        }
Exemplo n.º 12
0
        public async Task <(Result Result, string UserId)> RemoveClaimsFromUser(string userId, List <Claim> claim)
        {
            ApplicationUser user = await GetUserByIdAsync(userId);

            IdentityResult result = await _userManager.RemoveClaimsAsync(user, claim);

            return(result.ToApplicationResult(), user.Id);
        }
Exemplo n.º 13
0
        public async Task <(Result Result, string UserId)> ResetUserPasswordAsync(string userId, string token, string newPassword)
        {
            ApplicationUser user = await GetUserByIdAsync(userId);

            IdentityResult result = await _userManager.ResetPasswordAsync(user, token, newPassword);

            return(result.ToApplicationResult(), user.Id);
        }
Exemplo n.º 14
0
        public async Task <(Result Result, string UserId)> RemoveUserFromRoleAsync(string userId, string role)
        {
            ApplicationUser user = await GetUserByIdAsync(userId);

            IdentityResult result = await _userManager.RemoveFromRoleAsync(user, role);

            return(result.ToApplicationResult(), user.Id);
        }
        public async Task <(Result Result, string UserId)> CreateUserAsync(string userName, string password)
        {
            var user = new ApplicationUser
            {
                UserName = userName,
                Email    = userName,
            };

            IdentityResult result = await _userManager.CreateAsync(user, password);

            return(result.ToApplicationResult(), user.Id);
        }
Exemplo n.º 16
0
        public async Task <Result> AddToMedicalRoleAsync(string userToken, string healthSecurityId)
        {
            var medicalRoleName = Roles.Medical.ToString("G");

            logger.LogInformation("Start assignment of user with token {userToken} to {medicalRole} role.",
                                  userToken,
                                  medicalRoleName);

            (Result searchResult, ApplicationUser user) = await FindUserByTokenAsync(userToken);

            if (!searchResult.Succeeded)
            {
                logger.LogError("Error on assignment user with token {userToken}. Errors: {@message}",
                                userToken,
                                searchResult.Errors);

                return(searchResult);
            }

            if (await userManager.IsInRoleAsync(user, medicalRoleName))
            {
                logger.LogInformation("User with token {userToken} already assignment to {medicalRole} role.",
                                      userToken,
                                      medicalRoleName);

                return(Result.Success());
            }

            if (!medicalRegistrationRepository.TryRegistration(healthSecurityId))
            {
                Result result = Result.Failure(new InnerError(ErrorTarget.InvalidHealthSecurityId,
                                                              "Health security id is already taken or expired."));

                logger.LogWarning("Unsuccessful result on validation of the security Id {healthSecurityId}. Errors: {@message}",
                                  healthSecurityId,
                                  result.Errors);

                return(result);
            }

            IdentityResult roleAssignmentResult = await userManager.AddToRoleAsync(user, medicalRoleName);

            if (!roleAssignmentResult.Succeeded)
            {
                medicalRegistrationRepository.RollBackRegistration(healthSecurityId);

                Result result = roleAssignmentResult.ToApplicationResult();

                logger.LogError("Error on assignment user with token {userToken} to {medicalRole} role. Errors: {@message}",
                                userToken,
                                medicalRoleName,
                                result.Errors);

                return(result);
            }

            logger.LogInformation("Successfully assigned {userName} user with token {userToken} to {medicalRole} role..",
                                  user.UserName,
                                  userToken,
                                  medicalRoleName);

            return(Result.Success());
        }
        public async Task <Result> DeleteUserAsync(ApplicationUser user)
        {
            IdentityResult result = await _userManager.DeleteAsync(user);

            return(result.ToApplicationResult());
        }