/// <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();
        }
        /// <summary>
        /// Extracts Organisation Db from request
        /// </summary>
        /// <param name="context"></param>
        /// <param name="dbIdentifier">Identifier of a db to extract</param>
        /// <returns></returns>
        public static OrganizationDatabase GetOrganisationDatabase(HttpContext context, string dbIdentifier = "")
        {
            //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...

            //grab the specified org db
            //Note: because user config is returned with org databases there is no need to load or dbs explicitly
            var orgDb = UserConfigurationActionFilterAtribute.GetUserConfiguration(context)?.GetOrganizationdatabase(orgId, dbIdentifier);

            if (orgDb == null)
            {
                throw new ArgumentException($"No database found for given organizationId: '{orgId}' and dbIdentifier: '{dbIdentifier}'.");
            }

            return(orgDb);
        }
예제 #4
0
        public async Task <IActionResult> GetUserOrgsAsync()
        {
            var uuid = Cartomatic.Utils.Identity.GetUserGuid();

            //this should not happen really as otherwise the user would not be authenticated
            if (!uuid.HasValue)
            {
                return(BadRequest());
            }

            try
            {
                IEnumerable <Organization> orgs = null;

                var userCfg = UserConfigurationActionFilterAtribute.GetUserConfiguration(HttpContext);
                if (userCfg.IsUser)
                {
                    orgs = await MapHiveUser.GetUserOrganizationsAsync(_dbCtx, uuid.Value);
                }

                //make it possible to return orgs for a token too
                if (userCfg.IsToken)
                {
                    orgs = new[] { await userCfg.Token.GetOrganizationAsync(_dbCtx) }
                }
                ;


                if (!orgs.Any())
                {
                    return(NotFound());
                }

                return(Ok(orgs));
            }
            catch (Exception ex)
            {
                return(HandleException(ex));
            }
        }
 /// <summary>
 /// Resets organization context for given org id; this will cause a fresh reload of organization context for the subsequent api ops
 /// </summary>
 /// <param name="orgId"></param>
 public static void ResetOrganizationContext(Guid orgId)
 {
     UserConfigurationActionFilterAtribute.ResetUserConfigCacheByOrgId(orgId);
 }