2024-10-17 13:56:32 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace OCA\OrganizationFolders\Service;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
2024-11-06 19:58:57 +01:00
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
|
2024-10-17 13:56:32 +02:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
|
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
|
|
|
|
2024-11-04 19:24:12 +01:00
|
|
|
use OCA\GroupFolders\ACL\UserMapping\UserMappingManager;
|
|
|
|
use OCA\GroupFolders\ACL\Rule;
|
|
|
|
|
2024-10-17 13:56:32 +02:00
|
|
|
use OCA\OrganizationFolders\Db\Resource;
|
|
|
|
use OCA\OrganizationFolders\Db\FolderResource;
|
|
|
|
use OCA\OrganizationFolders\Db\ResourceMapper;
|
2024-11-04 19:24:12 +01:00
|
|
|
use OCA\OrganizationFolders\Model\OrganizationFolder;
|
2024-11-12 15:36:07 +01:00
|
|
|
use \OCA\OrganizationFolders\Model\Principal;
|
2024-11-16 03:06:21 +01:00
|
|
|
use OCA\OrganizationFolders\Enum\ResourceMemberPermissionLevel;
|
2024-11-12 15:36:07 +01:00
|
|
|
use OCA\OrganizationFolders\Enum\PrincipalType;
|
2024-10-17 13:56:32 +02:00
|
|
|
use OCA\OrganizationFolders\Errors\InvalidResourceType;
|
|
|
|
use OCA\OrganizationFolders\Errors\ResourceNotFound;
|
2024-10-30 01:51:48 +01:00
|
|
|
use OCA\OrganizationFolders\Errors\ResourceNameNotUnique;
|
2024-11-04 19:24:12 +01:00
|
|
|
use OCA\OrganizationFolders\Manager\PathManager;
|
|
|
|
use OCA\OrganizationFolders\Manager\ACLManager;
|
2024-11-05 00:45:44 +01:00
|
|
|
use OCA\OrganizationFolders\OrganizationProvider\OrganizationProviderManager;
|
2024-10-17 13:56:32 +02:00
|
|
|
|
|
|
|
class ResourceService {
|
|
|
|
public function __construct(
|
2024-11-05 00:45:44 +01:00
|
|
|
protected ResourceMapper $mapper,
|
|
|
|
protected PathManager $pathManager,
|
2024-11-04 19:24:12 +01:00
|
|
|
protected ACLManager $aclManager,
|
2024-11-05 00:45:44 +01:00
|
|
|
protected UserMappingManager $userMappingManager,
|
|
|
|
protected OrganizationProviderManager $organizationProviderManager,
|
2024-11-06 19:58:57 +01:00
|
|
|
protected OrganizationFolderService $organizationFolderService,
|
|
|
|
protected ContainerInterface $container,
|
2024-10-17 13:56:32 +02:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2024-11-12 15:36:07 +01:00
|
|
|
/**
|
|
|
|
* @param int $organizationFolderId
|
|
|
|
* @psalm-param int $organizationFolderId
|
|
|
|
* @param int|null $parentResourceId
|
|
|
|
* @psalm-param int|null $parentResourceId
|
|
|
|
* @param array $filters
|
|
|
|
* @psalm-param array $filters
|
|
|
|
* @psalm-return Resource[]
|
|
|
|
*/
|
|
|
|
public function findAll(int $organizationFolderId, ?int $parentResourceId = null, array $filters = []): array {
|
2024-11-04 19:24:12 +01:00
|
|
|
return $this->mapper->findAll($organizationFolderId, $parentResourceId, $filters);
|
2024-10-30 01:51:48 +01:00
|
|
|
}
|
|
|
|
|
2024-10-17 13:56:32 +02:00
|
|
|
private function handleException(Exception $e, int $id): void {
|
|
|
|
if ($e instanceof DoesNotExistException ||
|
|
|
|
$e instanceof MultipleObjectsReturnedException) {
|
|
|
|
throw new ResourceNotFound($id);
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function find(int $id): Resource {
|
|
|
|
try {
|
|
|
|
return $this->mapper->find($id);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->handleException($e, $id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-05 17:40:32 +01:00
|
|
|
public function findByFileId(int $fileId): FolderResource {
|
|
|
|
// TODO: improve error handling
|
|
|
|
return $this->mapper->findByFileId($fileId);
|
|
|
|
}
|
|
|
|
|
2024-10-17 13:56:32 +02:00
|
|
|
/* Use named arguments to call this function */
|
|
|
|
public function create(
|
|
|
|
string $type,
|
2024-10-30 01:49:41 +01:00
|
|
|
int $organizationFolderId,
|
2024-10-30 01:51:48 +01:00
|
|
|
string $name,
|
2024-11-04 19:24:12 +01:00
|
|
|
?int $parentResourceId = null,
|
2024-10-17 13:56:32 +02:00
|
|
|
bool $active = true,
|
2024-11-06 17:32:33 +01:00
|
|
|
bool $inheritManagers = true,
|
2024-10-17 13:56:32 +02:00
|
|
|
|
|
|
|
?int $membersAclPermission = null,
|
|
|
|
?int $managersAclPermission = null,
|
|
|
|
?int $inheritedAclPermission = null,
|
|
|
|
) {
|
|
|
|
if($type === "folder") {
|
|
|
|
$resource = new FolderResource();
|
|
|
|
} else {
|
|
|
|
throw new InvalidResourceType($type);
|
|
|
|
}
|
|
|
|
|
2024-11-04 19:24:12 +01:00
|
|
|
if(!$this->mapper->existsWithName($organizationFolderId, $parentResourceId, $name)) {
|
2024-10-31 17:29:15 +01:00
|
|
|
$resource->setOrganizationFolderId($organizationFolderId);
|
2024-10-30 01:51:48 +01:00
|
|
|
$resource->setName($name);
|
|
|
|
$resource->setActive($active);
|
2024-11-06 17:32:33 +01:00
|
|
|
$resource->setInheritManagers($inheritManagers);
|
2024-10-30 01:51:48 +01:00
|
|
|
$resource->setLastUpdatedTimestamp(time());
|
2024-10-17 13:56:32 +02:00
|
|
|
|
2024-11-04 19:24:12 +01:00
|
|
|
if(isset($parentResourceId)) {
|
|
|
|
$parentResource = $this->find($parentResourceId);
|
|
|
|
|
2024-11-05 00:45:44 +01:00
|
|
|
if($parentResource->getOrganizationFolderId() === $organizationFolderId) {
|
2024-11-12 15:36:07 +01:00
|
|
|
if($parentResource->getType() !== "folder") {
|
|
|
|
throw new Exception("Only folder resources can have sub-resources");
|
|
|
|
} else {
|
|
|
|
$resource->setParentResource($parentResource->getId());
|
|
|
|
}
|
2024-11-05 00:45:44 +01:00
|
|
|
} else {
|
|
|
|
throw new Exception("Cannot create child-resource of parent in different organizationId");
|
|
|
|
}
|
2024-11-04 19:24:12 +01:00
|
|
|
|
2024-11-05 00:45:44 +01:00
|
|
|
$parentNode = $this->getFolderResourceFilesystemNode($parentResource);
|
2024-11-04 19:24:12 +01:00
|
|
|
} else {
|
|
|
|
$parentNode = $this->pathManager->getOrganizationFolderNodeById($organizationFolderId);
|
|
|
|
}
|
|
|
|
|
2024-10-30 01:51:48 +01:00
|
|
|
if($type === "folder") {
|
2024-11-04 19:24:12 +01:00
|
|
|
$resourceNode = $parentNode->newFolder($name);
|
|
|
|
$fileId = $resourceNode->getId();
|
|
|
|
|
|
|
|
if($fileId === -1) {
|
|
|
|
throw new Exception("Unknown error occured while creating resource folder");
|
|
|
|
}
|
|
|
|
|
2024-10-30 01:51:48 +01:00
|
|
|
if(isset($membersAclPermission, $managersAclPermission, $inheritedAclPermission)) {
|
|
|
|
$resource->setMembersAclPermission($membersAclPermission);
|
|
|
|
$resource->setManagersAclPermission($managersAclPermission);
|
|
|
|
$resource->setInheritedAclPermission($inheritedAclPermission);
|
2024-11-04 19:24:12 +01:00
|
|
|
$resource->setFileId($fileId);
|
2024-10-30 01:51:48 +01:00
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException("Folder specific parameters must be included, when creating a resource of type folder");
|
|
|
|
}
|
2024-10-17 13:56:32 +02:00
|
|
|
}
|
|
|
|
|
2024-10-30 01:51:48 +01:00
|
|
|
$resource = $this->mapper->insert($resource);
|
2024-10-17 13:56:32 +02:00
|
|
|
|
2024-11-06 19:58:57 +01:00
|
|
|
$this->organizationFolderService->applyPermissions($organizationFolderId);
|
|
|
|
|
2024-10-30 01:51:48 +01:00
|
|
|
return $resource;
|
|
|
|
} else {
|
|
|
|
throw new ResourceNameNotUnique();
|
|
|
|
}
|
2024-10-17 13:56:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Use named arguments to call this function */
|
|
|
|
public function update(
|
|
|
|
int $id,
|
|
|
|
|
2024-10-30 01:51:48 +01:00
|
|
|
?string $name = null,
|
2024-10-17 13:56:32 +02:00
|
|
|
?bool $active = null,
|
2024-11-06 17:32:33 +01:00
|
|
|
?bool $inheritManagers = null,
|
2024-10-17 13:56:32 +02:00
|
|
|
|
|
|
|
?int $membersAclPermission = null,
|
|
|
|
?int $managersAclPermission = null,
|
|
|
|
?int $inheritedAclPermission = null,
|
|
|
|
): Resource {
|
|
|
|
$resource = $this->find($id);
|
|
|
|
|
2024-10-30 01:51:48 +01:00
|
|
|
if(isset($name)) {
|
2024-11-05 00:45:44 +01:00
|
|
|
if($this->mapper->existsWithName($resource->getOrganizationFolderId(), $resource->getParentResource(), $name)) {
|
2024-10-30 01:51:48 +01:00
|
|
|
throw new ResourceNameNotUnique();
|
|
|
|
} else {
|
2024-11-05 00:45:44 +01:00
|
|
|
if($resource->getType() === "folder") {
|
|
|
|
$resourceNode = $this->getFolderResourceFilesystemNode($resource);
|
|
|
|
$newPath = $resourceNode->getParent()->getPath() . "/" . $name;
|
|
|
|
$resourceNode->move($newPath);
|
|
|
|
}
|
|
|
|
|
2024-10-30 01:51:48 +01:00
|
|
|
$resource->setName($name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-17 13:56:32 +02:00
|
|
|
if(isset($active)) {
|
|
|
|
$resource->setActive($active);
|
|
|
|
}
|
|
|
|
|
2024-11-06 17:32:33 +01:00
|
|
|
if(isset($inheritManagers)) {
|
|
|
|
$resource->setInheritManagers($inheritManagers);
|
|
|
|
}
|
|
|
|
|
2024-10-17 13:56:32 +02:00
|
|
|
if($resource->getType() === "folder") {
|
|
|
|
if(isset($membersAclPermission)) {
|
|
|
|
$resource->setMembersAclPermission($membersAclPermission);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($managersAclPermission)) {
|
|
|
|
$resource->setManagersAclPermission($managersAclPermission);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($inheritedAclPermission)) {
|
|
|
|
$resource->setInheritedAclPermission($inheritedAclPermission);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new InvalidResourceType($resource->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count($resource->getUpdatedFields()) > 0) {
|
|
|
|
$resource->setLastUpdatedTimestamp(time());
|
|
|
|
}
|
|
|
|
|
2024-11-06 19:58:57 +01:00
|
|
|
$resource = $this->mapper->update($resource);
|
|
|
|
|
|
|
|
$this->organizationFolderService->applyPermissions($resource->getOrganizationFolderId());
|
|
|
|
|
|
|
|
return $resource;
|
|
|
|
|
2024-11-05 00:45:44 +01:00
|
|
|
// TODO: improve error handing: if db update fails roll back changes in the filesystem
|
2024-10-17 13:56:32 +02:00
|
|
|
}
|
|
|
|
|
2024-11-04 19:24:12 +01:00
|
|
|
public function setAllFolderResourceAclsInOrganizationFolder(OrganizationFolder $organizationFolder, array $inheritingGroups) {
|
|
|
|
$topLevelFolderResources = $this->findAll($organizationFolder->getId(), null, ["type" => "folder"]);
|
|
|
|
|
2024-11-06 19:58:57 +01:00
|
|
|
$inheritingPrincipals = [];
|
|
|
|
foreach($inheritingGroups as $inheritingGroup) {
|
2024-11-12 15:36:07 +01:00
|
|
|
$inheritingPrincipals[] = new Principal(PrincipalType::GROUP, $inheritingGroup);
|
2024-11-06 19:58:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->recursivelySetFolderResourceALCs($topLevelFolderResources, "", $inheritingPrincipals);
|
2024-11-04 19:24:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively overwrite ACL rules for an array of folder resources
|
|
|
|
*
|
|
|
|
* @param array $folderResources
|
|
|
|
* @psalm-param FolderResource[] $folderResources
|
|
|
|
* @param string $path
|
|
|
|
* @psalm-param string $path
|
2024-11-12 15:36:07 +01:00
|
|
|
* @param array $inheritingPrincipals
|
|
|
|
* @psalm-param Principal[] $inheritingPrincipals
|
2024-11-04 19:24:12 +01:00
|
|
|
*/
|
2024-11-06 19:58:57 +01:00
|
|
|
public function recursivelySetFolderResourceALCs(array $folderResources, string $path, array $inheritingPrincipals) {
|
2024-11-04 19:24:12 +01:00
|
|
|
foreach($folderResources as $folderResource) {
|
|
|
|
$resourceFileId = $folderResource->getFileId();
|
|
|
|
$acls = [];
|
|
|
|
|
2024-11-05 00:45:44 +01:00
|
|
|
// inherit ACLs
|
2024-11-06 19:58:57 +01:00
|
|
|
foreach($inheritingPrincipals as $inheritingPrincipal) {
|
2024-11-12 15:36:07 +01:00
|
|
|
$newACL = $this->aclManager->createAclRuleForPrincipal(
|
|
|
|
principal: $inheritingPrincipal,
|
|
|
|
fileId: $resourceFileId,
|
|
|
|
mask: 31,
|
|
|
|
permissions: $folderResource->getInheritedAclPermission(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// if mapping for principal could not be created, skip creating rule for it
|
|
|
|
if(!is_null($newACL)) {
|
|
|
|
$acls[] = $newACL;
|
2024-11-06 19:58:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// inherited principals will affect resources further down, if they have any permissions at this level
|
|
|
|
if($folderResource->getInheritedAclPermission() !== 0) {
|
|
|
|
$nextInheritingPrincipals = $inheritingPrincipals;
|
|
|
|
} else {
|
|
|
|
$nextInheritingPrincipals = [];
|
2024-11-04 19:24:12 +01:00
|
|
|
}
|
|
|
|
|
2024-11-05 00:45:44 +01:00
|
|
|
// member ACLs
|
2024-11-06 19:58:57 +01:00
|
|
|
/** @var ResourceService */
|
|
|
|
$resourceMemberService = $this->container->get(ResourceMemberService::class);
|
|
|
|
$resourceMembers = $resourceMemberService->findAll($folderResource->getId());
|
2024-11-12 15:36:07 +01:00
|
|
|
|
2024-11-05 00:45:44 +01:00
|
|
|
foreach($resourceMembers as $resourceMember) {
|
2024-11-16 03:06:21 +01:00
|
|
|
if($resourceMember->getPermissionLevel() === ResourceMemberPermissionLevel::MANAGER->value) {
|
2024-11-05 00:45:44 +01:00
|
|
|
$resourceMemberPermissions = $folderResource->getManagersAclPermission();
|
2024-11-16 03:06:21 +01:00
|
|
|
} else if($resourceMember->getPermissionLevel() === ResourceMemberPermissionLevel::MEMBER->value) {
|
2024-11-05 00:45:44 +01:00
|
|
|
$resourceMemberPermissions = $folderResource->getMembersAclPermission();
|
|
|
|
} else {
|
|
|
|
throw new Exception("invalid resource member permission level");
|
|
|
|
}
|
|
|
|
|
2024-11-06 19:58:57 +01:00
|
|
|
if($resourceMemberPermissions !== 0) {
|
2024-11-12 15:36:07 +01:00
|
|
|
$resourceMemberPrincipal = $resourceMember->getPrincipal();
|
2024-11-06 19:58:57 +01:00
|
|
|
|
2024-11-12 15:36:07 +01:00
|
|
|
$newACL = $this->aclManager->createAclRuleForPrincipal(
|
|
|
|
principal: $resourceMemberPrincipal,
|
2024-11-06 19:58:57 +01:00
|
|
|
fileId: $resourceFileId,
|
|
|
|
mask: 31,
|
|
|
|
permissions: $resourceMemberPermissions,
|
|
|
|
);
|
2024-11-12 15:36:07 +01:00
|
|
|
|
|
|
|
// if mapping for principal could not be created, skip creating rule for it
|
|
|
|
if(!is_null($newACL)) {
|
|
|
|
$acls[] = $newACL;
|
|
|
|
$nextInheritingPrincipals[] = $resourceMemberPrincipal;
|
|
|
|
}
|
2024-11-05 00:45:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-04 19:24:12 +01:00
|
|
|
$this->aclManager->overwriteACLsForFileId($resourceFileId, $acls);
|
|
|
|
|
2024-11-06 19:58:57 +01:00
|
|
|
// recurse sub-resources
|
|
|
|
$subFolderResources = $this->getSubResources($folderResource, ["type" => "folder"]);
|
|
|
|
$this->recursivelySetFolderResourceALCs($subFolderResources, $path . $folderResource->getName() . "/", $nextInheritingPrincipals);
|
2024-11-04 19:24:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-05 00:45:44 +01:00
|
|
|
public function getResourcePath(FolderResource $resource) {
|
|
|
|
$currentResource = $resource;
|
|
|
|
|
|
|
|
$invertedPath = [];
|
|
|
|
|
|
|
|
$invertedPath[] = $currentResource->getName();
|
|
|
|
|
|
|
|
while($currentResource->getParentResource()) {
|
|
|
|
$currentResource = $this->find($currentResource->getParentResource());
|
|
|
|
$invertedPath[] = $currentResource->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_reverse($invertedPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFolderResourceFilesystemNode(FolderResource $resource) {
|
|
|
|
return $this->pathManager->getOrganizationFolderByIdSubfolder($resource->getOrganizationFolderId(), $this->getResourcePath($resource));
|
|
|
|
}
|
2024-11-05 13:45:14 +01:00
|
|
|
/**
|
|
|
|
* get all direct sub-resources
|
|
|
|
*/
|
2024-11-06 19:58:57 +01:00
|
|
|
public function getSubResources(Resource $resource, array $filters = []) {
|
|
|
|
return $this->findAll($resource->getOrganizationFolderId(), $resource->getId(), $filters);
|
2024-11-05 13:45:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get all sub-resources recursively
|
|
|
|
*/
|
|
|
|
public function getAllSubResources(Resource $resource) {
|
|
|
|
$subResources = $this->getSubResources($resource);
|
2024-11-05 00:45:44 +01:00
|
|
|
|
2024-11-05 13:45:14 +01:00
|
|
|
foreach($subResources as $subResource) {
|
|
|
|
$subResources = array_merge($subResources, $this->getAllSubResources($subResource));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $subResources;
|
|
|
|
}
|
|
|
|
|
2024-11-06 17:32:33 +01:00
|
|
|
public function getParentResource(Resource $resource): ?Resource {
|
|
|
|
if(!is_null($resource->getParentResource())) {
|
|
|
|
return $this->find($resource->getParentResource());
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-05 13:45:14 +01:00
|
|
|
public function deleteById(int $id): Resource {
|
2024-10-17 13:56:32 +02:00
|
|
|
try {
|
|
|
|
$resource = $this->mapper->find($id);
|
2024-11-05 13:45:14 +01:00
|
|
|
return $this->delete($resource);
|
2024-10-17 13:56:32 +02:00
|
|
|
} catch (Exception $e) {
|
2024-11-05 13:45:14 +01:00
|
|
|
$this->handleException($e, $resource->getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete(Resource $resource): Resource {
|
|
|
|
// first delete all subresources recursively
|
|
|
|
$subResources = $this->getSubResources($resource);
|
|
|
|
|
|
|
|
foreach($subResources as $subResource) {
|
|
|
|
$this->delete($subResource);
|
2024-10-17 13:56:32 +02:00
|
|
|
}
|
2024-11-05 13:45:14 +01:00
|
|
|
|
|
|
|
// delete in filesystem if type folder
|
|
|
|
if($resource->getType() === "folder") {
|
|
|
|
$this->getFolderResourceFilesystemNode($resource)->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete in database
|
|
|
|
$this->mapper->delete($resource);
|
|
|
|
return $resource;
|
|
|
|
|
2024-10-17 13:56:32 +02:00
|
|
|
}
|
|
|
|
}
|