diff options
author | Vincent Petry <pvince81@owncloud.com> | 2016-05-11 12:25:09 +0200 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2016-05-20 17:56:02 +0200 |
commit | 3cd65fe25dc6f213dd7e4a1687616dc5e0960d4d (patch) | |
tree | 01a4e4a6bb7ff4f3a9243bd6c523fc5a738e1c65 | |
parent | 09b3883d9ceae77793e524209090f2e36ab61260 (diff) | |
download | nextcloud-server-3cd65fe25dc6f213dd7e4a1687616dc5e0960d4d.tar.gz nextcloud-server-3cd65fe25dc6f213dd7e4a1687616dc5e0960d4d.zip |
Add systemtag_group table and get/set methods
Added systemtag to group mapping table.
Added methods in ISystemTagManager to get/set the group mappings.
-rw-r--r-- | db_structure.xml | 43 | ||||
-rw-r--r-- | lib/private/SystemTag/SystemTagManager.php | 55 | ||||
-rw-r--r-- | lib/public/SystemTag/ISystemTagManager.php | 20 | ||||
-rw-r--r-- | tests/lib/SystemTag/SystemTagManagerTest.php | 17 |
4 files changed, 135 insertions, 0 deletions
diff --git a/db_structure.xml b/db_structure.xml index 6e57b003fcf..640edfbfd80 100644 --- a/db_structure.xml +++ b/db_structure.xml @@ -1383,6 +1383,49 @@ <table> <!-- + System tag to group mapping + --> + <name>*dbprefix*systemtag_group</name> + + <declaration> + + <!-- Foreign Key systemtag::id --> + <field> + <name>systemtagid</name> + <type>integer</type> + <default>0</default> + <notnull>true</notnull> + <unsigned>true</unsigned> + <length>4</length> + </field> + + <field> + <name>gid</name> + <type>string</type> + <notnull>true</notnull> + </field> + + <index> + <name>systemtag_group</name> + <primary>true</primary> + <unique>true</unique> + <field> + <name>gid</name> + <sorting>ascending</sorting> + </field> + <field> + <name>systemtagid</name> + <sorting>ascending</sorting> + </field> + </index> + + </declaration> + + </table> + + <table> + + <!-- Namespaced Key-Value Store for arbitrary data. - Keys are namespaced per userid and appid. - E.g. (admin, files, foo) -> bar diff --git a/lib/private/SystemTag/SystemTagManager.php b/lib/private/SystemTag/SystemTagManager.php index 495df674a03..1c91ad1f578 100644 --- a/lib/private/SystemTag/SystemTagManager.php +++ b/lib/private/SystemTag/SystemTagManager.php @@ -42,6 +42,7 @@ use OCP\IUser; class SystemTagManager implements ISystemTagManager { const TAG_TABLE = 'systemtag'; + const TAG_GROUP_TABLE = 'systemtag_group'; /** @var IDBConnection */ protected $connection; @@ -365,4 +366,58 @@ class SystemTagManager implements ISystemTagManager { private function createSystemTagFromRow($row) { return new SystemTag((int)$row['id'], $row['name'], (bool)$row['visibility'], (bool)$row['editable']); } + + /** + * {@inheritdoc} + */ + public function setTagGroups(ISystemTag $tag, $groupIds) { + // delete relationships first + $this->connection->beginTransaction(); + try { + $query = $this->connection->getQueryBuilder(); + $query->delete(self::TAG_GROUP_TABLE) + ->where($query->expr()->eq('systemtagid', $query->createNamedParameter($tag->getId()))) + ->execute(); + + // add each group id + $query = $this->connection->getQueryBuilder(); + $query->insert(self::TAG_GROUP_TABLE) + ->values([ + 'systemtagid' => $query->createNamedParameter($tag->getId()), + 'gid' => $query->createParameter('gid'), + ]); + foreach ($groupIds as $groupId) { + $query->setParameter('gid', $groupId); + $query->execute(); + } + + $this->connection->commit(); + } catch (\Exception $e) { + $this->connection->rollback(); + throw $e; + } + + return false; + } + + /** + * {@inheritdoc} + */ + public function getTagGroups(ISystemTag $tag) { + $groupIds = []; + $query = $this->connection->getQueryBuilder(); + $query->select('*') + ->from(self::TAG_GROUP_TABLE) + ->where($query->expr()->eq('systemtagid', $query->createNamedParameter($tag->getId()))) + ->orderBy('gid'); + + $result = $query->execute(); + while ($row = $result->fetch()) { + $groupIds[] = $row['gid']; + } + + $result->closeCursor(); + + return $groupIds; + } } diff --git a/lib/public/SystemTag/ISystemTagManager.php b/lib/public/SystemTag/ISystemTagManager.php index 283ca63e4f6..a2c61a8dcc2 100644 --- a/lib/public/SystemTag/ISystemTagManager.php +++ b/lib/public/SystemTag/ISystemTagManager.php @@ -141,4 +141,24 @@ interface ISystemTagManager { */ public function canUserSeeTag(ISystemTag $tag, IUser $userId); + /** + * Set groups that can assign a given tag. + * + * @param ISystemTag $tag tag for group assignment + * @param string[] $groupIds group ids of groups that can assign/unassign the tag + * + * @since 9.1.0 + */ + public function setTagGroups(ISystemTag $tag, $groupIds); + + /** + * Get groups that can assign a given tag. + * + * @param ISystemTag $tag tag for group assignment + * + * @return string[] group ids of groups that can assign/unassign the tag + * + * @since 9.1.0 + */ + public function getTagGroups(ISystemTag $tag); } diff --git a/tests/lib/SystemTag/SystemTagManagerTest.php b/tests/lib/SystemTag/SystemTagManagerTest.php index fa13d287ed7..408134a8757 100644 --- a/tests/lib/SystemTag/SystemTagManagerTest.php +++ b/tests/lib/SystemTag/SystemTagManagerTest.php @@ -478,6 +478,23 @@ class SystemTagManagerTest extends TestCase { $this->assertEquals($expectedResult, $this->tagManager->canUserAssignTag($tag1, $user)); } + public function testTagGroups() { + $tag1 = $this->tagManager->createTag('tag1', true, false); + $tag2 = $this->tagManager->createTag('tag2', true, false); + $this->tagManager->setTagGroups($tag1, ['group1', 'group2']); + $this->tagManager->setTagGroups($tag2, ['group2', 'group3']); + + $this->assertEquals(['group1', 'group2'], $this->tagManager->getTagGroups($tag1)); + $this->assertEquals(['group2', 'group3'], $this->tagManager->getTagGroups($tag2)); + + // change groups + $this->tagManager->setTagGroups($tag1, ['group3', 'group4']); + $this->tagManager->setTagGroups($tag2, []); + + $this->assertEquals(['group3', 'group4'], $this->tagManager->getTagGroups($tag1)); + $this->assertEquals([], $this->tagManager->getTagGroups($tag2)); + } + /** * @param ISystemTag $tag1 * @param ISystemTag $tag2 |