private void ApplyChanges(AppliedTags change, ContentItem item)
        {
            DetailCollection links = item.GetDetailCollection(Name, false);

            if (links == null)
            {
                if (change.Tags.Count == 0)
                {
                    return;
                }
                links = item.GetDetailCollection(Name, true);
            }

            List <ITag> currentTags = GetCurrentTags(change.Group, links);

            IEnumerable <string> addedTags = GetAddedTags(currentTags, change.Tags);

            foreach (string tagName in addedTags)
            {
                ITag tag = change.Group.GetOrCreateTag(tagName);
                links.Add(tag);
            }

            foreach (ContentItem tag in currentTags)
            {
                if (!change.Tags.Contains(tag.Title))
                {
                    links.Remove(tag);
                }
            }
        }
        private void ApplyChanges(AppliedTags change, ContentItem item)
        {
            DetailCollection links = item.GetDetailCollection(Name, false);
            if (links == null)
            {
                if (change.Tags.Count == 0)
                    return;
                links = item.GetDetailCollection(Name, true);
            }

            List<ITag> currentTags = GetCurrentTags(change.Group, links);

            IEnumerable<string> addedTags = GetAddedTags(currentTags, change.Tags);
            foreach(string tagName in addedTags)
            {
                ITag tag = change.Group.GetOrCreateTag(tagName);
                links.Add(tag);
            }
            
            foreach(ContentItem tag in currentTags)
            {
                if (!change.Tags.Contains(tag.Title))
                    links.Remove(tag);
            }
        }
Example #3
0
		public virtual void UpdateLinks(ContentItem item)
		{
			var referencedItems = FindLinkedObjects(item).ToList();
			DetailCollection links = item.GetDetailCollection(LinkDetailName, false);
			if (links == null && referencedItems.Count == 0)
				return;

			if (links == null)
				links = item.GetDetailCollection(LinkDetailName, true);

			// replace existing items
			for (int i = 0; i < referencedItems.Count && i < links.Count; i++)
			{
				if (links.Details[i].StringValue == referencedItems[i].StringValue)
					// this prevents clearing item references to children when moving hierarchies
					continue;
				links.Details[i].Extract(referencedItems[i]);
			}
			// add any new items
			for (int i = links.Details.Count; i < referencedItems.Count; i++)
			{
				links.Add(referencedItems[i]);
			}
			// remove any extra items
			while (links.Count > referencedItems.Count)
			{
				links.RemoveAt(links.Count - 1);
			}
		}
        public bool Transform(ContentItem item)
        {
            string text = item[Name] as string;
            if(text != null)
            {
                string detailName = Name + "_Tokens";
                int i = 0;
                var p = new Parser(new TemplateAnalyzer());
                foreach (var c in p.Parse(text).Where(c => c.Command != Parser.TextCommand))
                {
                    var dc = item.GetDetailCollection(detailName, true);
                    var cd = ContentDetail.Multi(detailName, stringValue: c.Tokens.Select(t => t.Fragment).StringJoin(), integerValue: c.Tokens.First().Index);
                    cd.EnclosingItem = item;
                    cd.EnclosingCollection = dc;

                    if (dc.Details.Count > i)
                        dc.Details[i] = cd;
                    else
                        dc.Details.Add(cd);
                    i++;
                }
                if (i > 0)
                {
                    var dc = item.GetDetailCollection(detailName, true);
                    for (int j = dc.Details.Count - 1; j >= i; j--)
                    {
                        dc.Details.RemoveAt(j);
                    }
                    return true;
                }
            }
            return false;
        }
Example #5
0
        private IEnumerable <ContentItem> UpdateLinksInternal(ContentItem item, bool recursive)
        {
            logger.DebugFormat("Updating link: {0}", item);

            if (recursive)
            {
                foreach (var part in item.Children.FindParts())
                {
                    foreach (var updatedItem in UpdateLinksInternal(part, recursive))
                    {
                        yield return(updatedItem);
                    }
                }
            }

            var referencedItems    = FindLinkedObjects(item).ToList();
            DetailCollection links = item.GetDetailCollection(LinkDetailName, false);

            if (links == null && referencedItems.Count == 0)
            {
                logger.Debug("Exiting due to no links and none to update");
                yield break;
            }

            if (links == null)
            {
                links = item.GetDetailCollection(LinkDetailName, true);
            }

            logger.DebugFormat("Updating {0} links to {1} existing", referencedItems.Count, links.Count);

            // replace existing items
            for (int i = 0; i < referencedItems.Count && i < links.Count; i++)
            {
                var extracted = referencedItems[i];
                var stored    = links.Details[i];
                if (stored.StringValue == extracted.StringValue && stored.Meta == extracted.Meta && stored.IntValue == extracted.IntValue && stored.BoolValue == extracted.BoolValue)
                {
                    // this prevents clearing item references to children when moving hierarchies
                    continue;
                }
                stored.Extract(extracted);
            }
            // add any new items
            for (int i = links.Details.Count; i < referencedItems.Count; i++)
            {
                links.Add(referencedItems[i]);
            }
            // remove any extra items
            while (links.Count > referencedItems.Count)
            {
                links.RemoveAt(links.Count - 1);
            }

            yield return(item);
        }
