Adding permissions to a media folder
Access permissions to media folders are given to groups. The permissions set for a folder apply to all subfolders and files in that folder.
Note: To run the sample code in this article the accelerator site needs to be set up.
The following properties need to be defined for the AddFolderPermission method used to assign the permissions.
- folderSystemId: The folder that the permissions should apply to.
- userGroupSystemId: The permissions are added to groups defined in the Customers area.
The following code demonstrates how to get the folder and check if the group already has any type of access to the folder. If the group does not have access, a new permission is added to the access control list for the particular group, using the Operations class in the Litium.Security namespace.
public void AddFolderPermission(Guid folderSystemId, Guid userGroupSystemId)
{
var folder = _folderService.Get(folderSystemId);
if(folder == null)
{
return;
}
folder = folder.MakeWritableClone();
var hasAccess = folder.AccessControlList.Any(x => x.GroupSystemId == userGroupSystemId);
if(!hasAccess)
{
folder.AccessControlList.Add(new Security.AccessControlEntry(Operations.Entity.Read, userGroupSystemId));
}
_folderService.Update(folder);
}
The following code calls the above implementation. Users with anonymous access are identified by calling the PersonService, and then the group that those users are part of is found.
//Get anonymous user.
var personService = IoC.Resolve<PersonService>();
var anonymousUser = personService.Get(SecurityContextService.Everyone.SystemId);
var anonymousUserGroupSystemId = anonymousUser?.GroupLinks.FirstOrDefault()?.GroupSystemId ?? Guid.Empty;
mediaService.AddFolderPermission(mediaFolder.SystemId, anonymousUserGroupSystemId);
Changing folder permissions
If you change the permissions for an existing folder in back office, and want that change to apply to all subfolders and files in that folder, select the checkbox Same permissions for subfolders and files. If it is not selected, the original permission settings will remain for the subfolders and files in the folder. The above programmatic example does not cover this scenario.