/// <summary> /// Use this to get a user context for the system user, useful /// if you need to impersonate the user to perform an action with elevated /// privileges. /// </summary> public virtual async Task <IUserContext> GetSystemUserContextAsync() { // BUG: Got a managed debugging assistant exception? Try this: // https://developercommunity.visualstudio.com/content/problem/29782/managed-debugging-assistant-fatalexecutionengineer.html var dbUser = await QuerySystemUser().FirstOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(dbUser, SuperAdminRole.SuperAdminRoleCode); var impersonatedUserContext = _userContextMapper.Map(dbUser); return(impersonatedUserContext); }
/// <summary> /// Queries the database for the system user and returns the result as an <see cref="IUserContext"/> /// projection. The result of this is cached in the <see cref="GetSystemUserContextAsync"/> method. /// </summary> protected virtual async Task <IUserContext> QuerySystemUserContextAsync() { var dbUser = await _dbContext .Users .Include(u => u.Role) .FilterByUserArea(CofoundryAdminUserArea.Code) .FilterEnabled() .Where(u => u.IsSystemAccount) .FirstOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(dbUser, "IsSystemAccount"); var impersonatedUserContext = _userContextMapper.Map(dbUser); return(impersonatedUserContext); }
private async Task <ExecutionContext> GetImpersonatedUserContext(IExecutionContext executionContext, int userId) { var dbUser = await _dbContext .Users .Include(u => u.Role) .FilterByUserArea(CofoundryAdminUserArea.AreaCode) .FilterById(userId) .SingleOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(dbUser, userId); var impersonatedUserContext = _userContextMapper.Map(dbUser); var impersonatedExecutionContext = new ExecutionContext() { ExecutionDate = executionContext.ExecutionDate, UserContext = impersonatedUserContext }; return(impersonatedExecutionContext); }