Example #6
0
        public void TrackSingleLink()
        {
            mocks.ReplayAll();

            root["TestDetail"] = "<a href='/item1.aspx'>first item</a>";
            persister.Save(root);

            Assert.AreEqual(1, root.GetDetailCollection("TrackedLinks", false).Count);
        }
        public void CanAdd_Role_ToItem()
        {
            DynamicPermissionMap.SetRoles(item, Permission.Write, new[] { "rolename" });

            var collection = item.GetDetailCollection(DynamicPermissionMap.AuthorizedRolesPrefix + Permission.Write, false);

            Assert.That(collection.Count, Is.EqualTo(1));
            Assert.That(collection[0], Is.EqualTo("rolename"));
        }
Example #8
0
        public void UpgradeVersion_CopiesDetailCollections()
        {
            using (persister)
            {
                version.GetDetailCollection("Hello").Add("World");
                persister.Save(version);
                worker.UpgradeVersion(version);
            }

            var newVersion = repository.GetVersion(master, version.VersionIndex);

            version.GetDetailCollection("Hello", createWhenEmpty: false)[0].ShouldBe("World");
        }
Example #9
0
		public void Updates_ItemDetails()
		{
			ContentItem source = CreateOneItem<AnItem>(1, "source", null);
			source["Hello"] = "World";
			source.GetDetailCollection("World", true).Add("Hello");

			ContentItem destination = CreateOneItem<AnItem>(2, "destination", null);
			((N2.Persistence.IUpdatable<ContentItem>)destination).UpdateFrom(source);

			Assert.That(destination["Hello"], Is.EqualTo("World"));
			Assert.That(destination.GetDetailCollection("World", false), Is.Not.Null);
			Assert.That(destination.GetDetailCollection("World", false).Count, Is.EqualTo(1));
			Assert.That(destination.GetDetailCollection("World", false)[0], Is.EqualTo("Hello"));
		}
Example #10
0
        /// <summary>Gets roles allowed for a certain permission stored in a content item.</summary>
        /// <param name="item">The item whose permitted roles get.</param>
        /// <param name="permission">The permission asked for.</param>
        /// <returns>Permitted roles.</returns>
        public static IEnumerable <string> GetRoles(ContentItem item, Permission permission)
        {
            List <string> roles = null;

            foreach (Permission permissionLevel in SplitPermission(permission))
            {
                if (permissionLevel == Permission.Read)
                {
                    foreach (AuthorizedRole role in item.AuthorizedRoles)
                    {
                        AddTo(ref roles, role.Role);
                    }
                    continue;
                }

                DetailCollection roleDetails = item.GetDetailCollection(AuthorizedRolesPrefix + permissionLevel, false);
                if (roleDetails == null)
                {
                    continue;
                }

                foreach (string role in roleDetails)
                {
                    roles = AddTo(ref roles, role);
                }
            }
            return(roles);
        }
Example #11
0
        public void CanRead_DetailCollection(IEnumerable values)
        {
            XmlableItem      item = CreateOneItem <XmlableItem>(1, "item", null);
            DetailCollection dc   = item.GetDetailCollection("Details", true);

            foreach (object detail in values)
            {
                dc.Add(detail);
            }

            ContentItem readItem = Mangle(item);

            DetailCollection readCollection = readItem.GetDetailCollection("Details", false);

            Assert.IsNotNull(readCollection);
            foreach (object detail in values)
            {
                if (detail is string)
                {
                    EnumerableAssert.Contains(readCollection, HttpUtility.HtmlEncode((string)detail));
                }
                else
                {
                    EnumerableAssert.Contains(readCollection, detail);
                }
            }
        }
