Has anyone else tried to programatically apply permissions to folders and experience some unexpected results? When permissions are saved with markAsInUse()
on a child folder, it seems to apply the new permissions to the parent folder the child previously inherited from even if it is explicitly set to override on the child. I have created a github issue with some sample code but was wondering if anyone on the forum would have some insight as well?
Hello. That page of the documentation is, unfortunately, outdated. Here’s the correct code:
<?php
$fileSystem = new \Concrete\Core\File\Filesystem();
$root = $fileSystem->getRootFolder();
$newFolder = \Concrete\Core\Tree\Node\Type\FileFolder::add('Test Folder', $root);
$newFolder->setTreeNodePermissionsToOverride();
$permissionEntity = \Concrete\Core\Permission\Access\Entity\GroupEntity::getOrCreate(\Group::getByID(GUEST_GROUP_ID));
$permissionKey = \Concrete\Core\Permission\Key\Key::getByHandle('view_file_folder_file');
$permissionKey->setPermissionObject($newFolder);
$permissionAssignmentObject = $permissionKey->getPermissionAssignmentObject();
$pa = \Concrete\Core\Permission\Access\Access::create($permissionKey);
// let's get the existing permissions
$accessListItems = $permissionAssignmentObject->getPermissionAccessObject()->getAccessListItems(\Concrete\Core\Permission\Key\Key::ACCESS_TYPE_ALL);
/* If we added the new list item to the existing Permission Access object
instead of a new one we would end up with the same situation
where the parent gets impacted. So we create a new one, assign it the existing
permission access object and add the new list item to it.
Also for some reason, if we do $pa->setListItems($accessListItems);
instead of the foreach loop below, the parent gets impacted. */
foreach($accessListItems as $accessListItem) {
$pa->addListItem($accessListItem->getAccessEntityObject(), $accessListItem->getPermissionDurationObject(), $accessListItem->getAccessType());
}
$pa->addListItem($permissionEntity, false, \Concrete\Core\Permission\Key\Key::ACCESS_TYPE_EXCLUDE);
$permissionAssignmentObject->assignPermissionAccess($pa);
I hope this helps.
2 Likes
Thank you very much! This is exactly what was needed.
I appreciate you taking the time to provide your insight on this.
Hopefully the docs can be updated at some point to help others in the future too.
Happy to help whenever I can
I Added it in the documentation, although for some reason it doesn’t appear in the list of topics just yet