Settings permission on Content with EntityPermissionSet
Im trying to set Permissions on Content from code, but I can't find where this is documented and Bing doesn't return anything usefull either.
What I eventually want to do is:
Set the public access option of the Content to Role Based
Set permissions on a Content item based on the logged in member's member group(s)
What I found so far:
ContentService.ReplaceContentPermissions()
This method takes an EntityPermissionSet
An EntityPermissionSet consists of:
int enitityId // is this the Id of the Content i would like to set permissions on?
IEnumerable<EntityPermissionSet.UserPermission> // I have no idea what this should contain
Some info about this would really be appreciated, since the documentation lacks on this subject.
Also another question:
If, in the razor template, I do a for example: rootnode.Children this returns all published children. However I only want the Children which the current logged in Member has permissions for. Is this possible with the built in security/permission features in Umbraco?
Very late, but I've been working this out for myself and couldn't find any resources, so maybe this will help someone else.
Yes, the entityId is the ID of the content you'd like to set permissions for.
The second parameter is a list of user-permissions combinations. Each item in this list contains a user ID and a permissions string.
Here's an example of setting permissions:
int totalUsers;
// Find all editor users (assuming we don't have more than a 100 users here)
var editorUsers = ApplicationContext.Current.Services.UserService.GetAll(0, 100, out totalUsers).Where(x => x.UserType.Alias == "editor");
var userPermissionsSet = new List<EntityPermissionSet.UserPermission>();
// For each user, add our set of permissions
foreach (var user in editorUsers)
{
foreach (char c in "FPUSHKA")
{
userPermissionsSet.Add(new EntityPermissionSet.UserPermission(user.Id, c.ToString()));
}
}
// Apply the permissions to your content entity
var permissions = new EntityPermissionSet(contentEntityId, userPermissionsSet);
ApplicationContext.Current.Services.ContentService.ReplaceContentPermissions(permissions);
The permissions string has a character for each permission you want to set. Here's a list of possible permissions, as far as I can see in 7.5.6:
I - Culture and hostnames
Z - Audit trail
F - Browse node
7 - Change document type
O - Copy
D - Delete
M - Move
C - Create
P - Public access
U - Publish
R - Permissions
K - Rollback
5 - Send to translation
S - Sort
H - Send to publish
4 - Translate
A - Update
EDIT
Just realised that you need to have a UserPermission for each individual permission-user combination, not a full string of combined permissions. Editing the code above, works fine now.
Settings permission on Content with EntityPermissionSet
Im trying to set Permissions on Content from code, but I can't find where this is documented and Bing doesn't return anything usefull either.
What I eventually want to do is: Set the public access option of the Content to Role Based Set permissions on a Content item based on the logged in member's member group(s)
What I found so far:
ContentService.ReplaceContentPermissions()
This method takes anEntityPermissionSet
An
EntityPermissionSet
consists of:Some info about this would really be appreciated, since the documentation lacks on this subject.
Also another question: If, in the razor template, I do a for example:
rootnode.Children
this returns all published children. However I only want the Children which the current logged in Member has permissions for. Is this possible with the built in security/permission features in Umbraco?Very late, but I've been working this out for myself and couldn't find any resources, so maybe this will help someone else.
Yes, the entityId is the ID of the content you'd like to set permissions for. The second parameter is a list of user-permissions combinations. Each item in this list contains a user ID and a permissions string.
Here's an example of setting permissions:
The permissions string has a character for each permission you want to set. Here's a list of possible permissions, as far as I can see in 7.5.6:
EDIT Just realised that you need to have a
UserPermission
for each individual permission-user combination, not a full string of combined permissions. Editing the code above, works fine now.Hi All,
can we restrict public access for an node to with specific members programmatically ?
Any help on this would be much appreciated. Umbarco v8.11
Thanks, Gurumurthy J V
Just wanted to say thanks to OP and Rahul Sekhar. Helped me solve my permissions issue, for particular content nodes on a legacy site we manage.
👍
is working on a reply...