2024-10-30 04:47:47 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\OrganizationFolders\Service;
|
|
|
|
|
|
|
|
use OCP\AppFramework\Db\TTransactional;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
|
|
|
|
use OCA\GroupFolders\Folder\FolderManager;
|
|
|
|
use OCA\GroupfolderTags\Service\TagService;
|
2024-11-04 19:24:12 +01:00
|
|
|
use OCA\GroupFolders\ACL\UserMapping\UserMappingManager;
|
|
|
|
use OCA\GroupFolders\ACL\Rule;
|
2024-10-30 04:47:47 +01:00
|
|
|
|
2024-11-02 20:14:52 +01:00
|
|
|
use OCA\OrganizationFolders\Errors\OrganizationFolderNotFound;
|
2024-10-30 04:47:47 +01:00
|
|
|
use OCA\OrganizationFolders\Model\OrganizationFolder;
|
|
|
|
use OCA\OrganizationFolders\OrganizationProvider\OrganizationProviderManager;
|
2024-11-04 19:24:12 +01:00
|
|
|
use OCA\OrganizationFolders\Manager\PathManager;
|
|
|
|
use OCA\OrganizationFolders\Manager\GroupfolderManager;
|
|
|
|
use OCA\OrganizationFolders\Manager\ACLManager;
|
2024-10-30 04:47:47 +01:00
|
|
|
|
|
|
|
class OrganizationFolderService {
|
|
|
|
use TTransactional;
|
|
|
|
|
|
|
|
public function __construct(
|
2024-11-04 19:24:12 +01:00
|
|
|
protected IDBConnection $db,
|
|
|
|
protected FolderManager $folderManager,
|
|
|
|
protected UserMappingManager $userMappingManager,
|
|
|
|
protected TagService $tagService,
|
|
|
|
protected OrganizationProviderManager $organizationProviderManager,
|
|
|
|
protected PathManager $pathManager,
|
|
|
|
protected GroupfolderManager $groupfolderManager,
|
|
|
|
protected ACLManager $aclManager,
|
|
|
|
protected ResourceService $resourceService,
|
2024-10-30 04:47:47 +01:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2024-11-01 02:52:42 +01:00
|
|
|
public function findAll() {
|
2024-10-30 04:47:47 +01:00
|
|
|
$result = [];
|
|
|
|
|
2024-11-01 02:52:42 +01:00
|
|
|
$groupfolders = $this->tagService->findGroupfoldersWithTagsGenerator([
|
|
|
|
["key" => "organization_folder"],
|
|
|
|
], ["organization_provider", "organization_id"]);
|
|
|
|
|
|
|
|
foreach ($groupfolders as $groupfolder) {
|
|
|
|
$result[] = new OrganizationFolder(
|
|
|
|
id: $groupfolder["id"],
|
|
|
|
name: $groupfolder["mount_point"],
|
|
|
|
quota: $groupfolder["quota"],
|
|
|
|
organizationProvider: $groupfolder["organization_provider"],
|
2024-11-03 17:24:30 +01:00
|
|
|
organizationId: (int)$groupfolder["organization_id"],
|
2024-11-01 02:52:42 +01:00
|
|
|
);
|
2024-10-30 04:47:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2024-11-02 20:14:52 +01:00
|
|
|
public function find(int $id): OrganizationFolder {
|
|
|
|
$groupfolder = $this->tagService->findGroupfolderWithTags($id,[
|
|
|
|
["key" => "organization_folder"],
|
|
|
|
], ["organization_provider", "organization_id"]);
|
|
|
|
|
|
|
|
if(is_null($groupfolder)) {
|
|
|
|
throw new OrganizationFolderNotFound($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new OrganizationFolder(
|
|
|
|
id: $groupfolder["id"],
|
|
|
|
name: $groupfolder["mount_point"],
|
|
|
|
quota: $groupfolder["quota"],
|
|
|
|
organizationProvider: $groupfolder["organization_provider"],
|
2024-11-03 17:24:30 +01:00
|
|
|
organizationId: (int)$groupfolder["organization_id"],
|
2024-11-02 20:14:52 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create(string $name, int $quota, ?string $organizationProvider = null,?int $organizationId = null): OrganizationFolder {
|
|
|
|
return $this->atomic(function () use ($name, $quota, $organizationProvider, $organizationId) {
|
2024-10-30 04:47:47 +01:00
|
|
|
$groupfolderId = $this->folderManager->createFolder($name);
|
|
|
|
$this->folderManager->setFolderQuota($groupfolderId, $quota);
|
|
|
|
$this->folderManager->setFolderACL($groupfolderId, true);
|
|
|
|
|
|
|
|
$this->tagService->update($groupfolderId, "organization_folder");
|
|
|
|
|
|
|
|
if(isset($organizationProvider) && $this->organizationProviderManager->hasOrganizationProvider($organizationProvider) && isset($organizationId)) {
|
|
|
|
$organization = $this->organizationProviderManager->getOrganizationProvider($organizationProvider)->getOrganization($organizationId);
|
|
|
|
|
|
|
|
$this->tagService->update($groupfolderId, "organization_provider", $organizationProvider);
|
2024-11-04 18:38:14 +01:00
|
|
|
$this->tagService->update($groupfolderId, "organization_id", (string)$organization->getId());
|
2024-10-30 04:47:47 +01:00
|
|
|
}
|
|
|
|
|
2024-11-02 20:14:52 +01:00
|
|
|
$organizationFolder = new OrganizationFolder(
|
|
|
|
id: $groupfolderId,
|
|
|
|
name: $name,
|
|
|
|
quota: $quota,
|
|
|
|
organizationProvider: $organizationProvider,
|
|
|
|
organizationId: $organizationId,
|
|
|
|
);
|
|
|
|
|
|
|
|
return $organizationFolder;
|
2024-10-30 04:47:47 +01:00
|
|
|
}, $this->db);
|
|
|
|
}
|
|
|
|
|
2024-11-04 18:38:14 +01:00
|
|
|
public function update(
|
|
|
|
int $id,
|
|
|
|
?string $name = null,
|
|
|
|
?int $quota = null,
|
|
|
|
?string $organizationProviderId = null,
|
|
|
|
?int $organizationId = null
|
|
|
|
): OrganizationFolder {
|
|
|
|
return $this->atomic(function () use ($id, $name, $quota, $organizationProviderId, $organizationId) {
|
|
|
|
if(isset($name)) {
|
|
|
|
$this->folderManager->renameFolder($id, $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($quota)) {
|
|
|
|
$this->folderManager->setFolderQuota($id, $quota);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($organizationProviderId) || isset($organizationId)) {
|
|
|
|
if(!isset($organizationProviderId)) {
|
|
|
|
$organizationProviderId = $this->tagService->find($id, "organization_provider")->getTagValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$this->organizationProviderManager->hasOrganizationProvider($organizationProviderId)) {
|
|
|
|
throw new \Exception("organization provider not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
$organizationProvider = $this->organizationProviderManager->getOrganizationProvider($organizationProviderId);
|
|
|
|
|
|
|
|
if(!isset($organizationId)) {
|
|
|
|
$organizationId = (int)$this->tagService->find($id, "organization_id")->getTagValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
$organization = $organizationProvider->getOrganization($organizationId);
|
|
|
|
|
|
|
|
$this->tagService->update($id, "organization_provider", $organizationProviderId);
|
|
|
|
$this->tagService->update($id, "organization_id", (string)$organization->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->find($id);
|
|
|
|
}, $this->db);
|
|
|
|
}
|
|
|
|
|
2024-10-30 04:47:47 +01:00
|
|
|
public function applyPermissions(int $id) {
|
2024-11-04 19:24:12 +01:00
|
|
|
$organizationFolder = $this->find($id);
|
|
|
|
|
|
|
|
$memberGroups = $this->getMemberGroups($organizationFolder);
|
|
|
|
|
|
|
|
$this->setGroupsAsGroupfolderMembers($organizationFolder->getId(), $memberGroups);
|
|
|
|
$this->setRootFolderACLs($organizationFolder, $memberGroups);
|
|
|
|
|
|
|
|
return $this->resourceService->setAllFolderResourceAclsInOrganizationFolder($organizationFolder, $memberGroups);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getMemberGroups(OrganizationFolder $organizationFolder) {
|
|
|
|
// TODO: fetch member groups, for now only use organization members
|
|
|
|
$memberGroups = [];
|
|
|
|
|
|
|
|
if(!is_null($organizationFolder->getOrganizationProvider()) && !is_null($organizationFolder->getOrganizationId())) {
|
|
|
|
$organizationProvider = $this->organizationProviderManager->getOrganizationProvider($organizationFolder->getOrganizationProvider());
|
|
|
|
$organization = $organizationProvider->getOrganization($organizationFolder->getOrganizationId());
|
|
|
|
|
|
|
|
$memberGroups[] = $organization->getMembersGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $memberGroups;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setGroupsAsGroupfolderMembers($groupfolderId, array $groups) {
|
|
|
|
$groupfolderMembers = [];
|
|
|
|
|
|
|
|
foreach($groups as $group) {
|
|
|
|
$groupfolderMembers[] = [
|
|
|
|
"group_id" => $group,
|
|
|
|
"permissions" => \OCP\Constants::PERMISSION_ALL,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->groupfolderManager->overwriteMemberGroups($groupfolderId, $groupfolderMembers);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* In the root folder of an organization folder only resource folders can exist
|
|
|
|
* To prevent adding files there all member groups of the groupfolder need to have a read-only ACL rule on the root folder
|
|
|
|
*/
|
|
|
|
protected function setRootFolderACLs(OrganizationFolder $organizationFolder, $groups) {
|
|
|
|
$folderNode = $this->pathManager->getOrganizationFolderNode($organizationFolder);
|
|
|
|
$fileId = $folderNode->getId();
|
|
|
|
|
|
|
|
$acls = [];
|
|
|
|
foreach($groups as $group) {
|
|
|
|
$acls[] = new Rule(userMapping: $this->userMappingManager->mappingFromId("group", $group),
|
|
|
|
fileId: $fileId,
|
|
|
|
mask: 31,
|
|
|
|
permissions: 1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->aclManager->overwriteACLsForFileId($fileId, $acls);
|
2024-10-30 04:47:47 +01:00
|
|
|
}
|
|
|
|
|
2024-11-02 20:14:52 +01:00
|
|
|
public function remove($id): void {
|
|
|
|
$organizationFolder = $this->find($id);
|
|
|
|
$this->folderManager->removeFolder($organizationFolder->getId());
|
|
|
|
}
|
|
|
|
|
2024-10-30 04:47:47 +01:00
|
|
|
}
|