public async Task <Result <DataTableResult <AuditTableModel> > > Get(DataTableRequest dataTableRequest, AuditTableRequest auditTableRequest) { ValidationResult validationResult = _dataTableRequestValidator.Validate(dataTableRequest); if (!validationResult.IsValid) { _logger.LogWarning($"Invalid {nameof(DataTableRequest)} model"); return(Result.Fail <DataTableResult <AuditTableModel> >(validationResult.ToResultError())); } ValidationResult auditTabeValidationResult = _auditTableRequestValidator.Validate(auditTableRequest); if (!auditTabeValidationResult.IsValid) { _logger.LogWarning($"Invalid {typeof(AuditTableRequest).Name} model"); return(Result.Fail <DataTableResult <AuditTableModel> >(validationResult.ToResultError())); } IBaseSpecificationBuilder <AuditEntity> baseSpecification = SpecificationBuilder .Create <AuditEntity>() .WithActionType(auditTableRequest.ActionType) .InRange(auditTableRequest.From?.UtcDateTime, auditTableRequest.To?.UtcDateTime) .WithUser(_identityUIUserInfoService.GetUserId()); IBaseSpecification <AuditEntity, AuditTableModel> selectSpecification = baseSpecification .OrderBy(x => x.Created, auditTableRequest.OrderBy.Value) .Paginate(dataTableRequest.Start, dataTableRequest.Length) .Select(x => new AuditTableModel( x.Id, x.ActionType.GetDescription(), x.ResourceName, x.Created.ToString("o"))) .Build(); int auditsCount = await _auditDAO.Count(baseSpecification.Build()); List <AuditTableModel> audits = await _auditDAO.Get(selectSpecification); DataTableResult <AuditTableModel> dataTableResult = new DataTableResult <AuditTableModel>( draw: dataTableRequest.Draw, recordsTotal: auditsCount, recordsFiltered: auditsCount, data: audits); return(Result.Ok(dataTableResult)); }
private async Task <Result <AppUserEntity> > GetAppUser() { string userId = _identityUIUserInfoService.GetUserId(); AppUserEntity appUser = await _userManager.FindByIdAsync(userId); if (appUser == null) { _logger.LogError($"No user. UserId {userId}"); return(Result.Fail <AppUserEntity>("No User", "no_user")); } if (appUser.TwoFactorEnabled) { _logger.LogError($"User already has TwoFactorAuthentication enabled. UserId {userId}"); return(Result.Fail <AppUserEntity>("2fa_already_enabled", "Two factor authentication is already enabled")); } return(Result.Ok(appUser)); }
private List <RoleListData> CanAssigneRolesOld() { string userId = _identityUIUserInfoService.GetUserId(); string groupId = _identityUIUserInfoService.GetGroupId(); bool hasGlobalAccess = _identityUIUserInfoService.HasPermission(IdentityUIPermissions.GROUP_CAN_MANAGE_ROLES); List <RoleListData> roles; if (hasGlobalAccess) { roles = GetAllGroupRoles(); } else { roles = GetRoleAssignmes(userId, groupId); } return(roles); }
public async Task <Result> CreatePassword(CreatePasswordRequest createPasswordRequest) { ValidationResult validationResult = _createPasswordValidator.Validate(createPasswordRequest); if (!validationResult.IsValid) { _logger.LogWarning($"Invalid {nameof(CreatePasswordRequest)} model"); return(Result.Fail(validationResult.Errors)); } string userId = _identityUIUserInfoService.GetUserId(); AppUserEntity appUser = await _userManager.FindByIdAsync(userId); if (appUser == null) { _logger.LogError($"No User. UserId {userId}"); return(Result.Fail("no_user", "No User")); } _logger.LogTrace($"Creating password. UserId {userId}"); IdentityResult identityResult = await _userManager.AddPasswordAsync(appUser, createPasswordRequest.NewPassword); if (!identityResult.Succeeded) { _logger.LogError($"Failed to add password. UserId {userId}"); return(Result.Fail(identityResult.Errors)); } Result loginResult = await _loginService.Login(userId); if (loginResult.Failure) { _logger.LogError($"Failed to log in user after password was added. UserId {userId}"); } return(Result.Ok()); }
private async Task <Result> Start(AppUserEntity appUser) { string impersonizerId = _identityUIUserInfoService.GetImpersonatorId(); if (!string.IsNullOrEmpty(impersonizerId)) { _logger.LogError($"User is already impersonating somebody"); return(Result.Fail(ALREADY_IMPERSONATING)); } string userId = _identityUIUserInfoService.GetUserId(); if (userId == appUser.Id) { _logger.LogError($"Can not impersonate self"); return(Result.Fail(CAN_NOT_IMPERSONATE_SELF)); } appUser.SessionCode = Guid.NewGuid().ToString(); Result addSessionResult = await _sessionService.Add(appUser.SessionCode, appUser.Id); if (addSessionResult.Failure) { return(Result.Fail(FAILED_TO_ADD_SESSION)); } string loggedInUserId = _identityUIUserInfoService.GetUserId(); appUser.ImpersonatorId = loggedInUserId; _logger.LogInformation($"User is starting to impersonate another user. ImpersnonazerId {loggedInUserId}, user to be impersonalized {appUser.Id}"); await _signInManager.SignOutAsync(); await _signInManager.SignInAsync(appUser, false); return(Result.Ok()); }
public async Task <CredentailsViewModel> GetViewModel() { string userId = _identityUIUserInfoService.GetUserId(); AppUserEntity appUser = await _userManager.FindByIdAsync(userId); IList <UserLoginInfo> logins = await _userManager.GetLoginsAsync(appUser); CredentailsViewModel credentailsViewModel = new CredentailsViewModel( hasPassword: !string.IsNullOrEmpty(appUser.PasswordHash), hasExternalLoginProvider: logins.Count > 0, externalLoginProvider: logins.Select(x => x.ProviderDisplayName).SingleOrDefault()); return(credentailsViewModel); }
public override string GetUserId() { return(_identityUIUserInfoService.GetUserId()); }