Example #12
0
        /// <summary>Gets roles allowed for a certain permission stored in a content item.</summary>
        /// <param name="item">The item whose permitted roles get.</param>
        /// <param name="permission">The permission asked for.</param>
        /// <returns>Permitted roles.</returns>
        public static IEnumerable<string> GetRoles(ContentItem item, Permission permission)
        {
            List<string> roles = null;
            foreach (Permission permissionLevel in SplitPermission(permission))
            {
                if(permissionLevel == Permission.Read)
                {
                    foreach(AuthorizedRole role in item.AuthorizedRoles)
                    {
                        AddTo(ref roles, role.Role);
                    }
                    continue;
                }

                DetailCollection roleDetails = item.GetDetailCollection(AuthorizedRolesPrefix + permissionLevel, false);
                if (roleDetails == null)
                    continue;

                foreach(string role in roleDetails)
                {
                    roles = AddTo(ref roles, role);
                }
            }
            return roles;
        }
Example #13
0
        public void CanExport_AndImport_DetailCollection_WithMultipleValues()
        {
            var item       = CreateOneItem <XmlableItem>(1, "item", null);
            var collection = item.GetDetailCollection("World", true);
            var detail     = N2.Details.ContentDetail.Multi("Hello",
                                                            booleanValue: true,
                                                            dateTimeValue: new DateTime(2010, 6, 16),
                                                            doubleValue: 10.7,
                                                            linkedValue: item,
                                                            integerValue: 123,
                                                            objectValue: new[] { 1, 2, 3 },
                                                            stringValue: "World");

            detail.AddTo(collection);

            string      xml        = ExportToString(item, CreateExporter(), ExportOptions.Default);
            ContentItem readItem   = ImportFromString(xml, CreateImporter()).RootItem;
            var         readDetail = readItem.GetDetailCollection("World", false).Details[0];

            Assert.That(readDetail.ValueTypeKey, Is.EqualTo(ContentDetail.TypeKeys.MultiType));
            Assert.That(readDetail.BoolValue, Is.EqualTo(detail.BoolValue));
            Assert.That(readDetail.DateTimeValue, Is.EqualTo(detail.DateTimeValue));
            Assert.That(readDetail.DoubleValue, Is.EqualTo(detail.DoubleValue));
            Assert.That(readDetail.IntValue, Is.EqualTo(detail.IntValue));
            Assert.That(readDetail.LinkedItem, Is.EqualTo(detail.LinkedItem));
            Assert.That(readDetail.ObjectValue, Is.EquivalentTo((IEnumerable)detail.ObjectValue));
            Assert.That(readDetail.StringValue, Is.EqualTo(detail.StringValue));
        }
Example #14
0
        public bool Transform(ContentItem item)
        {
            string text = item[Name] as string;

            if (text != null)
            {
                string collectionName = Name + CollectionSuffix;
                int    i = 0;
                var    p = new Parser(new TemplateAnalyzer());
                foreach (var c in p.Parse(text).Where(c => c.Command != Parser.TextCommand))
                {
                    var dc = item.GetDetailCollection(collectionName, true);
                    var cd = ContentDetail.Multi(collectionName, stringValue: c.Tokens.Select(t => t.Fragment).StringJoin(), integerValue: c.Tokens.First().Index);
                    cd.EnclosingItem       = item;
                    cd.EnclosingCollection = dc;

                    if (dc.Details.Count > i)
                    {
                        dc.Details[i] = cd;
                    }
                    else
                    {
                        dc.Details.Add(cd);
                    }
                    i++;
                }
                if (i > 0)
                {
                    var dc = item.GetDetailCollection(collectionName, true);
                    for (int j = dc.Details.Count - 1; j >= i; j--)
                    {
                        dc.Details.RemoveAt(j);
                    }
                    return(true);
                }
                else if (i == 0)
                {
                    var dc = item.GetDetailCollection(collectionName, false);
                    if (dc != null)
                    {
                        item.DetailCollections.Remove(dc);
                        return(true);
                    }
                }
            }
            return(false);
        }
        protected void ReadDetailCollection(XPathNavigator navigator, ContentItem item, ReadingJournal journal)
        {
            Dictionary<string, string> attributes = GetAttributes(navigator);
            string name = attributes["name"];

            foreach (XPathNavigator detailElement in EnumerateChildren(navigator))
                ReadDetail(detailElement, item.GetDetailCollection(name, true), journal);
        }
