예제 #1
0
        public async Task UGetRecursive(int siteId)
        {
            LogMaxLevel  = 1;
            onlyUtestLog = true;
            SiteAuthorizationHandler.LogDisabled = true;
            PageAuthorizationHandler.LogDisabled = true;
            PostAuthorizationHandler.LogDisabled = true;

            SiteData siteDt = await DbUtil.GetSiteData(siteId);

            foreach (SiteClaim region in siteDt.regions)
            {
                // Create and init the context...
                WcmsAppContext ctx = await CreateAndInitAppContext(null, siteDt.site.Domain, "/", region.StringValue, null, false);

                if (ctx != null)
                {
                    _Log(1, ctx, $">>Checking page recursive get for region={region.StringValue}...");

                    PageProvider       provider = new PageProvider(ctx);
                    IEnumerable <Page> pages    = await provider?.Get(false, null, true);

                    Assert.NotEqual(null, pages);
                    //TODO: Add a check to validate a get with recusion enabled.
                }
            }
        }
예제 #2
0
 /// <summary>
 /// Log message.
 /// </summary>
 /// <param name="level"></param>
 /// <param name="ctx"></param>
 /// <param name="message"></param>
 protected void _Log(int level, WcmsAppContext ctx, string message)
 {
     if (level > LogMaxLevel)
     {
         return;
     }
     ctx?.Log?.LogInformation(message);
     Output?.WriteLine(message);
 }
예제 #3
0
 public ManageController(
     UserManager <ApplicationUser> userManager,
     SignInManager <ApplicationUser> signInManager,
     IEmailSender emailSender,
     ISmsSender smsSender,
     ILoggerFactory loggerFactory,
     WcmsAppContext appContext)
     : base(appContext)
 {
     _userManager   = userManager;
     _signInManager = signInManager;
     _emailSender   = emailSender;
     _smsSender     = smsSender;
     _logger        = loggerFactory?.CreateLogger("ManageController");
 }
예제 #4
0
 protected void Assert_NotEqual <T>(WcmsAppContext ctx, T notExpected, T actual, string error)
 {
     try
     {
         //if (notExpected?.ToString() == actual?.ToString())
         //{
         //    int brk = 0;
         //}
         Assert.NotEqual(notExpected, actual);
     }
     catch (Exception e)
     {
         _Log(1, ctx, $"{notExpected} equal to {actual}: " + error);
         throw e;
     }
 }
예제 #5
0
        public AccountController(
            UserManager <ApplicationUser> userManager,
            SignInManager <ApplicationUser> signInManager,
            IEmailSender emailSender,
            ISmsSender smsSender,
            ILoggerFactory loggerFactory,
            WcmsAppContext appContext)
            : base(appContext)
        {
            _userManager          = userManager;
            _signInManager        = signInManager;
            _emailSender          = emailSender;
            _smsSender            = smsSender;
            _logger               = loggerFactory.CreateLogger <AccountController>();
            _applicationDbContext = appContext?.AppDbContext;

            //_logger?.LogInformation(1, "@@@@[Account/Login] (CONST).");
        }
예제 #6
0
        public async Task UGet(int siteId)
        {
            LogMaxLevel  = 1;
            onlyUtestLog = true;
            SiteAuthorizationHandler.LogDisabled = true;
            PageAuthorizationHandler.LogDisabled = true;
            PostAuthorizationHandler.LogDisabled = true;
            int countTest      = 0;
            int countQueryTest = 0;

            SiteData siteDt = await DbUtil.GetSiteData(siteId, true);

            foreach (SiteClaim region in siteDt.regions)
            {
                foreach (ApplicationUser user in siteDt.users)
                {
                    string         role = user.HigherRole();
                    WcmsAppContext ctx  = await CreateAndInitAppContext(null, siteDt.site.Domain, "/", region.StringValue, user, false);

                    if (ctx != null)
                    {
                        string testDesc1 = $"region={region.StringValue}, user={user?.UserName} ({role})";
                        _Log(1, ctx, $">>{++countQueryTest}: Checking page get for {testDesc1}...");

                        PageProvider       provider = new PageProvider(ctx);
                        IEnumerable <Page> pages    = await provider?.Get(false, -1);

                        if (user != null && DbUtil.IsSiteUser(siteId, user) == false)
                        {
                            // User extern to the site: cannot view pages.
                            Assert_Equal <IEnumerable <Page> >(ctx, null, pages, $"Check extern to the site failed: {countQueryTest}: {testDesc1}");
                        }
                        else
                        {
                            Assert.NotEqual(null, pages);
                            Assert.NotEqual(0, pages.Count());
                            foreach (Page page in pages)
                            {
                                string testDesc2 = $"page {page?.Id ?? 0}({countQueryTest}-{++countTest})({page?.Title ?? "null"})";
                                string testDescT = $"{testDesc2}: {testDesc1}";
                                _Log(2, ctx, $"  >Checking {testDesc2}...");
                                if (countTest == 2611)
                                {
                                    int brk = 0;
                                }
                                Assert_NotEqual <Page>(ctx, null, page, $"Null page: {testDescT}");
                                page.RequestSite = ctx.Site;

                                _Log(4, ctx, "     Check site ID...");
                                Assert_Equal <int>(ctx, 1, page.SiteId, $"Check site failed: {testDescT}");
                                _Log(4, ctx, "     Check region...");
                                Assert_Equal <bool>(ctx, true, (page.Region1 == 0 || page.Region1 == ctx.Region.Id) ||
                                                    (page.Region2 == 0 || page.Region2 == ctx.Region.Id) ||
                                                    (page.Region3 == 0 || page.Region3 == ctx.Region.Id) ||
                                                    (page.Region4 == 0 || page.Region4 == ctx.Region.Id) ||
                                                    (page.Region5 == 0 || page.Region5 == ctx.Region.Id) ||
                                                    (page.Region6 == 0 || page.Region6 == ctx.Region.Id) ||
                                                    (page.Region7 == 0 || page.Region7 == ctx.Region.Id) ||
                                                    (page.Region8 == 0 || page.Region8 == ctx.Region.Id) ||
                                                    (page.Region9 == 0 || page.Region9 == ctx.Region.Id) ||
                                                    (page.Region10 == 0 || page.Region10 == ctx.Region.Id), $"Check region failed: {testDescT}");
                                _Log(4, ctx, "     Check for authorization...");
                                Assert_Equal <bool>(ctx, true, ((await ctx.AuthzProvider.AuthorizeAsync(ctx.UserPrincipal, page, new List <OperationAuthorizationRequirement>()
                                {
                                    new OperationAuthorizationRequirement {
                                        Name = AuthorizationRequirement.Read
                                    }
                                }))?.Succeeded ?? false),
                                                    $"Check authorization failed: {testDescT}");
                            }
                        }
                    }
                }
            }
        }
