This project is read-only.

Filtering content

Topics: Customizing Orchard
Jun 9, 2011 at 12:57 PM

I'm trying to find a way to filter content - actually hiding and preventing some items from being shown (even identified as existing).

Ideally I would create a ContentPart where I can set the rules of that item being able to be shown. The rules will be based on some external library, and doesn't necessarily (or will not at all, in my case) have to be based in Orchard, but the ContentPart will be used for input. Where would the filtering be placed? Is there an example somewhere that uses this logic?

Jun 9, 2011 at 1:03 PM

I would say a Controller is the best place for such logic.

What type of content are you filtering? Content Items? or some external data?

Jun 9, 2011 at 1:07 PM

Yes, Content Items. The thing is though that I want to filter existing items as well - such as menu items and existing pages. Even things that exist in current modules. Need be, I will modify existing modules to solves this - but if there was some place I could filter it before the modules get the results (or some other general place in the life cycle) that would be even better I think.

Jun 9, 2011 at 1:28 PM

it depends on what you want to filter it by.

What happens is that on the DB, you only Have the Content Items Id, which is an autoincrement. Everyting else gets linked based on what parts are associated with the Content Item. The way you would filter would be by using the ContentManager.Query method.

I'm still a bit fuzzy regarding what your looking for.


Jun 9, 2011 at 1:35 PM

The case I have is the following: I have a site that handles logins (stored externally from orchard). The user-objects in turn belong to UserGroups. I want to somehow hook this UserGroup so that I can show different content in a menu (for example) depending on what UserGroup the user belongs to. I was thinking about adding a part where you can specify all the UserGroups that the content is allowed to be shown to - but I can't really figure out how to actually force this to the results.

Jun 9, 2011 at 1:54 PM

There are now a few ways to achieve this kind of thing (until recently there was nothing!)

- Steve Meyers' Very Simple Permissions module. This has a Part which adds a checkbox allowing you to make content "private" so only logged in users can see it. Obviously that just handles a single case and can't really have any custom logic plugged in (although you could use it as an example to write your own module with different logic)

- Piotr Szmyd's Content Authorization module. This does most of what you're after and has extensibility points so you can add your own logic.

- Mechanics - which you're already using - has a few ways it can be applied to filtering content. Firstly, any Sockets can be filtered when they're displayed. You can implement ISocketHandler (in the latest source) and use OnFiltering to add a filter object into the context, which lets you filter the content using a Linq query. You can look at SequenceSocketEvents.cs to see how the sorting is applied over SequencePart; you can do the same to add any of your own filtering or sorting logic.

Then there's my Quanta module, which leverages Mechanics to create roles-based connectors, so it can define per-item permissions. It also has Groups which allow you to define content permissions for groups of users, which is exactly what you're asking for. Unfortunately there's is currently no support for View permissions (due to them not existing in Orchard), only Edit/Delete, but I'll be getting around to this soon now it's possible in a couple of ways; and adding an ISocketHandler to perform view filtering based on them.

Finally you asked about menus - the Cartography module (also in Science Project!) gives you a Mechanics-based menu system, where again you could apply ISocketHandler filters (and of course Quanta view permissions will get automatically applied once they're working)

So there are a lot of options, it just depends what granularity of control you need. For reference there are a few fairly lengthy discussions on this topic in general:

Jun 9, 2011 at 3:06 PM

So, he should add a Migration that alters the content type definition,lets say for a menu item, by adding the custom part that allows him to filter? Am I following this right?

Also, how exactly are Sockets filtered when their displayed? Does that happen in the shape for the SocketPart?


Jun 9, 2011 at 3:28 PM

Sorry, I didn't describe that clearly :)

SocketsPart is a component of mechanics that renders all the items connected to the current item - so it only filters that connected list, not the parent item itself.

So it's kind of like ContainerPart, if you imagine that ContainerPart had an event that could filter the list as it gets displayed!

To hide the actual SocketsPart item itself you'd have to use any of thise other methods of view control.

Regarding your first question, that depends on what you were filtering. I'm not sure if Piotr's module lets you filter menu items, since menu items aren't usually rendered as content. However with my Cartography menu, the menu is content, and SocketsPart handles the hierarchy. So with Cartography you wouldn't need to add anything, just hook into ISocketHandler to check for MenuChild connectors and you can filter them. And with the next Quanta update, it would get filtered automatically based on user and group item permissions.

Jun 10, 2011 at 6:43 AM

Okay, I've checked out the modules you suggested, and I've come to this conclusion:

I'll be using Szmyd's Content Authorization module for the actual authorization, adding on a ContentPart to be able to control what items at all I want to filter. This will enable me to add on all the custom handling that I need for this.

As you say, the menu (I'm actually using Szymod's menu at the moment) does not have ContentItems as menu items, so I will be switching over to Cartography to handle that in the same way.

Do you see any upcoming issues with this solution? The only thing I want to be able to do is "hide" items (both menu items and pages) to certain users, depending on an external library (specifically a string defining what group he or she belongs to),

Jun 10, 2011 at 12:57 PM

Yep, that exactly what you'll be able to do. It doesn't work right now but it's something I wanted to support. Quanta already gives you a new content type called "Group" which you can add users and content to. So I'll write a socket filter based around that, to show or hide the menus. It's not hard to do, I just need to decide which of three slightly different ways to do it :) I'll try and get that working over the weekend so you can see it in action!

Jul 30, 2011 at 7:53 PM

#randompete. Can we allready see it in action? I also need to filter menu items based on user roles.

Dec 22, 2011 at 11:41 AM
randompete wrote:

- Steve Meyers' Very Simple Permissions module. This has a Part which adds a checkbox allowing you to make content "private" so only logged in users can see it. Obviously that just handles a single case and can't really have any custom logic plugged in (although you could use it as an example to write your own module with different logic)


Do u know what the name are for the module u are refering to is?, is it published at the gallery?


Feb 13, 2012 at 3:18 PM

Hi pete,

It is already a long time since you reacted on this post. But I was wandering if the menu filtering based on groups was already available. I tried it with the science modules, but couldnt find it/get it to work. Is it already available?