Example #16
0
		public void CanClone_Item_WithDetailCollection()
		{
			ContentItem root = CreateOneItem<AnItem>(1, "root", null);
			root.GetDetailCollection("TheDetailCollection", true).Add("TheValue");
			ContentItem clonedRoot = root.Clone(false);

			Assert.AreEqual("TheValue", clonedRoot.GetDetailCollection("TheDetailCollection", false)[0]);
		}
        protected override HashSet<int> GetStoredSelection(ContentItem item)
        {
            var detailLinks = item.GetDetailCollection(Name, false);

            if (detailLinks == null)
                return new HashSet<int>();

            return new HashSet<int>(detailLinks.Details.Where(d => d.LinkValue.HasValue).Select(d => d.LinkValue.Value));
        }
Example #18
0
        /// <summary>Is invoked when an item is beeing saved.</summary>
        /// <param name="item">The item that is beeing saved.</param>
        protected virtual void OnTrackingLinks(ContentItem item)
        {
            var referencedItems    = FindLinkedObjects(item).ToList();
            DetailCollection links = item.GetDetailCollection(LinkDetailName, false);

            if (referencedItems.Count > 0)
            {
                if (links == null)
                {
                    links = item.GetDetailCollection(LinkDetailName, true);
                }
                links.Replace(referencedItems);
            }
            else if (links != null && links.Count > 0)
            {
                links.Clear();
            }
        }
Example #19
0
        public void Track_LinkOnPart()
        {
            mocks.ReplayAll();

            item1.ZoneName      = "I'mAPart";
            item1["TestDetail"] = "<a href='/item1.aspx'>first item</a>";
            persister.Save(root);

            Assert.AreEqual(1, item1.GetDetailCollection("TrackedLinks", false).Count);
        }
Example #20
0
        public virtual void UpdateLinks(ContentItem item)
        {
            logger.DebugFormat("Updating link: {0}", item);

            var referencedItems    = FindLinkedObjects(item).ToList();
            DetailCollection links = item.GetDetailCollection(LinkDetailName, false);

            if (links == null && referencedItems.Count == 0)
            {
                logger.Debug("Exiting due to no links and none to update");
                return;
            }

            if (links == null)
            {
                links = item.GetDetailCollection(LinkDetailName, true);
            }

            logger.DebugFormat("Updating {0} links to {1} existing", referencedItems.Count, links.Count);

            // replace existing items
            for (int i = 0; i < referencedItems.Count && i < links.Count; i++)
            {
                var extracted = referencedItems[i];
                var stored    = links.Details[i];
                if (stored.StringValue == extracted.StringValue && stored.Meta == extracted.Meta && stored.IntValue == extracted.IntValue && stored.BoolValue == extracted.BoolValue)
                {
                    // this prevents clearing item references to children when moving hierarchies
                    continue;
                }
                stored.Extract(extracted);
            }
            // add any new items
            for (int i = links.Details.Count; i < referencedItems.Count; i++)
            {
                links.Add(referencedItems[i]);
            }
            // remove any extra items
            while (links.Count > referencedItems.Count)
            {
                links.RemoveAt(links.Count - 1);
            }
        }
Example #21
0
        protected void ReadDetailCollection(XPathNavigator navigator, ContentItem item, ReadingJournal journal)
        {
            Dictionary <string, string> attributes = GetAttributes(navigator);
            string name = attributes["name"];

            foreach (XPathNavigator detailElement in EnumerateChildren(navigator))
            {
                ReadDetail(detailElement, item.GetDetailCollection(name, true), journal);
            }
        }
        public static void RecordInstalledFeature(this ContentItem root, string feature)
        {
            var features = root.GetDetailCollection(InstallationManager.installationFeatures, true);

            if (features.Contains(feature))
            {
                return;
            }
            features.Add(feature);
        }
