/// <summary> /// Gets org context for the controller /// </summary> /// <param name="context"></param> /// <returns></returns> public static Organization GetOrganizationContext(HttpContext context) { //Note: as described in the OnActionExecuting, org related stuff is now always retrieved by the UserConfiguration attribute. //so provided an action is not an anonymous action, all the needed stuff should really be here! var orgId = GetOrganizationId(context); //Note: dependancy on the UserConfigurationActionFilterAtribute may not be that sensible. but i think should not hurt really... var org = UserConfigurationActionFilterAtribute.GetOrganization(context, orgId); if (org == null) { throw new ArgumentException("No organization found."); } return(org); }
/// <inheritdoc /> public override async Task OnActionExecutionAsync(ActionExecutingContext actionContext, ActionExecutionDelegate next) { //need to trigger db migrations as configured in order to enable the api to automigrate if (_orgDbMigrator != null && actionContext.ActionArguments.ContainsKey(OrgIdPropertyName)) { var orgId = (Guid)actionContext.ActionArguments[OrgIdPropertyName]; //avoid migrating more than once per app pool life time; when api gets re-published this should reset, so migrations shoudl kick in automatically if (!_autoOrgMigrationsCache.ContainsKey(orgId) || !_autoOrgMigrationsCache[orgId]) { //grab the org var org = UserConfigurationActionFilterAtribute.GetOrganization(actionContext.HttpContext, orgId); if (org != null) { EnsureUserImpersonation(actionContext); await _orgDbMigrator(org); _autoOrgMigrationsCache[orgId] = true; } } } //standard migrator with no org context - such apis can exist too ;) if (_dbMigrator != null && !_nonOrgCtxMigrated) { //try to grab the context... var ctrl = (IDbCtxController)actionContext.Controller; if (ctrl != null) { EnsureUserImpersonation(actionContext); await _dbMigrator(ctrl.GetDefaultDbContext()); _nonOrgCtxMigrated = true; } } await next(); }