/// <summary> /// Initializes a new instance of the <see cref="PersonalLinksData" /> class. /// </summary> /// <param name="personLinksSectionList">The person links section list.</param> /// <param name="currentPerson">The current person.</param> public PersonalLinksData(List <PersonLinksSectionData> personLinksSectionList, Person currentPerson) : this() { PersonLinksSectionList = personLinksSectionList.ToArray(); ModificationHash = PersonalLinkService.GetPersonalLinksModificationHash(currentPerson); LastNonSharedLinksModifiedDateTime = PersonalLinksHelper.GetPersonalLinksLastModifiedDateTime(currentPerson) ?? RockDateTime.Now; }
/// <summary> /// Gets the personal links data that the specified person is authorized to view /// </summary> /// <param name="currentPerson">The current person.</param> /// <returns>PersonalLinksData.</returns> public static PersonalLinksData GetPersonalLinksData(Person currentPerson) { var rockContext = new RockContext(); // get the sections (with the Links) that the user is authorized to view var orderedPersonalLinkSectionsWithLinks = new PersonalLinkService(rockContext) .GetOrderedPersonalLinkSectionsQuery(currentPerson) .Include(a => a.PersonAlias) .Include(a => a.PersonalLinks.Select(x => x.PersonAlias)) .AsNoTracking() .ToList() .Where(a => a.IsAuthorized(Rock.Security.Authorization.VIEW, currentPerson)) .Where(a => a.PersonalLinks.Any()).ToList(); int sectionOrder = 0; var personalLinksDataList = orderedPersonalLinkSectionsWithLinks.Select(a => { { PersonLinksSectionData result = new PersonLinksSectionData { Id = a.Id, Name = a.Name, IsShared = a.IsShared, Order = sectionOrder++ }; result.PersonalLinks = a.PersonalLinks .Where(xx => xx.IsAuthorized(Rock.Security.Authorization.VIEW, currentPerson)) .Select(l => new PersonalLinkData { Id = l.Id, Name = l.Name, Url = l.Url, SectionId = l.SectionId, Order = l.Order }) .OrderBy(xx => xx.Order) .ThenBy(xx => xx.Name) .ToList(); return(result); } }).ToList(); // The number of links for a section could be empty after remove ones that we aren't authorized to view (like other people's links). // If so, don't include those sections personalLinksDataList = personalLinksDataList.Where(a => a.PersonalLinks.Any()).ToList(); var personalLinksStorageData = new PersonalLinksData(personalLinksDataList, currentPerson); return(personalLinksStorageData); }
/// <summary> /// Gets current login's LastModifiedDateTime <see cref="Rock.Model.PersonalLink" /> data from <see cref="System.Web.SessionState">Session</see>. /// </summary> /// <remarks> /// We cache PersonalLinksLastUpdateDateTime in session so that we know if the LocalStorage of PersonalLinks needs to be updated. /// </remarks> public static DateTime?GetPersonalLinksLastModifiedDateTime(Person currentPerson) { if (currentPerson == null) { return(null); } DateTime?lastModifiedDateTime = null; if (HttpContext.Current?.Session != null) { // If we already got the LastModifiedDateTime, we'll store it in session so that we don't have to keep asking lastModifiedDateTime = HttpContext.Current.Session[SessionKey.PersonalLinksLastUpdateDateTime] as DateTime?; } else { // If we dont' have a Session, see if we are storing it for the current request; lastModifiedDateTime = HttpContext.Current?.Items[SessionKey.PersonalLinksLastUpdateDateTime] as DateTime?; } if (lastModifiedDateTime.HasValue) { return(lastModifiedDateTime); } // NOTE: Session can be null (probably because it is a REST call). Or maybe it hasn't been set in Session yet So, we'll get it from the Database; var rockContext = new RockContext(); var personAliasQuery = new PersonAliasService(rockContext).Queryable().Where(a => a.PersonId == currentPerson.Id); var sectionLastModifiedDateTimeQuery = new PersonalLinkSectionService(rockContext) .Queryable() .Where(a => !a.IsShared && a.ModifiedDateTime.HasValue && a.PersonAliasId.HasValue && personAliasQuery.Any(x => x.Id == a.PersonAliasId)) .Select(a => a.ModifiedDateTime); var linkLastModifiedDateTimeQuery = new PersonalLinkService(rockContext) .Queryable() .Where(a => a.ModifiedDateTime.HasValue && a.PersonAliasId.HasValue && personAliasQuery.Any(x => x.Id == a.PersonAliasId)) .Select(a => a.ModifiedDateTime); var linkOrderLastModifiedDateTimeQuery = new PersonalLinkSectionOrderService(rockContext) .Queryable() .Where(a => a.ModifiedDateTime.HasValue && personAliasQuery.Any(x => x.Id == a.PersonAliasId)) .Select(a => a.ModifiedDateTime); lastModifiedDateTime = sectionLastModifiedDateTimeQuery.Union(linkLastModifiedDateTimeQuery).Union(linkOrderLastModifiedDateTimeQuery).Max(a => a); if (HttpContext.Current?.Session != null) { if (lastModifiedDateTime == null) { // If LastModifiedDateTime is still null, they don't have any personal links, // So we'll set LastModifiedDateTime to Midnight (so it doesn't keep checking the database) lastModifiedDateTime = RockDateTime.Today; } // If we already got the LastModifiedDateTime, we'll store it in session so that we don't have to keep asking HttpContext.Current.Session[SessionKey.PersonalLinksLastUpdateDateTime] = lastModifiedDateTime; } else { // if we don't have Session, store it in the current request (just in case this called multiple times in the same request) if (HttpContext.Current != null) { HttpContext.Current.Items[SessionKey.PersonalLinksLastUpdateDateTime] = lastModifiedDateTime; } } return(lastModifiedDateTime); }