示例#1
0
        public Users CreateAccount(Users user)
        {
            Users newUser;

            if (_checkValidEmail(user.EmailAddress))
            {
                newUser = new Users
                {
                    EmailAddress = user.EmailAddress,
                    FirstName    = user.FirstName,
                    LastName     = user.LastName,
                    Role         = user.Role
                };

                newUser.Password = this._passwordHasher.HashPassword(newUser, user.Password);
            }
            else
            {
                return(null);
            }

            try
            {
                this._context.users.Add(newUser);
                this._context.SaveChanges();

                if (newUser.Role == Role.Admin)
                {
                    this._logger.LogInformation("Attempting to create admin request..");
                    newUser.Role = Role.User;
                    this._context.users.Update(newUser);

                    if (!this._createAdminRequest(newUser.ID))
                    {
                        this._logger.LogError("There was an issue making the admin request.");
                        return(null);
                    }
                    this._logger.LogInformation("Admin request created!");
                    this._context.SaveChanges();
                }

                String token = _tokenBuilder.GetAccountCreationToken(newUser);
                newUser.Token = token;

                string userFullName = newUser.FirstName + " " + newUser.LastName;
                this._emailService.ComposeEmail(userFullName, newUser.EmailAddress, "New Account", "Your account was created. Link: " + this.url + "/verifyEmail/" + newUser.ID + "?token=" + token);
                this._emailService.SendMessage();

                return(newUser);
            }
            catch (Exception ex)
            {
                _logger.LogError("Uh oh! Caught exception: " + ex.ToString());
                return(null);
            }
        }