From 3e304a9ff22d9d58a84a97226b997fe53e92ebe1 Mon Sep 17 00:00:00 2001 From: Julius Haertl Date: Mon, 13 Feb 2017 22:05:50 +0100 Subject: [PATCH 1/6] Use display name for users and groups MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- js/service/BoardService.js | 43 +++++++-------- lib/Db/Acl.php | 7 ++- lib/Db/AclMapper.php | 1 - lib/Db/Board.php | 5 +- lib/Db/RelationalEntity.php | 42 +++++++++++++++ lib/Db/RelationalObject.php | 61 +++++++++++++++++++++ lib/Db/User.php | 42 +++++++++++++++ lib/Service/BoardService.php | 56 ++++++++++++++++++-- templates/part.board.sidebarView.php | 12 ++--- templates/part.boardlist.php | 4 +- tests/unit/Db/AclTest.php | 6 ++- tests/unit/Service/BoardServiceTest.php | 70 +++++++++++++++++-------- 12 files changed, 288 insertions(+), 61 deletions(-) create mode 100644 lib/Db/RelationalObject.php create mode 100644 lib/Db/User.php diff --git a/js/service/BoardService.js b/js/service/BoardService.js index a7ea42352..7e60c88a9 100644 --- a/js/service/BoardService.js +++ b/js/service/BoardService.js @@ -55,6 +55,7 @@ app.factory('BoardService', function(ApiService, $http, $q){ // filter out everyone who is already in the share list angular.forEach(users, function (item) { + var acl = self.generateAcl('user', item); var exists = false; angular.forEach(self.getCurrent().acl, function (acl) { if (acl.participant.primaryKey === item.value.shareWith || OC.getCurrentUser() === item.value.shareWith) { @@ -62,19 +63,11 @@ app.factory('BoardService', function(ApiService, $http, $q){ } }); if (!exists) { - self.sharees.push({ - boardId: null, - id: null, - owner: false, - participant: item.value.shareWith, - permissionEdit: true, - permissionManage: true, - permissionShare: true, - type: 'user' - }); + self.sharees.push(acl); } }); angular.forEach(groups, function (item) { + var acl = self.generateAcl('group', item); var exists = false; angular.forEach(self.getCurrent().acl, function (acl) { if (acl.participant.primaryKey === item.value.shareWith) { @@ -82,16 +75,7 @@ app.factory('BoardService', function(ApiService, $http, $q){ } }); if (!exists) { - self.sharees.push({ - boardId: null, - id: null, - owner: false, - participant: item.value.shareWith, - permissionEdit: true, - permissionManage: true, - permissionShare: true, - type: 'group' - }); + self.sharees.push(acl); } }); @@ -103,13 +87,30 @@ app.factory('BoardService', function(ApiService, $http, $q){ return deferred.promise; }; + BoardService.prototype.generateAcl = function(type, ocsItem) { + return { + boardId: null, + id: null, + owner: false, + participant: { + primaryKey: ocsItem.value.shareWith, + uid: ocsItem.value.shareWith, + displayname: ocsItem.label + }, + permissionEdit: true, + permissionManage: true, + permissionShare: true, + type: type + } + }; + BoardService.prototype.addAcl = function(acl) { var board = this.getCurrent(); var deferred = $q.defer(); var self = this; var _acl = acl; $http.post(this.baseUrl + '/' + acl.boardId + '/acl', _acl).then(function (response) { - if(!board.acl) { + if(!board.acl || board.acl.length === 0) { board.acl = {}; } board.acl[response.data.id] = response.data; diff --git a/lib/Db/Acl.php b/lib/Db/Acl.php index b9ad83abf..f96a97b68 100644 --- a/lib/Db/Acl.php +++ b/lib/Db/Acl.php @@ -48,12 +48,10 @@ class Acl extends RelationalEntity implements \JsonSerializable { $this->addType('permissionEdit', 'boolean'); $this->addType('permissionShare', 'boolean'); $this->addType('permissionManage', 'boolean'); - $this->addType('owner', 'boolean'); $this->addType('type', 'integer'); + $this->addType('owner', 'boolean'); $this->addRelation('owner'); - $this->setPermissionEdit(false); - $this->setPermissionShare(false); - $this->setPermissionManage(false); + $this->addResolvable('participant'); } public function getPermission($permission) { @@ -93,4 +91,5 @@ class Acl extends RelationalEntity implements \JsonSerializable { $this->markFieldUpdated('type'); $this->type = $typeInt; } + } \ No newline at end of file diff --git a/lib/Db/AclMapper.php b/lib/Db/AclMapper.php index ff1cade1c..bd1ba187c 100644 --- a/lib/Db/AclMapper.php +++ b/lib/Db/AclMapper.php @@ -25,7 +25,6 @@ namespace OCA\Deck\Db; use OCP\IDBConnection; - class AclMapper extends DeckMapper implements IPermissionMapper { public function __construct(IDBConnection $db) { diff --git a/lib/Db/Board.php b/lib/Db/Board.php index b24a74102..4d3b5e42d 100644 --- a/lib/Db/Board.php +++ b/lib/Db/Board.php @@ -32,8 +32,8 @@ class Board extends RelationalEntity implements JsonSerializable { protected $owner; protected $color; protected $archived = false; - protected $labels; - protected $acl; + protected $labels = []; + protected $acl = []; protected $shared; public function __construct() { @@ -43,6 +43,7 @@ class Board extends RelationalEntity implements JsonSerializable { $this->addRelation('labels'); $this->addRelation('acl'); $this->addRelation('shared'); + $this->addResolvable('owner'); $this->shared = -1; } diff --git a/lib/Db/RelationalEntity.php b/lib/Db/RelationalEntity.php index 130e6fe09..498a22703 100644 --- a/lib/Db/RelationalEntity.php +++ b/lib/Db/RelationalEntity.php @@ -26,7 +26,9 @@ namespace OCA\Deck\Db; class RelationalEntity extends \OCP\AppFramework\Db\Entity implements \JsonSerializable { + private $primaryKey; private $_relations = array(); + private $_resolvedProperties = []; /** * Mark a property as relation so it will not get updated using Mapper::update @@ -37,6 +39,11 @@ class RelationalEntity extends \OCP\AppFramework\Db\Entity implements \JsonSeria $this->_relations[] = $property; } } + + public function addResolvable($property) { + $this->_resolvedProperties[$property] = null; + } + /** * Mark am attribute as updated * overwritten from \OCP\AppFramework\Db\Entity to avoid writing relational attributes @@ -65,5 +72,40 @@ class RelationalEntity extends \OCP\AppFramework\Db\Entity implements \JsonSeria } } return $json; + + public function resolveRelation($property, $resolver) { + if($property !== null && $this->$property !== null) { + $result = $resolver($this->$property); + } else { + $result = null; + } + + if($result instanceof RelationalObject || $result === null) { + $this->_resolvedProperties[$property] = $result; + } else { + throw new \Exception('resolver must return an instance of RelationalObject'); + } } + + public function __call($methodName, $args){ + $attr = lcfirst( substr($methodName, 7) ); + if(strpos($methodName, 'resolve') === 0 && array_key_exists($attr, $this->_resolvedProperties)) { + if($this->_resolvedProperties[$attr] !== null) { + return $this->_resolvedProperties[$attr]; + } else { + return $this->getter($attr, $args); + } + } + + $attr = lcfirst( substr($methodName, 3) ); + if(strpos($methodName, 'set') === 0 && array_key_exists($attr, $this->_resolvedProperties)) { + if(!is_scalar($args[0])) { + $args[0] = $args[0]['primaryKey']; + parent::setter($attr, $args); + } + parent::setter($attr, $args); + } + return parent::__call($methodName, $args); + } + } \ No newline at end of file diff --git a/lib/Db/RelationalObject.php b/lib/Db/RelationalObject.php new file mode 100644 index 000000000..8a3346c78 --- /dev/null +++ b/lib/Db/RelationalObject.php @@ -0,0 +1,61 @@ + + * + * @author Julius Härtl + * + * @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 . + * + */ + +/** + * Created by PhpStorm. + * User: jus + * Date: 27.02.17 + * Time: 14:05 + */ + +namespace OCA\Deck\Db; + + +abstract class RelationalObject implements \JsonSerializable { + + /** + * RelationalObject constructor. + * + * @param $primaryKey string + * @param $object + */ + public function __construct($primaryKey, $object) { + $this->primaryKey = $primaryKey; + $this->object = $object; + } + + public function jsonSerialize() { + return array_merge( + ['primaryKey' => $this->primaryKey], + $this->getObjectSerialization() + ); + } + + public function getObjectSerialization() { + $this->object->jsonSerialize(); + } + + public function getPrimaryKey() { + return $this->getPrimaryKey(); + } +} \ No newline at end of file diff --git a/lib/Db/User.php b/lib/Db/User.php new file mode 100644 index 000000000..436e5547f --- /dev/null +++ b/lib/Db/User.php @@ -0,0 +1,42 @@ + + * + * @author Julius Härtl + * + * @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 . + * + */ + +namespace OCA\Deck\Db; + +use OCP\IGroup; +use OCP\IUser; + +class User extends RelationalObject { + + public function __construct(IUser $user = null) { + $primaryKey = $user->getUID(); + parent::__construct($primaryKey, $user); + } + + public function getObjectSerialization() { + return [ + 'uid' => $this->object->getUID(), + 'displayname' => $this->object->getDisplayName() + ]; + } +} \ No newline at end of file diff --git a/lib/Service/BoardService.php b/lib/Service/BoardService.php index 579ac2393..664699445 100644 --- a/lib/Service/BoardService.php +++ b/lib/Service/BoardService.php @@ -25,14 +25,18 @@ namespace OCA\Deck\Service; use OCA\Deck\Db\Acl; use OCA\Deck\Db\AclMapper; +use OCA\Deck\Db\Group; use OCA\Deck\Db\Label; +use OCA\Deck\Db\User; +use OCP\IGroupManager; use OCP\IL10N; use OCA\Deck\Db\Board; use OCA\Deck\Db\BoardMapper; use OCA\Deck\Db\LabelMapper; +use OCP\IUserManager; class BoardService { @@ -43,24 +47,67 @@ class BoardService { private $l10n; private $permissionService; - public function __construct(BoardMapper $boardMapper, IL10N $l10n, LabelMapper $labelMapper, AclMapper $aclMapper, PermissionService $permissionService) { + public function __construct(BoardMapper $boardMapper, IL10N $l10n, LabelMapper $labelMapper, AclMapper $aclMapper, PermissionService $permissionService, IUserManager $userManager, IGroupManager $groupManager) { $this->boardMapper = $boardMapper; $this->labelMapper = $labelMapper; $this->aclMapper = $aclMapper; $this->l10n = $l10n; $this->permissionService = $permissionService; + $this->userManager = $userManager; + $this->groupManager = $groupManager; } public function findAll($userInfo) { $userBoards = $this->boardMapper->findAllByUser($userInfo['user']); $groupBoards = $this->boardMapper->findAllByGroups($userInfo['user'], $userInfo['groups']); $complete = array_merge($userBoards, $groupBoards); - return array_map("unserialize", array_unique(array_map("serialize", $complete))); + $result = []; + foreach($complete as &$item) { + if(!array_key_exists($item->getId(), $result)) { + $item = $this->mapOwner($item); + if($item->getAcl() !== null) { + foreach ($item->getAcl() as &$acl) { + $acl = $this->mapAcl($acl); + } + } + $result[$item->getId()] = $item; + } + } + return array_values($result); } public function find($boardId) { $this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ); - return $this->boardMapper->find($boardId, true, true); + $board = $this->boardMapper->find($boardId, true, true); + $board = $this->mapOwner($board); + foreach ($board->getAcl() as &$acl) { + if($acl !== null) { + $this->mapAcl($acl); + } + } + return $board; + } + + private function mapAcl(Acl &$acl) { + $userManager = $this->userManager; + $groupManager = $this->groupManager; + $acl->resolveRelation('participant', function($value) use (&$acl, &$userManager, &$groupManager) { + if($acl->getType() === Acl::PERMISSION_TYPE_USER) { + return new User($userManager->get($acl->getParticipant($value))); + } + if($acl->getType() === Acl::PERMISSION_TYPE_GROUP) { + return new Group($groupManager->get($acl->getParticipant($value))); + } + }); + return $acl; + } + + private function mapOwner(Board $board) { + $userManager = $this->userManager; + $board->resolveRelation('owner', function($value) use (&$userManager) { + return new User($userManager->get($value)); + }); + return $board; } public function create($title, $userId, $color) { @@ -113,6 +160,7 @@ class BoardService { $acl->setPermissionEdit($edit); $acl->setPermissionShare($share); $acl->setPermissionManage($manage); + $this->mapAcl($acl); return $this->aclMapper->insert($acl); } @@ -122,12 +170,14 @@ class BoardService { $acl->setPermissionEdit($edit); $acl->setPermissionShare($share); $acl->setPermissionManage($manage); + $this->mapAcl($acl); return $this->aclMapper->update($acl); } public function deleteAcl($id) { $this->permissionService->checkPermission($this->aclMapper, $id, Acl::PERMISSION_SHARE); $acl = $this->aclMapper->find($id); + $this->mapAcl($acl); return $this->aclMapper->delete($acl); } diff --git a/templates/part.board.sidebarView.php b/templates/part.board.sidebarView.php index 6f0e74204..ebb7c9fb6 100644 --- a/templates/part.board.sidebarView.php +++ b/templates/part.board.sidebarView.php @@ -19,10 +19,10 @@ - {{ $item.participant }} + {{ $item.participant.displayname }} - {{ sharee.participant }} + {{ sharee.participant.displayname }} t('No matching user or group found.')); ?> @@ -32,18 +32,18 @@
  • -
    +
    - {{ boardservice.getCurrent().owner }} + {{ boardservice.getCurrent().owner.displayname }}
  • -
    +
    - {{ acl.participant }} + {{ acl.participant.displayname }} diff --git a/templates/part.boardlist.php b/templates/part.boardlist.php index 3436c8db5..98ae6a08a 100644 --- a/templates/part.boardlist.php +++ b/templates/part.boardlist.php @@ -18,9 +18,9 @@
    + displayname="{{ b.owner.uid }}" title="{{ b.owner.displayname }}">
    diff --git a/tests/unit/Db/AclTest.php b/tests/unit/Db/AclTest.php index 8641a0500..befbeb3c5 100644 --- a/tests/unit/Db/AclTest.php +++ b/tests/unit/Db/AclTest.php @@ -23,7 +23,8 @@ namespace OCA\Deck\Db; -class AclTest extends \PHPUnit_Framework_TestCase { +class AclTest extends \Test\TestCase { + private function createAclUser() { $acl = new Acl(); $acl->setId(1); @@ -35,6 +36,7 @@ class AclTest extends \PHPUnit_Framework_TestCase { $acl->setPermissionManage(true); return $acl; } + private function createAclGroup() { $acl = new Acl(); $acl->setId(1); @@ -46,6 +48,7 @@ class AclTest extends \PHPUnit_Framework_TestCase { $acl->setPermissionManage(true); return $acl; } + public function testJsonSerialize() { $acl = $this->createAclUser(); $this->assertEquals([ @@ -70,6 +73,7 @@ class AclTest extends \PHPUnit_Framework_TestCase { 'owner' => false ], $acl->jsonSerialize()); } + public function testSetOwner() { $acl = $this->createAclUser(); $acl->setOwner(1); diff --git a/tests/unit/Service/BoardServiceTest.php b/tests/unit/Service/BoardServiceTest.php index 4330bc31d..21d881eaa 100644 --- a/tests/unit/Service/BoardServiceTest.php +++ b/tests/unit/Service/BoardServiceTest.php @@ -29,68 +29,89 @@ use OCA\Deck\Db\AclMapper; use OCA\Deck\Db\Board; use OCA\Deck\Db\BoardMapper; use OCA\Deck\Db\LabelMapper; +use OCA\Deck\Db\User; use OCP\IGroupManager; use OCP\ILogger; +use OCP\IUser; +use OCP\IUserManager; -class BoardServiceTest extends \PHPUnit_Framework_TestCase { +class BoardServiceTest extends \Test\TestCase { + /** @var BoardService */ private $service; - private $logger; + /** @var L10N */ private $l10n; + /** @var LabelMapper */ private $labelMapper; + /** @var AclMapper */ private $aclMapper; + /** @var BoardMapper */ private $boardMapper; + /** @var IUserManager */ + private $userManager; + /** @var IGroupManager */ private $groupManager; + /** @var PermissionService */ private $permissionService; private $userId = 'admin'; public function setUp() { - $this->l10n = $this->getMockBuilder(L10N::class) - ->disableOriginalConstructor() - ->getMock(); - $this->aclMapper = $this->getMockBuilder(AclMapper::class) - ->disableOriginalConstructor()->getMock(); - $this->boardMapper = $this->getMockBuilder(BoardMapper::class) - ->disableOriginalConstructor()->getMock(); - $this->labelMapper = $this->getMockBuilder(LabelMapper::class) - ->disableOriginalConstructor()->getMock(); - $this->permissionService = $this->getMockBuilder(PermissionService::class) - ->disableOriginalConstructor()->getMock(); + $this->l10n = $this->createMock(L10N::class); + $this->aclMapper = $this->createMock(AclMapper::class); + $this->boardMapper = $this->createMock(BoardMapper::class); + $this->labelMapper = $this->createMock(LabelMapper::class); + $this->permissionService = $this->createMock(PermissionService::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->groupManager = $this->createMock(IGroupManager::class); $this->service = new BoardService( $this->boardMapper, $this->l10n, $this->labelMapper, $this->aclMapper, - $this->permissionService + $this->permissionService, + $this->userManager, + $this->groupManager ); + + $user = $this->createMock(IUser::class); + $user->method('getUID')->willReturn('admin'); + $this->userManager->expects($this->any())->method('get')->with('admin')->willReturn($user); } public function testFindAll() { + $b1 = new Board(); + $b1->setId(1); + $b2 = new Board(); + $b2->setId(2); + $b3 = new Board(); + $b3->setId(3); $this->boardMapper->expects($this->once()) ->method('findAllByUser') ->with('admin') - ->willReturn([1,2,3,6,7]); + ->willReturn([$b1, $b2]); $this->boardMapper->expects($this->once()) ->method('findAllByGroups') ->with('admin', ['a', 'b', 'c']) - ->willReturn([4,5,6,7,8]); + ->willReturn([$b2, $b3]); $userinfo = [ 'user' => 'admin', 'groups' => ['a', 'b', 'c'] ]; $result = $this->service->findAll($userinfo); sort($result); - $this->assertEquals([1,2,3,4,5,6,7,8], $result); + $this->assertEquals([$b1, $b2, $b3], $result); } public function testFind() { + $b1 = new Board(); + $b1->setId(1); $this->boardMapper->expects($this->once()) ->method('find') - ->with(123) - ->willReturn(1); - $this->assertEquals(1, $this->service->find(123)); + ->with(1) + ->willReturn($b1); + $this->assertEquals($b1, $this->service->find(1)); } public function testCreate() { @@ -130,9 +151,11 @@ class BoardServiceTest extends \PHPUnit_Framework_TestCase { } public function testDelete() { + $board = new Board(); + $board->setOwner('admin'); $this->boardMapper->expects($this->once()) ->method('find') - ->willReturn(new Board()); + ->willReturn($board); $this->boardMapper->expects($this->once()) ->method('delete') ->willReturn(1); @@ -140,6 +163,8 @@ class BoardServiceTest extends \PHPUnit_Framework_TestCase { } public function testAddAcl() { + $user = $this->createMock(IUser::class); + $user->method('getUID')->willReturn('admin'); $acl = new Acl(); $acl->setBoardId(123); $acl->setType('user'); @@ -147,6 +172,9 @@ class BoardServiceTest extends \PHPUnit_Framework_TestCase { $acl->setPermissionEdit(true); $acl->setPermissionShare(true); $acl->setPermissionManage(true); + $acl->resolveRelation('participant', function($value) use (&$user) { + return new User($user); + }); $this->aclMapper->expects($this->once()) ->method('insert') ->with($acl) From 2b05227f4b1335a5630d9db8e0a205b30c1f9239 Mon Sep 17 00:00:00 2001 From: Julius Haertl Date: Tue, 28 Feb 2017 13:51:59 +0100 Subject: [PATCH 2/6] Add Group class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Db/Group.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lib/Db/Group.php diff --git a/lib/Db/Group.php b/lib/Db/Group.php new file mode 100644 index 000000000..5979c3894 --- /dev/null +++ b/lib/Db/Group.php @@ -0,0 +1,42 @@ + + * + * @author Julius Härtl + * + * @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 . + * + */ + +namespace OCA\Deck\Db; + +use OCP\IGroup; +use OCP\IUser; + +class Group extends RelationalObject { + + public function __construct(IGroup $group) { + $primaryKey = $group->getGID(); + parent::__construct($primaryKey, $group); + } + + public function getObjectSerialization() { + return [ + 'uid' => $this->object->getGID(), + 'displayname' => $this->object->getDisplayName() + ]; + } +} \ No newline at end of file From 1e9c86e15802d81fa5060e047d0afeba4ba0bd73 Mon Sep 17 00:00:00 2001 From: Julius Haertl Date: Tue, 28 Feb 2017 14:52:15 +0100 Subject: [PATCH 3/6] Code cleaup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Db/Group.php | 1 - lib/Db/RelationalEntity.php | 49 ++++++++++++++++++------- lib/Db/RelationalObject.php | 22 +++++------ lib/Db/User.php | 3 +- lib/Service/BoardService.php | 10 ++--- tests/unit/Db/BoardTest.php | 10 ++--- tests/unit/Service/BoardServiceTest.php | 2 +- 7 files changed, 58 insertions(+), 39 deletions(-) diff --git a/lib/Db/Group.php b/lib/Db/Group.php index 5979c3894..f668282b7 100644 --- a/lib/Db/Group.php +++ b/lib/Db/Group.php @@ -24,7 +24,6 @@ namespace OCA\Deck\Db; use OCP\IGroup; -use OCP\IUser; class Group extends RelationalObject { diff --git a/lib/Db/RelationalEntity.php b/lib/Db/RelationalEntity.php index 498a22703..eebd4576a 100644 --- a/lib/Db/RelationalEntity.php +++ b/lib/Db/RelationalEntity.php @@ -1,6 +1,6 @@ + * @copyright Copyright (c) 2017 Julius Härtl * * @author Julius Härtl * @@ -23,16 +23,14 @@ namespace OCA\Deck\Db; - class RelationalEntity extends \OCP\AppFramework\Db\Entity implements \JsonSerializable { - private $primaryKey; private $_relations = array(); private $_resolvedProperties = []; /** * Mark a property as relation so it will not get updated using Mapper::update - * @param string $property Name of the property + * @param $property string Name of the property */ public function addRelation($property) { if (!in_array($property, $this->_relations)) { @@ -40,6 +38,10 @@ class RelationalEntity extends \OCP\AppFramework\Db\Entity implements \JsonSeria } } + /** + * Mark a property as resolvable via resolveRelation() + * @param $property string Name of the property + */ public function addResolvable($property) { $this->_resolvedProperties[$property] = null; } @@ -63,21 +65,43 @@ class RelationalEntity extends \OCP\AppFramework\Db\Entity implements \JsonSeria $properties = get_object_vars($this); $reflection = new \ReflectionClass($this); $json = []; - foreach($properties as $property=>$value) { - if(substr($property, 0, 1) !== '_' && $reflection->hasProperty($property)) { + foreach ($properties as $property => $value) { + if (substr($property, 0, 1) !== '_' && $reflection->hasProperty($property)) { $propertyReflection = $reflection->getProperty($property); - if(!$propertyReflection->isPrivate()) { + if (!$propertyReflection->isPrivate()) { $json[$property] = $this->getter($property); } } } return $json; - + } + /* + * Resolve relational data from external methods + * + * example usage: + * + * in Board::__construct() + * $this->addResolvable('owner') + * + * in BoardService + * $board->resolveRelation('owner', function($owner) use (&$userManager) { + * return new \OCA\Deck\Db\User($userManager->get($owner)); + * }); + * + * resolved values can be obtained by calling resolveProperty + * e.g. $board->resolveOwner() + * + * TODO: Maybe move from callable to a custom Resolver class that can be reused and use DI? + * + * @param string $property name of the property + * @param callable $resolver anonymous function to resolve relational + * data defined by $property as unique identifier + * @throws \Exception + */ public function resolveRelation($property, $resolver) { + $result = null; if($property !== null && $this->$property !== null) { $result = $resolver($this->$property); - } else { - $result = null; } if($result instanceof RelationalObject || $result === null) { @@ -93,7 +117,7 @@ class RelationalEntity extends \OCP\AppFramework\Db\Entity implements \JsonSeria if($this->_resolvedProperties[$attr] !== null) { return $this->_resolvedProperties[$attr]; } else { - return $this->getter($attr, $args); + return $this->getter($attr); } } @@ -101,9 +125,8 @@ class RelationalEntity extends \OCP\AppFramework\Db\Entity implements \JsonSeria if(strpos($methodName, 'set') === 0 && array_key_exists($attr, $this->_resolvedProperties)) { if(!is_scalar($args[0])) { $args[0] = $args[0]['primaryKey']; - parent::setter($attr, $args); } - parent::setter($attr, $args); + return parent::setter($attr, $args); } return parent::__call($methodName, $args); } diff --git a/lib/Db/RelationalObject.php b/lib/Db/RelationalObject.php index 8a3346c78..e5740e754 100644 --- a/lib/Db/RelationalObject.php +++ b/lib/Db/RelationalObject.php @@ -21,17 +21,11 @@ * */ -/** - * Created by PhpStorm. - * User: jus - * Date: 27.02.17 - * Time: 14:05 - */ - namespace OCA\Deck\Db; +class RelationalObject implements \JsonSerializable { -abstract class RelationalObject implements \JsonSerializable { + private $primaryKey; /** * RelationalObject constructor. @@ -51,11 +45,15 @@ abstract class RelationalObject implements \JsonSerializable { ); } + /** + * This method should be overwritten if object doesn't implement \JsonSerializable + */ public function getObjectSerialization() { - $this->object->jsonSerialize(); + if($this->object instanceof \JsonSerializable) { + $this->object->jsonSerialize(); + } else { + throw new \Exception('jsonSerialize is not implemented on ' . get_class($this)); + } } - public function getPrimaryKey() { - return $this->getPrimaryKey(); - } } \ No newline at end of file diff --git a/lib/Db/User.php b/lib/Db/User.php index 436e5547f..8d0bbf2c4 100644 --- a/lib/Db/User.php +++ b/lib/Db/User.php @@ -23,12 +23,11 @@ namespace OCA\Deck\Db; -use OCP\IGroup; use OCP\IUser; class User extends RelationalObject { - public function __construct(IUser $user = null) { + public function __construct(IUser $user) { $primaryKey = $user->getUID(); parent::__construct($primaryKey, $user); } diff --git a/lib/Service/BoardService.php b/lib/Service/BoardService.php index 664699445..36b6ad89a 100644 --- a/lib/Service/BoardService.php +++ b/lib/Service/BoardService.php @@ -91,12 +91,12 @@ class BoardService { private function mapAcl(Acl &$acl) { $userManager = $this->userManager; $groupManager = $this->groupManager; - $acl->resolveRelation('participant', function($value) use (&$acl, &$userManager, &$groupManager) { + $acl->resolveRelation('participant', function($participant) use (&$acl, &$userManager, &$groupManager) { if($acl->getType() === Acl::PERMISSION_TYPE_USER) { - return new User($userManager->get($acl->getParticipant($value))); + return new User($userManager->get($acl->getParticipant($participant))); } if($acl->getType() === Acl::PERMISSION_TYPE_GROUP) { - return new Group($groupManager->get($acl->getParticipant($value))); + return new Group($groupManager->get($acl->getParticipant($participant))); } }); return $acl; @@ -104,8 +104,8 @@ class BoardService { private function mapOwner(Board $board) { $userManager = $this->userManager; - $board->resolveRelation('owner', function($value) use (&$userManager) { - return new User($userManager->get($value)); + $board->resolveRelation('owner', function($owner) use (&$userManager) { + return new User($userManager->get($owner)); }); return $board; } diff --git a/tests/unit/Db/BoardTest.php b/tests/unit/Db/BoardTest.php index b8af9b41d..7cbb11d50 100644 --- a/tests/unit/Db/BoardTest.php +++ b/tests/unit/Db/BoardTest.php @@ -20,8 +20,8 @@ class BoardTest extends \PHPUnit_Framework_TestCase { 'title' => "My Board", 'owner' => "admin", 'color' => "000000", - 'labels' => null, - 'acl' => null, + 'labels' => array(), + 'acl' => array(), 'archived' => false ], $board->jsonSerialize()); } @@ -35,7 +35,7 @@ class BoardTest extends \PHPUnit_Framework_TestCase { 'owner' => "admin", 'color' => "000000", 'labels' => array("foo", "bar"), - 'acl' => null, + 'acl' => array(), 'archived' => false ], $board->jsonSerialize()); } @@ -55,8 +55,8 @@ class BoardTest extends \PHPUnit_Framework_TestCase { 'title' => "My Board", 'owner' => "admin", 'color' => "000000", - 'labels' => null, - 'acl' => null, + 'labels' => array(), + 'acl' => array(), 'archived' => false, 'shared' => 1, ], $board->jsonSerialize()); diff --git a/tests/unit/Service/BoardServiceTest.php b/tests/unit/Service/BoardServiceTest.php index 21d881eaa..27f984878 100644 --- a/tests/unit/Service/BoardServiceTest.php +++ b/tests/unit/Service/BoardServiceTest.php @@ -172,7 +172,7 @@ class BoardServiceTest extends \Test\TestCase { $acl->setPermissionEdit(true); $acl->setPermissionShare(true); $acl->setPermissionManage(true); - $acl->resolveRelation('participant', function($value) use (&$user) { + $acl->resolveRelation('participant', function($participant) use (&$user) { return new User($user); }); $this->aclMapper->expects($this->once()) From 02eecb3a3fa5c7b14cb4ee3843204b8038ed92c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Mon, 27 Mar 2017 20:10:18 +0200 Subject: [PATCH 4/6] Use mapper classes for relational data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/Db/Board.php | 1 + lib/Db/BoardMapper.php | 40 ++++++++++++++++++- lib/Db/Card.php | 1 + lib/Db/CardMapper.php | 12 +++++- lib/Db/RelationalEntity.php | 19 ++++++--- lib/Service/BoardService.php | 53 +++++++------------------ lib/Service/CardService.php | 3 +- templates/part.boardlist.php | 7 +--- templates/part.card.php | 2 +- tests/unit/Db/AclMapperTest.php | 11 ++++- tests/unit/Db/BoardMapperTest.php | 11 ++++- tests/unit/Service/BoardServiceTest.php | 13 +----- 12 files changed, 106 insertions(+), 67 deletions(-) diff --git a/lib/Db/Board.php b/lib/Db/Board.php index 4d3b5e42d..868b54fde 100644 --- a/lib/Db/Board.php +++ b/lib/Db/Board.php @@ -52,6 +52,7 @@ class Board extends RelationalEntity implements JsonSerializable { if ($this->shared === -1) { unset($json['shared']); } + $json['owner'] = $this->resolveOwner(); return $json; } diff --git a/lib/Db/BoardMapper.php b/lib/Db/BoardMapper.php index 38652eceb..9d982070d 100644 --- a/lib/Db/BoardMapper.php +++ b/lib/Db/BoardMapper.php @@ -24,19 +24,31 @@ namespace OCA\Deck\Db; use OCP\IDBConnection; - +use OCP\IUserManager; +use OCP\IGroupManager; class BoardMapper extends DeckMapper implements IPermissionMapper { private $labelMapper; private $aclMapper; private $stackMapper; + private $userManager; + private $groupManager; - public function __construct(IDBConnection $db, LabelMapper $labelMapper, AclMapper $aclMapper, StackMapper $stackMapper) { + public function __construct( + IDBConnection $db, + LabelMapper $labelMapper, + AclMapper $aclMapper, + StackMapper $stackMapper, + IUserManager $userManager, + IGroupManager $groupManager + ) { parent::__construct($db, 'deck_boards', '\OCA\Deck\Db\Board'); $this->labelMapper = $labelMapper; $this->aclMapper = $aclMapper; $this->stackMapper = $stackMapper; + $this->userManager = $userManager; + $this->groupManager = $groupManager; } @@ -149,5 +161,29 @@ class BoardMapper extends DeckMapper implements IPermissionMapper { return $id; } + 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) { + return new User($userManager->get($acl->getParticipant($participant))); + } + if($acl->getType() === Acl::PERMISSION_TYPE_GROUP) { + return new Group($groupManager->get($acl->getParticipant($participant))); + } + throw new \Exception('Unknown permission type for mapping Acl'); + }); + } + + /** + * @param Board $board + */ + public function mapOwner(Board &$board) { + $userManager = $this->userManager; + $board->resolveRelation('owner', function($owner) use (&$userManager) { + return new User($userManager->get($owner)); + }); + } + } \ No newline at end of file diff --git a/lib/Db/Card.php b/lib/Db/Card.php index bdbde9cff..53f73771d 100644 --- a/lib/Db/Card.php +++ b/lib/Db/Card.php @@ -48,6 +48,7 @@ class Card extends RelationalEntity implements JsonSerializable { $this->addType('createdAt', 'integer'); $this->addType('archived', 'boolean'); $this->addRelation('labels'); + $this->addResolvable('owner'); } } diff --git a/lib/Db/CardMapper.php b/lib/Db/CardMapper.php index 961d77866..1542a95ea 100644 --- a/lib/Db/CardMapper.php +++ b/lib/Db/CardMapper.php @@ -25,15 +25,17 @@ namespace OCA\Deck\Db; use OCP\AppFramework\Db\Entity; use OCP\IDBConnection; +use OCP\IUserManager; class CardMapper extends DeckMapper implements IPermissionMapper { private $labelMapper; - public function __construct(IDBConnection $db, LabelMapper $labelMapper) { + public function __construct(IDBConnection $db, LabelMapper $labelMapper, IUserManager $userManager) { parent::__construct($db, 'deck_cards', '\OCA\Deck\Db\Card'); $this->labelMapper = $labelMapper; + $this->userManager = $userManager; } public function insert(Entity $entity) { @@ -57,6 +59,7 @@ class CardMapper extends DeckMapper implements IPermissionMapper { $card = $this->findEntity($sql, [$id]); $labels = $this->labelMapper->findAssignedLabelsForCard($card->id); $card->setLabels($labels); + $this->mapOwner($card); return $card; } @@ -125,5 +128,12 @@ class CardMapper extends DeckMapper implements IPermissionMapper { return $row['id']; } + public function mapOwner(Card &$card) { + $userManager = $this->userManager; + $card->resolveRelation('owner', function($owner) use (&$userManager) { + return new User($userManager->get($owner)); + }); + } + } \ No newline at end of file diff --git a/lib/Db/RelationalEntity.php b/lib/Db/RelationalEntity.php index eebd4576a..80895c963 100644 --- a/lib/Db/RelationalEntity.php +++ b/lib/Db/RelationalEntity.php @@ -23,7 +23,9 @@ namespace OCA\Deck\Db; -class RelationalEntity extends \OCP\AppFramework\Db\Entity implements \JsonSerializable { +use OCP\AppFramework\Db\Entity; + +class RelationalEntity extends Entity implements \JsonSerializable { private $_relations = array(); private $_resolvedProperties = []; @@ -73,9 +75,15 @@ class RelationalEntity extends \OCP\AppFramework\Db\Entity implements \JsonSeria } } } + foreach ($this->_resolvedProperties as $property => $value) { + if($value !== null) { + $json[$property] = $value; + } + } return $json; } - /* + + /* * Resolve relational data from external methods * * example usage: @@ -83,7 +91,7 @@ class RelationalEntity extends \OCP\AppFramework\Db\Entity implements \JsonSeria * in Board::__construct() * $this->addResolvable('owner') * - * in BoardService + * in BoardMapper * $board->resolveRelation('owner', function($owner) use (&$userManager) { * return new \OCA\Deck\Db\User($userManager->get($owner)); * }); @@ -91,8 +99,6 @@ class RelationalEntity extends \OCP\AppFramework\Db\Entity implements \JsonSeria * resolved values can be obtained by calling resolveProperty * e.g. $board->resolveOwner() * - * TODO: Maybe move from callable to a custom Resolver class that can be reused and use DI? - * * @param string $property name of the property * @param callable $resolver anonymous function to resolve relational * data defined by $property as unique identifier @@ -126,7 +132,8 @@ class RelationalEntity extends \OCP\AppFramework\Db\Entity implements \JsonSeria if(!is_scalar($args[0])) { $args[0] = $args[0]['primaryKey']; } - return parent::setter($attr, $args); + parent::setter($attr, $args); + return null; } return parent::__call($methodName, $args); } diff --git a/lib/Service/BoardService.php b/lib/Service/BoardService.php index 36b6ad89a..5a8af4f88 100644 --- a/lib/Service/BoardService.php +++ b/lib/Service/BoardService.php @@ -25,18 +25,11 @@ namespace OCA\Deck\Service; use OCA\Deck\Db\Acl; use OCA\Deck\Db\AclMapper; -use OCA\Deck\Db\Group; use OCA\Deck\Db\Label; - - -use OCA\Deck\Db\User; -use OCP\IGroupManager; use OCP\IL10N; - use OCA\Deck\Db\Board; use OCA\Deck\Db\BoardMapper; use OCA\Deck\Db\LabelMapper; -use OCP\IUserManager; class BoardService { @@ -47,14 +40,12 @@ class BoardService { private $l10n; private $permissionService; - public function __construct(BoardMapper $boardMapper, IL10N $l10n, LabelMapper $labelMapper, AclMapper $aclMapper, PermissionService $permissionService, IUserManager $userManager, IGroupManager $groupManager) { + public function __construct(BoardMapper $boardMapper, IL10N $l10n, LabelMapper $labelMapper, AclMapper $aclMapper, PermissionService $permissionService) { $this->boardMapper = $boardMapper; $this->labelMapper = $labelMapper; $this->aclMapper = $aclMapper; $this->l10n = $l10n; $this->permissionService = $permissionService; - $this->userManager = $userManager; - $this->groupManager = $groupManager; } public function findAll($userInfo) { @@ -64,10 +55,10 @@ class BoardService { $result = []; foreach($complete as &$item) { if(!array_key_exists($item->getId(), $result)) { - $item = $this->mapOwner($item); + $this->boardMapper->mapOwner($item); if($item->getAcl() !== null) { foreach ($item->getAcl() as &$acl) { - $acl = $this->mapAcl($acl); + $this->boardMapper->mapAcl($acl); } } $result[$item->getId()] = $item; @@ -78,37 +69,18 @@ class BoardService { public function find($boardId) { $this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ); + /** @var Board $board */ $board = $this->boardMapper->find($boardId, true, true); - $board = $this->mapOwner($board); + $this->boardMapper->mapOwner($board); foreach ($board->getAcl() as &$acl) { if($acl !== null) { - $this->mapAcl($acl); + $this->boardMapper->mapAcl($acl); } } return $board; } - private 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) { - return new User($userManager->get($acl->getParticipant($participant))); - } - if($acl->getType() === Acl::PERMISSION_TYPE_GROUP) { - return new Group($groupManager->get($acl->getParticipant($participant))); - } - }); - return $acl; - } - private function mapOwner(Board $board) { - $userManager = $this->userManager; - $board->resolveRelation('owner', function($owner) use (&$userManager) { - return new User($userManager->get($owner)); - }); - return $board; - } public function create($title, $userId, $color) { $board = new Board(); @@ -133,6 +105,7 @@ class BoardService { $labels[] = $this->labelMapper->insert($label); } $new_board->setLabels($labels); + $this->boardMapper->mapOwner($new_board); return $new_board; } @@ -147,6 +120,7 @@ class BoardService { $board = $this->find($id); $board->setTitle($title); $board->setColor($color); + $this->boardMapper->mapOwner($board); return $this->boardMapper->update($board); } @@ -160,24 +134,27 @@ class BoardService { $acl->setPermissionEdit($edit); $acl->setPermissionShare($share); $acl->setPermissionManage($manage); - $this->mapAcl($acl); - return $this->aclMapper->insert($acl); + $newAcl = $this->aclMapper->insert($acl); + $this->boardMapper->mapAcl($newAcl); + return $newAcl; } public function updateAcl($id, $edit, $share, $manage) { $this->permissionService->checkPermission($this->aclMapper, $id, Acl::PERMISSION_SHARE); + /** @var Acl $acl */ $acl = $this->aclMapper->find($id); $acl->setPermissionEdit($edit); $acl->setPermissionShare($share); $acl->setPermissionManage($manage); - $this->mapAcl($acl); + $this->boardMapper->mapAcl($acl); return $this->aclMapper->update($acl); } public function deleteAcl($id) { $this->permissionService->checkPermission($this->aclMapper, $id, Acl::PERMISSION_SHARE); + /** @var Acl $acl */ $acl = $this->aclMapper->find($id); - $this->mapAcl($acl); + $this->boardMapper->mapAcl($acl); return $this->aclMapper->delete($acl); } diff --git a/lib/Service/CardService.php b/lib/Service/CardService.php index bc7437c5b..7fd7a6b36 100644 --- a/lib/Service/CardService.php +++ b/lib/Service/CardService.php @@ -42,7 +42,8 @@ class CardService { public function find($cardId) { $this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_READ); - return $this->cardMapper->find($cardId); + $card = $this->cardMapper->find($cardId); + return $card; } /** diff --git a/templates/part.boardlist.php b/templates/part.boardlist.php index 98ae6a08a..f52e41564 100644 --- a/templates/part.boardlist.php +++ b/templates/part.boardlist.php @@ -17,11 +17,8 @@ {{ b.title }}
    -
    -
    +
    +
    diff --git a/templates/part.card.php b/templates/part.card.php index ec2548455..5be912ea4 100644 --- a/templates/part.card.php +++ b/templates/part.card.php @@ -26,7 +26,7 @@ t('Modified:')); ?> {{ cardservice.getCurrent().lastModified|relativeDateFilter }} t('Created:')); ?> {{ cardservice.getCurrent().createdAt|relativeDateFilter }} t('by')); ?> - {{ cardservice.getCurrent().owner }} + {{ cardservice.getCurrent().owner.displayname }}
    diff --git a/tests/unit/Db/AclMapperTest.php b/tests/unit/Db/AclMapperTest.php index 76c307301..ca2ea6b39 100644 --- a/tests/unit/Db/AclMapperTest.php +++ b/tests/unit/Db/AclMapperTest.php @@ -23,6 +23,8 @@ namespace OCA\Deck\Db; +use OCP\IGroupManager; +use OCP\IUserManager; use Test\AppFramework\Db\MapperTestUtility; /** @@ -33,6 +35,8 @@ class AclMapperTest extends MapperTestUtility { private $dbConnection; private $aclMapper; private $boardMapper; + private $userManager; + private $groupManager; // Data private $acls; @@ -43,11 +47,16 @@ class AclMapperTest extends MapperTestUtility { $this->dbConnection = \OC::$server->getDatabaseConnection(); $this->aclMapper = new AclMapper($this->dbConnection); + $this->userManager = $this->createMock(IUserManager::class); + $this->groupManager = $this->createMock(IGroupManager::class); $this->boardMapper = new BoardMapper( $this->dbConnection, \OC::$server->query(LabelMapper::class), $this->aclMapper, - \OC::$server->query(StackMapper::class)); + \OC::$server->query(StackMapper::class), + $this->userManager, + $this->groupManager + ); $this->boards = [ $this->boardMapper->insert($this->getBoard('MyBoard 1', 'user1')), diff --git a/tests/unit/Db/BoardMapperTest.php b/tests/unit/Db/BoardMapperTest.php index b186c0e3b..9b3e87d33 100644 --- a/tests/unit/Db/BoardMapperTest.php +++ b/tests/unit/Db/BoardMapperTest.php @@ -23,6 +23,8 @@ namespace OCA\Deck\Db; +use OCP\IGroupManager; +use OCP\IUserManager; use Test\AppFramework\Db\MapperTestUtility; /** @@ -33,6 +35,8 @@ class BoardMapperTest extends MapperTestUtility { private $dbConnection; private $aclMapper; private $boardMapper; + private $userManager; + private $groupManager; // Data private $acls; @@ -41,12 +45,17 @@ class BoardMapperTest extends MapperTestUtility { public function setup(){ parent::setUp(); + $this->userManager = $this->createMock(IUserManager::class); + $this->groupManager = $this->createMock(IGroupManager::class); + $this->dbConnection = \OC::$server->getDatabaseConnection(); $this->boardMapper = new BoardMapper( $this->dbConnection, \OC::$server->query(LabelMapper::class), \OC::$server->query(AclMapper::class), - \OC::$server->query(StackMapper::class) + \OC::$server->query(StackMapper::class), + $this->userManager, + $this->groupManager ); $this->aclMapper = \OC::$server->query(AclMapper::class); $this->labelMapper = \OC::$server->query(LabelMapper::class); diff --git a/tests/unit/Service/BoardServiceTest.php b/tests/unit/Service/BoardServiceTest.php index 27f984878..a60955123 100644 --- a/tests/unit/Service/BoardServiceTest.php +++ b/tests/unit/Service/BoardServiceTest.php @@ -47,10 +47,6 @@ class BoardServiceTest extends \Test\TestCase { private $aclMapper; /** @var BoardMapper */ private $boardMapper; - /** @var IUserManager */ - private $userManager; - /** @var IGroupManager */ - private $groupManager; /** @var PermissionService */ private $permissionService; @@ -62,22 +58,17 @@ class BoardServiceTest extends \Test\TestCase { $this->boardMapper = $this->createMock(BoardMapper::class); $this->labelMapper = $this->createMock(LabelMapper::class); $this->permissionService = $this->createMock(PermissionService::class); - $this->userManager = $this->createMock(IUserManager::class); - $this->groupManager = $this->createMock(IGroupManager::class); $this->service = new BoardService( $this->boardMapper, $this->l10n, $this->labelMapper, $this->aclMapper, - $this->permissionService, - $this->userManager, - $this->groupManager + $this->permissionService ); $user = $this->createMock(IUser::class); $user->method('getUID')->willReturn('admin'); - $this->userManager->expects($this->any())->method('get')->with('admin')->willReturn($user); } public function testFindAll() { @@ -173,7 +164,7 @@ class BoardServiceTest extends \Test\TestCase { $acl->setPermissionShare(true); $acl->setPermissionManage(true); $acl->resolveRelation('participant', function($participant) use (&$user) { - return new User($user); + return null; }); $this->aclMapper->expects($this->once()) ->method('insert') From 74c8a6e848995c860ada53732da4aeaf67994a86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Sat, 22 Apr 2017 21:32:35 +0200 Subject: [PATCH 5/6] Fix board acl type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- js/service/BoardService.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/js/service/BoardService.js b/js/service/BoardService.js index 7e60c88a9..cc7ad5e22 100644 --- a/js/service/BoardService.js +++ b/js/service/BoardService.js @@ -104,23 +104,23 @@ app.factory('BoardService', function(ApiService, $http, $q){ } }; - BoardService.prototype.addAcl = function(acl) { - var board = this.getCurrent(); - var deferred = $q.defer(); - var self = this; - var _acl = acl; - $http.post(this.baseUrl + '/' + acl.boardId + '/acl', _acl).then(function (response) { - if(!board.acl || board.acl.length === 0) { - board.acl = {}; - } - board.acl[response.data.id] = response.data; - deferred.resolve(response.data); - }, function (error) { - deferred.reject('Error creating ACL ' + _acl); - }); - acl = null; - return deferred.promise; - }; + BoardService.prototype.addAcl = function (acl) { + var board = this.getCurrent(); + var deferred = $q.defer(); + var self = this; + var _acl = acl; + $http.post(this.baseUrl + '/' + acl.boardId + '/acl', _acl).then(function (response) { + if (!board.acl || board.acl.length === 0) { + board.acl = {}; + } + board.acl[response.data.id] = response.data; + deferred.resolve(response.data); + }, function (error) { + deferred.reject('Error creating ACL ' + _acl); + }); + acl = null; + return deferred.promise; + }; BoardService.prototype.deleteAcl = function(acl) { var board = this.getCurrent(); From 04f5e2c2e1fa697de48e6cc3125709be4056a5fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Thu, 27 Apr 2017 13:14:58 +0200 Subject: [PATCH 6/6] Don't show own user account MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- js/service/BoardService.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/js/service/BoardService.js b/js/service/BoardService.js index cc7ad5e22..b359b1730 100644 --- a/js/service/BoardService.js +++ b/js/service/BoardService.js @@ -58,11 +58,11 @@ app.factory('BoardService', function(ApiService, $http, $q){ var acl = self.generateAcl('user', item); var exists = false; angular.forEach(self.getCurrent().acl, function (acl) { - if (acl.participant.primaryKey === item.value.shareWith || OC.getCurrentUser() === item.value.shareWith) { + if (acl.participant.primaryKey === item.value.shareWith) { exists = true; } }); - if (!exists) { + if (!exists && OC.getCurrentUser().uid !== item.value.shareWith) { self.sharees.push(acl); } }); @@ -156,7 +156,6 @@ app.factory('BoardService', function(ApiService, $http, $q){ var deferred = $q.defer(); $http.get(this.baseUrl + '/' + board.id + '/permissions').then(function (response) { board.permissions = response.data; - console.log(board.permissions); deferred.resolve(response.data); }, function (error) { deferred.reject('Error fetching board permissions ' + board);