Query Dynamic Module Permissions Programmatically

Posted by Community Admin on 04-Aug-2018 22:49

Query Dynamic Module Permissions Programmatically

All Replies

Posted by Community Admin on 07-Aug-2014 00:00

Hi,

I have created a dynamic type module using the module builder and set the security permissions (from backend) to view that type as "Administrators only". The purpose is to restrict the content viewable on the frontend to specific users.

To further secure the content, I am customizing the search results to return only those items which the user has permission to view. I am basing my custom module on this extension of the searchresults module: http://www.sitefinity.com/blogs/veselin-vasilev-blog/2013/03/18/trim-the-search-results-based-on-permissions-roles.

I would like to do the same but for dynamic module content items (i.e. the module I created in module builder) - how do I do that?

I don't care about individual items; what I would like to secure the entire type by checking if the current user has permissions to view that specific dynamic type (i.e. check permissions for current user for type "Telerik.Sitefinity.DynamicTypes.Model.X").

Thanks for your input/help.

Posted by Community Admin on 12-Aug-2014 00:00

Hi Martin,

I order to check the permissions granted for example to:
Module "TestModule"- Content type "TestContentType" of User "username" to Create Item of this type, you can check them by:

 bool hasAccess = CheckForPermissionsForUser("username");
 
 public static bool CheckForPermissionsForUser(string userName)
            
     var manager = ModuleBuilderManager.GetManager();
     Type contentType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.TestModule.TestContentType");
   
     var item = manager.Provider.GetDynamicModuleTypes()
         .Where(t => t.TypeName == contentType.Name && t.TypeNamespace == contentType.Namespace).Single();
      
     UserManager usersManager = UserManager.GetManager();
 
     Guid[] users = new Guid[] usersManager.GetUser(userName).Id ;
     return item.IsGranted(SecurityConstants.Sets.General.SetName, users, new string[] "Create");
 

More information about manage permissions programatically is available here.

Regards,
Svetoslav Manchev
Telerik
 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

Posted by Community Admin on 13-Aug-2014 00:00

Hi Svetoslav,

Your answer was great except for the line

return item.IsGranted(SecurityConstants.Sets.General.SetName, users, new string[] "Create");

as it always returned 'false'; I think the SetName ("General") was not correct for module builder types, or at least, in my case it was not. Instead, I changed it to:

return SecurityExtensions.IsSecurityActionTypeGranted(item, SecurityActionTypes.View);

 which gave me  what I wanted.

Thanks for your help!

 

Posted by Community Admin on 14-Aug-2014 00:00

Hello Martin,

Thank you for the shared solution with the community.

Regards,
Svetoslav Manchev
Telerik

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

Posted by Community Admin on 19-Sep-2016 00:00

I'm also facing the same issue with the code provided. It is always returning false. Please help! thanks

Posted by Community Admin on 19-Sep-2016 00:00

Ok, think I figured it out. The problem is the Guid[] parameter. If that is supplied, then the code checks if their is a permission assigned DIRECTLY to that user. If it sent as null then the code checks the roles for the permission and user. WTF?

so, this worked for me 

item.IsGranted(SecurityConstants.Sets.General.SetName, (Guid[]) null, "View");

This thread is closed