Set permissions on file

Permalink
Hi,
In an add-on I import some files. The files are stored in a folder (or set) per year. Now I want to set permissions on that file. With a page there is something like $page->assignPermissions(...), but how do I do that with files?

Thanks!

SnefIT
 
SnefIT replied on at Permalink Reply
SnefIT
After some code-sniffing I've found a way. It adjusts the permission on the file itself.

// Assume we have a file ($file) and an userinfo object ($userinfo).
// Access entity (who?).
$ae = \Concrete\Core\Permission\Access\Entity\UserEntity::getOrCreate($userinfo);
// Permission key (what?).
$pk = \Concrete\Core\Permission\Key\Key::getByHandle('view_file');
// Set permission object.
$pk->setPermissionObject($file);
// Permission access.
$pa = \Concrete\Core\Permission\Access\Access::create($pk);
// Add access entity.
$pa->addListItem($ae);
// Get permission assignment object.
$pt = $pk->getPermissionAssignmentObject();
// Assign permissions.
$pt->assignPermissionAccess($pa);


I've taken the code from the Filesystem::setDefaultPermissions and played around with it.
When I check the permissions on the file, I see that (only) the given user has view rights.