public static AddEditContentViewModel Populate(this AddEditContentViewModel model, string path, UserContext.UserContextName contextName, int version, ProviderPortalEntities db, UserContext.UserContextInfo userContext) { var content = new ContentViewModel(); model = content.Populate(path, contextName, version, db, userContext).Content; var availableContexts = GetAvailableContexts(path, db); model.ContextsInUse = UserContext.UserContextName.All ^ availableContexts; return(model); }
public static ContentViewModel Populate(this ContentViewModel model, string path, UserContext.UserContextName contextName, int version, ProviderPortalEntities db, UserContext.UserContextInfo userContext) { model = model.PopulateInner(path, contextName, version, db, userContext); // Safely embed if required to prevent infinite recursion if (model.SafeEmbed && model.Status != ContentStatus.ExistingPage) { return(new ContentViewModel { Status = ContentStatus.ExistingPage, Content = new AddEditContentViewModel { Path = path, Embed = true, UserContext = userContext.ContextName }, SafeEmbed = model.SafeEmbed }); } return(model); }
private static ContentViewModel PopulateInner(this ContentViewModel model, string path, UserContext.UserContextName contextName, int version, ProviderPortalEntities db, UserContext.UserContextInfo userContext) { bool canManageContent = Permission.HasPermission(false, true, Permission.PermissionName.CanManageContent); version = canManageContent ? version : PublishedVersion; contextName = canManageContent && contextName != UserContext.UserContextName.None ? contextName : userContext.ContextName; if (String.IsNullOrWhiteSpace(path)) { return(new ContentViewModel { Content = null, Status = ContentStatus.NotFound }); } var cachedVersion = version == PublishedVersion ? ContentCache.Get(path, contextName) : null; if (cachedVersion != null) { return(cachedVersion); } var query = db.Contents.Where(x => x.Path.Equals(path, StringComparison.CurrentCultureIgnoreCase)) .AsQueryable(); var content = version == PublishedVersion ? query.FirstOrDefault(x => x.RecordStatusId == (int)Constants.RecordStatus.Live && ((int)contextName & x.UserContext) != 0) : query.FirstOrDefault(x => x.Version == version); // No content exists for the current user context return an error if (content == null && !canManageContent) { return(new ContentViewModel { Content = null, Status = query.Any() && !HttpContext.Current.Request.IsAuthenticated ? ContentStatus.AuthenticationRequired : ContentStatus.NotFound, SafeEmbed = model.SafeEmbed }); } var otherAvailableContexts = UserContext.UserContextName.None; if (canManageContent) { // Work out what other contexts the content is available in var availableContexts = query .Where(x => x.RecordStatusId == (int)Constants.RecordStatus.Live) .Select(x => new { x.ContentId, x.UserContext }).ToList(); foreach (var item in availableContexts) { if (content == null || content.ContentId != item.ContentId) { otherAvailableContexts |= (UserContext.UserContextName)item.UserContext; } } otherAvailableContexts ^= content == null ? UserContext.UserContextName.None : (UserContext.UserContextName)content.UserContext; // The page doesn't exist, offer to create a new one if (content == null) { var defaultContent = db.Contents.FirstOrDefault(x => x.Path == "DefaultContent") ?? new Content(); return(new ContentViewModel { Content = new AddEditContentViewModel { Version = 1, Path = path, Title = defaultContent.Title, Body = defaultContent.Body, Scripts = defaultContent.Scripts, Styles = defaultContent.Styles, Summary = null, UserContext = UserContext.UserContextName.All ^ otherAvailableContexts, Embed = false, RecordStatusId = (int)Constants.RecordStatus.Pending, LanguageId = defaultContent.LanguageId, ContextsInUse = otherAvailableContexts }, Status = ContentStatus.NewPage, SafeEmbed = model.SafeEmbed }); } } // Page exists and isn't new var result = new ContentViewModel { Content = new AddEditContentViewModel { ContentId = content.ContentId, Version = content.Version, Path = content.Path, Title = content.Title, Body = content.Body, Scripts = content.Scripts, Styles = content.Styles, Summary = null, UserContext = (UserContext.UserContextName)content.UserContext, Embed = content.Embed, RecordStatusId = content.RecordStatusId, LanguageId = content.LanguageId, ContextsInUse = otherAvailableContexts }, Status = ContentStatus.ExistingPage, SafeEmbed = model.SafeEmbed }; if (result.Content.ContentId != 0 && version == PublishedVersion) { ContentCache.Add(result); } return(result); }