Esempio n. 1
0
        /// <summary>
        /// The CreateIndividualUser method.
        /// Contains business logic to create an individual user.
        /// <para>
        /// @author: Jennifer Nguyen
        /// @updated: 03/13/2018
        /// </para>
        /// </summary>
        /// <param name="registerUserDto"></param>
        /// <returns>ResponseDto</returns>
        public ResponseDto <RegisterUserDto> CreateIndividualUser(RegisterUserDto registerUserDto)
        {
            var createIndividualPreLogicValidationStrategy = new CreateIndividualPreLogicValidationStrategy(registerUserDto);

            // Validate data transfer object
            var result = createIndividualPreLogicValidationStrategy.ExecuteStrategy();

            if (result.Error != null)
            {
                return(new ResponseDto <RegisterUserDto>
                {
                    Data = registerUserDto,
                    Error = result.Error
                });
            }

            var mappingResult = MapIndividualDtoToModel(registerUserDto, out var userAccount, out var passwordSalt, out var userClaims, out var userProfile, out var securityQuestions, out var securityAnswerSalts);

            var createIndividualPostLogicValdiationStrategy = new CreateIndividualPostLogicValidationStrategy(userAccount, passwordSalt, userClaims, userProfile, securityQuestions, securityAnswerSalts);
            var validateResult = createIndividualPostLogicValdiationStrategy.ExecuteStrategy();

            if (!validateResult.Data)
            {
                return(new ResponseDto <RegisterUserDto>
                {
                    Data = registerUserDto,
                    Error = GeneralErrorMessages.GENERAL_ERROR
                });
            }

            // Store user in database
            using (var userGateway = new UserGateway())
            {
                var gatewayResult = userGateway.StoreIndividualUser(userAccount, passwordSalt, userClaims, userProfile, securityQuestions, securityAnswerSalts);
                if (gatewayResult.Data == false)
                {
                    return(new ResponseDto <RegisterUserDto>()
                    {
                        Data = registerUserDto,
                        Error = GeneralErrorMessages.GENERAL_ERROR
                    });
                }
            }

            return(new ResponseDto <RegisterUserDto>
            {
                Data = registerUserDto
            });
        }
