Пример #1
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="appContext"></param>
 public SiteApiController(Services.WcmsAppContext appContext)
     : base(appContext)
 {
     provider = new SiteProvider(appContext);
 }
Пример #2
0
        /// <summary>
        /// Init the application context.
        /// Return -1: Access denied.
        /// Return  0: When the site doesn't exist.
        /// Return  1: When the site exist.
        /// Return  2: When the site exist but the region doesn't exist.
        /// Return  3: When the site module cannot be found.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="authzProvider"></param>
        /// <returns></returns>
        public async Task <int> InitSiteAsync(HttpContext context, IAuthorizationService authzProvider)
        {
            int res = -1;

            // Clean...
            _Clean();
            // Checking...
            if (context == null)
            {
                Log?.LogError("Failed to init the app context: Invalid context.");
                // Trace performance...
                AddPerfLog("AppContext::InitSiteAsync::Invalid provider");
                return(res);
            }
            // Init...
            AuthzProvider = authzProvider;

            // En cours: SiteId, group and region dans les data utilisateur
            // ==> a relire de la base ==> rajouter un cache!!!
            // Init context properties...
            Host          = context.Request.Host;
            UserPrincipal = context.User;
            if (UserPrincipal != null)
            {
                await GetUser();

                if (_InvalidUserSite == true)
                {
                    Log?.LogError("Failed to init the app context: Invalid user site.");
                    // Trace performance...
                    AddPerfLog("AppContext::InitSiteAsync::Invalid user site");
                    return(-1);
                }
                // Trace performance...
                AddPerfLog("AppContext::InitSiteAsync::User information");
            }
            AppDbContext.TimeSpan = TimeSpan = DateTime.Now.TimeOfDay;
            SiteProvider siteProvider = new SiteProvider(this);

            // Get site module...
            if ((Module = Factory.GetModule(context.Request.Host.Value)) == null)
            {
                // The site module cannot be found.
                res = 3;
            }
            // Load site from domain...
            else if ((Site = await siteProvider?.Get(Module.Domain)) != null)
            {
                // The site exist and the user can view it.
                res = 1;
            }
            else if (siteProvider.Exist != 1)
            {
                // The site doesn't exist.
                res = 0;
            }
            // The site cannot be viewed by the user.
            // Check right depending on the site url...
            else if ((Site = await siteProvider?.Get(Module.Domain, context)) != null)
            {
                // The asked url can be viewed.
                res = 1;
            }

            // Only in case where the site is accessible to the user...
            if (res == 1)
            {
                Log?.LogDebug("Site: {0}, Module: {1}.", Site.Title, Module.Name);

                // Total of regions...
                RegionCount = (Site.HasRegions == false) ? 0 : (Site?.GetRegions()?.Count ?? 0);
                // In case of an API, try to get the region from the context...
                // Because IRouter is not handling web api since the 1.0.0RTM version:
                //  * https://github.com/aspnet/Announcements/issues/193
                //  * https://github.com/aspnet/Routing/issues/321
                if ((context.Request?.Path.ToString()?.StartsWith("/api") ?? false) == true)
                {
                    _GetRegion(context, ref res);
                }
            }

            // Trace performance...
            AddPerfLog("AppContext::InitSiteAsync");
            return(res);
        }