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

chore: Clean up permission service #6011

Merged
merged 1 commit into from
Jun 24, 2024
Merged
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
63 changes: 16 additions & 47 deletions lib/Service/PermissionService.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
Expand All @@ -20,68 +21,37 @@
use OCP\Cache\CappedMemoryCache;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IUserManager;
use OCP\Share\IManager;
use Psr\Log\LoggerInterface;

class PermissionService {

/** @var CirclesService */
private $circlesService;
/** @var BoardMapper */
private $boardMapper;
/** @var AclMapper */
private $aclMapper;
/** @var ILogger */
private $logger;
/** @var IUserManager */
private $userManager;
/** @var IGroupManager */
private $groupManager;
/** @var IConfig */
private $config;
/** @var IManager */
private $shareManager;
/** @var string */
private $userId;
/** @var array */
private $users = [];
private array $users = [];

private CappedMemoryCache $boardCache;
private CappedMemoryCache $permissionCache;

public function __construct(
ILogger $logger,
CirclesService $circlesService,
AclMapper $aclMapper,
BoardMapper $boardMapper,
IUserManager $userManager,
IGroupManager $groupManager,
IManager $shareManager,
IConfig $config,
$userId
private LoggerInterface $logger,
private CirclesService $circlesService,
private AclMapper $aclMapper,
private BoardMapper $boardMapper,
private IUserManager $userManager,
private IGroupManager $groupManager,
private IManager $shareManager,
private IConfig $config,
private ?string $userId
) {
$this->circlesService = $circlesService;
$this->aclMapper = $aclMapper;
$this->boardMapper = $boardMapper;
$this->logger = $logger;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->shareManager = $shareManager;
$this->config = $config;
$this->userId = $userId;

$this->boardCache = new CappedMemoryCache();
$this->permissionCache = new CappedMemoryCache();
}

/**
* Get current user permissions for a board by id
*
* @param $boardId
* @return bool|array
*/
public function getPermissions($boardId, ?string $userId = null) {
public function getPermissions(int $boardId, ?string $userId = null) {
if ($userId === null) {
$userId = $this->userId;
}
Expand All @@ -99,7 +69,7 @@ public function getPermissions($boardId, ?string $userId = null) {
$owner = false;
$acls = [];
}

$permissions = [
Acl::PERMISSION_READ => $owner || $this->userCan($acls, Acl::PERMISSION_READ, $userId),
Acl::PERMISSION_EDIT => $owner || $this->userCan($acls, Acl::PERMISSION_EDIT, $userId),
Expand Down Expand Up @@ -133,11 +103,10 @@ public function matchPermissions(Board $board) {
/**
* check permissions for replacing dark magic middleware
*
* @param numeric $id
* @throws NoPermissionException
*/
public function checkPermission(?IPermissionMapper $mapper, $id, int $permission, $userId = null, bool $allowDeletedCard = false): bool {
$boardId = $id;
$boardId = (int)$id;
if ($mapper instanceof IPermissionMapper && !($mapper instanceof BoardMapper)) {
$boardId = $mapper->findBoardId($id);
}
Expand All @@ -150,7 +119,7 @@ public function checkPermission(?IPermissionMapper $mapper, $id, int $permission
if ($permissions[$permission] === true) {

if (!$allowDeletedCard && $mapper instanceof CardMapper) {
$card = $mapper->find($id);
$card = $mapper->find((int)$id);
if ($card->getDeletedAt() > 0) {
throw new NoPermissionException('Card is deleted');
}
Expand Down
37 changes: 13 additions & 24 deletions tests/unit/Service/PermissionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,28 @@
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Share\IManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;

class PermissionServiceTest extends \Test\TestCase {

/** @var IRequest */
private $request;
private $circlesService;
/** @var PermissionService*/
private $service;
/** @var ILogger */
private $logger;
/** @var AclMapper */
private $aclMapper;
/** @var BoardMapper */
private $boardMapper;
/** @var IUserManager */
private $userManager;
/** @var IGroupManager */
private $groupManager;
/** @var IManager */
private $shareManager;
/** @var IConfig */
private $config;
/** @var string */
private $userId = 'admin';
private IRequest|MockObject $request;
private CirclesService|MockObject $circlesService;
private PermissionService $service;
private LoggerInterface|MockObject $logger;
private AclMapper|MockObject $aclMapper;
private BoardMapper|MockObject $boardMapper;
private IUserManager $userManager;
private IGroupManager|MockObject $groupManager;
private MockObject|IManager $shareManager;
private IConfig|MockObject $config;

public function setUp(): void {
parent::setUp();
$this->logger = $this->createMock(ILogger::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->request = $this->createMock(IRequest::class);
$this->circlesService = $this->createMock(CirclesService::class);
$this->aclMapper = $this->createMock(AclMapper::class);
Expand Down