mirror of
https://github.com/nextcloud/server.git
synced 2026-04-15 22:11:17 -04:00
Add systemtag_group table and get/set methods
Added systemtag to group mapping table. Added methods in ISystemTagManager to get/set the group mappings.
This commit is contained in:
parent
09b3883d9c
commit
3cd65fe25d
4 changed files with 135 additions and 0 deletions
|
|
@ -1380,6 +1380,49 @@
|
|||
|
||||
</table>
|
||||
|
||||
<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>
|
||||
|
||||
<!--
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue