Esempio n. 1
0
        /// <summary>
        /// Add user to data storage.
        /// </summary>
        /// <param name="coreUser">User model to save in data storage.</param>
        /// <returns>
        /// true - if user is saved.
        /// false - if something went wrong.
        /// </returns>
        public async Task <bool> AddUserAsync(CoreModels.User coreUser, string password)
        {
            _logger.LogInformation($"Invoked UserService.AddUserAsync with data [{coreUser}]");

            bool userIsExists = await _userRepository
                                .UserExistsAsync(username : coreUser.Username);

            if (!userIsExists)
            {
                byte[] passwordHash, passwordHashingKey;

                CreatePasswordHash(password, out passwordHash, out passwordHashingKey);

                // Set "User" type for new created user.
                if (coreUser.Username == "Admin" || coreUser.Username == "root")
                {
                    coreUser.RoleId = 1;
                }
                else if (coreUser.Username == "Bot")
                {
                    coreUser.RoleId = 3;
                }
                else
                {
                    coreUser.RoleId = 2;
                }

                coreUser.IsDeleted          = false;
                coreUser.PasswordHash       = passwordHash;
                coreUser.PasswordHashingKey = passwordHashingKey;

                try
                {
                    await _userRepository.AddUserAsync(coreUser);

                    return(true);
                }
                catch (Exception ex)
                {
                    _logger.LogInformation(ex.Message);
                }
            }

            return(false);
        }