private async Task <TenantEntity> GetDefaultTenantAsync() { var tenant = await _tenantRepository.FirstOrDefaultAsync(t => t.TenancyName == "Default"); if (tenant == null) { throw new AbpException("There should be a 'Default' tenant if multi-tenancy is disabled!"); } return(tenant); }
public async Task Create(RoleCreateDto input) { if (await _repository.FirstOrDefaultAsync(x => x.RoleCode == input.RoleCode) != null) { throw new UserFriendlyException(string.Format(L("DuplicateRoleCode"), input.RoleCode)); } var role = input.MapTo <RoleEntity>(); role.Id = Guid.NewGuid(); await _repository.InsertAsync(role); }
public async Task <Guid> CreateUser(CreateUserDto input) { var user = input.MapTo <UserEntity>(); var userId = Guid.NewGuid(); user.Id = Guid.NewGuid(); if (!input.Password.IsNullOrEmpty()) { var rs = await new PasswordValidator().ValidateAsync(input.Password); rs.CheckErrors(); } else { input.Password = UserEntity.CreateRandomPassword(); } user.Password = new PasswordHasher().HashPassword(input.Password); if (await _repository.FirstOrDefaultAsync(x => x.UserName == user.UserName) != null) { throw new UserFriendlyException(string.Format(L("Identity.DuplicateName"), user.UserName)); } if (await _repository.FirstOrDefaultAsync(x => x.Email == user.Email) != null) { throw new UserFriendlyException(string.Format(L("Identity.DuplicateEmail"), user.Email)); } if (AbpSession.TenantId.HasValue && AbpSession.TenantId != default(Guid)) { user.TenantId = AbpSession.TenantId.Value; } await _repository.InsertAsync(user); return(user.Id); }
public async Task <GetCurrentLoginInformationsOutput> GetCurrentLoginInformations() { var userId = AbpSession.UserId ?? default(Guid); var userInfo = await _userRepository.FirstOrDefaultAsync(userId); var output = new GetCurrentLoginInformationsOutput { User = userInfo.MapTo <UserLoginInfoDto>() }; if (AbpSession.TenantId.HasValue) { var tenantInfo = await _tenantRepository.FirstOrDefaultAsync(AbpSession.TenantId.Value); output.Tenant = tenantInfo.MapTo <TenantLoginInfoDto>(); } return(output); }
public async Task CreateOrUpdate(IEnumerable <RoleRightDto> inputs) { foreach (var item in inputs) { var rs = item.MapTo <RoleRightEntity>(); if (!item.Id.HasValue) { if (await _roleRightRepository.FirstOrDefaultAsync( x => x.ActionModuleId == item.ActionModuleId && x.RoleId == item.RoleId) == null) { rs.Id = Guid.NewGuid(); await _roleRightRepository.InsertAsync(rs); } } else { await _roleRightRepository.UpdateAsync(rs); } } }
public async Task CreateOrUpdate(IEnumerable <ActionModuleDto> inputs) { foreach (var item in inputs) { var mapped = item.MapTo <ActionModuleEntity>(); if (!item.Id.HasValue) { if ( await _actionModuleRepository.FirstOrDefaultAsync( x => x.ActionId == item.ActionId && x.ModuleId == item.ModuleId) == null) { mapped.Id = Guid.NewGuid(); await _actionModuleRepository.InsertAsync(mapped); } } else { await _actionModuleRepository.UpdateAsync(mapped); } } }
public async Task CreateTenant(CreateTenantDto input) { var tenant = new TenantEntity(input.TenancyName, input.DisplayName) { IsActive = input.IsActive }; if (await _repository.FirstOrDefaultAsync(x => x.TenancyName == tenant.TenancyName) != null) { throw new UserFriendlyException(string.Format(L("TenancyNameIsAlreadyTaken"), tenant.TenancyName)); } await _repository.InsertAsync(tenant); await CurrentUnitOfWork.SaveChangesAsync(); //To get new tenant's id. //We are working entities of new tenant, so changing tenant filter CurrentUnitOfWork.SetFilterParameter(AbpDataFilters.MayHaveTenant, AbpDataFilters.Parameters.TenantId, tenant.Id); }
public async Task <LoginResultDto> Login(string userNameOrEmailAddress, string plainPassword, string tenancyName = null) { if (userNameOrEmailAddress.IsNullOrEmpty()) { throw new ArgumentNullException("userNameOrEmailAddress"); } if (plainPassword.IsNullOrEmpty()) { throw new ArgumentNullException("plainPassword"); } TenantEntity tenant = null; if (!_multiTenancyConfig.IsEnabled) { tenant = await GetDefaultTenantAsync(); } else if (!string.IsNullOrWhiteSpace(tenancyName)) { tenant = await _tenantRepository.FirstOrDefaultAsync(t => t.TenancyName == tenancyName); if (tenant == null) { return(new LoginResultDto(LoginResultType.InvalidTenancyName)); } if (!tenant.IsActive) { return(new LoginResultDto(LoginResultType.TenantIsNotActive)); } } using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MustHaveTenant)) { var user = await _repository.FirstOrDefaultAsync(x => x.TenantId == tenant.Id && (x.UserName == userNameOrEmailAddress || x.Email == userNameOrEmailAddress)); if (user == null) { return(new LoginResultDto(LoginResultType.InvalidUserNameOrEmailAddress)); } var verificationResult = new PasswordHasher().VerifyHashedPassword(user.Password, plainPassword); if (verificationResult != PasswordVerificationResult.Success) { return(new LoginResultDto(LoginResultType.InvalidPassword)); } if (!user.IsActive) { return(new LoginResultDto(LoginResultType.UserIsNotActive)); } var info = user.MapTo <UserEditDto>(); return(new LoginResultDto(info, CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie))); } }
public async Task <ModuleEditDto> GetModule(IdInput <Guid> input) { var rs = await _repository.FirstOrDefaultAsync(x => x.Id == input.Id); return(rs.MapTo <ModuleEditDto>()); }
public async Task <ActionEditDto> GetAction(IdInput <Guid> id) { var action = await _repository.FirstOrDefaultAsync(x => x.Id == id.Id); return(action.MapTo <ActionEditDto>()); }