Contextual permissions in Orchard 1.4.2

Topics: Customizing Orchard
Sep 28, 2012 at 11:05 AM

Hi,

I want to be able to grant or deny permission to edit/delete based on the ContentItem and User in the Admin pannel. 

So For ex:

If I am trying to delete an item in the admin panel I want to be able to call a service and find out whether the user can delete this content or not. I have tried to implement IAuthorizationServiceEventHandler. But the user is able to access the page even after setting it to false. I have ket a break point and its definitely reaching there

 

 

public class XYZ : IAuthorizationServiceEventHandler
    {
        public void Checking(CheckAccessContext context)
        {
        }

        public void Adjust(CheckAccessContext context)
        {
            if (!context.Granted && context.Content != null
                && context.Content.Is<ICommonPart>())
            {
                var contentItem = context.Content.ContentItem;
                if (contentItem.ContentType == "ContentItemTypeIWantToDelete")
                {
                    //Now here we have the content Item as well as user
                    //using this data we can decide whether the user can edit the item or not.
					//As an example I am trying to match content item 123 for user name xyz and 
					//trying to block user access to the page /Admin/Contents/Edit/123
                    if (contentItem.Id == 123 && context.User.UserName == "XYZ")
                    {
                        context.Adjusted = false;
                    }
                    else
                    {
                        context.Adjusted = true;
                    }
                }
            }

        }

        public void Complete(CheckAccessContext context)
        {

        }

Any help is much appreciated. I am using Orchard 1.4.2.

 

Thanks.

Coordinator
Sep 30, 2012 at 9:42 PM

Upgrade to 1.5.1. Content item permissions are in there.