Пример #1
0
 public async Task <ActionResult <UserOutoutDTO> > AddNewUser([FromBody] UserAddInputDTO userAddInputDTO, int userId)
 {
     try
     {
         return(await this.userService.AddNewUser(userAddInputDTO, userId));
     }
     catch (Exception ex)
     {
         return(BadRequest(new { message = new List <string> {
                                     ex.Message
                                 } }));
     }
 }
Пример #2
0
        /// <summary>
        /// Add New User
        /// </summary>
        /// <param name="userAddInputDTO"></param>
        /// <returns></returns>
        public async Task <UserOutoutDTO> AddNewUser(UserAddInputDTO userAddInputDTO, int userId)
        {
            UserEntity newUser           = this.mapper.Map <UserEntity>(userAddInputDTO);
            bool       hasAccessToCreate = true;
            UserEntity loggedInUser      = await this.userRepository.GetUser(userId);

            byte[] passwordHash, passwordSalt;
            this.authService.CreatePasswordHash(newUser.EmailAddress, out passwordHash, out passwordSalt);
            newUser.passwordSalt = passwordSalt;
            newUser.passwordHash = passwordHash;

            if (loggedInUser.Role == Roles.BusinessUser)
            {
                UserEntity parent = await this.userRepository.GetUser(loggedInUser.ParentUserId);

                if (parent.AddedUsers + 1 > parent.MaxUsers)
                {
                    throw new ApplicationException("Business cannot add more users");
                }
                // Update user added users
                parent.AddedUsers = parent.AddedUsers + 1;
                await this.userRepository.UpdateUser(parent);

                UserAccessEntity userAccess = await this.userAccessRepository.GetUserAccess(userId);

                hasAccessToCreate    = userAccess != null ? userAccess.Delete : false;
                newUser.ParentUserId = loggedInUser.ParentUserId;
            }

            if (loggedInUser.Role == Roles.Business)
            {
                newUser.ParentUserId = loggedInUser.UserId;
                if (loggedInUser.AddedUsers + 1 > loggedInUser.MaxUsers)
                {
                    throw new ApplicationException("Business cannot add more users");
                }
            }

            if (hasAccessToCreate)
            {
                UserEntity updatedUser = await this.userRepository.AddUser(newUser);

                if (!updatedUser.EmailVerified)
                {
                    await this.accountService.SendVerificationEmail(updatedUser.EmailAddress);
                }

                if (loggedInUser.Role == Roles.Business)
                {
                    // Update user added users
                    loggedInUser.AddedUsers = loggedInUser.AddedUsers + 1;
                    await this.userRepository.UpdateUser(loggedInUser);
                }

                return(this.mapper.Map <UserOutoutDTO>(updatedUser));
            }
            else
            {
                throw new UnauthorizedAccessException("User don't have access to perform the add user operation");
            }
        }