8.4+: How to set group permission to Add Files

Permalink
Referring to this:https://documentation.concrete5.org/developers/permissions-access-se...

I need to programmatically set permissions to a group of users to add files to the file manager. The following throws an error: "Call to a member function addListItem() on null"
use Concrete\Core\User\Group\Group;
use Concrete\Core\Permission\Key\Key as PermissionKey;
use Concrete\Core\Permission\Access\Entity\GroupEntity;
$group = Group::getByName('VIPs');
$pk = PermissionKey::getByHandle('add_file');
$pa = $pk->getPermissionAccessObject();
if (is_object($group)) {
    $pae = GroupEntity::getOrCreate($group);
    $pa->addListItem($pae, false, PermissionKey::ACCESS_TYPE_INCLUDE);
}
$pa->markAsInUse();

What am I missing?

Do I also have to assign file replace/edit/delete permissions to enable user to replace files?

Thank you.

linuxoid
 
linuxoid replied on at Permalink Reply
linuxoid
I finally got this to work (not sure if it's the right/best way though):
if (is_object($group)) {
    $file_system = new Filesystem();
    $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']);
    }
}