Esempio n. 1
0
 public JsonUserGroup(ApplicationUser user, SiteClaim claim)
 {
     // Set...
     if (user != null && claim != null)
     {
         Id      = 0;
         UserId  = user.Id;
         GroupId = claim.Id;
         Name    = claim.StringValue;
     }
 }
Esempio n. 2
0
 public JsonPostTag(Post post, SiteClaim claim)
 {
     // Set...
     if (post != null && claim != null)
     {
         Id     = 0;
         PostId = post.Id;
         TagId  = claim.Id;
         Name   = claim.StringValue;
     }
 }
Esempio n. 3
0
 public Task <IList <ISiteUser> > GetUsersForClaimAsync(Claim claim, CancellationToken cancellationToken)
 {
     return(Task.Run(async() => {
         SiteClaim sc = (SiteClaim)claim;
         var userList = new List <ISiteUser>();
         var claimList = (from c in _dbContext.UserClaims
                          where c.ClaimId == (from cl in _dbContext.SiteClaims
                                              where cl.Claim == sc.Claim
                                              select cl.Id).First()
                          select c);
         foreach (var obj in claimList)
         {
             userList.Add(await FindByIdAsync(new SecurityIdentifier(obj.UserId, 0).ToString(), new CancellationToken()));
         }
         return userList as IList <ISiteUser>;
     }));
 }
Esempio n. 4
0
 /// <summary>
 /// Json site claim contructor.
 /// </summary>
 /// <param name="post"></param>
 public JsonSiteClaim(SiteClaim claim)
 {
     // Set...
     if (claim != null)
     {
         Id            = claim?.Id ?? 0;
         Type          = claim?.Type;
         Value         = claim?.Value;
         StringValue   = claim?.StringValue;
         DateTimeValue = claim?.DateTimeValue;
         Parent        = claim?.ParentId ?? 0;
         if (claim.Childs != null)
         {
             Childs = new List <JsonSiteClaim>();
             foreach (SiteClaim child in claim.Childs)
             {
                 Childs.Add(new JsonSiteClaim(child));
             }
         }
     }
 }
Esempio n. 5
0
 public Task AddClaimsAsync(ISiteUser user, IEnumerable <Claim> claims, CancellationToken cancellationToken)
 {
     foreach (var claim in claims)
     {
         SiteClaim sc      = claim;
         SiteClaim dbClaim = _dbContext.SiteClaims.Where(a => a.Claim == sc.Claim) as SiteClaim;
         if (dbClaim == null)
         {
             dbClaim = new SiteClaim {
                 Claim = sc.Claim
             };
             _dbContext.SiteClaims.Add(dbClaim);
             _dbContext.SaveChanges();
         }
         _dbContext.UserClaims.Add(new UserClaim {
             ClaimId = dbClaim.Id,
             UserId  = user.Sid
         });
         _dbContext.SaveChanges();
     }
     return(Task.CompletedTask);
 }
Esempio n. 6
0
        /// <summary>
        /// Get current page path.
        /// </summary>
        /// <returns></returns>
        public async Task <Dictionary <string, string> > GetNavPath()
        {
            // Checking...
            if (IsValid() == false ||
                Page == null)
            {
                return(null);
            }
            Page         page                = Page;
            SiteClaim    cat                 = Cat;
            bool         addPage             = false;
            PageProvider provider            = new PageProvider(this);
            Dictionary <string, string> path = new Dictionary <string, string>()
            {
            };
            // Url extension...
            string urlExtension = (_Controller == "Post" && _Action == "Calendar") ? "/cldr" : null;

            // Add the current category...
            if (Post != null && cat != null)
            {
                path.Add(cat.StringValue, page.GetCategoryUrl(this, cat, urlExtension));
            }
            // Add cats...
            if (cat != null && _Controller != null && _Action != null)
            {
                // Add the current cat...
                //path.Add(cat.StringValue, page.GetCategoryUrl(this, cat, urlExtension));
                // Add the parent cats...
                while (cat?.ParentId != null &&
                       (cat = Site?.GetCategory(cat.ParentId.Value)) != null)
                {
                    // Add the parent cat (do not add the page category)...
                    if (cat.Id == page.Category1 ||
                        cat.Id == page.Category2 ||
                        cat.Id == page.Category3 ||
                        cat.Id == page.Category4 ||
                        cat.Id == page.Category5 ||
                        cat.Id == page.Category6 ||
                        cat.Id == page.Category7 ||
                        cat.Id == page.Category8 ||
                        cat.Id == page.Category9 ||
                        cat.Id == page.Category10)
                    {
                        break;
                    }
                    path.Add(cat.StringValue, page.GetCategoryUrl(this, cat, urlExtension));
                }
                // Add the current page...
                addPage = true;
            }
            else if (Post != null)
            {
                // Add the current page...
                addPage = true;
            }
            // Add the current page...
            if (page != null && addPage == true)
            {
                path.Add(page.Title, page.GetUrl(this));
            }
            // Add the parent pages...
            while (page?.ParentId != null &&
                   (page = await provider?.Get(page.ParentId.Value)) != null)
            {
                // Add the parent page...
                path.Add(page.Title, page.GetUrl(this));
            }
            //if (Site.HasRegions == true)
            //{
            //    path.Add(Region?.StringValue, $"/{Region?.StringValue.ToLower()}");
            //}
            //else
            //{
            //    path.Add("Home", "/");
            //}

            // Trace performance...
            AddPerfLog("AppContext::GetNavPath");
            return(path);
        }
