public string Create(AGSUserEntity user) { // creat the Identity User with the provided password EFApplicationUser appUser = new EFApplicationUser(); MapAGSUserEntityToEFApplicationUser(user, appUser); appUser.Id = CommonConstant.GenerateId(); // set the default password for the newly created user _ = _userManager.CreateAsync(appUser).Result; _ = _userManager.AddClaimsAsync(appUser, new Claim[] { new Claim(JwtClaimTypes.Name, user.Username), new Claim(JwtClaimTypes.Email, user.Email) }).Result; // update the associated groups if (user.GroupIds != null) { foreach (var groupId in user.GroupIds) { Console.WriteLine($"Adding group:{groupId}"); this.AddUserToGroup(appUser, groupId); } } return(appUser.Id); }
public void MapAGSGroupEntityToEFApplicationRole(AGSGroupEntity groupEntity, EFApplicationRole efApplicationRole) { efApplicationRole.Id = groupEntity.Id; efApplicationRole.Name = groupEntity.Name; efApplicationRole.NormalizedName = groupEntity.Name; efApplicationRole.ConcurrencyStamp = CommonConstant.GenerateId(); }
public void InitializeApplicationData() { // add all function claims into Database var ags_identity_constant_type = typeof(CommonConstant); var constant_fields = ags_identity_constant_type.GetFields(); foreach (var constant_field in constant_fields) { if (constant_field.Name.EndsWith("ClaimConstant")) { var claimValue = (string)(constant_field.GetValue(null)); _applicationDbContext.FunctionClaims.Add(new EFFunctionClaim() { Id = CommonConstant.GenerateId(), Name = claimValue }); } } // create admin user var userName = AGSAdminName; var email = _configuration["default_user_email"]; var userPassword = _configuration["default_user_password"]; var user = new EFApplicationUser { Id = CommonConstant.GenerateId(), UserName = userName, NormalizedEmail = email, NormalizedUserName = userName, Email = email, First_Name = "Tim", Last_Name = "Ng", Title = "Developer", SecurityStamp = CommonConstant.GenerateId(), // need to add this !!! }; _ = _userManager.CreateAsync(user, userPassword).Result; var group1 = new EFApplicationRole { Id = CommonConstant.GenerateId(), Name = "Group_1_Test", NormalizedName = "Group_1_Test", ConcurrencyStamp = CommonConstant.GenerateId() }; var group2 = new EFApplicationRole { Id = CommonConstant.GenerateId(), Name = "Group_2_Test", NormalizedName = "Group_2_Test", ConcurrencyStamp = CommonConstant.GenerateId() }; _ = _roleManager.CreateAsync(group1).Result; _ = _roleManager.CreateAsync(group2).Result; _applicationDbContext.SaveChanges(); }
public string Create(AGSFunctionClaimEntity functionClaim) { var result = new EFFunctionClaim(); UpdateFunctionClaim(functionClaim, result); result.Id = CommonConstant.GenerateId(); _applicationDbContext.FunctionClaims.Add(result); return(result.Id); }
public void CreateUser_IdNotEmpty_ThrowException() { var repository = new Mock <IRepository>(); var newUser = new AGSUserEntity() { Id = CommonConstant.GenerateId() }; var usersHelper = new UsersHelper(repository.Object, _configuration); Assert.Throws <ArgumentException>(() => usersHelper.CreateUser(newUser)); }
public void CreateFunctionClaim_IdProvided_ThrowException() { // mock the IRepository object start var functionClaimsRepository = new Mock <IRepository>(); functionClaimsRepository.Setup(_ => _.FunctionClaimsRepository.Create(It.Is <AGSFunctionClaimEntity>(g => string.IsNullOrEmpty(g.Id)))).Returns(CommonConstant.GenerateId()); // end var functionClaimsHelper = new FunctionClaimsHelper(functionClaimsRepository.Object); var newFunctionClaim = new AGSFunctionClaimEntity() { Id = CommonConstant.GenerateId() }; Assert.Throws <ArgumentException>(() => functionClaimsHelper.CreateFunctionClaim(newFunctionClaim)); }
public string Create(AGSGroupEntity group) { // create a new role in ASP.NET identity core var role = new EFApplicationRole(); MapAGSGroupEntityToEFApplicationRole(group, role); role.Id = CommonConstant.GenerateId(); // assign id here _ = _roleManager.CreateAsync(role).Result; // update the associated Function Claims if (group.FunctionClaimIds != null) { foreach (var functionClaimId in group.FunctionClaimIds) { this.AddFunctionClaimToGroup(role, functionClaimId); } } return(role.Id); }
public void CreateFunctionClaim_Valid_Success(string name) { // mock the IRepository object start var functionClaimsRepository = new Mock <IRepository>(); functionClaimsRepository.Setup(_ => _.FunctionClaimsRepository.Create(It.Is <AGSFunctionClaimEntity>(g => string.IsNullOrEmpty(g.Id)))).Returns(CommonConstant.GenerateId()); // end var functionClaimsHelper = new FunctionClaimsHelper(functionClaimsRepository.Object); var newFunctionClaim = new AGSFunctionClaimEntity() { Name = name }; var result = functionClaimsHelper.CreateFunctionClaim(newFunctionClaim); Assert.NotNull(result); }
public void CreateUser_EmptyId_Success() { var repository = new Mock <IRepository>(); repository.Setup(_ => _.UsersRepository.Create(It.IsAny <AGSUserEntity>())).Returns(CommonConstant.GenerateId()); var newUser = new AGSUserEntity() { Username = "******" }; var usersHelper = new UsersHelper(repository.Object, _configuration); var result = usersHelper.CreateUser(newUser); Assert.NotNull(result); }