Example #23
0
        protected override HashSet <int> GetStoredSelection(ContentItem item)
        {
            var detailLinks = item.GetDetailCollection(Name, false);

            if (detailLinks == null)
            {
                return(new HashSet <int>());
            }

            return(new HashSet <int>(detailLinks.Details.Where(d => d.LinkValue.HasValue).Select(d => d.LinkValue.Value)));
        }
        public static IEnumerable <ImageSizeElement> GetInstalledImageSizes(this ContentItem root)
        {
            var features = root.GetDetailCollection(InstallationManager.installationImageSizes, false);

            if (features == null)
            {
                return(new ImageSizeElement[0]);
            }

            return(features.OfType <string>().Select(s => ImageSizeElement.Parse(s)).ToList());
        }
        public static string[] GetInstalledFeatures(this ContentItem root)
        {
            var features = root.GetDetailCollection(InstallationManager.installationFeatures, false);

            if (features == null)
            {
                return(new string[0]);
            }

            return(features.OfType <string>().ToArray());
        }
Example #26
0
        public static IEnumerable <DisplayableToken> GetTokens(this ContentItem item, string propertyName)
        {
            var tokens = item.GetDetailCollection(propertyName + DisplayableTokensAttribute.CollectionSuffix, false);

            if (tokens == null)
            {
                return(Enumerable.Empty <DisplayableToken>());
            }

            return(tokens.Details.Select(d => d.StringValue.ExtractToken(d.IntValue ?? 0)));
        }
Example #27
0
        // static helpers

        public static List <T> GetStoredItems <T>(string detailName, ContentItem item) where T : ContentItem
        {
            DetailCollection detailCollection = item.GetDetailCollection(detailName, false);

            if (detailCollection == null)
            {
                return(new List <T>());
            }
            return(new List <T>(from d in detailCollection.Details
                                where d.LinkedItem != null
                                select d.LinkedItem as T));
        }
        public override void Write(ContentItem item, string propertyName, System.IO.TextWriter writer)
        {
            var items = item.GetDetailCollection(Name, false);

            if (items != null)
            {
                foreach (var referencedItem in items.OfType<ContentItem>())
                {
                    DisplayableAnchorAttribute.GetLinkBuilder(item, referencedItem, propertyName, null, null).WriteTo(writer);
                }
            }
        }
Example #29
0
        public override void Write(ContentItem item, string propertyName, System.IO.TextWriter writer)
        {
            var items = item.GetDetailCollection(Name, false);

            if (items != null)
            {
                foreach (var referencedItem in items.OfType <ContentItem>())
                {
                    DisplayableAnchorAttribute.GetLinkBuilder(item, referencedItem, propertyName, null, null).WriteTo(writer);
                }
            }
        }
Example #30
0
 protected virtual void OnReadingDetailsInCollection(XPathNavigator navigator, ContentItem item)
 {
     if (navigator.MoveToFirstChild())
     {
         Dictionary <string, string> attributes = GetAttributes(navigator);
         DetailCollection            collection = item.GetDetailCollection(attributes["name"], true);
         do
         {
             OnAddingDetail(navigator, collection);
         } while (navigator.MoveToNext());
         navigator.MoveToParent();
     }
 }
Example #31
0
        public static void StoreEmbeddedPart(this ContentItem item, string keyPrefix, ContentItem part)
        {
            DetailCollection collection = item.GetDetailCollection(keyPrefix, true);

            foreach (var propertyName in ContentItem.KnownProperties.WritablePartProperties)
            {
                SetDetail(item, collection, keyPrefix + "." + propertyName, part[propertyName]);
            }
            foreach (var cd in part.Details)
            {
                SetDetail(item, collection, keyPrefix + "." + cd.Name, cd.Value);
            }
        }
		public override bool UpdateItem(ContentItem item, Control editor)
		{
			CheckBoxList cbl = editor as CheckBoxList;
			List<string> roles = new List<string>();
			foreach (ListItem li in cbl.Items)
				if (li.Selected)
					roles.Add(li.Value);

			DetailCollection dc = item.GetDetailCollection(Name, true);
			dc.Replace(roles);

			return true;
		}
