public OperationResult<UserWithRoles> CreateUser(string username, string email, string password, string[] roles) { bool existingUser = _userRepository.GetAll().Any(u => u.Name == username); if (existingUser) return new OperationResult<UserWithRoles>(false); string pwdSalt = _cryptoService.GenerateSalt(); var user = new User { Name = username, Email = email, CreatedOn = DateTime.Now, HashedPassword = _cryptoService.EncryptPassword(password, pwdSalt), IsLocked = false }; _userRepository.Add(user); _userRepository.Save(); if (roles != null && roles.Length > 0) roles.ForEach(r => AddUserToRole(user, r)); return new OperationResult<UserWithRoles>(true){Entity = GetUserWithRoles(user)}; }
private void AddUserToRole(User user, string roleName) { var role = _roleRepository.GetSingleByRoleName(roleName); if (role==null) { var newRole = new Role {Name = roleName}; _roleRepository.Add(newRole); _roleRepository.Save(); role = newRole; } var userInRole = new UserInRole {RoleKey = role.Key, UserKey = user.Key}; _userInRoleRepository.Add(userInRole); _roleRepository.Save(); }
private UserWithRoles GetUserWithRoles(User user) { if (user != null) { var userRoles = GetUserRoles(user.Key); return new UserWithRoles() { User = user, Roles = userRoles }; } return null; }
public UserWithRoles UpdateUser(User user, string username, string email) { throw new NotImplementedException(); }