public virtual NhUserAccount CreateAccount(NhUserAccount item)
 {
     NhUserAccount newAccount;
     using (var scope = new UnitOfWorkScope())
     {
         newAccount = _userAccountService.CreateAccount(item.Username, item.HashedPassword, item.Email, item.FirstName, item.LastName);
         scope.Commit();
     }
     return newAccount;
 }
 public bool ChangePasswordFromResetKey(string key, string newPassword, out NhUserAccount account)
 {
     bool success;
     using (var scope = new UnitOfWorkScope())
     {
         success=_userAccountService.ChangePasswordFromResetKey(key,newPassword,out account);
         scope.Commit();
     }
     return success;
 }
 public ActionResult Index(RegisterInputModel model)
 {
     if (ModelState.IsValid)
     {
         try
         {
             NhUserAccount account = new NhUserAccount { Username = model.Username, HashedPassword = model.Password, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName };
             account = _userService.CreateAccount(account);
             //account.FirstName = model.FirstName;
             //if (!string.IsNullOrWhiteSpace(model.LastName))
             //    account.LastName = model.LastName;
             //this.userAccountService.Update(account);
             ViewData["RequireAccountVerification"] = this._userAccountService.Configuration.RequireAccountVerification;
             return View("Success", model);
         }
         catch (ValidationException ex)
         {
             ModelState.AddModelError("", ex.Message);
         }
     }
     return View(model);
 }
 public virtual NhUserAccount CreateAccountWithTempPassword(NhUserAccount item)
 {
     if (item == null)
         throw new Exception("User cannot be empty.");
     if (string.IsNullOrEmpty(item.Username))
         throw new Exception("Username cannot be empty.");
     if (string.IsNullOrEmpty(item.Email))
        throw new Exception("User email cannot be empty.");
     item.Username = item.Username.Trim();
     item.Email = string.IsNullOrEmpty(item.Email) ? null : item.Email.Trim();
     item.FirstName = string.IsNullOrEmpty(item.FirstName) ? null : item.FirstName.Trim();
     item.LastName = string.IsNullOrEmpty(item.LastName) ? null : item.LastName.Trim();
     if (string.IsNullOrEmpty(item.HashedPassword))
     {
         var passwordGenerator = IoC.GetService<IPasswordGenerator>();
         item.HashedPassword = passwordGenerator.GeneratePassword();
     }
     if (_membershipConfiguration.UsernamesUniqueAcrossTenants)
     {
         var account = _userRepository.Query.SingleOrDefault(x => x.Username == item.Username);
         if (account != null)
         {
             throw new Exception(string.Format("Username {0} is not available.", item.Username)); ;
         }
     }
     else
     {
         var account = _userRepository.Query.SingleOrDefault(x => x.Tenant == item.Tenant && x.Username == item.Username);
         if (account != null)
         {
             throw new Exception(string.Format("Username {0} is not available.", item.Username)); ;
         }
     }
     NhUserAccount newAccount;
     using (var scope = new UnitOfWorkScope())
     {
         newAccount = _userAccountService.CreateAccountWithTempPassword(item.Tenant, item.Username, item.HashedPassword, item.Email, item.FirstName, item.LastName,item.IsLoginAllowed,item.IsAccountVerified);
         scope.Commit();
     }
     using (var scope = new UnitOfWorkScope())
     {
         _userAccountService.SetRequiresPasswordReset(newAccount.ID, true);
         scope.Commit();
         return newAccount;
     }
 }
 public void VerifyEmailFromKey(string key, string password, out NhUserAccount account)
 {
     using (var scope = new UnitOfWorkScope())
     {
         _userAccountService.VerifyEmailFromKey(key, password, out account);
         scope.Commit();
     }
 }
 public virtual void Update(NhUserAccount user)
 {
     if (user == null)
         throw new ArgumentNullException("User cannot be empty.");
     if(string.IsNullOrEmpty(user.Username))
         throw new Exception("Username cannot be empty.");
     if(string.IsNullOrEmpty(user.Email))
         throw new Exception("User email cannot be empty.");
     if (_membershipConfiguration.UsernamesUniqueAcrossTenants)
     {
         var account = _userRepository.Query.SingleOrDefault(x => x.Username == user.Username);
         if (account != null && account.ID != user.ID)
         {
             throw new Exception(string.Format("Username {0} is not available.", user.Username));;
         }
     }
     else
     {
         var account = _userRepository.Query.SingleOrDefault(x => x.Username == user.Username);
         if (account != null && account.ID != user.ID)
         {
             throw new Exception(string.Format("Username {0} is not available.", user.Username)); ;
         }
     }
     using (var scope = new UnitOfWorkScope())
     {
         _userRepository.Update(user);
         scope.Commit();
     }
 }
 public virtual void Save(NhUserAccount user)
 {
     if (user == null)
         throw new ArgumentNullException("User cannot be empty.");
     if (string.IsNullOrEmpty(user.Username))
         throw new Exception("Username cannot be empty.");
     if (string.IsNullOrEmpty(user.Email))
         throw new Exception("User email cannot be empty.");
     if (_membershipConfiguration.UsernamesUniqueAcrossTenants)
     {
         var account = _userRepository.Query.SingleOrDefault(x => x.Username == user.Username);
         if (account != null && account.ID != user.ID)
         {
             throw new Exception(string.Format("Username {0} is not available.", user.Username)); ;
         }
     }
     else
     {
         var account = _userRepository.Query.SingleOrDefault(x => x.Username == user.Username);
         if (account!=null && (user.ID==default(Guid) || account.ID != user.ID))
         {
             throw new Exception(string.Format("Username {0} is not available.", user.Username)); ;
         }
     }
     using (var scope = new UnitOfWorkScope())
     {
         _userRepository.Add(user);//.Save(user);
         scope.Commit();
     }
     StringBuilder message = new StringBuilder();
     message.AppendFormat("User {0} is created.", user.Username);
     ActivityLog activityItem = new ActivityLog(ActivityType.AddUser.ToString(), message.ToString());
     _activityLogService.Add(activityItem);
 }
 public IHttpActionResult Register([ModelBinder(typeof(Web.Infrastruture.FieldValueModelBinder))] RegisterInputModel item)
 {
     StringBuilder message = new StringBuilder();
     if (string.IsNullOrEmpty(item.Email))
         return BadRequest("Email cannot be empty.");
     if (string.IsNullOrEmpty(item.Username))
         return BadRequest("Username cannot be empty.");
     if (string.IsNullOrEmpty(item.Password))
         return BadRequest("Password cannot be empty.");
     if (item.Password!=item.ConfirmPassword)
         return BadRequest("Password and confirm password do not match.");
     if (string.IsNullOrEmpty(item.FirstName))
         return BadRequest("Firstname cannot be empty.");
     try
     {
         NhUserAccount account = new NhUserAccount { Username = item.Username, HashedPassword = item.Password, Email = item.Email, FirstName = item.FirstName, LastName = item.LastName };
         account = _userService.CreateAccount(account);
     }
     catch (Exception ex)
     {
         message.Append(ex.Message);
         return Json<object>(new { Success = false, Message = message.ToString() });
     }
     message.AppendFormat("User {0} is created successfully.",item.Username);
     return Json<object>(new { Success = true, Message = message.ToString() });
 }
        private static void SeedDatabase()
        {
            string userEmail = ConfigurationManager.AppSettings[Constants.ADMIN_EMAIL];
            if (!string.IsNullOrWhiteSpace(userEmail))
            {
                IUserService userService = IoC.GetService<IUserService>();
                //string username = userEmail.Substring(0, userEmail.IndexOf('@') > 0 ? userEmail.IndexOf('@') : userEmail.Length);
                string username = userEmail;
                var account = userService.FindBy(x => x.Username == username || x.Email==userEmail);
                if (account == null)
                {
                    NhUserAccount user = new NhUserAccount() { Username = username, Email = userEmail, HashedPassword = "******", FirstName = "Admin",IsLoginAllowed=true,IsAccountVerified=true};
                    account = userService.CreateAccountWithTempPassword(user);
                }

                IRoleService roleServie = IoC.GetService<IRoleService>();
                Role adminRole = new Role { Name = Constants.ROLE_ADMIN, Description = "System Administrator" };
                if (!roleServie.RoleExists(adminRole.Name))
                {
                    roleServie.CreateRole(adminRole);
                }
                if (!roleServie.IsUserInRole(account.ID, Constants.ROLE_ADMIN))
                    roleServie.AddUsersToRoles(new List<Guid>() { account.ID }, new List<string>() { adminRole.Name });
            }
        }