Пример #1
0
        public static async Task <User> ConvertToDbUser(UserDto user, OperationMode operationMode,
                                                        ISqlSettingsRepository settingsRepository, int userId = 0)
        {
            var databaseUser = new User
            {
                Department          = user.Department,
                Enabled             = user.Enabled,
                ExpirePassword      = user.ExpirePassword,
                GroupMembership     = user.GroupMembership,
                Guest               = user.Guest,
                Image_ImageId       = user.Image_ImageId,
                Title               = user.Title,
                Login               = user.Login,
                Source              = user.Source,
                InstanceAdminRoleId = user.InstanceAdminRoleId,
                AllowFallback       = user.AllowFallback,
                DisplayName         = user.DisplayName,
                FirstName           = user.FirstName,
                LastName            = user.LastName,
                Email               = user.Email,
                UserSALT            = Guid.NewGuid(),
                Id             = userId,
                CurrentVersion = user.CurrentVersion
            };

            if (operationMode == OperationMode.Create)
            {
                var settings = await settingsRepository.GetUserManagementSettingsAsync();

                string decodedPassword;

                try
                {
                    decodedPassword = SystemEncryptions.Decode(user.Password);
                }
                catch (FormatException)
                {
                    throw new BadRequestException(ErrorMessages.IncorrectBase64FormatPasswordField, ErrorCodes.BadRequest);
                }

                if (string.IsNullOrWhiteSpace(decodedPassword) &&
                    (!user.AllowFallback.HasValue || !user.AllowFallback.Value) &&
                    settings.IsFederatedAuthenticationEnabled)
                {
                    databaseUser.Password = GeneratePassword();
                }
                else
                {
                    ValidatePassword(databaseUser, decodedPassword);
                    databaseUser.Password = HashingUtilities.GenerateSaltedHash(decodedPassword, databaseUser.UserSALT);
                }
            }

            return(databaseUser);
        }
Пример #2
0
        public async Task <IHttpActionResult> GetUserManagementSettings()
        {
            var user = await _userRepository.GetLoginUserByIdAsync(Session.UserId);

            if (user == null)
            {
                throw new AuthenticationException($"User does not exist with UserId: {Session.UserId}");
            }

            if (!user.InstanceAdminRoleId.HasValue)
            {
                throw new AuthorizationException("You do not have permission to access this area.", ErrorCodes.UnauthorizedAccess);
            }

            var settings = await _settingsRepository.GetUserManagementSettingsAsync();

            return(Ok(settings));
        }