Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC: create and use Circles instead of Groups #1597

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
build
vendor
js/
.idea/
22 changes: 15 additions & 7 deletions lib/ACL/RuleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,31 @@

namespace OCA\GroupFolders\ACL;

use OCA\GroupFolders\ACL\UserMapping\IEntityMappingManager;
use OCA\GroupFolders\ACL\UserMapping\IUserMapping;
use OCA\GroupFolders\ACL\UserMapping\IUserMappingManager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IUser;

class RuleManager {
private $connection;
private $userMappingManager;

public function __construct(IDBConnection $connection, IUserMappingManager $userMappingManager) {
/**
* @var IEntityMappingManager
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we find a better naming here instead of entity?

*/
private $entityMappingManager;


public function __construct(
IDBConnection $connection,
IEntityMappingManager $entityMappingManager
) {
$this->connection = $connection;
$this->userMappingManager = $userMappingManager;
$this->entityMappingManager = $entityMappingManager;
}

private function createRule(array $data): ?Rule {
$mapping = $this->userMappingManager->mappingFromId($data['mapping_type'], $data['mapping_id']);
$mapping = $this->entityMappingManager->mappingFromId($data['mapping_id']);
if ($mapping) {
return new Rule(
$mapping,
Expand All @@ -56,7 +64,7 @@ private function createRule(array $data): ?Rule {
* @return (Rule[])[] [$fileId => Rule[]]
*/
public function getRulesForFilesById(IUser $user, array $fileIds): array {
$userMappings = $this->userMappingManager->getMappingsForUser($user);
$userMappings = $this->entityMappingManager->getMappingsForUser($user);

$query = $this->connection->getQueryBuilder();
$query->select(['fileid', 'mapping_type', 'mapping_id', 'mask', 'permissions'])
Expand Down Expand Up @@ -242,7 +250,7 @@ public function getAllRulesForPrefix(int $storageId, string $prefix): array {
* @return array (Rule[])[] [$path => Rule[]]
*/
public function getRulesForPrefix(IUser $user, int $storageId, string $prefix): array {
$userMappings = $this->userMappingManager->getMappingsForUser($user);
$userMappings = $this->entityMappingManager->getMappingsForUser($user);

$query = $this->connection->getQueryBuilder();
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'path'])
Expand Down
136 changes: 136 additions & 0 deletions lib/ACL/UserMapping/EntityMappingManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2021 Maxence Lange <maxence@artificial-owl.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\GroupFolders\ACL\UserMapping;

use OCA\Circles\Exceptions\CircleNotFoundException;
use OCA\Circles\Exceptions\FederatedItemException;
use OCA\Circles\Exceptions\FederatedUserException;
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
use OCA\Circles\Exceptions\InvalidIdException;
use OCA\Circles\Exceptions\MemberNotFoundException;
use OCA\Circles\Exceptions\OwnerNotFoundException;
use OCA\Circles\Exceptions\RemoteInstanceException;
use OCA\Circles\Exceptions\RemoteNotFoundException;
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
use OCA\Circles\Exceptions\RequestBuilderException;
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
use OCA\Circles\Exceptions\UnknownRemoteException;
use OCA\Circles\Exceptions\UserTypeNotFoundException;
use OCA\Circles\Model\Member;
use OCA\Circles\Model\Probes\CircleProbe;
use OCP\Circles\ICirclesManager;
use OCP\IUser;


/**
* Class EntityMappingManager
*
* @package OCA\GroupFolders\ACL\UserMapping
*/
class EntityMappingManager implements IEntityMappingManager {

/** @var ICirclesManager */
private $circlesManager;


/**
* EntityMappingManager constructor.
*
* @param ICirclesManager $circlesManager
*/
public function __construct(ICirclesManager $circlesManager) {
$this->circlesManager = $circlesManager;
}


/**
* @param IUser $user
* @param bool $userAssignable
*
* @return array
* @throws CircleNotFoundException
* @throws FederatedItemException
* @throws FederatedUserException
* @throws FederatedUserNotFoundException
* @throws InvalidIdException
* @throws MemberNotFoundException
* @throws OwnerNotFoundException
* @throws RemoteInstanceException
* @throws RemoteNotFoundException
* @throws RemoteResourceNotFoundException
* @throws RequestBuilderException
* @throws SingleCircleNotFoundException
* @throws UnknownRemoteException
* @throws UserTypeNotFoundException
*/
public function getMappingsForUser(IUser $user, bool $userAssignable = true): array {
$federatedUser = $this->circlesManager->getFederatedUser($user->getUID(), Member::TYPE_USER);

$mappings = [];
foreach ($federatedUser->getMemberships() as $membership) {
// TODO: lighten by providing details within getMemberships();
$probe = new CircleProbe();
$probe->includeSystemCircles();
$probe->includeSingleCircles();
$circle = $this->circlesManager->getCircle($membership->getCircleId(), $probe);

$mappings[] =
new UserMapping(
'entity',
$membership->getCircleId(),
$circle->getDisplayName()
);
}

return $mappings;
}


/**
* @param string $id
*
* @return IUserMapping|null
* @throws CircleNotFoundException
* @throws FederatedItemException
* @throws FederatedUserException
* @throws FederatedUserNotFoundException
* @throws InvalidIdException
* @throws MemberNotFoundException
* @throws OwnerNotFoundException
* @throws RemoteInstanceException
* @throws RemoteNotFoundException
* @throws RemoteResourceNotFoundException
* @throws RequestBuilderException
* @throws SingleCircleNotFoundException
* @throws UnknownRemoteException
* @throws UserTypeNotFoundException
*/
public function mappingFromId(string $id): ?IUserMapping {
$federatedUser = $this->circlesManager->getFederatedUser($id);

return new UserMapping('entity', $id, $federatedUser->getDisplayName());
}

}
48 changes: 48 additions & 0 deletions lib/ACL/UserMapping/IEntityMappingManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2021 Maxence Lange <maxence@artificial-owl.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\GroupFolders\ACL\UserMapping;

use OCP\IUser;


/**
* Interface IEntityMappingManager
*
* @package OCA\GroupFolders\ACL\UserMapping
*/
interface IEntityMappingManager {
/**
* @param IUser $user
* @param bool $userAssignable whether to include mappings that are assignable by non admin users
* @return IUserMapping[]
*/
public function getMappingsForUser(IUser $user, bool $userAssignable = true): array;

/**
* @param string $id
* @return IUserMapping|null
*/
public function mappingFromId(string $id): ?IUserMapping;
}
4 changes: 3 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\GroupFolders\ACL\ACLManagerFactory;
use OCA\GroupFolders\ACL\RuleManager;
use OCA\GroupFolders\ACL\UserMapping\EntityMappingManager;
use OCA\GroupFolders\ACL\UserMapping\IEntityMappingManager;
use OCA\GroupFolders\ACL\UserMapping\IUserMappingManager;
use OCA\GroupFolders\ACL\UserMapping\UserMappingManager;
use OCA\GroupFolders\CacheListener;
Expand Down Expand Up @@ -127,7 +129,7 @@ public function register(IRegistrationContext $context): void {
);
});

$context->registerServiceAlias(IUserMappingManager::class, UserMappingManager::class);
$context->registerServiceAlias(IEntityMappingManager::class, EntityMappingManager::class);
}

public function boot(IBootContext $context): void {
Expand Down
49 changes: 34 additions & 15 deletions lib/Command/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
namespace OCA\GroupFolders\Command;

use OC\Core\Command\Base;
use OCA\Circles\CirclesManager;
use OCA\Circles\Model\Probes\CircleProbe;
use OCA\GroupFolders\Folder\FolderManager;
use OCP\Circles\ICirclesManager;
use OCP\Constants;
use OCP\Files\IRootFolder;
use OCP\IGroupManager;
Expand All @@ -44,11 +47,20 @@ class Group extends Base {
private $rootFolder;
private $groupManager;

public function __construct(FolderManager $folderManager, IRootFolder $rootFolder, IGroupManager $groupManager) {
/** @var ICirclesManager */
private $circlesManager;

public function __construct(
FolderManager $folderManager,
IRootFolder $rootFolder,
IGroupManager $groupManager,
ICirclesManager $circlesManager
) {
parent::__construct();
$this->folderManager = $folderManager;
$this->rootFolder = $rootFolder;
$this->groupManager = $groupManager;
$this->circlesManager = $circlesManager;
}

protected function configure() {
Expand All @@ -68,25 +80,32 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$folder = $this->folderManager->getFolder($folderId, $this->rootFolder->getMountPoint()->getNumericStorageId());
if ($folder) {
$groupString = $input->getArgument('group');
$group = $this->groupManager->get($groupString);

// confirmation that $groupString is a valid CircleId
$this->circlesManager->startSuperSession();

$probe = new CircleProbe();
$probe->includeSystemCircles();
$this->circlesManager->getCircle($groupString);

// $group = $this->groupManager->get($groupString);
if ($input->getOption('delete')) {
$this->folderManager->removeApplicableGroup($folderId, $groupString);
return 0;
} elseif ($group) {
$permissionsString = $input->getArgument('permissions');
$permissions = $this->getNewPermissions($permissionsString);
if ($permissions) {
if (!isset($folder['groups'][$groupString])) {
$this->folderManager->addApplicableGroup($folderId, $groupString);
}
$this->folderManager->setGroupPermissions($folderId, $groupString, $permissions);
return 0;
} else {
$output->writeln('<error>Unable to parse permissions input: ' . implode(' ', $permissionsString) . '</error>');
return -1;
}

$permissionsString = $input->getArgument('permissions');
$permissions = $this->getNewPermissions($permissionsString);
if ($permissions) {
if (!isset($folder['groups'][$groupString])) {
$this->folderManager->addApplicableGroup($folderId, $groupString);
}
$this->folderManager->setGroupPermissions($folderId, $groupString, $permissions);

return 0;
} else {
$output->writeln('<error>group not found: ' . $groupString . '</error>');
$output->writeln('<error>Unable to parse permissions input: ' . implode(' ', $permissionsString) . '</error>');

return -1;
}
} else {
Expand Down
12 changes: 9 additions & 3 deletions lib/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use OC\Core\Command\Base;
use OCA\GroupFolders\Folder\FolderManager;
use OCP\Circles\ICirclesManager;
use OCP\Constants;
use OCP\Files\IRootFolder;
use Symfony\Component\Console\Helper\Table;
Expand All @@ -38,11 +39,13 @@ class ListCommand extends Base {
];

private $folderManager;
private $circlesManager;
private $rootFolder;

public function __construct(FolderManager $folderManager, IRootFolder $rootFolder) {
public function __construct(FolderManager $folderManager, ICirclesManager $circlesManager, IRootFolder $rootFolder) {
parent::__construct();
$this->folderManager = $folderManager;
$this->circlesManager = $circlesManager;
$this->rootFolder = $rootFolder;
}

Expand All @@ -54,6 +57,7 @@ protected function configure() {
}

protected function execute(InputInterface $input, OutputInterface $output) {
$this->circlesManager->startSuperSession();
$folders = $this->folderManager->getAllFoldersWithSize($this->rootFolder->getMountPoint()->getNumericStorageId());
usort($folders, function ($a, $b) {
return $a['id'] - $b['id'];
Expand All @@ -73,19 +77,21 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$this->writeArrayInOutputFormat($input, $output, $folders);
} else {
$table = new Table($output);
$table->setHeaders(['Folder Id', 'Name', 'Groups', 'Quota', 'Size', 'Advanced Permissions', 'Manage advanced permissions']);
$table->setHeaders(['Folder Id', 'Name', 'Groups', 'Display Name', 'Quota', 'Size', 'Advanced Permissions', 'Manage advanced permissions']);
$table->setRows(array_map(function ($folder) {
$folder['size'] = \OCP\Util::humanFileSize($folder['size']);
$folder['quota'] = ($folder['quota'] > 0) ? \OCP\Util::humanFileSize($folder['quota']) : 'Unlimited';
$groupStrings = array_map(function (string $groupId, int $permissions) {
return $groupId . ': ' . $this->permissionsToString($permissions);
$circle = $this->circlesManager->getCircle($groupId);
return $circle->getDisplayName(). ' (' . $groupId . ') : ' . $this->permissionsToString($permissions);
}, array_keys($folder['groups']), array_values($folder['groups']));
$folder['groups'] = implode("\n", $groupStrings);
$folder['acl'] = $folder['acl'] ? 'Enabled' : 'Disabled';
$manageStrings = array_map(function ($manage) {
return $manage['id'] . ' (' . $manage['type'] . ')';
}, $folder['manage']);
$folder['manage'] = implode("\n", $manageStrings);

return $folder;
}, $folders));
$table->render();
Expand Down
Loading