Example #33
0
        public override bool Authorizes(IPrincipal user, ContentItem item, Permission permission)
        {
            if (permission == Permission.None)
            {
                return(true);
            }
            if (!MapsTo(permission))
            {
                return(false);
            }

            bool isContentAuthorized = false;

            foreach (Permission permissionLevel in SplitPermission(permission))
            {
                if (!MapsTo(permissionLevel))
                {
                    continue;
                }

                if ((item.AlteredPermissions & permissionLevel) == Permission.None)
                {
                    continue;
                }

                if (permissionLevel == Permission.Read)
                {
                    if (!item.IsAuthorized(user))
                    {
                        return(false);
                    }

                    isContentAuthorized = true;
                    continue;
                }

                DetailCollection details = item.GetDetailCollection(AuthorizedRolesPrefix + permissionLevel, false);
                if (details != null)
                {
                    string[] rolesAuthorizedByItem = details.ToArray <string>();
                    if (!IsInRoles(user, rolesAuthorizedByItem) && !IsInUsers(user.Identity.Name))
                    {
                        return(false);
                    }

                    isContentAuthorized = true;
                }
            }

            return(isContentAuthorized || base.Authorizes(user, item, permission));
        }
        public bool Transform(ContentItem item)
        {
            string text = item[Name] as string;

            if (text != null)
            {
                string detailName = Name + "_Tokens";
                int    i          = 0;
                var    p          = new Parser(new TemplateAnalyzer());
                foreach (var c in p.Parse(text).Where(c => c.Command != Parser.TextCommand))
                {
                    var dc = item.GetDetailCollection(detailName, true);
                    var cd = new ContentDetail(item, detailName, c.Tokens.Select(t => t.Fragment).StringJoin())
                    {
                        EnclosingCollection = dc, IntValue = c.Tokens.First().Index
                    };
                    if (dc.Details.Count > i)
                    {
                        dc.Details[i] = cd;
                    }
                    else
                    {
                        dc.Details.Add(cd);
                    }
                    i++;
                }
                if (i > 0)
                {
                    var dc = item.GetDetailCollection(detailName, true);
                    for (int j = dc.Details.Count - 1; j >= i; i--)
                    {
                        dc.Details.RemoveAt(j);
                    }
                    return(true);
                }
            }
            return(false);
        }
Example #35
0
		public virtual void UpdateLinks(ContentItem item)
		{
			logger.DebugFormat("Updating link: {0}", item);

			var referencedItems = FindLinkedObjects(item).ToList();
			DetailCollection links = item.GetDetailCollection(LinkDetailName, false);
			if (links == null && referencedItems.Count == 0)
			{
				logger.Debug("Exiting due to no links and none to update");
				return;
			}

			if (links == null)
				links = item.GetDetailCollection(LinkDetailName, true);

			logger.DebugFormat("Updating {0} links to {1} existing", referencedItems.Count, links.Count);

			// replace existing items
			for (int i = 0; i < referencedItems.Count && i < links.Count; i++)
			{
				var extracted = referencedItems[i];
				var stored = links.Details[i];
				if (stored.StringValue == extracted.StringValue && stored.Meta == extracted.Meta && stored.IntValue == extracted.IntValue && stored.BoolValue == extracted.BoolValue)
					// this prevents clearing item references to children when moving hierarchies
					continue;
				stored.Extract(extracted);
			}
			// add any new items
			for (int i = links.Details.Count; i < referencedItems.Count; i++)
			{
				links.Add(referencedItems[i]);
			}
			// remove any extra items
			while (links.Count > referencedItems.Count)
			{
				links.RemoveAt(links.Count - 1);
			}
		}
        public void SelfLinks_InCollections_AreStraightened()
        {
            ContentItem item = CreateOneItem <PersistableItem1>(0, "root", null);

            persister.Save(item);

            var version = versioner.AddVersion(item);

            version.GetDetailCollection("Hello", true).Add(version);

            versioner.ReplaceVersion(item, version, storeCurrentVersion: false);

            item.GetDetailCollection("Hello", false)[0].ShouldBe(item);
        }
Example #37
0
        public static T LoadEmbeddedPart <T>(this ContentItem item, string keyPrefix) where T : ContentItem, new()
        {
            var part       = new T();
            var collection = item.GetDetailCollection(keyPrefix, false);

            if (collection != null)
            {
                foreach (var cd in collection.Details)
                {
                    part[cd.Name.Substring(keyPrefix.Length + 1)] = cd.Value;
                }
            }
            return(part);
        }
Example #38
0
        private ContentItem CreatePageBelow(ContentItem parentPage, int index)
        {
            ContentItem item = CreateOneItem <PersistableItem2>(0, "item" + index, parentPage);

            N2.Details.DetailCollection details = item.GetDetailCollection("DetailCollection", true);
            details.Add(true);
            details.Add(index * 1000 + 555);
            details.Add(index * 1000.0 + 555.55);
            details.Add("string in a collection " + index);
            details.Add(parentPage);
            details.Add(new DateTime(2009 + index, 1, 1));

            engine.Persister.Save(item);
            return(item);
        }