Esempio n. 7
0
        /// <summary>
        /// Set site claims.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="inputClaims"></param>
        /// <returns></returns>
        public async Task <bool> SetClaims(int id, IEnumerable <SiteClaim> inputClaims)
        {
            try
            {
                Site site = null;
                // Checking...
                if (inputClaims == null)
                {
                    _Log?.LogError("Failed to set site claims: Invalid claims.");
                    // Trace performance and exit...
                    AppContext?.AddPerfLog("SiteProvider::SetClaims::Invalid claims");
                    return(false);
                }
                else if (AppContext == null ||
                         AppContext.AppDbContext == null ||
                         AppContext.AuthzProvider == null)
                {
                    _Log?.LogError("Failed to set site claims: Invalid contexts.");
                    // Trace performance and exit...
                    AppContext?.AddPerfLog("SiteProvider::SetClaims::Invalid contexts");
                    return(false);
                }
                // Retrieve the site...
                else if ((site = await Get(id)) == null)
                {
                    return(false);
                }
                // Check for update authorization...
                else if (((await AppContext.AuthzProvider.AuthorizeAsync(AppContext.UserPrincipal,
                                                                         site, new List <OperationAuthorizationRequirement>()
                {
                    new OperationAuthorizationRequirement {
                        Name = AuthorizationRequirement.Update
                    }
                }))?.Succeeded ?? false) == false)
                {
                    _Log?.LogWarning("Failed to set site {0}({1}) claims: Access denied.", id, site?.Title);
                    // Trace performance and exit...
                    AppContext?.AddPerfLog("SiteProvider::SetClaims::Access denied");
                    return(false);
                }

                // Add\update claims...
                foreach (SiteClaim inputClaim in inputClaims)
                {
                    if (inputClaim.Id == 0)
                    {
                        // Add as new claim...
                        inputClaim.Site = site;
                        AppContext.AppDbContext.SiteClaims.Add(inputClaim);
                    }
                    else
                    {
                        // Update an existing claim...
                        SiteClaim siteClaim =
                            site.GetClaim(inputClaim.Id);   //No db connection!
                        //AppContext.AppDbContext.SiteClaims.FirstOrDefault(stc => stc.Id == inputClaim.Id);  //This cause a DB connection!
                        if (siteClaim != null)
                        {
                            if (inputClaim.Value != null || inputClaim.StringValue != null)
                            {
                                // Update the claim value...
                                siteClaim.Value       = inputClaim.Value;
                                siteClaim.StringValue = inputClaim.StringValue;
                            }
                            else
                            {
                                // Delete the claim...
                                AppContext.AppDbContext.SiteClaims.Remove(siteClaim);
                            }
                        }
                    }
                }

                // Commit changes...
                bool res = (await AppContext.AppDbContext.SaveChangesAsync() > 0 /*== inputClaims.Count()*/) ? true : false;
                // Trace performance and exit...
                AppContext?.AddPerfLog("SiteProvider::SetClaims");
                return(res);
            }
            catch (Exception e)
            {
                _Log?.LogError("Failed to set site {0} claims: {1}.", id, e.Message);
                // Trace performance and exit...
                AppContext?.AddPerfLog("SiteProvider::SetClaims::Exception");
                return(false);
            }
        }