Programmatically setting access rights for assets folder

Vote:
 

Is there a way to programmatically restrict access to a assets folder?

I have tried the following code, which didn't affect the access rights:

var folder = _repository.GetDefault<ContentFolder>(SiteDefinition.Current.GlobalAssetsRoot);
folder.Name = "Restricted folder";
var contentReference = _repository.Save(folder, SaveAction.Publish, AccessLevel.NoAccess);
var folderSecurity = _securityRepository.Get(contentReference).CreateWritableClone() as IContentSecurityDescriptor;
if (folderSecurity != null)
{
    if (folderSecurity.IsInherited) folderSecurity.ToLocal();

    var webadminAccess = folderSecurity.Entries.SingleOrDefault(e => e.Name == "WebAdmins");
    if (webadminAccess != null) folderSecurity.RemoveEntry(webadminAccess);
    folderSecurity.AddEntry(new AccessControlEntry("WebAdmins", AccessLevel.Read));

    _securityRepository.Save(contentReference, folderSecurity, SecuritySaveType.ReplaceChildPermissions);
}

    

#81184
Feb 11, 2014 11:15
- Mar 09, 2021 2:47
Where did you write this code. In Initialization module?
Vote:
 

Solved it. I used the wrong SecuritySaveType on line 13. Should have been _securityRepository.Save(contentReference, folderSecurity, SecuritySaveType.Replace);

#81640
Feb 21, 2014 12:11
Vote:
 

its working only for groups, not for individual user, any idea why its not working for users?

#147976
Apr 29, 2016 8:30
Vote:
 
   folderSecurity.AddEntry(new AccessControlEntry("daniel.ovaska", AccessLevel.Read, SecurityEntityType.User));

Need to change the type of SecurityEntityType above...

#147989
Apr 29, 2016 10:47
Vote:
 

Thanks Daniel Ovaska, this fixes my issue.

#147993
Apr 29, 2016 11:05
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.