Example #39
0
        IEnumerable <string> GetSelectedTags(ContentItem item, IGroup group)
        {
            DetailCollection links = item.GetDetailCollection(Name, false);

            if (links != null)
            {
                foreach (ContentItem link in links)
                {
                    if (link.Parent != null && link.Parent.Equals(group))
                    {
                        yield return(link.Title);
                    }
                }
            }
        }
Example #40
0
		private IEnumerable<ContentItem> UpdateLinksInternal(ContentItem item, bool recursive)
		{
			logger.DebugFormat("Updating link: {0}", item);

			if (recursive)
				foreach (var part in item.Children.FindParts())
					foreach (var updatedItem in UpdateLinksInternal(part, recursive))
						yield return updatedItem;

			var referencedItems = FindLinkedObjects(item).ToList();
			DetailCollection links = item.GetDetailCollection(LinkDetailName, false);
			if (links == null && referencedItems.Count == 0)
			{
				logger.Debug("Exiting due to no links and none to update");
				yield break;
			}

			if (links == null)
				links = item.GetDetailCollection(LinkDetailName, true);

			logger.DebugFormat("Updating {0} links to {1} existing", referencedItems.Count, links.Count);

			// replace existing items
			for (int i = 0; i < referencedItems.Count && i < links.Count; i++)
			{
				var extracted = referencedItems[i];
				var stored = links.Details[i];
				if (stored.StringValue == extracted.StringValue && stored.Meta == extracted.Meta && stored.IntValue == extracted.IntValue && stored.BoolValue == extracted.BoolValue)
					// this prevents clearing item references to children when moving hierarchies
					continue;
				stored.Extract(extracted);
			}
			// add any new items
			for (int i = links.Details.Count; i < referencedItems.Count; i++)
			{
				links.Add(referencedItems[i]);
			}
			// remove any extra items
			while (links.Count > referencedItems.Count)
			{
				links.RemoveAt(links.Count - 1);
			}

			yield return item;
		}
Example #41
0
        public override bool Authorizes(IPrincipal user, ContentItem item, Permission permission)
        {
            if(permission == Permission.None)
                return true;
            if (!MapsTo(permission))
                return false;

            bool isContentAuthorized = false;

            foreach(Permission permissionLevel in SplitPermission(permission))
            {
                if(!MapsTo(permissionLevel))
                    continue;

                if ((item.AlteredPermissions & permissionLevel) == Permission.None)
                    continue;

                if(permissionLevel == Permission.Read)
                {
                    if(!item.IsAuthorized(user))
                        return false;
                    
                    isContentAuthorized = true;
                    continue;
                }

                DetailCollection details = item.GetDetailCollection(AuthorizedRolesPrefix + permissionLevel, false);
                if(details != null)
                {
                    string[] rolesAuthorizedByItem = details.ToArray<string>();
                    if (!IsInRoles(user, rolesAuthorizedByItem) && !IsInUsers(user.Identity.Name))
                        return false;

                    isContentAuthorized = true;
                }
            }

            return isContentAuthorized || base.Authorizes(user, item, permission);
        }
		public override void UpdateEditor(ContentItem item, Control editor)
		{
			CheckBoxList cbl = editor as CheckBoxList;
			DetailCollection dc = item.GetDetailCollection(Name, false);
			if (dc != null)
			{
				foreach (string role in dc)
				{
					ListItem li = cbl.Items.FindByValue(role);
					if (li != null)
					{
						li.Selected = true;
					}
					else
					{
						li = new ListItem(role);
						li.Selected = true;
						li.Attributes["style"] = "color:silver";
						cbl.Items.Add(li);
					}
				}
			}
		}