Esempio n. 2
0
        /// <summary>
        /// The CreateAdmin method.
        /// Contains business logic to create an admin user.
        /// <para>
        /// @author: Jennifer Nguyen, Angelica Salas
        /// @updated: 04/26/2018
        /// </para>
        /// </summary>
        /// <param name="registerUserDto"></param>
        /// <returns>ResponseDto</returns>
        public ResponseDto <RegisterUserDto> CreateAdmin(RegisterUserDto registerUserDto)
        {
            var createIndividualPreLogicValidationStrategy = new CreateIndividualPreLogicValidationStrategy(registerUserDto);
            var securityAnswerSalts = new List <SecurityAnswerSalt>();
            var saltGenerator       = new SaltGenerator();
            var payloadHasher       = new PayloadHasher();
            var claimsFactory       = new ClaimsFactory();

            // Validate data transfer object
            var result = createIndividualPreLogicValidationStrategy.ExecuteStrategy();

            if (result.Error != null)
            {
                return(new ResponseDto <RegisterUserDto>
                {
                    Data = registerUserDto,
                    Error = result.Error
                });
            }

            // Map data transfer object to domain models
            var userAccount       = new UserAccount(username: registerUserDto.UserAccountDto.Username, password: registerUserDto.UserAccountDto.Password, isActive: true, isFirstTimeUser: false, roleType: "private");
            var securityQuestions = registerUserDto.SecurityQuestionDtos
                                    .Select(securityQuestionDto => new SecurityQuestion(
                                                securityQuestionDto.Question, securityQuestionDto.Answer))
                                    .ToList();

            //Admin User Profile
            var displayImagePath = ConfigurationManager.AppSettings["DefaultURLProfileImagePath"];
            var userProfile      = new UserProfile(displayPicture: displayImagePath, displayName: registerUserDto.UserProfileDto.DisplayName);

            // Set user claims to be stored in UserClaims table as administrator
            var userClaims = new UserClaims(claimsFactory.Create(AccountTypes.Admin));

            // Hash password
            var passwordSalt = new PasswordSalt(saltGenerator.GenerateSalt(128));

            userAccount.Password = payloadHasher.Sha256HashWithSalt(passwordSalt.Salt, userAccount.Password);

            // Hash security answers
            for (var i = 0; i < securityQuestions.Count; i++)
            {
                securityAnswerSalts.Add(new SecurityAnswerSalt {
                    Salt = saltGenerator.GenerateSalt(128)
                });
                securityQuestions[i].Answer = payloadHasher.Sha256HashWithSalt(securityAnswerSalts[i].Salt, securityQuestions[i].Answer);
            }

            var createIndividualPostLogicValdiationStrategy = new CreateIndividualPostLogicValidationStrategy(userAccount, passwordSalt, userClaims, userProfile, securityQuestions, securityAnswerSalts);
            var validateResult = createIndividualPostLogicValdiationStrategy.ExecuteStrategy();

            if (!validateResult.Data)
            {
                return(new ResponseDto <RegisterUserDto>
                {
                    Data = registerUserDto,
                    Error = GeneralErrorMessages.GENERAL_ERROR
                });
            }

            // Store user in database
            using (var userGateway = new UserGateway())
            {
                var gatewayResult = userGateway.StoreIndividualUser(userAccount, passwordSalt, userClaims, userProfile, securityQuestions, securityAnswerSalts);
                if (gatewayResult.Data == false)
                {
                    return(new ResponseDto <RegisterUserDto>()
                    {
                        Data = registerUserDto,
                        Error = GeneralErrorMessages.GENERAL_ERROR
                    });
                }
            }

            return(new ResponseDto <RegisterUserDto>
            {
                Data = registerUserDto
            });
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a restaurant user as part of first time registration
        /// </summary>
        /// <para>
        /// @author: Brian Fann
        /// @updated: 04/25/2018
        /// </para>
        /// <param name="registerRestaurantDto">Incoming Dto</param>
        /// <returns></returns>
        public ResponseDto <RegisterRestaurantDto> CreateFirstTimeRestaurantUser(RegisterRestaurantDto registerRestaurantDto)
        {
            // Validate incoming user account in the dto
            var userPreLogicValidationStrategy = new CreateFirstTimeIndividualPreLogicValidationStrategy(registerRestaurantDto);
            var userResult = userPreLogicValidationStrategy.ExecuteStrategy();

            if (userResult.Error != null)
            {
                return(new ResponseDto <RegisterRestaurantDto>
                {
                    Data = registerRestaurantDto,
                    Error = userResult.Error
                });
            }

            // Validate incoming restaurant details in the dto
            var restaurantPreLogicValidationStrategy = new CreateRestaurantPreLogicValidationStrategy(registerRestaurantDto);
            var restaurantResult = restaurantPreLogicValidationStrategy.ExecuteStrategy();

            if (restaurantResult.Error != null)
            {
                return(new ResponseDto <RegisterRestaurantDto>
                {
                    Data = registerRestaurantDto,
                    Error = restaurantResult.Error
                });
            }

            // Authenticate user credentials against the database
            var credentialsValidator = new CredentialsValidator();
            var credentialsResult    = credentialsValidator.IsCredentialsValid(registerRestaurantDto.UserAccountDto.Username, registerRestaurantDto.UserAccountDto.Password);

            if (!credentialsResult.Data)
            {
                return(new ResponseDto <RegisterRestaurantDto>()
                {
                    Data = registerRestaurantDto,
                    Error = credentialsResult.Error
                });
            }

            // Create a domain model based on the dto.
            var mappingResult = MapRestaurantDtoToModels(registerRestaurantDto, out var userAccount, out var passwordSalt, out var userClaims, out var userProfile, out var securityQuestions, out var securityAnswerSalts, out var restaurantProfile, out var businessHours, out var foodPreferences);

            if (!mappingResult.Data)
            {
                return(new ResponseDto <RegisterRestaurantDto>()
                {
                    Data = registerRestaurantDto,
                    Error = mappingResult.Error
                });
            }

            // Validate domain models created from dto
            var userPostLogicValidationStrategy = new CreateFirstTimeIndividualPostLogicValidationStrategy(userAccount, passwordSalt, userClaims, userProfile, securityQuestions, securityAnswerSalts);

            userResult = userPostLogicValidationStrategy.ExecuteStrategy();

            if (userResult.Error != null)
            {
                return(new ResponseDto <RegisterRestaurantDto>
                {
                    Data = registerRestaurantDto,
                    Error = userResult.Error
                });
            }

            // Map the user's id in the database to the generated domain model.
            var userIdResult = GetFirstTimeUserAccountId(userAccount.Username);

            if (userIdResult.Error != null)
            {
                return(new ResponseDto <RegisterRestaurantDto>()
                {
                    Data = registerRestaurantDto,
                    Error = userIdResult.Error
                });
            }

            userAccount.Id = userIdResult.Data;

            // Apply post logic validation to the user account information
            var userPostLogicValdiationStrategy = new CreateIndividualPostLogicValidationStrategy(userAccount, passwordSalt, userClaims, userProfile, securityQuestions, securityAnswerSalts);
            var userPostResult = userPostLogicValdiationStrategy.ExecuteStrategy();

            if (!userPostResult.Data)
            {
                return(new ResponseDto <RegisterRestaurantDto>
                {
                    Data = registerRestaurantDto,
                    Error = GeneralErrorMessages.GENERAL_ERROR
                });
            }

            // Apply post logic validation to the restaurant information
            var restaurantPostLogicValdiationStrategy = new CreateRestaurantPostLogicValidationStrategy(restaurantProfile, businessHours);
            var restaurantPostResult = restaurantPostLogicValdiationStrategy.ExecuteStrategy();

            if (!restaurantPostResult.Data)
            {
                return(new ResponseDto <RegisterRestaurantDto>
                {
                    Data = registerRestaurantDto,
                    Error = GeneralErrorMessages.GENERAL_ERROR
                });
            }

            // Store user in database
            using (var userGateway = new UserGateway())
            {
                var createResult = userGateway.StoreRestaurantUser(userAccount, passwordSalt, userClaims, userProfile, restaurantProfile, securityQuestions, securityAnswerSalts, foodPreferences, businessHours);

                if (!createResult.Data)
                {
                    return(new ResponseDto <RegisterRestaurantDto>()
                    {
                        Data = registerRestaurantDto,
                        Error = createResult.Error
                    });
                }
            }

            return(new ResponseDto <RegisterRestaurantDto>
            {
                Data = registerRestaurantDto
            });
        }
Esempio n. 4
0
        /// <summary>
        /// The CreateRestaurantUser method.
        /// Contains business logic for creating a restaurant user.
        /// <para>
        /// @author: Jennifer Nguyen, Brian Fann
        /// @updated: 04/25/2018
        /// </para>
        /// </summary>
        /// <param name="registerRestaurantDto"></param>
        /// <returns>ResponseDto</returns>
        public ResponseDto <RegisterRestaurantDto> CreateRestaurantUser(RegisterRestaurantDto registerRestaurantDto)
        {
            var userPreLogicValidationStrategy = new CreateIndividualPreLogicValidationStrategy(registerRestaurantDto);

            var userResult = userPreLogicValidationStrategy.ExecuteStrategy();

            if (userResult.Error != null)
            {
                return(new ResponseDto <RegisterRestaurantDto>
                {
                    Data = registerRestaurantDto,
                    Error = userResult.Error
                });
            }

            var restaurantPreLogicValidationStrategy = new CreateRestaurantPreLogicValidationStrategy(registerRestaurantDto);

            // Validate data transfer object
            var restaurantResult = restaurantPreLogicValidationStrategy.ExecuteStrategy();

            if (restaurantResult.Error != null)
            {
                return(new ResponseDto <RegisterRestaurantDto>
                {
                    Data = registerRestaurantDto,
                    Error = restaurantResult.Error
                });
            }

            // Create a domain model based on the dto.
            var mappingResult = MapRestaurantDtoToModels(registerRestaurantDto, out var userAccount, out var passwordSalt, out var userClaims, out var userProfile, out var securityQuestions, out var securityAnswerSalts, out var restaurantProfile, out var businessHours, out var foodPreferences);

            if (!mappingResult.Data)
            {
                return(new ResponseDto <RegisterRestaurantDto>()
                {
                    Data = registerRestaurantDto,
                    Error = mappingResult.Error
                });
            }

            // Validate domain models
            var userPostLogicValidationStrategy = new CreateIndividualPostLogicValidationStrategy(userAccount, passwordSalt, userClaims, userProfile, securityQuestions, securityAnswerSalts);

            userResult = userPostLogicValidationStrategy.ExecuteStrategy();

            if (userResult.Error != null)
            {
                return(new ResponseDto <RegisterRestaurantDto>
                {
                    Data = registerRestaurantDto,
                    Error = userResult.Error
                });
            }

            var createRestaurantPostLogicValdiationStrategy = new CreateRestaurantPostLogicValidationStrategy(restaurantProfile, businessHours);
            var validateResult = createRestaurantPostLogicValdiationStrategy.ExecuteStrategy();

            if (!validateResult.Data)
            {
                return(new ResponseDto <RegisterRestaurantDto>
                {
                    Data = registerRestaurantDto,
                    Error = GeneralErrorMessages.GENERAL_ERROR
                });
            }

            // Store user in database
            using (var userGateway = new UserGateway())
            {
                var createResult = userGateway.StoreRestaurantUser(userAccount, passwordSalt, userClaims, userProfile, restaurantProfile, securityQuestions, securityAnswerSalts, foodPreferences, businessHours);

                if (!createResult.Data)
                {
                    return(new ResponseDto <RegisterRestaurantDto>()
                    {
                        Data = registerRestaurantDto,
                        Error = createResult.Error
                    });
                }
            }

            return(new ResponseDto <RegisterRestaurantDto>
            {
                Data = registerRestaurantDto
            });
        }