Query Dynamic Module Permissions Programmatically
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.
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"
);
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!
Hello Martin,
Thank you for the shared solution with the community.
Regards,
Svetoslav Manchev
Telerik
I'm also facing the same issue with the code provided. It is always returning false. Please help! thanks
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");