Example #43
0
        private static void UpdateCurrentItemData(ContentItem currentItem, ContentItem replacementItem)
        {
            for (Type t = currentItem.GetType(); t.BaseType != null; t = t.BaseType)
            {
                foreach (PropertyInfo pi in t.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
                    if (pi.GetCustomAttributes(typeof(CopyAttribute), true).Length > 0)
                        pi.SetValue(currentItem, pi.GetValue(replacementItem, null), null);

                foreach (FieldInfo fi in t.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
                {
                    if (fi.GetCustomAttributes(typeof(CopyAttribute), true).Length > 0)
                        fi.SetValue(currentItem, fi.GetValue(replacementItem));
                    if (fi.Name == "_url")
                        fi.SetValue(currentItem, null);
                }
            }

            foreach (PropertyData detail in replacementItem.Details.Values)
                currentItem[detail.Name] = detail.Value;

            foreach (PropertyCollection collection in replacementItem.DetailCollections.Values)
            {
                PropertyCollection newCollection = currentItem.GetDetailCollection(collection.Name, true);
                foreach (PropertyData detail in collection.Details)
                    newCollection.Add(detail.Value);
            }
        }
Example #44
0
 /// <summary>Is invoked when an item is beeing saved.</summary>
 /// <param name="item">The item that is beeing saved.</param>
 protected virtual void OnTrackingLinks(ContentItem item)
 {
     IList<ContentItem> referencedItems = FindLinkedItems(item);
     DetailCollection links = item.GetDetailCollection(LinkDetailName, false);
     if (referencedItems.Count > 0)
     {
         if (links == null)
             links = item.GetDetailCollection(LinkDetailName, true);
         links.Replace(referencedItems);
     }
     else if (links != null && links.Count > 0)
     {
         links.Clear();
     }
 }
 IEnumerable<string> GetSelectedTags(ContentItem item, IGroup group)
 {
     DetailCollection links = item.GetDetailCollection(Name, false);
     if (links != null)
     {
         foreach (ContentItem link in links)
             if(link.Parent != null && link.Parent.Equals(group))
                 yield return link.Title;
     }
 }
Example #46
0
        public static void SetRoles(ContentItem item, Permission permission, params string[] roles)
        {
            foreach (Permission permissionLevel in SplitPermission(permission))
            {
                if(permissionLevel == Permission.Read)
                {
                    for (int i = item.AuthorizedRoles.Count - 1; i >= 0; i--)
                    {
                        AuthorizedRole role = item.AuthorizedRoles[i];
                        if(Array.IndexOf(roles, role.Role) < 0)
                            item.AuthorizedRoles.RemoveAt(i);
                    }
                    foreach(string role in roles)
                    {
                        AuthorizedRole temp = new AuthorizedRole(item, role);
                        if(!item.AuthorizedRoles.Contains(temp))
                            item.AuthorizedRoles.Add(temp);
                    }

                    AlterPermissions(item.AuthorizedRoles.Count > 0, item, permissionLevel);
                }
                else
                {
                    string collectionName = AuthorizedRolesPrefix + permissionLevel;
                    if (roles.Length == 0)
                    {
                        if (item.DetailCollections.ContainsKey(collectionName))
                        {
                            item.DetailCollections[collectionName].EnclosingItem = null;
                            item.DetailCollections.Remove(collectionName);
                        }
                    }
                    else
                    {
                        DetailCollection details = item.GetDetailCollection(collectionName, true);
                        details.Replace(roles);
                    }
                    AlterPermissions(roles.Length > 0, item, permissionLevel);
                }
            }
        }
Example #47
0
        public static void SetAllRoles(ContentItem item, Permission permission)
        {
            foreach (Permission permissionLevel in SplitPermission(permission))
            {
                AlterPermissions(false, item, permissionLevel);
                
                if (permissionLevel == Permission.Read)
                {
                    item.AuthorizedRoles.Clear();
                    continue;
                }

                DetailCollection details = item.GetDetailCollection(AuthorizedRolesPrefix + permissionLevel, false);
                if (details != null)
                    item.DetailCollections.Remove(details.Name);
            }
        }
 protected override void ReplaceStoredValue(ContentItem item, IEnumerable<ContentItem> linksToReplace)
 {
     item.GetDetailCollection(Name, true).Replace(linksToReplace);
 }
Example #49
0
		protected virtual void OnReadingDetailsInCollection(XPathNavigator navigator, ContentItem item)
		{
			if (navigator.MoveToFirstChild())
			{
				Dictionary<string, string> attributes = GetAttributes(navigator);
				DetailCollection collection = item.GetDetailCollection(attributes["name"], true);
				do
				{
					OnAddingDetail(navigator, collection);
				} while (navigator.MoveToNext());
				navigator.MoveToParent();
			}
		}
Example #50
0
 public void AddTagToItem(Tag tag, ContentItem item)
 {
     PropertyCollection tags = item.GetDetailCollection("Tags", true);
     if (!tags.Contains(tag))
         tags.Add(tag);
 }
Example #51
0
 public IEnumerable<Tag> GetTags(ContentItem contentItem)
 {
     return contentItem.GetDetailCollection("Tags", true).Cast<Tag>();
 }