diff --git a/lib/Db/AssignmentMapper.php b/lib/Db/AssignmentMapper.php index 816276ace..44aa178df 100644 --- a/lib/Db/AssignmentMapper.php +++ b/lib/Db/AssignmentMapper.php @@ -144,8 +144,8 @@ class AssignmentMapper extends QBMapper implements IPermissionMapper { private function getOrigin(Assignment $assignment) { if ($assignment->getType() === Assignment::TYPE_USER) { - $origin = $this->userManager->get($assignment->getParticipant()); - return $origin ? new User($origin) : null; + $origin = $this->userManager->userExists($assignment->getParticipant()); + return $origin ? new User($assignment->getParticipant(), $this->userManager) : null; } if ($assignment->getType() === Assignment::TYPE_GROUP) { $origin = $this->groupManager->get($assignment->getParticipant()); diff --git a/lib/Db/BoardMapper.php b/lib/Db/BoardMapper.php index 5db3c3994..cb5d2c38d 100644 --- a/lib/Db/BoardMapper.php +++ b/lib/Db/BoardMapper.php @@ -455,13 +455,11 @@ class BoardMapper extends QBMapper implements IPermissionMapper { } public function mapAcl(Acl &$acl) { - $userManager = $this->userManager; $groupManager = $this->groupManager; $acl->resolveRelation('participant', function ($participant) use (&$acl, &$userManager, &$groupManager) { if ($acl->getType() === Acl::PERMISSION_TYPE_USER) { - $user = $userManager->get($participant); - if ($user !== null) { - return new User($user); + if ($this->userManager->userExists($acl->getParticipant())) { + return new User($acl->getParticipant(), $this->userManager); } $this->logger->debug('User ' . $acl->getId() . ' not found when mapping acl ' . $acl->getParticipant()); return null; @@ -499,9 +497,8 @@ class BoardMapper extends QBMapper implements IPermissionMapper { public function mapOwner(Board &$board) { $userManager = $this->userManager; $board->resolveRelation('owner', function ($owner) use (&$userManager) { - $user = $userManager->get($owner); - if ($user !== null) { - return new User($user); + if ($this->userManager->userExists($owner)) { + return new User($owner, $userManager); } return null; }); diff --git a/lib/Db/CardMapper.php b/lib/Db/CardMapper.php index d1b70c600..0b5170a54 100644 --- a/lib/Db/CardMapper.php +++ b/lib/Db/CardMapper.php @@ -607,9 +607,8 @@ class CardMapper extends QBMapper implements IPermissionMapper { public function mapOwner(Card &$card) { $userManager = $this->userManager; $card->resolveRelation('owner', function ($owner) use (&$userManager) { - $user = $userManager->get($owner); - if ($user !== null) { - return new User($user); + if ($userManager->userExists($owner)) { + return new User($owner, $this->userManager); } return null; }); diff --git a/lib/Db/RelationalObject.php b/lib/Db/RelationalObject.php index bd176e1b8..1928db097 100644 --- a/lib/Db/RelationalObject.php +++ b/lib/Db/RelationalObject.php @@ -33,7 +33,7 @@ class RelationalObject implements JsonSerializable { * RelationalObject constructor. * * @param $primaryKey string - * @param $object + * @param callable|mixed $object */ public function __construct($primaryKey, $object) { $this->primaryKey = $primaryKey; @@ -47,16 +47,24 @@ class RelationalObject implements JsonSerializable { ); } + public function getObject() { + if (is_callable($this->object)) { + $this->object = call_user_func($this->object, $this); + } + + return $this->object; + } + /** * This method should be overwritten if object doesn't implement \JsonSerializable * * @throws \Exception */ public function getObjectSerialization() { - if ($this->object instanceof JsonSerializable) { - return $this->object->jsonSerialize(); + if ($this->getObject() instanceof JsonSerializable) { + return $this->getObject()->jsonSerialize(); } else { - throw new \Exception('jsonSerialize is not implemented on ' . get_class($this)); + throw new \Exception('jsonSerialize is not implemented on ' . get_class($this->getObject())); } } diff --git a/lib/Db/User.php b/lib/Db/User.php index d595ece75..c9750508d 100644 --- a/lib/Db/User.php +++ b/lib/Db/User.php @@ -24,26 +24,31 @@ namespace OCA\Deck\Db; use OCP\IUser; +use OCP\IUserManager; class User extends RelationalObject { - public function __construct(IUser $user) { - $primaryKey = $user->getUID(); - parent::__construct($primaryKey, $user); + + private IUserManager $userManager; + public function __construct($uid, IUserManager $userManager) { + $this->userManager = $userManager; + parent::__construct($uid, function ($object) { + return $this->userManager->get($object->getPrimaryKey()); + }); } public function getObjectSerialization() { return [ - 'uid' => $this->object->getUID(), - 'displayname' => $this->object->getDisplayName(), - 'type' => 0 + 'uid' => $this->getObject()->getUID(), + 'displayname' => $this->getObject()->getDisplayName(), + 'type' => Acl::PERMISSION_TYPE_USER ]; } public function getUID() { - return $this->object->getUID(); + return $this->getPrimaryKey(); } public function getDisplayName() { - return $this->object->getDisplayName(); + return $this->userManager->getDisplayName($this->getPrimaryKey()); } } diff --git a/lib/Service/CardService.php b/lib/Service/CardService.php index 869022dcd..196e12859 100644 --- a/lib/Service/CardService.php +++ b/lib/Service/CardService.php @@ -119,7 +119,7 @@ class CardService { public function enrichCards($cards) { $user = $this->userManager->get($this->currentUser); - $cardIds = array_map(function (Card $card) { + $cardIds = array_map(function (Card $card) use ($user) { // Everything done in here might be heavy as it is executed for every card $cardId = $card->getId(); $this->cardMapper->mapOwner($card); diff --git a/lib/Service/PermissionService.php b/lib/Service/PermissionService.php index d0630a633..c3bb6c133 100644 --- a/lib/Service/PermissionService.php +++ b/lib/Service/PermissionService.php @@ -260,22 +260,20 @@ class PermissionService { } $users = []; - $owner = $this->userManager->get($board->getOwner()); - if ($owner === null) { + if (!$this->userManager->userExists($board->getOwner())) { $this->logger->info('No owner found for board ' . $board->getId()); } else { - $users[$owner->getUID()] = new User($owner); + $users[$board->getOwner()] = new User($board->getOwner(), $this->userManager); } $acls = $this->aclMapper->findAll($boardId); /** @var Acl $acl */ foreach ($acls as $acl) { if ($acl->getType() === Acl::PERMISSION_TYPE_USER) { - $user = $this->userManager->get($acl->getParticipant()); - if ($user === null) { + if (!$this->userManager->userExists($acl->getParticipant())) { $this->logger->info('No user found for acl rule ' . $acl->getId()); continue; } - $users[$user->getUID()] = new User($user); + $users[$acl->getParticipant()] = new User($acl->getParticipant(), $this->userManager); } if ($acl->getType() === Acl::PERMISSION_TYPE_GROUP) { $group = $this->groupManager->get($acl->getParticipant()); @@ -284,7 +282,7 @@ class PermissionService { continue; } foreach ($group->getUsers() as $user) { - $users[$user->getUID()] = new User($user); + $users[$user->getUID()] = new User($user->getUID(), $this->userManager); } } @@ -305,7 +303,7 @@ class PermissionService { if ($user === null) { $this->logger->info('No user found for circle member ' . $member->getUserId()); } else { - $users[$member->getUserId()] = new User($user); + $users[$member->getUserId()] = new User($member->getUserId(), $this->userManager); } } } catch (\Exception $e) {