예제 #7
0
        public async Task UAuthorisations(int siteId)
        {
            LogMaxLevel  = 1;
            onlyUtestLog = true;
            SiteAuthorizationHandler.LogDisabled = true;
            PageAuthorizationHandler.LogDisabled = true;
            PostAuthorizationHandler.LogDisabled = true;

            SiteData siteDt = await DbUtil.GetSiteData(siteId, true);

            using (AppDbContext dbctx = CreateAndCheckDbContext())
            {
                int totalPage = await dbctx.Pages.CountAsync();

                foreach (SiteClaim region in siteDt.regions) // Not usefull - get the first region.
                {
                    foreach (ApplicationUser user in siteDt.users)
                    {
                        string         role = user.HigherRole();
                        WcmsAppContext ctx  = await CreateAndInitAppContext(null /*dbctx*/, siteDt.site.Domain, "/", region.StringValue, user, false);

                        if (ctx != null)
                        {
                            int skip  = 0;
                            int take  = 200;
                            int count = 0;
                            _Log(1, ctx, $">>Checking page authorization for region={region.StringValue} and user={user?.UserName} ({role})...");
                            while (true)
                            {
                                _Log(2, ctx, $"  >Pages from {skip * take}...");
                                List <Page> pages = await dbctx.Pages.Skip(skip *take).Take(take)
#                                   if !DENORMALIZE
                                                    .Include(p => p.PageGroups)
#                                   endif
                                                    .Include(p => p.Site)
                                                    .AsNoTracking()
                                                    .ToListAsync();

                                if (pages == null || pages.Count == 0)
                                {
                                    // Will assert if we don't test all the existing pages.
                                    Assert.Equal(totalPage, count);
                                    break;
                                }
                                count += pages.Count;

                                foreach (Page page in pages)
                                {
                                    Assert.NotEqual(null, page);
                                    _Log(3, ctx, $"    Checking {page?.Id ?? 0}: {page?.Title ?? "null"}...");
                                    for (int i = 0; i < 4; i += 1)
                                    {
                                        bool authorized        = false;
                                        bool addAuthorized     = false;
                                        bool updateAuthorized  = false;
                                        bool publishAuthorized = false;

                                        // Play with the RequestSite to invole the site membership checks in the authorization module.
                                        switch (i)
                                        {
                                        case 0:
                                            // Simulate case where the user don't have access to the site.
                                            page.RequestSite = null;
                                            break;

                                        case 1:
                                            // Can be like if a user was able to log into a site on which he's not register.
                                            page.RequestSite = siteDt.site;
                                            break;

                                        case 2:
                                            // Force the request site to be the site of the page.
                                            page.RequestSite = page.Site;
                                            break;
                                        }

                                        // Compute authorization...
                                        {
                                            if (page.Site == null || page.RequestSite == null)
                                            {
                                                // We need to have the page site and the site requesting the page.
                                                authorized = false;
                                            }
                                            else if (page.SiteId != page.RequestSite.Id)
                                            {
                                                // A site cannot request pages of another site.
                                                authorized = false;
                                            }
                                            else if (page.Site.Private == true && page.Private == false)
                                            {
                                                // No public page in a private site.
                                                authorized = false;
                                            }
                                            else if (user == null)
                                            {
                                                // Published and public pages are granted to anonymous.
                                                if (page.State == State.Valided && page.Private == false)
                                                {
                                                    authorized = true;
                                                }
                                                else
                                                {
                                                    authorized = false;
                                                }
                                            }
                                            else if (DbUtil.IsSiteUser(page.SiteId, user) == false)
                                            {
                                                // Page can be see only by user of the same site.
                                                authorized = false;
                                            }
                                            // Authenticated users can see only public page and page of theirs groups.
#                                           if !DENORMALIZE
                                            else if (page.Private == true && DbUtil.MemberOf(user, page?.PageGroups) == false)
#                                           else
                                            else if (page.Private == true && user.MemberOf(page) == false)
#                                           endif
                                            {
                                                authorized = false;
                                            }
예제 #8
0
 /// <summary>
 /// The post controller constructor.
 /// </summary>
 /// <param name="appContext"></param>
 public PostController(WcmsAppContext appContext, IEmailSender emailSender)
     : base(appContext, emailSender)
 {
     int i = 0;
 }
예제 #9
0
 /// <summary>
 /// The Home controller constructor.
 /// </summary>
 /// <param name="appContext"></param>
 public HomeController(WcmsAppContext appContext)
     : base(appContext)
 {
     //_Log?.LogDebug("vep.HomeController: {0}", this.GetType().Name);
     //Console.WriteLine($"--- www.vep.HomeController...");
 }
예제 #10
0
        /// <summary>
        /// Create and init the context.
        /// </summary>
        /// <param name="dbctx"></param>
        /// <param name="host"></param>
        /// <param name="path"></param>
        /// <param name="regionName"></param>
        /// <param name="user"></param>
        /// <param name="checkInitResults"></param>
        /// <returns></returns>
        protected async Task <WcmsAppContext> CreateAndInitAppContext(AppDbContext dbctx, string host, string path, string regionName, ApplicationUser user = null, bool checkInitResults = true)
        {
            // Check the service provider...
            Assert.NotEqual(null, _serviceProvider);

            // Create and init the http context...
            var httpContext = new DefaultHttpContext();

            Assert.NotEqual(null, httpContext);
            // Configure the http context...
            httpContext.RequestServices = _services.BuildServiceProvider();
            Assert.NotEqual(null, httpContext.RequestServices);
            httpContext.Request.Host = new HostString(host);
            httpContext.Request.Path = new PathString(path);
            // Add user to the http context...
            if (user != null)
            {
                UserClaimsPrincipalFactory <ApplicationUser, IdentityRole> clmFact
                    = _GetRequiredServicee <UserClaimsPrincipalFactory <ApplicationUser, IdentityRole> >(httpContext.RequestServices);
                Assert.NotEqual(null, clmFact);
                httpContext.User = /*ClaimsPrincipal upp =*/ await clmFact.CreateAsync(user);

                Assert.NotEqual(null, httpContext.User /*upp*/);
                //httpContext.User = new ClaimsPrincipal(upp);
            }

            // Create and init the route context...
            var routeContext = new RouteContext(httpContext);

            Assert.NotEqual(null, routeContext);
            // Configure the route context...
            routeContext.RouteData = new RouteData();
            Assert.NotEqual(null, routeContext.RouteData);
            routeContext.RouteData.Values.Add(CRoute.RegionTagName, regionName);

            // Build loger factory...
            var logFactory =
                _GetRequiredServicee <ILoggerFactory>(httpContext.RequestServices);

            Assert.NotEqual(null, logFactory);
#if DEBUG
            //logFactory.AddConsole(_LogFilter);
            logFactory.AddDebug(_LogFilter);
#endif

            // Create and init the context...
            WcmsAppContext ctx =
                _GetRequiredServicee <WcmsAppContext>(httpContext.RequestServices);
            Assert.NotEqual(null, ctx);
            Assert.Equal(null, ctx.User);
            Assert.Equal(null, ctx.Site);
            //ctx.UnitTestInit(dbctx);
            int initSiteAsyncRes = await ctx.InitSiteAsync(httpContext,
                                                           _GetRequiredServicee <IAuthorizationService>(httpContext.RequestServices));

            bool initRouteAsyncRes = await ctx.InitRouteAsync(routeContext);

            if (checkInitResults == true)
            {
                Assert.Equal(3, initSiteAsyncRes); // No module registered.
                Assert.Equal(true, initRouteAsyncRes);
            }
            return(ctx);
        }
예제 #11
0
 /// <summary>
 /// The Home controller constructor.
 /// </summary>
 /// <param name="appContext"></param>
 public DfideHomeController(WcmsAppContext appContext)
     : base(appContext)
 {
 }
예제 #12
0
 /// <summary>
 /// The post controller constructor.
 /// </summary>
 /// <param name="appContext"></param>
 public DfidePostController(WcmsAppContext appContext, IEmailSender emailSender)
     : base(appContext, emailSender)
 {
 }