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:
$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.

linuxoid
 
linuxoid replied on at Permalink Reply
linuxoid
Anyone?
linuxoid replied on at Permalink Reply
linuxoid
3rd time lucky?
mnakalay replied on at Permalink Reply
mnakalay
Are you sure the code you posted actually does set permissions?

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']);
    }
linuxoid replied on at Permalink Reply
linuxoid
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.
mnakalay replied on at Permalink Reply
mnakalay
linuxoid replied on at Permalink Reply
linuxoid
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
mnakalay replied on at Permalink Reply
mnakalay
So this required some investigation but I have a working piece of code.

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();

There might be easier ways that don't require so many steps. All I know is this one works.

I hope it helps.
linuxoid replied on at Permalink Reply
linuxoid
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?