*/ class AdminService { /** * Set a new Owner, removing the old owner(s) * * @param string $resourceUri * @param string $userUri * @return bool */ public static function setOwner($resourceUri, $userUri) { /** @var DataBaseAccess $db */ $db = self::getServiceManager()->get(DataBaseAccess::SERVICE_ID); // Needs better abstraction $dbRow = $db->getUsersWithPermissions([$resourceUri]); foreach ($dbRow as $row) { if ($row['resource_id'] == $resourceUri && $row['privilege'] == 'OWNER') { $db->removePermissions($row['user_id'], $resourceUri, ['OWNER']); } } return $db->addPermissions($userUri, $resourceUri, ['OWNER']); } /** * Get a list of users with permissions for a given resource * * Returns an associative array with userid as key and an array of rights as value * * @param $resourceId * @return array */ public static function getUsersPermissions($resourceId) { return self::getRolePrivilegeRetriever()->retrieveByResourceIds([$resourceId]); } /** * recursivly add permissions to a class and all instances * @param \core_kernel_classes_Class $class * @param $userUri * @param $rights */ public static function addPermissionToClass(\core_kernel_classes_Class $class, $userUri, $rights) { /** @var DataBaseAccess $dbAccess */ $dbAccess = self::getServiceManager()->get(DataBaseAccess::SERVICE_ID); $dbAccess->addPermissions($userUri, $class->getUri(), $rights); foreach ($class->getInstances(false) as $instance) { $dbAccess->addPermissions($userUri, $instance->getUri(), $rights); } foreach ($class->getSubClasses(false) as $subclass) { self::addPermissionToClass($subclass, $userUri, $rights); } } public static function getServiceManager() { return ServiceManager::getServiceManager(); } private static function getRolePrivilegeRetriever(): RolePrivilegeRetriever { return self::getServiceManager()->get(RolePrivilegeRetriever::class); } }