8.4+ How to add/remove group permissions to Files programmatically?
Permalink
Hello.
How can I add group permissions to the File Manager programmatically, that is equivalent to adding group permissions in the Dashboard -> System & Settings -> Files -> File Manager Permissions -> Add File?
I can add group permissions to a folder:
But how do I do that so that it's set in the Dashboard?
I tried this, but it doesn't work:
And how do I remove it on package uninstall?
Thank you.
How can I add group permissions to the File Manager programmatically, that is equivalent to adding group permissions in the Dashboard -> System & Settings -> Files -> File Manager Permissions -> Add File?
I can add group permissions to a folder:
$group = Group::getByName('Group Name'); if (is_object($group)) { $file_system = $this->app->make(Filesystem::class); $root_folder = $file_system->getRootFolder(); $folder_name = t('My Folder'); $folder = FileFolder::getNodeByName($folder_name); if (!is_object($folder) || !$folder instanceof FileFolder) { $folder->assignPermissions($group, ['add_file']); } }
But how do I do that so that it's set in the Dashboard?
I tried this, but it doesn't work:
$pk = PermissionKey::getByHandle('add_file'); $pk->setPermissionObject($folder); $pa = $pk->getPermissionAccessObject(); $pe = GroupEntity::getOrCreate($group); $pa->addListItem($pe, false, PermissionKey::ACCESS_TYPE_INCLUDE);
And how do I remove it on package uninstall?
Thank you.
Anyone?
3rd time lucky?
Are you sure the code you posted actually does set permissions?
You have this
so basically if the folder exists the permission is not applied.
I think it should be
You have this
if (!is_object($folder) || !$folder instanceof FileFolder) { $folder->assignPermissions($group, ['add_file']); }
so basically if the folder exists the permission is not applied.
I think it should be
if (is_object($folder) && $folder instanceof FileFolder) { $folder->assignPermissions($group, ['add_file']); }
I have no idea how that happened, but you're right, I should set the permissions only if the folder exists.
But it's not the point. My question is how can I add group permissions to the File Manager programmatically, that is equivalent to adding group permissions in the Dashboard -> System & Settings -> Files -> File Manager Permissions -> Add File? I can't figure out or find out in any documentation or internet how to do that.
But it's not the point. My question is how can I add group permissions to the File Manager programmatically, that is equivalent to adding group permissions in the Dashboard -> System & Settings -> Files -> File Manager Permissions -> Add File? I can't figure out or find out in any documentation or internet how to do that.
I believe this is what you want:https://documentation.concrete5.org/developers/permissions-access-se...
I've already read that. Either I need other permissions or that code doesn't work.
I need to set file upload permissions to a group during a package installation that is equivalent to adding group permissions in the Dashboard -> System & Settings -> Files -> File Manager Permissions -> Add File
I need to set file upload permissions to a group during a package installation that is equivalent to adding group permissions in the Dashboard -> System & Settings -> Files -> File Manager Permissions -> Add File
So this required some investigation but I have a working piece of code.
There might be easier ways that don't require so many steps. All I know is this one works.
I hope it helps.
use Group; use Concrete\Core\Tree\Node\Type\FileFolder; use PermissionKey; use Concrete\Core\Permission\Access\Entity\GroupEntity as GroupPermissionAccessEntity; $group = Group::getByName('Test group'); if (is_object($group)) { $folder_name = t('My Test Folder'); $folder = FileFolder::getNodeByName($folder_name); if (is_object($folder) && $folder instanceof FileFolder) { $pk = PermissionKey::getByHandle('add_file'); $pk->setPermissionObject($folder); $pt = $pk->getPermissionAssignmentObject(); if (is_object($pt)) { $pt->clearPermissionAssignment(); $pa = $pk->getPermissionAccessObject();
Viewing 15 lines of 25 lines. View entire code block.
There might be easier ways that don't require so many steps. All I know is this one works.
I hope it helps.
mnakalay,
Thank you very much for your suggestion, that really seemed to work for 1 group, I see the group permission to add files has been added to the File Manager during installation.
But I have, say, 2 groups and 2 folders, each are set within 2 different functions during installation, one after the other. The above only sets permissions for the 1st group, the 2nd group is ignored, 2nd group permissions don't get added to the File Manager permissions. Do you know why?
Is there any way to check what the actual folder permissions are now?
Thank you very much for your suggestion, that really seemed to work for 1 group, I see the group permission to add files has been added to the File Manager during installation.
But I have, say, 2 groups and 2 folders, each are set within 2 different functions during installation, one after the other. The above only sets permissions for the 1st group, the 2nd group is ignored, 2nd group permissions don't get added to the File Manager permissions. Do you know why?
Is there any way to check what the actual folder permissions are now?