Skip to content

Commit

Permalink
Merge pull request #6011 from nextcloud/chore/property-promition
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusknorr authored Jun 24, 2024
2 parents 3250efc + ec9e87d commit ea4655b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 71 deletions.
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

0 comments on commit ea4655b

Please sign in to comment.