Start implementing board sharing

This commit is contained in:
Julius Haertl
2016-08-16 10:29:37 +02:00
parent 135d9c9cbc
commit 6a040f1470
10 changed files with 140 additions and 25 deletions

View File

@@ -4,6 +4,7 @@ namespace OCA\Deck\Db;
use OCP\IDb;
use OCP\AppFramework\Db\Mapper;
use Symfony\Component\Config\Definition\Exception\Exception;
class BoardMapper extends Mapper {
@@ -27,7 +28,7 @@ class BoardMapper extends Mapper {
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
*/
public function find($id) {
$sql = 'SELECT * FROM `*PREFIX*deck_boards` ' .
$sql = 'SELECT id, title, owner, color, archived FROM `*PREFIX*deck_boards` ' .
'WHERE `id` = ?';
$board = $this->findEntity($sql, [$id]);
@@ -49,9 +50,39 @@ class BoardMapper extends Mapper {
* @param null $offset
* @return array
*/
public function findAll($userId, $limit=null, $offset=null) {
$sql = 'SELECT * FROM `*PREFIX*deck_boards` WHERE `owner` = ? ORDER BY `title`';
$entries = $this->findEntities($sql, [$userId], $limit, $offset);
public function findAllByUser($userId, $limit=null, $offset=null) {
$sql = 'SELECT id, title, owner, color, archived, 0 as shared FROM oc_deck_boards WHERE owner = ? UNION ' .
'SELECT boards.id, title, owner, color, archived, 1 as shared FROM oc_deck_boards as boards ' .
'JOIN oc_deck_board_acl as acl ON boards.id=acl.board_id WHERE acl.participant=? AND acl.type=\'user\' AND boards.owner != ?';
$entries = $this->findEntities($sql, [$userId, $userId, $userId], $limit, $offset);
/* @var Board $entry */
foreach ($entries as $entry) {
$acl = $this->aclMapper->findAll($entry->id);
$entry->setAcl($acl);
}
return $entries;
}
/**
* Find all boards for a given user
* @param $groups
* @param null $limit
* @param null $offset
* @return array
*/
public function findAllByGroups($userId, $groups, $limit=null, $offset=null) {
if(count($groups)<=0) {
return [];
}
$sql = 'SELECT boards.id, title, owner, color, archived, 2 as shared FROM oc_deck_boards as boards ' .
'INNER JOIN oc_deck_board_acl as acl ON boards.id=acl.board_id WHERE owner != ? AND type=\'group\' AND (';
$countGroups = 0;
foreach ($groups as $group) {
$sql .= 'acl.participant = ? ';
if(count($groups)>1 && $countGroups++<count($groups)-1)
$sql .= ' OR ';
}
$sql .= ');';
$entries = $this->findEntities($sql, array_merge([$userId], $groups), $limit, $offset);
/* @var Board $entry */
foreach ($entries as $entry) {
$acl = $this->aclMapper->findAll($entry->id);
@@ -65,4 +96,23 @@ class BoardMapper extends Mapper {
return parent::delete($entity);
}
public function userCanView($boardId, $userInfo) {
$board = $this->find($boardId);
if($board->getOwner()===$userInfo['user']) {
return true;
}
try {
$sql = 'SELECT acl.* FROM oc_deck_boards as boards ' .
'JOIN oc_deck_board_acl as acl ON boards.id=acl.board_id WHERE acl.participant=? AND acl.type=\'user\' AND boards.id = ? AND boards.owner != ?';
$acl = $this->find($sql, [$userInfo['user'], $boardId, $userInfo['user']], $limit, $offset);
return true;
} catch (Exception $e) { }
try {
$acl = $this->find($sql, [$userInfo['user'], $boardId, $userInfo['user']], $limit, $offset);
return true;
} catch (Exception $e) {
}
}
}