public User RegisterUser(User user) { Check.IsNotNull<User>(user, "user"); Check.IsNotEmptyOrWhiteSpace(user.NameIdentifier, "user.NameIdentifier"); try { User existingUser = this.userRepository.GetUserByNameIdentifier(user.NameIdentifier); if (null != existingUser) { throw new UserAlreadyExistsException() { NameIdentifier = user.NameIdentifier, IdentityProvider = user.IdentityProvider }; } user.CreatedOn = DateTime.UtcNow; user.ModifiedOn = DateTime.UtcNow; user.IsActive = true; user.UserRoles.Add(new UserRole() { RoleId = (int)Roles.User }); User registeredUser = this.userRepository.AddUser(user); unitOfWork.Commit(); // TODO: v-rajask, The code below was added to return the roles. // Entity framework does not pupoulate the complete graph (it only // populates the keys. Role object on UserRole is empty. This // issue needs investigation and proper fix. Role userRole = new Role() { Name = Roles.User.ToString() }; registeredUser.UserRoles.First().Role = userRole; return registeredUser; } catch (UnitOfWorkCommitException uowce) { throw new UserDataUpdateException("Failed to register user.", uowce); } }
private void AddDefaultRoles() { Role userRole = new Role() { Name = UserRoleName }; Role adminRole = new Role() { Name = AdministratorRoleName }; testDBContext.Roles.Add(userRole); testDBContext.Roles.Add(adminRole); testDBContext.Commit(); }