public async Task <bool> CreateCompanyAsync(CreateCompanyBindingModel model, SteuUser user) { if (user == null) { throw new ArgumentNullException("User is null"); } if (model.Name == null) { throw new ArgumentNullException("Company name is null"); } if (model.UIC == null) { throw new ArgumentNullException("UIC is null"); } Company company = new Company() { Name = model.Name, UIC = model.UIC, LicenseUrl = model.LicenseUrl, }; company.Employees.Add(user); await this.companyRepositori.AddAsync(company); await this.companyRepositori.SaveChangesAsync(); return(true); }
public async Task GetCompanyManagerByCompanyIdAsync_WithCorrectData_ShouldReturnCompany() { var context = SteuDbContextInMemoryFactory.InitializeContext(); var company = new Company() { Id = "asdasd", Name = "Test Ltd", }; var user = new SteuUser() { Id = "managerId", Manager = true, }; company.Employees.Add(user); await context.AddAsync(company); await context.SaveChangesAsync(); MapperInitializer.InitializeMapper(); var repository = new EfDeletableEntityRepository <Company>(context); var service = new CompaniesService(repository); var actualResult = await service.GetCompanyManagerByCompanyIdAsync("asdasd"); Assert.Equal("managerId", actualResult); }
public async Task CreateCompanyAsyncTest_WhitCorrectData_ShouldReturnTrue() { var context = SteuDbContextInMemoryFactory.InitializeContext(); MapperInitializer.InitializeMapper(); var repository = new EfDeletableEntityRepository <Company>(context); var service = new CompaniesService(repository); var model = new CreateCompanyBindingModel() { Name = "Test Ltd", UIC = "123123123", UserId = "asdasd asd asd asd ", LicenseUrl = "dadadasdadasd", }; var user = new SteuUser() { Id = "asdadas", }; var actualResult = await service.CreateCompanyAsync(model, user); Assert.True(actualResult); }
public async Task GetCompanyManagerByCompanyIdAsync_WithIncorectMenager_ShouldReturnArgumentNullException() { var context = SteuDbContextInMemoryFactory.InitializeContext(); var company = new Company() { Id = "asdasd", Name = "Test Ltd", }; var user = new SteuUser() { Id = "managerId", Manager = false, }; company.Employees.Add(user); await context.AddAsync(company); await context.SaveChangesAsync(); MapperInitializer.InitializeMapper(); var repository = new EfDeletableEntityRepository <Company>(context); var service = new CompaniesService(repository); await Assert.ThrowsAsync <ArgumentNullException>(() => service.GetCompanyManagerByCompanyIdAsync("asdasd")); }
public async Task GetAllCompanyEmployeesByOwnerIdAsync_WithNullUserId_ShouldReturnArgumentNullException() { var context = SteuDbContextInMemoryFactory.InitializeContext(); var service = this.IntializeUsersService(context); var repository = new EfDeletableEntityRepository <SteuUser>(context); var company = new Company() { Id = "companyId", Name = "companyName", }; var user = new SteuUser() { Id = "userId", IsAuthorize = true, Company = company, }; var user2 = new SteuUser() { Id = "userId2", IsAuthorize = true, Company = company, }; await repository.AddAsync(user); await repository.AddAsync(user2); await repository.SaveChangesAsync(); await Assert.ThrowsAsync <ArgumentNullException>(() => service.GetAllCompanyEmployeesByOwnerIdAsync <CompanyUsersViewModel>(null)); }
public async Task GetAllCompanyEmployeesByOwnerIdAsync_WithCorrectData_ShouldReturnCompanyEmp() { var context = SteuDbContextInMemoryFactory.InitializeContext(); var service = this.IntializeUsersService(context); var repository = new EfDeletableEntityRepository <SteuUser>(context); var company = new Company() { Id = "companyId", Name = "companyName", }; var user = new SteuUser() { Id = "userId", IsAuthorize = true, Company = company, }; var user2 = new SteuUser() { Id = "userId2", IsAuthorize = true, Company = company, }; await repository.AddAsync(user); await repository.AddAsync(user2); await repository.SaveChangesAsync(); var actualResult = await service .GetAllCompanyEmployeesByOwnerIdAsync <CompanyUsersViewModel>("userId"); Assert.Single(actualResult); }
protected override IdentityUserLogin <string> CreateUserLogin(SteuUser user, UserLoginInfo login) => new IdentityUserLogin <string> { UserId = user.Id, ProviderKey = login.ProviderKey, LoginProvider = login.LoginProvider, ProviderDisplayName = login.ProviderDisplayName, };
protected override IdentityUserClaim <string> CreateUserClaim(SteuUser user, Claim claim) { var identityUserClaim = new IdentityUserClaim <string> { UserId = user.Id }; identityUserClaim.InitializeFromClaim(claim); return(identityUserClaim); }
public async Task <bool> DeleteUserRolesByUserAsync(SteuUser user) { if (user == null) { throw new ArgumentNullException("User is Null"); } if (user != null) { var roles = await this.userManager.GetRolesAsync(user); await this.userManager.RemoveFromRolesAsync(user, roles.ToArray()); } return(true); }
protected override IdentityUserToken <string> CreateUserToken( SteuUser user, string loginProvider, string name, string value) { var token = new IdentityUserToken <string> { UserId = user.Id, LoginProvider = loginProvider, Name = name, Value = value, }; return(token); }
private async Task LoadSharedKeyAndQrCodeUriAsync(SteuUser user) { // Load the authenticator key & QR code URI to display on the form var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); if (string.IsNullOrEmpty(unformattedKey)) { await _userManager.ResetAuthenticatorKeyAsync(user); unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); } SharedKey = FormatKey(unformattedKey); var email = await _userManager.GetEmailAsync(user); AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey); }
public async Task AddUserLikeModeratorAsync_WithNullUserId_ShouldReturnArgumentNullException() { var context = SteuDbContextInMemoryFactory.InitializeContext(); var service = this.IntializeUsersService(context); var repository = new EfDeletableEntityRepository <SteuUser>(context); var user = new SteuUser() { Id = "userId", }; await repository.AddAsync(user); await repository.SaveChangesAsync(); await Assert.ThrowsAsync <ArgumentNullException>(() => service.AddUserLikeModeratorAsync(null)); }
public async Task ChangeUserIsAuthorizeSatusByUserIdAsync_WithNullStatus_ShouldReturnArgumentNullException() { var context = SteuDbContextInMemoryFactory.InitializeContext(); var service = this.IntializeUsersService(context); var repository = new EfDeletableEntityRepository <SteuUser>(context); var user = new SteuUser() { Id = "userId", IsAuthorize = false, }; await repository.AddAsync(user); await repository.SaveChangesAsync(); await Assert.ThrowsAsync <ArgumentNullException>(() => service.ChangeUserIsAuthorizeSatusByUserIdAsync("userId", null)); }
public async Task AddUserLikeModeratorAsync_WithCorrectData_ShouldReturnTrue() { var context = SteuDbContextInMemoryFactory.InitializeContext(); var service = this.IntializeUsersService(context); var repository = new EfDeletableEntityRepository <SteuUser>(context); var user = new SteuUser() { Id = "userId", }; await repository.AddAsync(user); await repository.SaveChangesAsync(); var actualResult = await service.AddUserLikeModeratorAsync("userId"); Assert.True(actualResult); }
public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information during confirmation."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } if (ModelState.IsValid) { var user = new SteuUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } LoginProvider = info.LoginProvider; ReturnUrl = returnUrl; return(Page()); }
public async Task ChangeUserIsAuthorizeSatusByUserIdAsync_WithCorrectData_ShouldChangeUserStatus() { var context = SteuDbContextInMemoryFactory.InitializeContext(); var service = this.IntializeUsersService(context); var repository = new EfDeletableEntityRepository <SteuUser>(context); var user = new SteuUser() { Id = "userId", IsAuthorize = false, }; await repository.AddAsync(user); await repository.SaveChangesAsync(); await service.ChangeUserIsAuthorizeSatusByUserIdAsync("userId", "true"); var actualResultUser = await repository.All().SingleOrDefaultAsync(x => x.Id == "userId"); Assert.True(actualResultUser.IsAuthorize); }
public async Task CreateCompanyAsyncTest_WithNullUIC_ShouldReturnArgumentNullException() { var context = SteuDbContextInMemoryFactory.InitializeContext(); MapperInitializer.InitializeMapper(); var repository = new EfDeletableEntityRepository <Company>(context); var service = new CompaniesService(repository); var model = new CreateCompanyBindingModel() { Name = "tralala", UIC = null, UserId = "asdasd asd asd asd ", LicenseUrl = "dadadasdadasd", }; var user = new SteuUser() { Id = "asdadas", }; await Assert.ThrowsAsync <ArgumentNullException>(() => service.CreateCompanyAsync(model, user)); }
protected override IdentityUserRole <string> CreateUserRole(SteuUser user, SteuRole role) { return(new IdentityUserRole <string> { RoleId = role.Id, UserId = user.Id }); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? this.Url.Content("~/Identity/Account/Login"); if (this.Input.LicenseUrl == null) { this.ModelState.AddModelError("FileURL", "Please upload file"); } if (this.ModelState.IsValid) { var user = new SteuUser { UserName = this.Input.Username, Email = this.Input.Email, FirstName = this.Input.FirstName, LastName = this.Input.LastName }; var result = await _userManager.CreateAsync(user, this.Input.Password); if (result.Succeeded) { //_logger.LogInformation("User created a new account with password."); //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = Url.Page( // "/Account/ConfirmEmail", // pageHandler: null, // values: new { userId = user.Id, code = code }, // protocol: Request.Scheme); //await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", // $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); if (await _userManager.Users.CountAsync() == 1) { await _userManager.AddToRoleAsync(user, GlobalConstants.AdministratorRoleName); await _userManager.AddToRoleAsync(user, GlobalConstants.ModeratorRoleName); await this.usersService.ChangeUserIsAuthorizeSatusByUserIdAsync(user.Id, "true"); } else { await _userManager.AddToRoleAsync(user, GlobalConstants.OwnerRoleName); await this.usersService.ChangeUserIsAuthorizeSatusByUserIdAsync(user.Id, "false"); } var licenseUrl = await this.cloudinaryService.UploadPictureAsync(this.Input.LicenseUrl, this.Input.LicenseUrl.FileName); CreateCompanyBindingModel model = new CreateCompanyBindingModel() { Name = this.Input.Company, UIC = this.Input.UIC, UserId = user.Id, LicenseUrl = licenseUrl, }; await this.usersService.SetMenagerByIdAsync(user.Id); await _companiesService.CreateCompanyAsync(model, user); return(this.LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { this.ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(this.Page()); }