+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Query Dynamic Module Permissions Programmatically

Query Dynamic Module Permissions Programmatically

6 posts, 1 answered
  1. MartinM
    MartinM avatar
    2 posts
    Registered:
    09 Dec 2009
    07 Aug 2014
    Link to this post

    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.

  2. Svetoslav Manchev
    Svetoslav Manchev avatar
    735 posts
    Registered:
    07 Dec 2016
    12 Aug 2014
    Link to this post
    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
     
  3. MartinM
    MartinM avatar
    2 posts
    Registered:
    09 Dec 2009
    13 Aug 2014 in reply to Svetoslav Manchev
    Link to this post

    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!

     

    Answered
  4. Svetoslav Manchev
    Svetoslav Manchev avatar
    735 posts
    Registered:
    07 Dec 2016
    14 Aug 2014
    Link to this post
    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
     
  5. Adam
    Adam avatar
    23 posts
    Registered:
    05 Apr 2013
    19 Sep in reply to Svetoslav Manchev
    Link to this post
    I'm also facing the same issue with the code provided. It is always returning false. Please help! thanks
  6. Adam
    Adam avatar
    23 posts
    Registered:
    05 Apr 2013
    19 Sep
    Link to this post

    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");

6 posts, 1 answered