@@ -231,7 +231,7 @@ |
||
| 231 | 231 | $result->closeCursor(); |
| 232 | 232 | |
| 233 | 233 | if ($all) { |
| 234 | - return ((int)$row[0] === count($objIds)); |
|
| 234 | + return ((int) $row[0] === count($objIds)); |
|
| 235 | 235 | } else { |
| 236 | 236 | return (bool) $row; |
| 237 | 237 | } |
@@ -35,229 +35,229 @@ |
||
| 35 | 35 | |
| 36 | 36 | class SystemTagObjectMapper implements ISystemTagObjectMapper { |
| 37 | 37 | |
| 38 | - const RELATION_TABLE = 'systemtag_object_mapping'; |
|
| 39 | - |
|
| 40 | - /** @var ISystemTagManager */ |
|
| 41 | - protected $tagManager; |
|
| 42 | - |
|
| 43 | - /** @var IDBConnection */ |
|
| 44 | - protected $connection; |
|
| 45 | - |
|
| 46 | - /** @var EventDispatcherInterface */ |
|
| 47 | - protected $dispatcher; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Constructor. |
|
| 51 | - * |
|
| 52 | - * @param IDBConnection $connection database connection |
|
| 53 | - * @param ISystemTagManager $tagManager system tag manager |
|
| 54 | - * @param EventDispatcherInterface $dispatcher |
|
| 55 | - */ |
|
| 56 | - public function __construct(IDBConnection $connection, ISystemTagManager $tagManager, EventDispatcherInterface $dispatcher) { |
|
| 57 | - $this->connection = $connection; |
|
| 58 | - $this->tagManager = $tagManager; |
|
| 59 | - $this->dispatcher = $dispatcher; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * {@inheritdoc} |
|
| 64 | - */ |
|
| 65 | - public function getTagIdsForObjects($objIds, $objectType) { |
|
| 66 | - if (!is_array($objIds)) { |
|
| 67 | - $objIds = [$objIds]; |
|
| 68 | - } else if (empty($objIds)) { |
|
| 69 | - return []; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - $query = $this->connection->getQueryBuilder(); |
|
| 73 | - $query->select(['systemtagid', 'objectid']) |
|
| 74 | - ->from(self::RELATION_TABLE) |
|
| 75 | - ->where($query->expr()->in('objectid', $query->createParameter('objectids'))) |
|
| 76 | - ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
| 77 | - ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
| 78 | - ->setParameter('objecttype', $objectType) |
|
| 79 | - ->addOrderBy('objectid', 'ASC') |
|
| 80 | - ->addOrderBy('systemtagid', 'ASC'); |
|
| 81 | - |
|
| 82 | - $mapping = []; |
|
| 83 | - foreach ($objIds as $objId) { |
|
| 84 | - $mapping[$objId] = []; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - $result = $query->execute(); |
|
| 88 | - while ($row = $result->fetch()) { |
|
| 89 | - $objectId = $row['objectid']; |
|
| 90 | - $mapping[$objectId][] = $row['systemtagid']; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $result->closeCursor(); |
|
| 94 | - |
|
| 95 | - return $mapping; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * {@inheritdoc} |
|
| 100 | - */ |
|
| 101 | - public function getObjectIdsForTags($tagIds, $objectType, $limit = 0, $offset = '') { |
|
| 102 | - if (!is_array($tagIds)) { |
|
| 103 | - $tagIds = [$tagIds]; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $this->assertTagsExist($tagIds); |
|
| 107 | - |
|
| 108 | - $query = $this->connection->getQueryBuilder(); |
|
| 109 | - $query->selectDistinct('objectid') |
|
| 110 | - ->from(self::RELATION_TABLE) |
|
| 111 | - ->where($query->expr()->in('systemtagid', $query->createNamedParameter($tagIds, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 112 | - ->andWhere($query->expr()->eq('objecttype', $query->createNamedParameter($objectType))); |
|
| 113 | - |
|
| 114 | - if ($limit) { |
|
| 115 | - if (count($tagIds) !== 1) { |
|
| 116 | - throw new \InvalidArgumentException('Limit is only allowed with a single tag'); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - $query->setMaxResults($limit) |
|
| 120 | - ->orderBy('objectid', 'ASC'); |
|
| 121 | - |
|
| 122 | - if ($offset !== '') { |
|
| 123 | - $query->andWhere($query->expr()->gt('objectid', $query->createNamedParameter($offset))); |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $objectIds = []; |
|
| 128 | - |
|
| 129 | - $result = $query->execute(); |
|
| 130 | - while ($row = $result->fetch()) { |
|
| 131 | - $objectIds[] = $row['objectid']; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return $objectIds; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * {@inheritdoc} |
|
| 139 | - */ |
|
| 140 | - public function assignTags($objId, $objectType, $tagIds) { |
|
| 141 | - if (!is_array($tagIds)) { |
|
| 142 | - $tagIds = [$tagIds]; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - $this->assertTagsExist($tagIds); |
|
| 146 | - |
|
| 147 | - $query = $this->connection->getQueryBuilder(); |
|
| 148 | - $query->insert(self::RELATION_TABLE) |
|
| 149 | - ->values([ |
|
| 150 | - 'objectid' => $query->createNamedParameter($objId), |
|
| 151 | - 'objecttype' => $query->createNamedParameter($objectType), |
|
| 152 | - 'systemtagid' => $query->createParameter('tagid'), |
|
| 153 | - ]); |
|
| 154 | - |
|
| 155 | - foreach ($tagIds as $tagId) { |
|
| 156 | - try { |
|
| 157 | - $query->setParameter('tagid', $tagId); |
|
| 158 | - $query->execute(); |
|
| 159 | - } catch (UniqueConstraintViolationException $e) { |
|
| 160 | - // ignore existing relations |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - $this->dispatcher->dispatch(MapperEvent::EVENT_ASSIGN, new MapperEvent( |
|
| 165 | - MapperEvent::EVENT_ASSIGN, |
|
| 166 | - $objectType, |
|
| 167 | - $objId, |
|
| 168 | - $tagIds |
|
| 169 | - )); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * {@inheritdoc} |
|
| 174 | - */ |
|
| 175 | - public function unassignTags($objId, $objectType, $tagIds) { |
|
| 176 | - if (!is_array($tagIds)) { |
|
| 177 | - $tagIds = [$tagIds]; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - $this->assertTagsExist($tagIds); |
|
| 181 | - |
|
| 182 | - $query = $this->connection->getQueryBuilder(); |
|
| 183 | - $query->delete(self::RELATION_TABLE) |
|
| 184 | - ->where($query->expr()->eq('objectid', $query->createParameter('objectid'))) |
|
| 185 | - ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
| 186 | - ->andWhere($query->expr()->in('systemtagid', $query->createParameter('tagids'))) |
|
| 187 | - ->setParameter('objectid', $objId) |
|
| 188 | - ->setParameter('objecttype', $objectType) |
|
| 189 | - ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
| 190 | - ->execute(); |
|
| 191 | - |
|
| 192 | - $this->dispatcher->dispatch(MapperEvent::EVENT_UNASSIGN, new MapperEvent( |
|
| 193 | - MapperEvent::EVENT_UNASSIGN, |
|
| 194 | - $objectType, |
|
| 195 | - $objId, |
|
| 196 | - $tagIds |
|
| 197 | - )); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * {@inheritdoc} |
|
| 202 | - */ |
|
| 203 | - public function haveTag($objIds, $objectType, $tagId, $all = true) { |
|
| 204 | - $this->assertTagsExist([$tagId]); |
|
| 205 | - |
|
| 206 | - if (!is_array($objIds)) { |
|
| 207 | - $objIds = [$objIds]; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - $query = $this->connection->getQueryBuilder(); |
|
| 211 | - |
|
| 212 | - if (!$all) { |
|
| 213 | - // If we only need one entry, we make the query lighter, by not |
|
| 214 | - // counting the elements |
|
| 215 | - $query->select('*') |
|
| 216 | - ->setMaxResults(1); |
|
| 217 | - } else { |
|
| 218 | - $query->select($query->createFunction('COUNT(1)')); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - $query->from(self::RELATION_TABLE) |
|
| 222 | - ->where($query->expr()->in('objectid', $query->createParameter('objectids'))) |
|
| 223 | - ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
| 224 | - ->andWhere($query->expr()->eq('systemtagid', $query->createParameter('tagid'))) |
|
| 225 | - ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY) |
|
| 226 | - ->setParameter('tagid', $tagId) |
|
| 227 | - ->setParameter('objecttype', $objectType); |
|
| 228 | - |
|
| 229 | - $result = $query->execute(); |
|
| 230 | - $row = $result->fetch(\PDO::FETCH_NUM); |
|
| 231 | - $result->closeCursor(); |
|
| 232 | - |
|
| 233 | - if ($all) { |
|
| 234 | - return ((int)$row[0] === count($objIds)); |
|
| 235 | - } else { |
|
| 236 | - return (bool) $row; |
|
| 237 | - } |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Asserts that all the given tag ids exist. |
|
| 242 | - * |
|
| 243 | - * @param string[] $tagIds tag ids to check |
|
| 244 | - * |
|
| 245 | - * @throws \OCP\SystemTag\TagNotFoundException if at least one tag did not exist |
|
| 246 | - */ |
|
| 247 | - private function assertTagsExist($tagIds) { |
|
| 248 | - $tags = $this->tagManager->getTagsByIds($tagIds); |
|
| 249 | - if (count($tags) !== count($tagIds)) { |
|
| 250 | - // at least one tag missing, bail out |
|
| 251 | - $foundTagIds = array_map( |
|
| 252 | - function(ISystemTag $tag) { |
|
| 253 | - return $tag->getId(); |
|
| 254 | - }, |
|
| 255 | - $tags |
|
| 256 | - ); |
|
| 257 | - $missingTagIds = array_diff($tagIds, $foundTagIds); |
|
| 258 | - throw new TagNotFoundException( |
|
| 259 | - 'Tags not found', 0, null, $missingTagIds |
|
| 260 | - ); |
|
| 261 | - } |
|
| 262 | - } |
|
| 38 | + const RELATION_TABLE = 'systemtag_object_mapping'; |
|
| 39 | + |
|
| 40 | + /** @var ISystemTagManager */ |
|
| 41 | + protected $tagManager; |
|
| 42 | + |
|
| 43 | + /** @var IDBConnection */ |
|
| 44 | + protected $connection; |
|
| 45 | + |
|
| 46 | + /** @var EventDispatcherInterface */ |
|
| 47 | + protected $dispatcher; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Constructor. |
|
| 51 | + * |
|
| 52 | + * @param IDBConnection $connection database connection |
|
| 53 | + * @param ISystemTagManager $tagManager system tag manager |
|
| 54 | + * @param EventDispatcherInterface $dispatcher |
|
| 55 | + */ |
|
| 56 | + public function __construct(IDBConnection $connection, ISystemTagManager $tagManager, EventDispatcherInterface $dispatcher) { |
|
| 57 | + $this->connection = $connection; |
|
| 58 | + $this->tagManager = $tagManager; |
|
| 59 | + $this->dispatcher = $dispatcher; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * {@inheritdoc} |
|
| 64 | + */ |
|
| 65 | + public function getTagIdsForObjects($objIds, $objectType) { |
|
| 66 | + if (!is_array($objIds)) { |
|
| 67 | + $objIds = [$objIds]; |
|
| 68 | + } else if (empty($objIds)) { |
|
| 69 | + return []; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + $query = $this->connection->getQueryBuilder(); |
|
| 73 | + $query->select(['systemtagid', 'objectid']) |
|
| 74 | + ->from(self::RELATION_TABLE) |
|
| 75 | + ->where($query->expr()->in('objectid', $query->createParameter('objectids'))) |
|
| 76 | + ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
| 77 | + ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
| 78 | + ->setParameter('objecttype', $objectType) |
|
| 79 | + ->addOrderBy('objectid', 'ASC') |
|
| 80 | + ->addOrderBy('systemtagid', 'ASC'); |
|
| 81 | + |
|
| 82 | + $mapping = []; |
|
| 83 | + foreach ($objIds as $objId) { |
|
| 84 | + $mapping[$objId] = []; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + $result = $query->execute(); |
|
| 88 | + while ($row = $result->fetch()) { |
|
| 89 | + $objectId = $row['objectid']; |
|
| 90 | + $mapping[$objectId][] = $row['systemtagid']; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $result->closeCursor(); |
|
| 94 | + |
|
| 95 | + return $mapping; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * {@inheritdoc} |
|
| 100 | + */ |
|
| 101 | + public function getObjectIdsForTags($tagIds, $objectType, $limit = 0, $offset = '') { |
|
| 102 | + if (!is_array($tagIds)) { |
|
| 103 | + $tagIds = [$tagIds]; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $this->assertTagsExist($tagIds); |
|
| 107 | + |
|
| 108 | + $query = $this->connection->getQueryBuilder(); |
|
| 109 | + $query->selectDistinct('objectid') |
|
| 110 | + ->from(self::RELATION_TABLE) |
|
| 111 | + ->where($query->expr()->in('systemtagid', $query->createNamedParameter($tagIds, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 112 | + ->andWhere($query->expr()->eq('objecttype', $query->createNamedParameter($objectType))); |
|
| 113 | + |
|
| 114 | + if ($limit) { |
|
| 115 | + if (count($tagIds) !== 1) { |
|
| 116 | + throw new \InvalidArgumentException('Limit is only allowed with a single tag'); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + $query->setMaxResults($limit) |
|
| 120 | + ->orderBy('objectid', 'ASC'); |
|
| 121 | + |
|
| 122 | + if ($offset !== '') { |
|
| 123 | + $query->andWhere($query->expr()->gt('objectid', $query->createNamedParameter($offset))); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $objectIds = []; |
|
| 128 | + |
|
| 129 | + $result = $query->execute(); |
|
| 130 | + while ($row = $result->fetch()) { |
|
| 131 | + $objectIds[] = $row['objectid']; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return $objectIds; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * {@inheritdoc} |
|
| 139 | + */ |
|
| 140 | + public function assignTags($objId, $objectType, $tagIds) { |
|
| 141 | + if (!is_array($tagIds)) { |
|
| 142 | + $tagIds = [$tagIds]; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + $this->assertTagsExist($tagIds); |
|
| 146 | + |
|
| 147 | + $query = $this->connection->getQueryBuilder(); |
|
| 148 | + $query->insert(self::RELATION_TABLE) |
|
| 149 | + ->values([ |
|
| 150 | + 'objectid' => $query->createNamedParameter($objId), |
|
| 151 | + 'objecttype' => $query->createNamedParameter($objectType), |
|
| 152 | + 'systemtagid' => $query->createParameter('tagid'), |
|
| 153 | + ]); |
|
| 154 | + |
|
| 155 | + foreach ($tagIds as $tagId) { |
|
| 156 | + try { |
|
| 157 | + $query->setParameter('tagid', $tagId); |
|
| 158 | + $query->execute(); |
|
| 159 | + } catch (UniqueConstraintViolationException $e) { |
|
| 160 | + // ignore existing relations |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + $this->dispatcher->dispatch(MapperEvent::EVENT_ASSIGN, new MapperEvent( |
|
| 165 | + MapperEvent::EVENT_ASSIGN, |
|
| 166 | + $objectType, |
|
| 167 | + $objId, |
|
| 168 | + $tagIds |
|
| 169 | + )); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * {@inheritdoc} |
|
| 174 | + */ |
|
| 175 | + public function unassignTags($objId, $objectType, $tagIds) { |
|
| 176 | + if (!is_array($tagIds)) { |
|
| 177 | + $tagIds = [$tagIds]; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + $this->assertTagsExist($tagIds); |
|
| 181 | + |
|
| 182 | + $query = $this->connection->getQueryBuilder(); |
|
| 183 | + $query->delete(self::RELATION_TABLE) |
|
| 184 | + ->where($query->expr()->eq('objectid', $query->createParameter('objectid'))) |
|
| 185 | + ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
| 186 | + ->andWhere($query->expr()->in('systemtagid', $query->createParameter('tagids'))) |
|
| 187 | + ->setParameter('objectid', $objId) |
|
| 188 | + ->setParameter('objecttype', $objectType) |
|
| 189 | + ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
| 190 | + ->execute(); |
|
| 191 | + |
|
| 192 | + $this->dispatcher->dispatch(MapperEvent::EVENT_UNASSIGN, new MapperEvent( |
|
| 193 | + MapperEvent::EVENT_UNASSIGN, |
|
| 194 | + $objectType, |
|
| 195 | + $objId, |
|
| 196 | + $tagIds |
|
| 197 | + )); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * {@inheritdoc} |
|
| 202 | + */ |
|
| 203 | + public function haveTag($objIds, $objectType, $tagId, $all = true) { |
|
| 204 | + $this->assertTagsExist([$tagId]); |
|
| 205 | + |
|
| 206 | + if (!is_array($objIds)) { |
|
| 207 | + $objIds = [$objIds]; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + $query = $this->connection->getQueryBuilder(); |
|
| 211 | + |
|
| 212 | + if (!$all) { |
|
| 213 | + // If we only need one entry, we make the query lighter, by not |
|
| 214 | + // counting the elements |
|
| 215 | + $query->select('*') |
|
| 216 | + ->setMaxResults(1); |
|
| 217 | + } else { |
|
| 218 | + $query->select($query->createFunction('COUNT(1)')); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + $query->from(self::RELATION_TABLE) |
|
| 222 | + ->where($query->expr()->in('objectid', $query->createParameter('objectids'))) |
|
| 223 | + ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
| 224 | + ->andWhere($query->expr()->eq('systemtagid', $query->createParameter('tagid'))) |
|
| 225 | + ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY) |
|
| 226 | + ->setParameter('tagid', $tagId) |
|
| 227 | + ->setParameter('objecttype', $objectType); |
|
| 228 | + |
|
| 229 | + $result = $query->execute(); |
|
| 230 | + $row = $result->fetch(\PDO::FETCH_NUM); |
|
| 231 | + $result->closeCursor(); |
|
| 232 | + |
|
| 233 | + if ($all) { |
|
| 234 | + return ((int)$row[0] === count($objIds)); |
|
| 235 | + } else { |
|
| 236 | + return (bool) $row; |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Asserts that all the given tag ids exist. |
|
| 242 | + * |
|
| 243 | + * @param string[] $tagIds tag ids to check |
|
| 244 | + * |
|
| 245 | + * @throws \OCP\SystemTag\TagNotFoundException if at least one tag did not exist |
|
| 246 | + */ |
|
| 247 | + private function assertTagsExist($tagIds) { |
|
| 248 | + $tags = $this->tagManager->getTagsByIds($tagIds); |
|
| 249 | + if (count($tags) !== count($tagIds)) { |
|
| 250 | + // at least one tag missing, bail out |
|
| 251 | + $foundTagIds = array_map( |
|
| 252 | + function(ISystemTag $tag) { |
|
| 253 | + return $tag->getId(); |
|
| 254 | + }, |
|
| 255 | + $tags |
|
| 256 | + ); |
|
| 257 | + $missingTagIds = array_diff($tagIds, $foundTagIds); |
|
| 258 | + throw new TagNotFoundException( |
|
| 259 | + 'Tags not found', 0, null, $missingTagIds |
|
| 260 | + ); |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | 263 | } |
@@ -136,14 +136,14 @@ discard block |
||
| 136 | 136 | ->from(self::TAG_TABLE); |
| 137 | 137 | |
| 138 | 138 | if (!is_null($visibilityFilter)) { |
| 139 | - $query->andWhere($query->expr()->eq('visibility', $query->createNamedParameter((int)$visibilityFilter))); |
|
| 139 | + $query->andWhere($query->expr()->eq('visibility', $query->createNamedParameter((int) $visibilityFilter))); |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | if (!empty($nameSearchPattern)) { |
| 143 | 143 | $query->andWhere( |
| 144 | 144 | $query->expr()->like( |
| 145 | 145 | 'name', |
| 146 | - $query->createNamedParameter('%' . $this->connection->escapeLikeParameter($nameSearchPattern). '%') |
|
| 146 | + $query->createNamedParameter('%'.$this->connection->escapeLikeParameter($nameSearchPattern).'%') |
|
| 147 | 147 | ) |
| 148 | 148 | ); |
| 149 | 149 | } |
@@ -167,8 +167,8 @@ discard block |
||
| 167 | 167 | * {@inheritdoc} |
| 168 | 168 | */ |
| 169 | 169 | public function getTag($tagName, $userVisible, $userAssignable) { |
| 170 | - $userVisible = (int)$userVisible; |
|
| 171 | - $userAssignable = (int)$userAssignable; |
|
| 170 | + $userVisible = (int) $userVisible; |
|
| 171 | + $userAssignable = (int) $userAssignable; |
|
| 172 | 172 | |
| 173 | 173 | $result = $this->selectTagQuery |
| 174 | 174 | ->setParameter('name', $tagName) |
@@ -180,7 +180,7 @@ discard block |
||
| 180 | 180 | $result->closeCursor(); |
| 181 | 181 | if (!$row) { |
| 182 | 182 | throw new TagNotFoundException( |
| 183 | - 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') does not exist' |
|
| 183 | + 'Tag ("'.$tagName.'", '.$userVisible.', '.$userAssignable.') does not exist' |
|
| 184 | 184 | ); |
| 185 | 185 | } |
| 186 | 186 | |
@@ -191,8 +191,8 @@ discard block |
||
| 191 | 191 | * {@inheritdoc} |
| 192 | 192 | */ |
| 193 | 193 | public function createTag($tagName, $userVisible, $userAssignable) { |
| 194 | - $userVisible = (int)$userVisible; |
|
| 195 | - $userAssignable = (int)$userAssignable; |
|
| 194 | + $userVisible = (int) $userVisible; |
|
| 195 | + $userAssignable = (int) $userAssignable; |
|
| 196 | 196 | |
| 197 | 197 | $query = $this->connection->getQueryBuilder(); |
| 198 | 198 | $query->insert(self::TAG_TABLE) |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | $query->execute(); |
| 207 | 207 | } catch (UniqueConstraintViolationException $e) { |
| 208 | 208 | throw new TagAlreadyExistsException( |
| 209 | - 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists', |
|
| 209 | + 'Tag ("'.$tagName.'", '.$userVisible.', '.$userAssignable.') already exists', |
|
| 210 | 210 | 0, |
| 211 | 211 | $e |
| 212 | 212 | ); |
@@ -215,10 +215,10 @@ discard block |
||
| 215 | 215 | $tagId = $query->getLastInsertId(); |
| 216 | 216 | |
| 217 | 217 | $tag = new SystemTag( |
| 218 | - (int)$tagId, |
|
| 218 | + (int) $tagId, |
|
| 219 | 219 | $tagName, |
| 220 | - (bool)$userVisible, |
|
| 221 | - (bool)$userAssignable |
|
| 220 | + (bool) $userVisible, |
|
| 221 | + (bool) $userAssignable |
|
| 222 | 222 | ); |
| 223 | 223 | |
| 224 | 224 | $this->dispatcher->dispatch(ManagerEvent::EVENT_CREATE, new ManagerEvent( |
@@ -232,8 +232,8 @@ discard block |
||
| 232 | 232 | * {@inheritdoc} |
| 233 | 233 | */ |
| 234 | 234 | public function updateTag($tagId, $tagName, $userVisible, $userAssignable) { |
| 235 | - $userVisible = (int)$userVisible; |
|
| 236 | - $userAssignable = (int)$userAssignable; |
|
| 235 | + $userVisible = (int) $userVisible; |
|
| 236 | + $userAssignable = (int) $userAssignable; |
|
| 237 | 237 | |
| 238 | 238 | try { |
| 239 | 239 | $tags = $this->getTagsByIds($tagId); |
@@ -270,7 +270,7 @@ discard block |
||
| 270 | 270 | } |
| 271 | 271 | } catch (UniqueConstraintViolationException $e) { |
| 272 | 272 | throw new TagAlreadyExistsException( |
| 273 | - 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists', |
|
| 273 | + 'Tag ("'.$tagName.'", '.$userVisible.', '.$userAssignable.') already exists', |
|
| 274 | 274 | 0, |
| 275 | 275 | $e |
| 276 | 276 | ); |
@@ -377,7 +377,7 @@ discard block |
||
| 377 | 377 | } |
| 378 | 378 | |
| 379 | 379 | private function createSystemTagFromRow($row) { |
| 380 | - return new SystemTag((int)$row['id'], $row['name'], (bool)$row['visibility'], (bool)$row['editable']); |
|
| 380 | + return new SystemTag((int) $row['id'], $row['name'], (bool) $row['visibility'], (bool) $row['editable']); |
|
| 381 | 381 | } |
| 382 | 382 | |
| 383 | 383 | /** |
@@ -40,397 +40,397 @@ |
||
| 40 | 40 | */ |
| 41 | 41 | class SystemTagManager implements ISystemTagManager { |
| 42 | 42 | |
| 43 | - const TAG_TABLE = 'systemtag'; |
|
| 44 | - const TAG_GROUP_TABLE = 'systemtag_group'; |
|
| 45 | - |
|
| 46 | - /** @var IDBConnection */ |
|
| 47 | - protected $connection; |
|
| 48 | - |
|
| 49 | - /** @var EventDispatcherInterface */ |
|
| 50 | - protected $dispatcher; |
|
| 51 | - |
|
| 52 | - /** @var IGroupManager */ |
|
| 53 | - protected $groupManager; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Prepared query for selecting tags directly |
|
| 57 | - * |
|
| 58 | - * @var \OCP\DB\QueryBuilder\IQueryBuilder |
|
| 59 | - */ |
|
| 60 | - private $selectTagQuery; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Constructor. |
|
| 64 | - * |
|
| 65 | - * @param IDBConnection $connection database connection |
|
| 66 | - * @param EventDispatcherInterface $dispatcher |
|
| 67 | - */ |
|
| 68 | - public function __construct( |
|
| 69 | - IDBConnection $connection, |
|
| 70 | - IGroupManager $groupManager, |
|
| 71 | - EventDispatcherInterface $dispatcher |
|
| 72 | - ) { |
|
| 73 | - $this->connection = $connection; |
|
| 74 | - $this->groupManager = $groupManager; |
|
| 75 | - $this->dispatcher = $dispatcher; |
|
| 76 | - |
|
| 77 | - $query = $this->connection->getQueryBuilder(); |
|
| 78 | - $this->selectTagQuery = $query->select('*') |
|
| 79 | - ->from(self::TAG_TABLE) |
|
| 80 | - ->where($query->expr()->eq('name', $query->createParameter('name'))) |
|
| 81 | - ->andWhere($query->expr()->eq('visibility', $query->createParameter('visibility'))) |
|
| 82 | - ->andWhere($query->expr()->eq('editable', $query->createParameter('editable'))); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * {@inheritdoc} |
|
| 87 | - */ |
|
| 88 | - public function getTagsByIds($tagIds) { |
|
| 89 | - if (!is_array($tagIds)) { |
|
| 90 | - $tagIds = [$tagIds]; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $tags = []; |
|
| 94 | - |
|
| 95 | - // note: not all databases will fail if it's a string or starts with a number |
|
| 96 | - foreach ($tagIds as $tagId) { |
|
| 97 | - if (!is_numeric($tagId)) { |
|
| 98 | - throw new \InvalidArgumentException('Tag id must be integer'); |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - $query = $this->connection->getQueryBuilder(); |
|
| 103 | - $query->select('*') |
|
| 104 | - ->from(self::TAG_TABLE) |
|
| 105 | - ->where($query->expr()->in('id', $query->createParameter('tagids'))) |
|
| 106 | - ->addOrderBy('name', 'ASC') |
|
| 107 | - ->addOrderBy('visibility', 'ASC') |
|
| 108 | - ->addOrderBy('editable', 'ASC') |
|
| 109 | - ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY); |
|
| 110 | - |
|
| 111 | - $result = $query->execute(); |
|
| 112 | - while ($row = $result->fetch()) { |
|
| 113 | - $tags[$row['id']] = $this->createSystemTagFromRow($row); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $result->closeCursor(); |
|
| 117 | - |
|
| 118 | - if (count($tags) !== count($tagIds)) { |
|
| 119 | - throw new TagNotFoundException( |
|
| 120 | - 'Tag id(s) not found', 0, null, array_diff($tagIds, array_keys($tags)) |
|
| 121 | - ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return $tags; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * {@inheritdoc} |
|
| 129 | - */ |
|
| 130 | - public function getAllTags($visibilityFilter = null, $nameSearchPattern = null) { |
|
| 131 | - $tags = []; |
|
| 132 | - |
|
| 133 | - $query = $this->connection->getQueryBuilder(); |
|
| 134 | - $query->select('*') |
|
| 135 | - ->from(self::TAG_TABLE); |
|
| 136 | - |
|
| 137 | - if (!is_null($visibilityFilter)) { |
|
| 138 | - $query->andWhere($query->expr()->eq('visibility', $query->createNamedParameter((int)$visibilityFilter))); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - if (!empty($nameSearchPattern)) { |
|
| 142 | - $query->andWhere( |
|
| 143 | - $query->expr()->like( |
|
| 144 | - 'name', |
|
| 145 | - $query->createNamedParameter('%' . $this->connection->escapeLikeParameter($nameSearchPattern). '%') |
|
| 146 | - ) |
|
| 147 | - ); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $query |
|
| 151 | - ->addOrderBy('name', 'ASC') |
|
| 152 | - ->addOrderBy('visibility', 'ASC') |
|
| 153 | - ->addOrderBy('editable', 'ASC'); |
|
| 154 | - |
|
| 155 | - $result = $query->execute(); |
|
| 156 | - while ($row = $result->fetch()) { |
|
| 157 | - $tags[$row['id']] = $this->createSystemTagFromRow($row); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - $result->closeCursor(); |
|
| 161 | - |
|
| 162 | - return $tags; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * {@inheritdoc} |
|
| 167 | - */ |
|
| 168 | - public function getTag($tagName, $userVisible, $userAssignable) { |
|
| 169 | - $userVisible = (int)$userVisible; |
|
| 170 | - $userAssignable = (int)$userAssignable; |
|
| 171 | - |
|
| 172 | - $result = $this->selectTagQuery |
|
| 173 | - ->setParameter('name', $tagName) |
|
| 174 | - ->setParameter('visibility', $userVisible) |
|
| 175 | - ->setParameter('editable', $userAssignable) |
|
| 176 | - ->execute(); |
|
| 177 | - |
|
| 178 | - $row = $result->fetch(); |
|
| 179 | - $result->closeCursor(); |
|
| 180 | - if (!$row) { |
|
| 181 | - throw new TagNotFoundException( |
|
| 182 | - 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') does not exist' |
|
| 183 | - ); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - return $this->createSystemTagFromRow($row); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * {@inheritdoc} |
|
| 191 | - */ |
|
| 192 | - public function createTag($tagName, $userVisible, $userAssignable) { |
|
| 193 | - $userVisible = (int)$userVisible; |
|
| 194 | - $userAssignable = (int)$userAssignable; |
|
| 195 | - |
|
| 196 | - $query = $this->connection->getQueryBuilder(); |
|
| 197 | - $query->insert(self::TAG_TABLE) |
|
| 198 | - ->values([ |
|
| 199 | - 'name' => $query->createNamedParameter($tagName), |
|
| 200 | - 'visibility' => $query->createNamedParameter($userVisible), |
|
| 201 | - 'editable' => $query->createNamedParameter($userAssignable), |
|
| 202 | - ]); |
|
| 203 | - |
|
| 204 | - try { |
|
| 205 | - $query->execute(); |
|
| 206 | - } catch (UniqueConstraintViolationException $e) { |
|
| 207 | - throw new TagAlreadyExistsException( |
|
| 208 | - 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists', |
|
| 209 | - 0, |
|
| 210 | - $e |
|
| 211 | - ); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - $tagId = $query->getLastInsertId(); |
|
| 215 | - |
|
| 216 | - $tag = new SystemTag( |
|
| 217 | - (int)$tagId, |
|
| 218 | - $tagName, |
|
| 219 | - (bool)$userVisible, |
|
| 220 | - (bool)$userAssignable |
|
| 221 | - ); |
|
| 222 | - |
|
| 223 | - $this->dispatcher->dispatch(ManagerEvent::EVENT_CREATE, new ManagerEvent( |
|
| 224 | - ManagerEvent::EVENT_CREATE, $tag |
|
| 225 | - )); |
|
| 226 | - |
|
| 227 | - return $tag; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * {@inheritdoc} |
|
| 232 | - */ |
|
| 233 | - public function updateTag($tagId, $tagName, $userVisible, $userAssignable) { |
|
| 234 | - $userVisible = (int)$userVisible; |
|
| 235 | - $userAssignable = (int)$userAssignable; |
|
| 236 | - |
|
| 237 | - try { |
|
| 238 | - $tags = $this->getTagsByIds($tagId); |
|
| 239 | - } catch (TagNotFoundException $e) { |
|
| 240 | - throw new TagNotFoundException( |
|
| 241 | - 'Tag does not exist', 0, null, [$tagId] |
|
| 242 | - ); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - $beforeUpdate = array_shift($tags); |
|
| 246 | - $afterUpdate = new SystemTag( |
|
| 247 | - (int) $tagId, |
|
| 248 | - $tagName, |
|
| 249 | - (bool) $userVisible, |
|
| 250 | - (bool) $userAssignable |
|
| 251 | - ); |
|
| 252 | - |
|
| 253 | - $query = $this->connection->getQueryBuilder(); |
|
| 254 | - $query->update(self::TAG_TABLE) |
|
| 255 | - ->set('name', $query->createParameter('name')) |
|
| 256 | - ->set('visibility', $query->createParameter('visibility')) |
|
| 257 | - ->set('editable', $query->createParameter('editable')) |
|
| 258 | - ->where($query->expr()->eq('id', $query->createParameter('tagid'))) |
|
| 259 | - ->setParameter('name', $tagName) |
|
| 260 | - ->setParameter('visibility', $userVisible) |
|
| 261 | - ->setParameter('editable', $userAssignable) |
|
| 262 | - ->setParameter('tagid', $tagId); |
|
| 263 | - |
|
| 264 | - try { |
|
| 265 | - if ($query->execute() === 0) { |
|
| 266 | - throw new TagNotFoundException( |
|
| 267 | - 'Tag does not exist', 0, null, [$tagId] |
|
| 268 | - ); |
|
| 269 | - } |
|
| 270 | - } catch (UniqueConstraintViolationException $e) { |
|
| 271 | - throw new TagAlreadyExistsException( |
|
| 272 | - 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists', |
|
| 273 | - 0, |
|
| 274 | - $e |
|
| 275 | - ); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - $this->dispatcher->dispatch(ManagerEvent::EVENT_UPDATE, new ManagerEvent( |
|
| 279 | - ManagerEvent::EVENT_UPDATE, $afterUpdate, $beforeUpdate |
|
| 280 | - )); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * {@inheritdoc} |
|
| 285 | - */ |
|
| 286 | - public function deleteTags($tagIds) { |
|
| 287 | - if (!is_array($tagIds)) { |
|
| 288 | - $tagIds = [$tagIds]; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - $tagNotFoundException = null; |
|
| 292 | - $tags = []; |
|
| 293 | - try { |
|
| 294 | - $tags = $this->getTagsByIds($tagIds); |
|
| 295 | - } catch (TagNotFoundException $e) { |
|
| 296 | - $tagNotFoundException = $e; |
|
| 297 | - |
|
| 298 | - // Get existing tag objects for the hooks later |
|
| 299 | - $existingTags = array_diff($tagIds, $tagNotFoundException->getMissingTags()); |
|
| 300 | - if (!empty($existingTags)) { |
|
| 301 | - try { |
|
| 302 | - $tags = $this->getTagsByIds($existingTags); |
|
| 303 | - } catch (TagNotFoundException $e) { |
|
| 304 | - // Ignore further errors... |
|
| 305 | - } |
|
| 306 | - } |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - // delete relationships first |
|
| 310 | - $query = $this->connection->getQueryBuilder(); |
|
| 311 | - $query->delete(SystemTagObjectMapper::RELATION_TABLE) |
|
| 312 | - ->where($query->expr()->in('systemtagid', $query->createParameter('tagids'))) |
|
| 313 | - ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
| 314 | - ->execute(); |
|
| 315 | - |
|
| 316 | - $query = $this->connection->getQueryBuilder(); |
|
| 317 | - $query->delete(self::TAG_TABLE) |
|
| 318 | - ->where($query->expr()->in('id', $query->createParameter('tagids'))) |
|
| 319 | - ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
| 320 | - ->execute(); |
|
| 321 | - |
|
| 322 | - foreach ($tags as $tag) { |
|
| 323 | - $this->dispatcher->dispatch(ManagerEvent::EVENT_DELETE, new ManagerEvent( |
|
| 324 | - ManagerEvent::EVENT_DELETE, $tag |
|
| 325 | - )); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - if ($tagNotFoundException !== null) { |
|
| 329 | - throw new TagNotFoundException( |
|
| 330 | - 'Tag id(s) not found', 0, $tagNotFoundException, $tagNotFoundException->getMissingTags() |
|
| 331 | - ); |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * {@inheritdoc} |
|
| 337 | - */ |
|
| 338 | - public function canUserAssignTag(ISystemTag $tag, IUser $user) { |
|
| 339 | - // early check to avoid unneeded group lookups |
|
| 340 | - if ($tag->isUserAssignable() && $tag->isUserVisible()) { |
|
| 341 | - return true; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - if ($this->groupManager->isAdmin($user->getUID())) { |
|
| 345 | - return true; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - if (!$tag->isUserVisible()) { |
|
| 349 | - return false; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - $groupIds = $this->groupManager->getUserGroupIds($user); |
|
| 353 | - if (!empty($groupIds)) { |
|
| 354 | - $matchingGroups = array_intersect($groupIds, $this->getTagGroups($tag)); |
|
| 355 | - if (!empty($matchingGroups)) { |
|
| 356 | - return true; |
|
| 357 | - } |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - return false; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * {@inheritdoc} |
|
| 365 | - */ |
|
| 366 | - public function canUserSeeTag(ISystemTag $tag, IUser $user) { |
|
| 367 | - if ($tag->isUserVisible()) { |
|
| 368 | - return true; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - if ($this->groupManager->isAdmin($user->getUID())) { |
|
| 372 | - return true; |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - return false; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - private function createSystemTagFromRow($row) { |
|
| 379 | - return new SystemTag((int)$row['id'], $row['name'], (bool)$row['visibility'], (bool)$row['editable']); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * {@inheritdoc} |
|
| 384 | - */ |
|
| 385 | - public function setTagGroups(ISystemTag $tag, $groupIds) { |
|
| 386 | - // delete relationships first |
|
| 387 | - $this->connection->beginTransaction(); |
|
| 388 | - try { |
|
| 389 | - $query = $this->connection->getQueryBuilder(); |
|
| 390 | - $query->delete(self::TAG_GROUP_TABLE) |
|
| 391 | - ->where($query->expr()->eq('systemtagid', $query->createNamedParameter($tag->getId()))) |
|
| 392 | - ->execute(); |
|
| 393 | - |
|
| 394 | - // add each group id |
|
| 395 | - $query = $this->connection->getQueryBuilder(); |
|
| 396 | - $query->insert(self::TAG_GROUP_TABLE) |
|
| 397 | - ->values([ |
|
| 398 | - 'systemtagid' => $query->createNamedParameter($tag->getId()), |
|
| 399 | - 'gid' => $query->createParameter('gid'), |
|
| 400 | - ]); |
|
| 401 | - foreach ($groupIds as $groupId) { |
|
| 402 | - if ($groupId === '') { |
|
| 403 | - continue; |
|
| 404 | - } |
|
| 405 | - $query->setParameter('gid', $groupId); |
|
| 406 | - $query->execute(); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - $this->connection->commit(); |
|
| 410 | - } catch (\Exception $e) { |
|
| 411 | - $this->connection->rollBack(); |
|
| 412 | - throw $e; |
|
| 413 | - } |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * {@inheritdoc} |
|
| 418 | - */ |
|
| 419 | - public function getTagGroups(ISystemTag $tag) { |
|
| 420 | - $groupIds = []; |
|
| 421 | - $query = $this->connection->getQueryBuilder(); |
|
| 422 | - $query->select('gid') |
|
| 423 | - ->from(self::TAG_GROUP_TABLE) |
|
| 424 | - ->where($query->expr()->eq('systemtagid', $query->createNamedParameter($tag->getId()))) |
|
| 425 | - ->orderBy('gid'); |
|
| 426 | - |
|
| 427 | - $result = $query->execute(); |
|
| 428 | - while ($row = $result->fetch()) { |
|
| 429 | - $groupIds[] = $row['gid']; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - $result->closeCursor(); |
|
| 433 | - |
|
| 434 | - return $groupIds; |
|
| 435 | - } |
|
| 43 | + const TAG_TABLE = 'systemtag'; |
|
| 44 | + const TAG_GROUP_TABLE = 'systemtag_group'; |
|
| 45 | + |
|
| 46 | + /** @var IDBConnection */ |
|
| 47 | + protected $connection; |
|
| 48 | + |
|
| 49 | + /** @var EventDispatcherInterface */ |
|
| 50 | + protected $dispatcher; |
|
| 51 | + |
|
| 52 | + /** @var IGroupManager */ |
|
| 53 | + protected $groupManager; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Prepared query for selecting tags directly |
|
| 57 | + * |
|
| 58 | + * @var \OCP\DB\QueryBuilder\IQueryBuilder |
|
| 59 | + */ |
|
| 60 | + private $selectTagQuery; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Constructor. |
|
| 64 | + * |
|
| 65 | + * @param IDBConnection $connection database connection |
|
| 66 | + * @param EventDispatcherInterface $dispatcher |
|
| 67 | + */ |
|
| 68 | + public function __construct( |
|
| 69 | + IDBConnection $connection, |
|
| 70 | + IGroupManager $groupManager, |
|
| 71 | + EventDispatcherInterface $dispatcher |
|
| 72 | + ) { |
|
| 73 | + $this->connection = $connection; |
|
| 74 | + $this->groupManager = $groupManager; |
|
| 75 | + $this->dispatcher = $dispatcher; |
|
| 76 | + |
|
| 77 | + $query = $this->connection->getQueryBuilder(); |
|
| 78 | + $this->selectTagQuery = $query->select('*') |
|
| 79 | + ->from(self::TAG_TABLE) |
|
| 80 | + ->where($query->expr()->eq('name', $query->createParameter('name'))) |
|
| 81 | + ->andWhere($query->expr()->eq('visibility', $query->createParameter('visibility'))) |
|
| 82 | + ->andWhere($query->expr()->eq('editable', $query->createParameter('editable'))); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * {@inheritdoc} |
|
| 87 | + */ |
|
| 88 | + public function getTagsByIds($tagIds) { |
|
| 89 | + if (!is_array($tagIds)) { |
|
| 90 | + $tagIds = [$tagIds]; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $tags = []; |
|
| 94 | + |
|
| 95 | + // note: not all databases will fail if it's a string or starts with a number |
|
| 96 | + foreach ($tagIds as $tagId) { |
|
| 97 | + if (!is_numeric($tagId)) { |
|
| 98 | + throw new \InvalidArgumentException('Tag id must be integer'); |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + $query = $this->connection->getQueryBuilder(); |
|
| 103 | + $query->select('*') |
|
| 104 | + ->from(self::TAG_TABLE) |
|
| 105 | + ->where($query->expr()->in('id', $query->createParameter('tagids'))) |
|
| 106 | + ->addOrderBy('name', 'ASC') |
|
| 107 | + ->addOrderBy('visibility', 'ASC') |
|
| 108 | + ->addOrderBy('editable', 'ASC') |
|
| 109 | + ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY); |
|
| 110 | + |
|
| 111 | + $result = $query->execute(); |
|
| 112 | + while ($row = $result->fetch()) { |
|
| 113 | + $tags[$row['id']] = $this->createSystemTagFromRow($row); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $result->closeCursor(); |
|
| 117 | + |
|
| 118 | + if (count($tags) !== count($tagIds)) { |
|
| 119 | + throw new TagNotFoundException( |
|
| 120 | + 'Tag id(s) not found', 0, null, array_diff($tagIds, array_keys($tags)) |
|
| 121 | + ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return $tags; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * {@inheritdoc} |
|
| 129 | + */ |
|
| 130 | + public function getAllTags($visibilityFilter = null, $nameSearchPattern = null) { |
|
| 131 | + $tags = []; |
|
| 132 | + |
|
| 133 | + $query = $this->connection->getQueryBuilder(); |
|
| 134 | + $query->select('*') |
|
| 135 | + ->from(self::TAG_TABLE); |
|
| 136 | + |
|
| 137 | + if (!is_null($visibilityFilter)) { |
|
| 138 | + $query->andWhere($query->expr()->eq('visibility', $query->createNamedParameter((int)$visibilityFilter))); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + if (!empty($nameSearchPattern)) { |
|
| 142 | + $query->andWhere( |
|
| 143 | + $query->expr()->like( |
|
| 144 | + 'name', |
|
| 145 | + $query->createNamedParameter('%' . $this->connection->escapeLikeParameter($nameSearchPattern). '%') |
|
| 146 | + ) |
|
| 147 | + ); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $query |
|
| 151 | + ->addOrderBy('name', 'ASC') |
|
| 152 | + ->addOrderBy('visibility', 'ASC') |
|
| 153 | + ->addOrderBy('editable', 'ASC'); |
|
| 154 | + |
|
| 155 | + $result = $query->execute(); |
|
| 156 | + while ($row = $result->fetch()) { |
|
| 157 | + $tags[$row['id']] = $this->createSystemTagFromRow($row); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + $result->closeCursor(); |
|
| 161 | + |
|
| 162 | + return $tags; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * {@inheritdoc} |
|
| 167 | + */ |
|
| 168 | + public function getTag($tagName, $userVisible, $userAssignable) { |
|
| 169 | + $userVisible = (int)$userVisible; |
|
| 170 | + $userAssignable = (int)$userAssignable; |
|
| 171 | + |
|
| 172 | + $result = $this->selectTagQuery |
|
| 173 | + ->setParameter('name', $tagName) |
|
| 174 | + ->setParameter('visibility', $userVisible) |
|
| 175 | + ->setParameter('editable', $userAssignable) |
|
| 176 | + ->execute(); |
|
| 177 | + |
|
| 178 | + $row = $result->fetch(); |
|
| 179 | + $result->closeCursor(); |
|
| 180 | + if (!$row) { |
|
| 181 | + throw new TagNotFoundException( |
|
| 182 | + 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') does not exist' |
|
| 183 | + ); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + return $this->createSystemTagFromRow($row); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * {@inheritdoc} |
|
| 191 | + */ |
|
| 192 | + public function createTag($tagName, $userVisible, $userAssignable) { |
|
| 193 | + $userVisible = (int)$userVisible; |
|
| 194 | + $userAssignable = (int)$userAssignable; |
|
| 195 | + |
|
| 196 | + $query = $this->connection->getQueryBuilder(); |
|
| 197 | + $query->insert(self::TAG_TABLE) |
|
| 198 | + ->values([ |
|
| 199 | + 'name' => $query->createNamedParameter($tagName), |
|
| 200 | + 'visibility' => $query->createNamedParameter($userVisible), |
|
| 201 | + 'editable' => $query->createNamedParameter($userAssignable), |
|
| 202 | + ]); |
|
| 203 | + |
|
| 204 | + try { |
|
| 205 | + $query->execute(); |
|
| 206 | + } catch (UniqueConstraintViolationException $e) { |
|
| 207 | + throw new TagAlreadyExistsException( |
|
| 208 | + 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists', |
|
| 209 | + 0, |
|
| 210 | + $e |
|
| 211 | + ); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + $tagId = $query->getLastInsertId(); |
|
| 215 | + |
|
| 216 | + $tag = new SystemTag( |
|
| 217 | + (int)$tagId, |
|
| 218 | + $tagName, |
|
| 219 | + (bool)$userVisible, |
|
| 220 | + (bool)$userAssignable |
|
| 221 | + ); |
|
| 222 | + |
|
| 223 | + $this->dispatcher->dispatch(ManagerEvent::EVENT_CREATE, new ManagerEvent( |
|
| 224 | + ManagerEvent::EVENT_CREATE, $tag |
|
| 225 | + )); |
|
| 226 | + |
|
| 227 | + return $tag; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * {@inheritdoc} |
|
| 232 | + */ |
|
| 233 | + public function updateTag($tagId, $tagName, $userVisible, $userAssignable) { |
|
| 234 | + $userVisible = (int)$userVisible; |
|
| 235 | + $userAssignable = (int)$userAssignable; |
|
| 236 | + |
|
| 237 | + try { |
|
| 238 | + $tags = $this->getTagsByIds($tagId); |
|
| 239 | + } catch (TagNotFoundException $e) { |
|
| 240 | + throw new TagNotFoundException( |
|
| 241 | + 'Tag does not exist', 0, null, [$tagId] |
|
| 242 | + ); |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + $beforeUpdate = array_shift($tags); |
|
| 246 | + $afterUpdate = new SystemTag( |
|
| 247 | + (int) $tagId, |
|
| 248 | + $tagName, |
|
| 249 | + (bool) $userVisible, |
|
| 250 | + (bool) $userAssignable |
|
| 251 | + ); |
|
| 252 | + |
|
| 253 | + $query = $this->connection->getQueryBuilder(); |
|
| 254 | + $query->update(self::TAG_TABLE) |
|
| 255 | + ->set('name', $query->createParameter('name')) |
|
| 256 | + ->set('visibility', $query->createParameter('visibility')) |
|
| 257 | + ->set('editable', $query->createParameter('editable')) |
|
| 258 | + ->where($query->expr()->eq('id', $query->createParameter('tagid'))) |
|
| 259 | + ->setParameter('name', $tagName) |
|
| 260 | + ->setParameter('visibility', $userVisible) |
|
| 261 | + ->setParameter('editable', $userAssignable) |
|
| 262 | + ->setParameter('tagid', $tagId); |
|
| 263 | + |
|
| 264 | + try { |
|
| 265 | + if ($query->execute() === 0) { |
|
| 266 | + throw new TagNotFoundException( |
|
| 267 | + 'Tag does not exist', 0, null, [$tagId] |
|
| 268 | + ); |
|
| 269 | + } |
|
| 270 | + } catch (UniqueConstraintViolationException $e) { |
|
| 271 | + throw new TagAlreadyExistsException( |
|
| 272 | + 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists', |
|
| 273 | + 0, |
|
| 274 | + $e |
|
| 275 | + ); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + $this->dispatcher->dispatch(ManagerEvent::EVENT_UPDATE, new ManagerEvent( |
|
| 279 | + ManagerEvent::EVENT_UPDATE, $afterUpdate, $beforeUpdate |
|
| 280 | + )); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * {@inheritdoc} |
|
| 285 | + */ |
|
| 286 | + public function deleteTags($tagIds) { |
|
| 287 | + if (!is_array($tagIds)) { |
|
| 288 | + $tagIds = [$tagIds]; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + $tagNotFoundException = null; |
|
| 292 | + $tags = []; |
|
| 293 | + try { |
|
| 294 | + $tags = $this->getTagsByIds($tagIds); |
|
| 295 | + } catch (TagNotFoundException $e) { |
|
| 296 | + $tagNotFoundException = $e; |
|
| 297 | + |
|
| 298 | + // Get existing tag objects for the hooks later |
|
| 299 | + $existingTags = array_diff($tagIds, $tagNotFoundException->getMissingTags()); |
|
| 300 | + if (!empty($existingTags)) { |
|
| 301 | + try { |
|
| 302 | + $tags = $this->getTagsByIds($existingTags); |
|
| 303 | + } catch (TagNotFoundException $e) { |
|
| 304 | + // Ignore further errors... |
|
| 305 | + } |
|
| 306 | + } |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + // delete relationships first |
|
| 310 | + $query = $this->connection->getQueryBuilder(); |
|
| 311 | + $query->delete(SystemTagObjectMapper::RELATION_TABLE) |
|
| 312 | + ->where($query->expr()->in('systemtagid', $query->createParameter('tagids'))) |
|
| 313 | + ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
| 314 | + ->execute(); |
|
| 315 | + |
|
| 316 | + $query = $this->connection->getQueryBuilder(); |
|
| 317 | + $query->delete(self::TAG_TABLE) |
|
| 318 | + ->where($query->expr()->in('id', $query->createParameter('tagids'))) |
|
| 319 | + ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
| 320 | + ->execute(); |
|
| 321 | + |
|
| 322 | + foreach ($tags as $tag) { |
|
| 323 | + $this->dispatcher->dispatch(ManagerEvent::EVENT_DELETE, new ManagerEvent( |
|
| 324 | + ManagerEvent::EVENT_DELETE, $tag |
|
| 325 | + )); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + if ($tagNotFoundException !== null) { |
|
| 329 | + throw new TagNotFoundException( |
|
| 330 | + 'Tag id(s) not found', 0, $tagNotFoundException, $tagNotFoundException->getMissingTags() |
|
| 331 | + ); |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * {@inheritdoc} |
|
| 337 | + */ |
|
| 338 | + public function canUserAssignTag(ISystemTag $tag, IUser $user) { |
|
| 339 | + // early check to avoid unneeded group lookups |
|
| 340 | + if ($tag->isUserAssignable() && $tag->isUserVisible()) { |
|
| 341 | + return true; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + if ($this->groupManager->isAdmin($user->getUID())) { |
|
| 345 | + return true; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + if (!$tag->isUserVisible()) { |
|
| 349 | + return false; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + $groupIds = $this->groupManager->getUserGroupIds($user); |
|
| 353 | + if (!empty($groupIds)) { |
|
| 354 | + $matchingGroups = array_intersect($groupIds, $this->getTagGroups($tag)); |
|
| 355 | + if (!empty($matchingGroups)) { |
|
| 356 | + return true; |
|
| 357 | + } |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + return false; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * {@inheritdoc} |
|
| 365 | + */ |
|
| 366 | + public function canUserSeeTag(ISystemTag $tag, IUser $user) { |
|
| 367 | + if ($tag->isUserVisible()) { |
|
| 368 | + return true; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + if ($this->groupManager->isAdmin($user->getUID())) { |
|
| 372 | + return true; |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + return false; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + private function createSystemTagFromRow($row) { |
|
| 379 | + return new SystemTag((int)$row['id'], $row['name'], (bool)$row['visibility'], (bool)$row['editable']); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * {@inheritdoc} |
|
| 384 | + */ |
|
| 385 | + public function setTagGroups(ISystemTag $tag, $groupIds) { |
|
| 386 | + // delete relationships first |
|
| 387 | + $this->connection->beginTransaction(); |
|
| 388 | + try { |
|
| 389 | + $query = $this->connection->getQueryBuilder(); |
|
| 390 | + $query->delete(self::TAG_GROUP_TABLE) |
|
| 391 | + ->where($query->expr()->eq('systemtagid', $query->createNamedParameter($tag->getId()))) |
|
| 392 | + ->execute(); |
|
| 393 | + |
|
| 394 | + // add each group id |
|
| 395 | + $query = $this->connection->getQueryBuilder(); |
|
| 396 | + $query->insert(self::TAG_GROUP_TABLE) |
|
| 397 | + ->values([ |
|
| 398 | + 'systemtagid' => $query->createNamedParameter($tag->getId()), |
|
| 399 | + 'gid' => $query->createParameter('gid'), |
|
| 400 | + ]); |
|
| 401 | + foreach ($groupIds as $groupId) { |
|
| 402 | + if ($groupId === '') { |
|
| 403 | + continue; |
|
| 404 | + } |
|
| 405 | + $query->setParameter('gid', $groupId); |
|
| 406 | + $query->execute(); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + $this->connection->commit(); |
|
| 410 | + } catch (\Exception $e) { |
|
| 411 | + $this->connection->rollBack(); |
|
| 412 | + throw $e; |
|
| 413 | + } |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * {@inheritdoc} |
|
| 418 | + */ |
|
| 419 | + public function getTagGroups(ISystemTag $tag) { |
|
| 420 | + $groupIds = []; |
|
| 421 | + $query = $this->connection->getQueryBuilder(); |
|
| 422 | + $query->select('gid') |
|
| 423 | + ->from(self::TAG_GROUP_TABLE) |
|
| 424 | + ->where($query->expr()->eq('systemtagid', $query->createNamedParameter($tag->getId()))) |
|
| 425 | + ->orderBy('gid'); |
|
| 426 | + |
|
| 427 | + $result = $query->execute(); |
|
| 428 | + while ($row = $result->fetch()) { |
|
| 429 | + $groupIds[] = $row['gid']; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + $result->closeCursor(); |
|
| 433 | + |
|
| 434 | + return $groupIds; |
|
| 435 | + } |
|
| 436 | 436 | } |
@@ -222,8 +222,8 @@ |
||
| 222 | 222 | public function count($search = '') { |
| 223 | 223 | $users = false; |
| 224 | 224 | foreach ($this->backends as $backend) { |
| 225 | - if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
|
| 226 | - if($users === false) { |
|
| 225 | + if ($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
|
| 226 | + if ($users === false) { |
|
| 227 | 227 | //we could directly add to a bool variable, but this would |
| 228 | 228 | //be ugly |
| 229 | 229 | $users = 0; |
@@ -32,272 +32,272 @@ |
||
| 32 | 32 | use OCP\IUser; |
| 33 | 33 | |
| 34 | 34 | class Group implements IGroup { |
| 35 | - /** @var null|string */ |
|
| 36 | - protected $displayName; |
|
| 35 | + /** @var null|string */ |
|
| 36 | + protected $displayName; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @var string $id |
|
| 40 | - */ |
|
| 41 | - private $gid; |
|
| 38 | + /** |
|
| 39 | + * @var string $id |
|
| 40 | + */ |
|
| 41 | + private $gid; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @var \OC\User\User[] $users |
|
| 45 | - */ |
|
| 46 | - private $users = array(); |
|
| 43 | + /** |
|
| 44 | + * @var \OC\User\User[] $users |
|
| 45 | + */ |
|
| 46 | + private $users = array(); |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @var bool $usersLoaded |
|
| 50 | - */ |
|
| 51 | - private $usersLoaded; |
|
| 48 | + /** |
|
| 49 | + * @var bool $usersLoaded |
|
| 50 | + */ |
|
| 51 | + private $usersLoaded; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @var \OC\Group\Backend[]|\OC\Group\Database[] $backend |
|
| 55 | - */ |
|
| 56 | - private $backends; |
|
| 53 | + /** |
|
| 54 | + * @var \OC\Group\Backend[]|\OC\Group\Database[] $backend |
|
| 55 | + */ |
|
| 56 | + private $backends; |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * @var \OC\Hooks\PublicEmitter $emitter |
|
| 60 | - */ |
|
| 61 | - private $emitter; |
|
| 58 | + /** |
|
| 59 | + * @var \OC\Hooks\PublicEmitter $emitter |
|
| 60 | + */ |
|
| 61 | + private $emitter; |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * @var \OC\User\Manager $userManager |
|
| 65 | - */ |
|
| 66 | - private $userManager; |
|
| 63 | + /** |
|
| 64 | + * @var \OC\User\Manager $userManager |
|
| 65 | + */ |
|
| 66 | + private $userManager; |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * @param string $gid |
|
| 70 | - * @param \OC\Group\Backend[] $backends |
|
| 71 | - * @param \OC\User\Manager $userManager |
|
| 72 | - * @param \OC\Hooks\PublicEmitter $emitter |
|
| 73 | - * @param string $displayName |
|
| 74 | - */ |
|
| 75 | - public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) { |
|
| 76 | - $this->gid = $gid; |
|
| 77 | - $this->backends = $backends; |
|
| 78 | - $this->userManager = $userManager; |
|
| 79 | - $this->emitter = $emitter; |
|
| 80 | - $this->displayName = $displayName; |
|
| 81 | - } |
|
| 68 | + /** |
|
| 69 | + * @param string $gid |
|
| 70 | + * @param \OC\Group\Backend[] $backends |
|
| 71 | + * @param \OC\User\Manager $userManager |
|
| 72 | + * @param \OC\Hooks\PublicEmitter $emitter |
|
| 73 | + * @param string $displayName |
|
| 74 | + */ |
|
| 75 | + public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) { |
|
| 76 | + $this->gid = $gid; |
|
| 77 | + $this->backends = $backends; |
|
| 78 | + $this->userManager = $userManager; |
|
| 79 | + $this->emitter = $emitter; |
|
| 80 | + $this->displayName = $displayName; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - public function getGID() { |
|
| 84 | - return $this->gid; |
|
| 85 | - } |
|
| 83 | + public function getGID() { |
|
| 84 | + return $this->gid; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - public function getDisplayName() { |
|
| 88 | - if (is_null($this->displayName)) { |
|
| 89 | - return $this->gid; |
|
| 90 | - } |
|
| 91 | - return $this->displayName; |
|
| 92 | - } |
|
| 87 | + public function getDisplayName() { |
|
| 88 | + if (is_null($this->displayName)) { |
|
| 89 | + return $this->gid; |
|
| 90 | + } |
|
| 91 | + return $this->displayName; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * get all users in the group |
|
| 96 | - * |
|
| 97 | - * @return \OC\User\User[] |
|
| 98 | - */ |
|
| 99 | - public function getUsers() { |
|
| 100 | - if ($this->usersLoaded) { |
|
| 101 | - return $this->users; |
|
| 102 | - } |
|
| 94 | + /** |
|
| 95 | + * get all users in the group |
|
| 96 | + * |
|
| 97 | + * @return \OC\User\User[] |
|
| 98 | + */ |
|
| 99 | + public function getUsers() { |
|
| 100 | + if ($this->usersLoaded) { |
|
| 101 | + return $this->users; |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - $userIds = array(); |
|
| 105 | - foreach ($this->backends as $backend) { |
|
| 106 | - $diff = array_diff( |
|
| 107 | - $backend->usersInGroup($this->gid), |
|
| 108 | - $userIds |
|
| 109 | - ); |
|
| 110 | - if ($diff) { |
|
| 111 | - $userIds = array_merge($userIds, $diff); |
|
| 112 | - } |
|
| 113 | - } |
|
| 104 | + $userIds = array(); |
|
| 105 | + foreach ($this->backends as $backend) { |
|
| 106 | + $diff = array_diff( |
|
| 107 | + $backend->usersInGroup($this->gid), |
|
| 108 | + $userIds |
|
| 109 | + ); |
|
| 110 | + if ($diff) { |
|
| 111 | + $userIds = array_merge($userIds, $diff); |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - $this->users = $this->getVerifiedUsers($userIds); |
|
| 116 | - $this->usersLoaded = true; |
|
| 117 | - return $this->users; |
|
| 118 | - } |
|
| 115 | + $this->users = $this->getVerifiedUsers($userIds); |
|
| 116 | + $this->usersLoaded = true; |
|
| 117 | + return $this->users; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * check if a user is in the group |
|
| 122 | - * |
|
| 123 | - * @param IUser $user |
|
| 124 | - * @return bool |
|
| 125 | - */ |
|
| 126 | - public function inGroup(IUser $user) { |
|
| 127 | - if (isset($this->users[$user->getUID()])) { |
|
| 128 | - return true; |
|
| 129 | - } |
|
| 130 | - foreach ($this->backends as $backend) { |
|
| 131 | - if ($backend->inGroup($user->getUID(), $this->gid)) { |
|
| 132 | - $this->users[$user->getUID()] = $user; |
|
| 133 | - return true; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - return false; |
|
| 137 | - } |
|
| 120 | + /** |
|
| 121 | + * check if a user is in the group |
|
| 122 | + * |
|
| 123 | + * @param IUser $user |
|
| 124 | + * @return bool |
|
| 125 | + */ |
|
| 126 | + public function inGroup(IUser $user) { |
|
| 127 | + if (isset($this->users[$user->getUID()])) { |
|
| 128 | + return true; |
|
| 129 | + } |
|
| 130 | + foreach ($this->backends as $backend) { |
|
| 131 | + if ($backend->inGroup($user->getUID(), $this->gid)) { |
|
| 132 | + $this->users[$user->getUID()] = $user; |
|
| 133 | + return true; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + return false; |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * add a user to the group |
|
| 141 | - * |
|
| 142 | - * @param IUser $user |
|
| 143 | - */ |
|
| 144 | - public function addUser(IUser $user) { |
|
| 145 | - if ($this->inGroup($user)) { |
|
| 146 | - return; |
|
| 147 | - } |
|
| 139 | + /** |
|
| 140 | + * add a user to the group |
|
| 141 | + * |
|
| 142 | + * @param IUser $user |
|
| 143 | + */ |
|
| 144 | + public function addUser(IUser $user) { |
|
| 145 | + if ($this->inGroup($user)) { |
|
| 146 | + return; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - if ($this->emitter) { |
|
| 150 | - $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); |
|
| 151 | - } |
|
| 152 | - foreach ($this->backends as $backend) { |
|
| 153 | - if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) { |
|
| 154 | - $backend->addToGroup($user->getUID(), $this->gid); |
|
| 155 | - if ($this->users) { |
|
| 156 | - $this->users[$user->getUID()] = $user; |
|
| 157 | - } |
|
| 158 | - if ($this->emitter) { |
|
| 159 | - $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); |
|
| 160 | - } |
|
| 161 | - return; |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - } |
|
| 149 | + if ($this->emitter) { |
|
| 150 | + $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); |
|
| 151 | + } |
|
| 152 | + foreach ($this->backends as $backend) { |
|
| 153 | + if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) { |
|
| 154 | + $backend->addToGroup($user->getUID(), $this->gid); |
|
| 155 | + if ($this->users) { |
|
| 156 | + $this->users[$user->getUID()] = $user; |
|
| 157 | + } |
|
| 158 | + if ($this->emitter) { |
|
| 159 | + $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); |
|
| 160 | + } |
|
| 161 | + return; |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - /** |
|
| 167 | - * remove a user from the group |
|
| 168 | - * |
|
| 169 | - * @param \OC\User\User $user |
|
| 170 | - */ |
|
| 171 | - public function removeUser($user) { |
|
| 172 | - $result = false; |
|
| 173 | - if ($this->emitter) { |
|
| 174 | - $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); |
|
| 175 | - } |
|
| 176 | - foreach ($this->backends as $backend) { |
|
| 177 | - if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) { |
|
| 178 | - $backend->removeFromGroup($user->getUID(), $this->gid); |
|
| 179 | - $result = true; |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - if ($result) { |
|
| 183 | - if ($this->emitter) { |
|
| 184 | - $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user)); |
|
| 185 | - } |
|
| 186 | - if ($this->users) { |
|
| 187 | - foreach ($this->users as $index => $groupUser) { |
|
| 188 | - if ($groupUser->getUID() === $user->getUID()) { |
|
| 189 | - unset($this->users[$index]); |
|
| 190 | - return; |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - } |
|
| 166 | + /** |
|
| 167 | + * remove a user from the group |
|
| 168 | + * |
|
| 169 | + * @param \OC\User\User $user |
|
| 170 | + */ |
|
| 171 | + public function removeUser($user) { |
|
| 172 | + $result = false; |
|
| 173 | + if ($this->emitter) { |
|
| 174 | + $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); |
|
| 175 | + } |
|
| 176 | + foreach ($this->backends as $backend) { |
|
| 177 | + if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) { |
|
| 178 | + $backend->removeFromGroup($user->getUID(), $this->gid); |
|
| 179 | + $result = true; |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + if ($result) { |
|
| 183 | + if ($this->emitter) { |
|
| 184 | + $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user)); |
|
| 185 | + } |
|
| 186 | + if ($this->users) { |
|
| 187 | + foreach ($this->users as $index => $groupUser) { |
|
| 188 | + if ($groupUser->getUID() === $user->getUID()) { |
|
| 189 | + unset($this->users[$index]); |
|
| 190 | + return; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - /** |
|
| 198 | - * search for users in the group by userid |
|
| 199 | - * |
|
| 200 | - * @param string $search |
|
| 201 | - * @param int $limit |
|
| 202 | - * @param int $offset |
|
| 203 | - * @return \OC\User\User[] |
|
| 204 | - */ |
|
| 205 | - public function searchUsers($search, $limit = null, $offset = null) { |
|
| 206 | - $users = array(); |
|
| 207 | - foreach ($this->backends as $backend) { |
|
| 208 | - $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
| 209 | - $users += $this->getVerifiedUsers($userIds); |
|
| 210 | - if (!is_null($limit) and $limit <= 0) { |
|
| 211 | - return array_values($users); |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - return array_values($users); |
|
| 215 | - } |
|
| 197 | + /** |
|
| 198 | + * search for users in the group by userid |
|
| 199 | + * |
|
| 200 | + * @param string $search |
|
| 201 | + * @param int $limit |
|
| 202 | + * @param int $offset |
|
| 203 | + * @return \OC\User\User[] |
|
| 204 | + */ |
|
| 205 | + public function searchUsers($search, $limit = null, $offset = null) { |
|
| 206 | + $users = array(); |
|
| 207 | + foreach ($this->backends as $backend) { |
|
| 208 | + $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
| 209 | + $users += $this->getVerifiedUsers($userIds); |
|
| 210 | + if (!is_null($limit) and $limit <= 0) { |
|
| 211 | + return array_values($users); |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + return array_values($users); |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | - /** |
|
| 218 | - * returns the number of users matching the search string |
|
| 219 | - * |
|
| 220 | - * @param string $search |
|
| 221 | - * @return int|bool |
|
| 222 | - */ |
|
| 223 | - public function count($search = '') { |
|
| 224 | - $users = false; |
|
| 225 | - foreach ($this->backends as $backend) { |
|
| 226 | - if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
|
| 227 | - if($users === false) { |
|
| 228 | - //we could directly add to a bool variable, but this would |
|
| 229 | - //be ugly |
|
| 230 | - $users = 0; |
|
| 231 | - } |
|
| 232 | - $users += $backend->countUsersInGroup($this->gid, $search); |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - return $users; |
|
| 236 | - } |
|
| 217 | + /** |
|
| 218 | + * returns the number of users matching the search string |
|
| 219 | + * |
|
| 220 | + * @param string $search |
|
| 221 | + * @return int|bool |
|
| 222 | + */ |
|
| 223 | + public function count($search = '') { |
|
| 224 | + $users = false; |
|
| 225 | + foreach ($this->backends as $backend) { |
|
| 226 | + if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
|
| 227 | + if($users === false) { |
|
| 228 | + //we could directly add to a bool variable, but this would |
|
| 229 | + //be ugly |
|
| 230 | + $users = 0; |
|
| 231 | + } |
|
| 232 | + $users += $backend->countUsersInGroup($this->gid, $search); |
|
| 233 | + } |
|
| 234 | + } |
|
| 235 | + return $users; |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - /** |
|
| 239 | - * search for users in the group by displayname |
|
| 240 | - * |
|
| 241 | - * @param string $search |
|
| 242 | - * @param int $limit |
|
| 243 | - * @param int $offset |
|
| 244 | - * @return \OC\User\User[] |
|
| 245 | - */ |
|
| 246 | - public function searchDisplayName($search, $limit = null, $offset = null) { |
|
| 247 | - $users = array(); |
|
| 248 | - foreach ($this->backends as $backend) { |
|
| 249 | - $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
| 250 | - $users = $this->getVerifiedUsers($userIds); |
|
| 251 | - if (!is_null($limit) and $limit <= 0) { |
|
| 252 | - return array_values($users); |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - return array_values($users); |
|
| 256 | - } |
|
| 238 | + /** |
|
| 239 | + * search for users in the group by displayname |
|
| 240 | + * |
|
| 241 | + * @param string $search |
|
| 242 | + * @param int $limit |
|
| 243 | + * @param int $offset |
|
| 244 | + * @return \OC\User\User[] |
|
| 245 | + */ |
|
| 246 | + public function searchDisplayName($search, $limit = null, $offset = null) { |
|
| 247 | + $users = array(); |
|
| 248 | + foreach ($this->backends as $backend) { |
|
| 249 | + $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
| 250 | + $users = $this->getVerifiedUsers($userIds); |
|
| 251 | + if (!is_null($limit) and $limit <= 0) { |
|
| 252 | + return array_values($users); |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + return array_values($users); |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - /** |
|
| 259 | - * delete the group |
|
| 260 | - * |
|
| 261 | - * @return bool |
|
| 262 | - */ |
|
| 263 | - public function delete() { |
|
| 264 | - // Prevent users from deleting group admin |
|
| 265 | - if ($this->getGID() === 'admin') { |
|
| 266 | - return false; |
|
| 267 | - } |
|
| 258 | + /** |
|
| 259 | + * delete the group |
|
| 260 | + * |
|
| 261 | + * @return bool |
|
| 262 | + */ |
|
| 263 | + public function delete() { |
|
| 264 | + // Prevent users from deleting group admin |
|
| 265 | + if ($this->getGID() === 'admin') { |
|
| 266 | + return false; |
|
| 267 | + } |
|
| 268 | 268 | |
| 269 | - $result = false; |
|
| 270 | - if ($this->emitter) { |
|
| 271 | - $this->emitter->emit('\OC\Group', 'preDelete', array($this)); |
|
| 272 | - } |
|
| 273 | - foreach ($this->backends as $backend) { |
|
| 274 | - if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) { |
|
| 275 | - $result = true; |
|
| 276 | - $backend->deleteGroup($this->gid); |
|
| 277 | - } |
|
| 278 | - } |
|
| 279 | - if ($result and $this->emitter) { |
|
| 280 | - $this->emitter->emit('\OC\Group', 'postDelete', array($this)); |
|
| 281 | - } |
|
| 282 | - return $result; |
|
| 283 | - } |
|
| 269 | + $result = false; |
|
| 270 | + if ($this->emitter) { |
|
| 271 | + $this->emitter->emit('\OC\Group', 'preDelete', array($this)); |
|
| 272 | + } |
|
| 273 | + foreach ($this->backends as $backend) { |
|
| 274 | + if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) { |
|
| 275 | + $result = true; |
|
| 276 | + $backend->deleteGroup($this->gid); |
|
| 277 | + } |
|
| 278 | + } |
|
| 279 | + if ($result and $this->emitter) { |
|
| 280 | + $this->emitter->emit('\OC\Group', 'postDelete', array($this)); |
|
| 281 | + } |
|
| 282 | + return $result; |
|
| 283 | + } |
|
| 284 | 284 | |
| 285 | - /** |
|
| 286 | - * returns all the Users from an array that really exists |
|
| 287 | - * @param string[] $userIds an array containing user IDs |
|
| 288 | - * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value |
|
| 289 | - */ |
|
| 290 | - private function getVerifiedUsers($userIds) { |
|
| 291 | - if (!is_array($userIds)) { |
|
| 292 | - return array(); |
|
| 293 | - } |
|
| 294 | - $users = array(); |
|
| 295 | - foreach ($userIds as $userId) { |
|
| 296 | - $user = $this->userManager->get($userId); |
|
| 297 | - if (!is_null($user)) { |
|
| 298 | - $users[$userId] = $user; |
|
| 299 | - } |
|
| 300 | - } |
|
| 301 | - return $users; |
|
| 302 | - } |
|
| 285 | + /** |
|
| 286 | + * returns all the Users from an array that really exists |
|
| 287 | + * @param string[] $userIds an array containing user IDs |
|
| 288 | + * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value |
|
| 289 | + */ |
|
| 290 | + private function getVerifiedUsers($userIds) { |
|
| 291 | + if (!is_array($userIds)) { |
|
| 292 | + return array(); |
|
| 293 | + } |
|
| 294 | + $users = array(); |
|
| 295 | + foreach ($userIds as $userId) { |
|
| 296 | + $user = $this->userManager->get($userId); |
|
| 297 | + if (!is_null($user)) { |
|
| 298 | + $users[$userId] = $user; |
|
| 299 | + } |
|
| 300 | + } |
|
| 301 | + return $users; |
|
| 302 | + } |
|
| 303 | 303 | } |
@@ -44,291 +44,291 @@ |
||
| 44 | 44 | */ |
| 45 | 45 | class Database extends \OC\Group\Backend { |
| 46 | 46 | |
| 47 | - /** @var string[] */ |
|
| 48 | - private $groupCache = []; |
|
| 49 | - |
|
| 50 | - /** @var \OCP\IDBConnection */ |
|
| 51 | - private $dbConn; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * \OC\Group\Database constructor. |
|
| 55 | - * |
|
| 56 | - * @param \OCP\IDBConnection|null $dbConn |
|
| 57 | - */ |
|
| 58 | - public function __construct(\OCP\IDBConnection $dbConn = null) { |
|
| 59 | - $this->dbConn = $dbConn; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * FIXME: This function should not be required! |
|
| 64 | - */ |
|
| 65 | - private function fixDI() { |
|
| 66 | - if ($this->dbConn === null) { |
|
| 67 | - $this->dbConn = \OC::$server->getDatabaseConnection(); |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Try to create a new group |
|
| 73 | - * @param string $gid The name of the group to create |
|
| 74 | - * @return bool |
|
| 75 | - * |
|
| 76 | - * Tries to create a new group. If the group name already exists, false will |
|
| 77 | - * be returned. |
|
| 78 | - */ |
|
| 79 | - public function createGroup( $gid ) { |
|
| 80 | - $this->fixDI(); |
|
| 81 | - |
|
| 82 | - // Add group |
|
| 83 | - $result = $this->dbConn->insertIfNotExist('*PREFIX*groups', [ |
|
| 84 | - 'gid' => $gid, |
|
| 85 | - ]); |
|
| 86 | - |
|
| 87 | - // Add to cache |
|
| 88 | - $this->groupCache[$gid] = $gid; |
|
| 89 | - |
|
| 90 | - return $result === 1; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * delete a group |
|
| 95 | - * @param string $gid gid of the group to delete |
|
| 96 | - * @return bool |
|
| 97 | - * |
|
| 98 | - * Deletes a group and removes it from the group_user-table |
|
| 99 | - */ |
|
| 100 | - public function deleteGroup( $gid ) { |
|
| 101 | - $this->fixDI(); |
|
| 102 | - |
|
| 103 | - // Delete the group |
|
| 104 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 105 | - $qb->delete('groups') |
|
| 106 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 107 | - ->execute(); |
|
| 108 | - |
|
| 109 | - // Delete the group-user relation |
|
| 110 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 111 | - $qb->delete('group_user') |
|
| 112 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 113 | - ->execute(); |
|
| 114 | - |
|
| 115 | - // Delete the group-groupadmin relation |
|
| 116 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 117 | - $qb->delete('group_admin') |
|
| 118 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 119 | - ->execute(); |
|
| 120 | - |
|
| 121 | - // Delete from cache |
|
| 122 | - unset($this->groupCache[$gid]); |
|
| 123 | - |
|
| 124 | - return true; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * is user in group? |
|
| 129 | - * @param string $uid uid of the user |
|
| 130 | - * @param string $gid gid of the group |
|
| 131 | - * @return bool |
|
| 132 | - * |
|
| 133 | - * Checks whether the user is member of a group or not. |
|
| 134 | - */ |
|
| 135 | - public function inGroup( $uid, $gid ) { |
|
| 136 | - $this->fixDI(); |
|
| 137 | - |
|
| 138 | - // check |
|
| 139 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 140 | - $cursor = $qb->select('uid') |
|
| 141 | - ->from('group_user') |
|
| 142 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 143 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 144 | - ->execute(); |
|
| 145 | - |
|
| 146 | - $result = $cursor->fetch(); |
|
| 147 | - $cursor->closeCursor(); |
|
| 148 | - |
|
| 149 | - return $result ? true : false; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Add a user to a group |
|
| 154 | - * @param string $uid Name of the user to add to group |
|
| 155 | - * @param string $gid Name of the group in which add the user |
|
| 156 | - * @return bool |
|
| 157 | - * |
|
| 158 | - * Adds a user to a group. |
|
| 159 | - */ |
|
| 160 | - public function addToGroup( $uid, $gid ) { |
|
| 161 | - $this->fixDI(); |
|
| 162 | - |
|
| 163 | - // No duplicate entries! |
|
| 164 | - if( !$this->inGroup( $uid, $gid )) { |
|
| 165 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 166 | - $qb->insert('group_user') |
|
| 167 | - ->setValue('uid', $qb->createNamedParameter($uid)) |
|
| 168 | - ->setValue('gid', $qb->createNamedParameter($gid)) |
|
| 169 | - ->execute(); |
|
| 170 | - return true; |
|
| 171 | - }else{ |
|
| 172 | - return false; |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Removes a user from a group |
|
| 178 | - * @param string $uid Name of the user to remove from group |
|
| 179 | - * @param string $gid Name of the group from which remove the user |
|
| 180 | - * @return bool |
|
| 181 | - * |
|
| 182 | - * removes the user from a group. |
|
| 183 | - */ |
|
| 184 | - public function removeFromGroup( $uid, $gid ) { |
|
| 185 | - $this->fixDI(); |
|
| 186 | - |
|
| 187 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 188 | - $qb->delete('group_user') |
|
| 189 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 190 | - ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 191 | - ->execute(); |
|
| 192 | - |
|
| 193 | - return true; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Get all groups a user belongs to |
|
| 198 | - * @param string $uid Name of the user |
|
| 199 | - * @return array an array of group names |
|
| 200 | - * |
|
| 201 | - * This function fetches all groups a user belongs to. It does not check |
|
| 202 | - * if the user exists at all. |
|
| 203 | - */ |
|
| 204 | - public function getUserGroups( $uid ) { |
|
| 205 | - //guests has empty or null $uid |
|
| 206 | - if ($uid === null || $uid === '') { |
|
| 207 | - return []; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - $this->fixDI(); |
|
| 211 | - |
|
| 212 | - // No magic! |
|
| 213 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 214 | - $cursor = $qb->select('gid') |
|
| 215 | - ->from('group_user') |
|
| 216 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 217 | - ->execute(); |
|
| 218 | - |
|
| 219 | - $groups = []; |
|
| 220 | - while( $row = $cursor->fetch()) { |
|
| 221 | - $groups[] = $row["gid"]; |
|
| 222 | - $this->groupCache[$row['gid']] = $row['gid']; |
|
| 223 | - } |
|
| 224 | - $cursor->closeCursor(); |
|
| 225 | - |
|
| 226 | - return $groups; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * get a list of all groups |
|
| 231 | - * @param string $search |
|
| 232 | - * @param int $limit |
|
| 233 | - * @param int $offset |
|
| 234 | - * @return array an array of group names |
|
| 235 | - * |
|
| 236 | - * Returns a list with all groups |
|
| 237 | - */ |
|
| 238 | - public function getGroups($search = '', $limit = null, $offset = null) { |
|
| 239 | - $parameters = []; |
|
| 240 | - $searchLike = ''; |
|
| 241 | - if ($search !== '') { |
|
| 242 | - $parameters[] = '%' . $search . '%'; |
|
| 243 | - $searchLike = ' WHERE LOWER(`gid`) LIKE LOWER(?)'; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - $stmt = \OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups`' . $searchLike . ' ORDER BY `gid` ASC', $limit, $offset); |
|
| 247 | - $result = $stmt->execute($parameters); |
|
| 248 | - $groups = array(); |
|
| 249 | - while ($row = $result->fetchRow()) { |
|
| 250 | - $groups[] = $row['gid']; |
|
| 251 | - } |
|
| 252 | - return $groups; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * check if a group exists |
|
| 257 | - * @param string $gid |
|
| 258 | - * @return bool |
|
| 259 | - */ |
|
| 260 | - public function groupExists($gid) { |
|
| 261 | - $this->fixDI(); |
|
| 262 | - |
|
| 263 | - // Check cache first |
|
| 264 | - if (isset($this->groupCache[$gid])) { |
|
| 265 | - return true; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 269 | - $cursor = $qb->select('gid') |
|
| 270 | - ->from('groups') |
|
| 271 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 272 | - ->execute(); |
|
| 273 | - $result = $cursor->fetch(); |
|
| 274 | - $cursor->closeCursor(); |
|
| 275 | - |
|
| 276 | - if ($result !== false) { |
|
| 277 | - $this->groupCache[$gid] = $gid; |
|
| 278 | - return true; |
|
| 279 | - } |
|
| 280 | - return false; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * get a list of all users in a group |
|
| 285 | - * @param string $gid |
|
| 286 | - * @param string $search |
|
| 287 | - * @param int $limit |
|
| 288 | - * @param int $offset |
|
| 289 | - * @return array an array of user ids |
|
| 290 | - */ |
|
| 291 | - public function usersInGroup($gid, $search = '', $limit = null, $offset = null) { |
|
| 292 | - $parameters = [$gid]; |
|
| 293 | - $searchLike = ''; |
|
| 294 | - if ($search !== '') { |
|
| 295 | - $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%'; |
|
| 296 | - $searchLike = ' AND `uid` LIKE ?'; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - $stmt = \OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike . ' ORDER BY `uid` ASC', |
|
| 300 | - $limit, |
|
| 301 | - $offset); |
|
| 302 | - $result = $stmt->execute($parameters); |
|
| 303 | - $users = array(); |
|
| 304 | - while ($row = $result->fetchRow()) { |
|
| 305 | - $users[] = $row['uid']; |
|
| 306 | - } |
|
| 307 | - return $users; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * get the number of all users matching the search string in a group |
|
| 312 | - * @param string $gid |
|
| 313 | - * @param string $search |
|
| 314 | - * @return int|false |
|
| 315 | - * @throws \OC\DatabaseException |
|
| 316 | - */ |
|
| 317 | - public function countUsersInGroup($gid, $search = '') { |
|
| 318 | - $parameters = [$gid]; |
|
| 319 | - $searchLike = ''; |
|
| 320 | - if ($search !== '') { |
|
| 321 | - $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%'; |
|
| 322 | - $searchLike = ' AND `uid` LIKE ?'; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - $stmt = \OC_DB::prepare('SELECT COUNT(`uid`) AS `count` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike); |
|
| 326 | - $result = $stmt->execute($parameters); |
|
| 327 | - $count = $result->fetchOne(); |
|
| 328 | - if($count !== false) { |
|
| 329 | - $count = intval($count); |
|
| 330 | - } |
|
| 331 | - return $count; |
|
| 332 | - } |
|
| 47 | + /** @var string[] */ |
|
| 48 | + private $groupCache = []; |
|
| 49 | + |
|
| 50 | + /** @var \OCP\IDBConnection */ |
|
| 51 | + private $dbConn; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * \OC\Group\Database constructor. |
|
| 55 | + * |
|
| 56 | + * @param \OCP\IDBConnection|null $dbConn |
|
| 57 | + */ |
|
| 58 | + public function __construct(\OCP\IDBConnection $dbConn = null) { |
|
| 59 | + $this->dbConn = $dbConn; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * FIXME: This function should not be required! |
|
| 64 | + */ |
|
| 65 | + private function fixDI() { |
|
| 66 | + if ($this->dbConn === null) { |
|
| 67 | + $this->dbConn = \OC::$server->getDatabaseConnection(); |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Try to create a new group |
|
| 73 | + * @param string $gid The name of the group to create |
|
| 74 | + * @return bool |
|
| 75 | + * |
|
| 76 | + * Tries to create a new group. If the group name already exists, false will |
|
| 77 | + * be returned. |
|
| 78 | + */ |
|
| 79 | + public function createGroup( $gid ) { |
|
| 80 | + $this->fixDI(); |
|
| 81 | + |
|
| 82 | + // Add group |
|
| 83 | + $result = $this->dbConn->insertIfNotExist('*PREFIX*groups', [ |
|
| 84 | + 'gid' => $gid, |
|
| 85 | + ]); |
|
| 86 | + |
|
| 87 | + // Add to cache |
|
| 88 | + $this->groupCache[$gid] = $gid; |
|
| 89 | + |
|
| 90 | + return $result === 1; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * delete a group |
|
| 95 | + * @param string $gid gid of the group to delete |
|
| 96 | + * @return bool |
|
| 97 | + * |
|
| 98 | + * Deletes a group and removes it from the group_user-table |
|
| 99 | + */ |
|
| 100 | + public function deleteGroup( $gid ) { |
|
| 101 | + $this->fixDI(); |
|
| 102 | + |
|
| 103 | + // Delete the group |
|
| 104 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 105 | + $qb->delete('groups') |
|
| 106 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 107 | + ->execute(); |
|
| 108 | + |
|
| 109 | + // Delete the group-user relation |
|
| 110 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 111 | + $qb->delete('group_user') |
|
| 112 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 113 | + ->execute(); |
|
| 114 | + |
|
| 115 | + // Delete the group-groupadmin relation |
|
| 116 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 117 | + $qb->delete('group_admin') |
|
| 118 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 119 | + ->execute(); |
|
| 120 | + |
|
| 121 | + // Delete from cache |
|
| 122 | + unset($this->groupCache[$gid]); |
|
| 123 | + |
|
| 124 | + return true; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * is user in group? |
|
| 129 | + * @param string $uid uid of the user |
|
| 130 | + * @param string $gid gid of the group |
|
| 131 | + * @return bool |
|
| 132 | + * |
|
| 133 | + * Checks whether the user is member of a group or not. |
|
| 134 | + */ |
|
| 135 | + public function inGroup( $uid, $gid ) { |
|
| 136 | + $this->fixDI(); |
|
| 137 | + |
|
| 138 | + // check |
|
| 139 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 140 | + $cursor = $qb->select('uid') |
|
| 141 | + ->from('group_user') |
|
| 142 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 143 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 144 | + ->execute(); |
|
| 145 | + |
|
| 146 | + $result = $cursor->fetch(); |
|
| 147 | + $cursor->closeCursor(); |
|
| 148 | + |
|
| 149 | + return $result ? true : false; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Add a user to a group |
|
| 154 | + * @param string $uid Name of the user to add to group |
|
| 155 | + * @param string $gid Name of the group in which add the user |
|
| 156 | + * @return bool |
|
| 157 | + * |
|
| 158 | + * Adds a user to a group. |
|
| 159 | + */ |
|
| 160 | + public function addToGroup( $uid, $gid ) { |
|
| 161 | + $this->fixDI(); |
|
| 162 | + |
|
| 163 | + // No duplicate entries! |
|
| 164 | + if( !$this->inGroup( $uid, $gid )) { |
|
| 165 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 166 | + $qb->insert('group_user') |
|
| 167 | + ->setValue('uid', $qb->createNamedParameter($uid)) |
|
| 168 | + ->setValue('gid', $qb->createNamedParameter($gid)) |
|
| 169 | + ->execute(); |
|
| 170 | + return true; |
|
| 171 | + }else{ |
|
| 172 | + return false; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Removes a user from a group |
|
| 178 | + * @param string $uid Name of the user to remove from group |
|
| 179 | + * @param string $gid Name of the group from which remove the user |
|
| 180 | + * @return bool |
|
| 181 | + * |
|
| 182 | + * removes the user from a group. |
|
| 183 | + */ |
|
| 184 | + public function removeFromGroup( $uid, $gid ) { |
|
| 185 | + $this->fixDI(); |
|
| 186 | + |
|
| 187 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 188 | + $qb->delete('group_user') |
|
| 189 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 190 | + ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 191 | + ->execute(); |
|
| 192 | + |
|
| 193 | + return true; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * Get all groups a user belongs to |
|
| 198 | + * @param string $uid Name of the user |
|
| 199 | + * @return array an array of group names |
|
| 200 | + * |
|
| 201 | + * This function fetches all groups a user belongs to. It does not check |
|
| 202 | + * if the user exists at all. |
|
| 203 | + */ |
|
| 204 | + public function getUserGroups( $uid ) { |
|
| 205 | + //guests has empty or null $uid |
|
| 206 | + if ($uid === null || $uid === '') { |
|
| 207 | + return []; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + $this->fixDI(); |
|
| 211 | + |
|
| 212 | + // No magic! |
|
| 213 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 214 | + $cursor = $qb->select('gid') |
|
| 215 | + ->from('group_user') |
|
| 216 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 217 | + ->execute(); |
|
| 218 | + |
|
| 219 | + $groups = []; |
|
| 220 | + while( $row = $cursor->fetch()) { |
|
| 221 | + $groups[] = $row["gid"]; |
|
| 222 | + $this->groupCache[$row['gid']] = $row['gid']; |
|
| 223 | + } |
|
| 224 | + $cursor->closeCursor(); |
|
| 225 | + |
|
| 226 | + return $groups; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * get a list of all groups |
|
| 231 | + * @param string $search |
|
| 232 | + * @param int $limit |
|
| 233 | + * @param int $offset |
|
| 234 | + * @return array an array of group names |
|
| 235 | + * |
|
| 236 | + * Returns a list with all groups |
|
| 237 | + */ |
|
| 238 | + public function getGroups($search = '', $limit = null, $offset = null) { |
|
| 239 | + $parameters = []; |
|
| 240 | + $searchLike = ''; |
|
| 241 | + if ($search !== '') { |
|
| 242 | + $parameters[] = '%' . $search . '%'; |
|
| 243 | + $searchLike = ' WHERE LOWER(`gid`) LIKE LOWER(?)'; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + $stmt = \OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups`' . $searchLike . ' ORDER BY `gid` ASC', $limit, $offset); |
|
| 247 | + $result = $stmt->execute($parameters); |
|
| 248 | + $groups = array(); |
|
| 249 | + while ($row = $result->fetchRow()) { |
|
| 250 | + $groups[] = $row['gid']; |
|
| 251 | + } |
|
| 252 | + return $groups; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * check if a group exists |
|
| 257 | + * @param string $gid |
|
| 258 | + * @return bool |
|
| 259 | + */ |
|
| 260 | + public function groupExists($gid) { |
|
| 261 | + $this->fixDI(); |
|
| 262 | + |
|
| 263 | + // Check cache first |
|
| 264 | + if (isset($this->groupCache[$gid])) { |
|
| 265 | + return true; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 269 | + $cursor = $qb->select('gid') |
|
| 270 | + ->from('groups') |
|
| 271 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 272 | + ->execute(); |
|
| 273 | + $result = $cursor->fetch(); |
|
| 274 | + $cursor->closeCursor(); |
|
| 275 | + |
|
| 276 | + if ($result !== false) { |
|
| 277 | + $this->groupCache[$gid] = $gid; |
|
| 278 | + return true; |
|
| 279 | + } |
|
| 280 | + return false; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * get a list of all users in a group |
|
| 285 | + * @param string $gid |
|
| 286 | + * @param string $search |
|
| 287 | + * @param int $limit |
|
| 288 | + * @param int $offset |
|
| 289 | + * @return array an array of user ids |
|
| 290 | + */ |
|
| 291 | + public function usersInGroup($gid, $search = '', $limit = null, $offset = null) { |
|
| 292 | + $parameters = [$gid]; |
|
| 293 | + $searchLike = ''; |
|
| 294 | + if ($search !== '') { |
|
| 295 | + $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%'; |
|
| 296 | + $searchLike = ' AND `uid` LIKE ?'; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + $stmt = \OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike . ' ORDER BY `uid` ASC', |
|
| 300 | + $limit, |
|
| 301 | + $offset); |
|
| 302 | + $result = $stmt->execute($parameters); |
|
| 303 | + $users = array(); |
|
| 304 | + while ($row = $result->fetchRow()) { |
|
| 305 | + $users[] = $row['uid']; |
|
| 306 | + } |
|
| 307 | + return $users; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * get the number of all users matching the search string in a group |
|
| 312 | + * @param string $gid |
|
| 313 | + * @param string $search |
|
| 314 | + * @return int|false |
|
| 315 | + * @throws \OC\DatabaseException |
|
| 316 | + */ |
|
| 317 | + public function countUsersInGroup($gid, $search = '') { |
|
| 318 | + $parameters = [$gid]; |
|
| 319 | + $searchLike = ''; |
|
| 320 | + if ($search !== '') { |
|
| 321 | + $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%'; |
|
| 322 | + $searchLike = ' AND `uid` LIKE ?'; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + $stmt = \OC_DB::prepare('SELECT COUNT(`uid`) AS `count` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike); |
|
| 326 | + $result = $stmt->execute($parameters); |
|
| 327 | + $count = $result->fetchOne(); |
|
| 328 | + if($count !== false) { |
|
| 329 | + $count = intval($count); |
|
| 330 | + } |
|
| 331 | + return $count; |
|
| 332 | + } |
|
| 333 | 333 | |
| 334 | 334 | } |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | * Tries to create a new group. If the group name already exists, false will |
| 77 | 77 | * be returned. |
| 78 | 78 | */ |
| 79 | - public function createGroup( $gid ) { |
|
| 79 | + public function createGroup($gid) { |
|
| 80 | 80 | $this->fixDI(); |
| 81 | 81 | |
| 82 | 82 | // Add group |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | * |
| 98 | 98 | * Deletes a group and removes it from the group_user-table |
| 99 | 99 | */ |
| 100 | - public function deleteGroup( $gid ) { |
|
| 100 | + public function deleteGroup($gid) { |
|
| 101 | 101 | $this->fixDI(); |
| 102 | 102 | |
| 103 | 103 | // Delete the group |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | * |
| 133 | 133 | * Checks whether the user is member of a group or not. |
| 134 | 134 | */ |
| 135 | - public function inGroup( $uid, $gid ) { |
|
| 135 | + public function inGroup($uid, $gid) { |
|
| 136 | 136 | $this->fixDI(); |
| 137 | 137 | |
| 138 | 138 | // check |
@@ -157,18 +157,18 @@ discard block |
||
| 157 | 157 | * |
| 158 | 158 | * Adds a user to a group. |
| 159 | 159 | */ |
| 160 | - public function addToGroup( $uid, $gid ) { |
|
| 160 | + public function addToGroup($uid, $gid) { |
|
| 161 | 161 | $this->fixDI(); |
| 162 | 162 | |
| 163 | 163 | // No duplicate entries! |
| 164 | - if( !$this->inGroup( $uid, $gid )) { |
|
| 164 | + if (!$this->inGroup($uid, $gid)) { |
|
| 165 | 165 | $qb = $this->dbConn->getQueryBuilder(); |
| 166 | 166 | $qb->insert('group_user') |
| 167 | 167 | ->setValue('uid', $qb->createNamedParameter($uid)) |
| 168 | 168 | ->setValue('gid', $qb->createNamedParameter($gid)) |
| 169 | 169 | ->execute(); |
| 170 | 170 | return true; |
| 171 | - }else{ |
|
| 171 | + } else { |
|
| 172 | 172 | return false; |
| 173 | 173 | } |
| 174 | 174 | } |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | * |
| 182 | 182 | * removes the user from a group. |
| 183 | 183 | */ |
| 184 | - public function removeFromGroup( $uid, $gid ) { |
|
| 184 | + public function removeFromGroup($uid, $gid) { |
|
| 185 | 185 | $this->fixDI(); |
| 186 | 186 | |
| 187 | 187 | $qb = $this->dbConn->getQueryBuilder(); |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | * This function fetches all groups a user belongs to. It does not check |
| 202 | 202 | * if the user exists at all. |
| 203 | 203 | */ |
| 204 | - public function getUserGroups( $uid ) { |
|
| 204 | + public function getUserGroups($uid) { |
|
| 205 | 205 | //guests has empty or null $uid |
| 206 | 206 | if ($uid === null || $uid === '') { |
| 207 | 207 | return []; |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | ->execute(); |
| 218 | 218 | |
| 219 | 219 | $groups = []; |
| 220 | - while( $row = $cursor->fetch()) { |
|
| 220 | + while ($row = $cursor->fetch()) { |
|
| 221 | 221 | $groups[] = $row["gid"]; |
| 222 | 222 | $this->groupCache[$row['gid']] = $row['gid']; |
| 223 | 223 | } |
@@ -239,11 +239,11 @@ discard block |
||
| 239 | 239 | $parameters = []; |
| 240 | 240 | $searchLike = ''; |
| 241 | 241 | if ($search !== '') { |
| 242 | - $parameters[] = '%' . $search . '%'; |
|
| 242 | + $parameters[] = '%'.$search.'%'; |
|
| 243 | 243 | $searchLike = ' WHERE LOWER(`gid`) LIKE LOWER(?)'; |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | - $stmt = \OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups`' . $searchLike . ' ORDER BY `gid` ASC', $limit, $offset); |
|
| 246 | + $stmt = \OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups`'.$searchLike.' ORDER BY `gid` ASC', $limit, $offset); |
|
| 247 | 247 | $result = $stmt->execute($parameters); |
| 248 | 248 | $groups = array(); |
| 249 | 249 | while ($row = $result->fetchRow()) { |
@@ -292,11 +292,11 @@ discard block |
||
| 292 | 292 | $parameters = [$gid]; |
| 293 | 293 | $searchLike = ''; |
| 294 | 294 | if ($search !== '') { |
| 295 | - $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%'; |
|
| 295 | + $parameters[] = '%'.$this->dbConn->escapeLikeParameter($search).'%'; |
|
| 296 | 296 | $searchLike = ' AND `uid` LIKE ?'; |
| 297 | 297 | } |
| 298 | 298 | |
| 299 | - $stmt = \OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike . ' ORDER BY `uid` ASC', |
|
| 299 | + $stmt = \OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ?'.$searchLike.' ORDER BY `uid` ASC', |
|
| 300 | 300 | $limit, |
| 301 | 301 | $offset); |
| 302 | 302 | $result = $stmt->execute($parameters); |
@@ -318,14 +318,14 @@ discard block |
||
| 318 | 318 | $parameters = [$gid]; |
| 319 | 319 | $searchLike = ''; |
| 320 | 320 | if ($search !== '') { |
| 321 | - $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%'; |
|
| 321 | + $parameters[] = '%'.$this->dbConn->escapeLikeParameter($search).'%'; |
|
| 322 | 322 | $searchLike = ' AND `uid` LIKE ?'; |
| 323 | 323 | } |
| 324 | 324 | |
| 325 | - $stmt = \OC_DB::prepare('SELECT COUNT(`uid`) AS `count` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike); |
|
| 325 | + $stmt = \OC_DB::prepare('SELECT COUNT(`uid`) AS `count` FROM `*PREFIX*group_user` WHERE `gid` = ?'.$searchLike); |
|
| 326 | 326 | $result = $stmt->execute($parameters); |
| 327 | 327 | $count = $result->fetchOne(); |
| 328 | - if($count !== false) { |
|
| 328 | + if ($count !== false) { |
|
| 329 | 329 | $count = intval($count); |
| 330 | 330 | } |
| 331 | 331 | return $count; |
@@ -168,7 +168,7 @@ |
||
| 168 | 168 | ->setValue('gid', $qb->createNamedParameter($gid)) |
| 169 | 169 | ->execute(); |
| 170 | 170 | return true; |
| 171 | - }else{ |
|
| 171 | + } else{ |
|
| 172 | 172 | return false; |
| 173 | 173 | } |
| 174 | 174 | } |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | IUserSession $userSession |
| 61 | 61 | ) { |
| 62 | 62 | $this->user = $user; |
| 63 | - $this->isAdmin = (bool)$isAdmin; |
|
| 63 | + $this->isAdmin = (bool) $isAdmin; |
|
| 64 | 64 | $this->groupManager = $groupManager; |
| 65 | 65 | $this->userSession = $userSession; |
| 66 | 66 | } |
@@ -76,8 +76,8 @@ discard block |
||
| 76 | 76 | * @return array |
| 77 | 77 | */ |
| 78 | 78 | public function get($groupSearch = '', $userSearch = '') { |
| 79 | - $key = $groupSearch . '::' . $userSearch; |
|
| 80 | - if(isset($this->metaData[$key])) { |
|
| 79 | + $key = $groupSearch.'::'.$userSearch; |
|
| 80 | + if (isset($this->metaData[$key])) { |
|
| 81 | 81 | return $this->metaData[$key]; |
| 82 | 82 | } |
| 83 | 83 | |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | $sortAdminGroupsIndex = 0; |
| 89 | 89 | $sortAdminGroupsKeys = array(); |
| 90 | 90 | |
| 91 | - foreach($this->getGroups($groupSearch) as $group) { |
|
| 91 | + foreach ($this->getGroups($groupSearch) as $group) { |
|
| 92 | 92 | $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
| 93 | 93 | if (strtolower($group->getGID()) !== 'admin') { |
| 94 | 94 | $this->addEntry( |
@@ -185,11 +185,11 @@ discard block |
||
| 185 | 185 | * @return \OCP\IGroup[] |
| 186 | 186 | */ |
| 187 | 187 | protected function getGroups($search = '') { |
| 188 | - if($this->isAdmin) { |
|
| 188 | + if ($this->isAdmin) { |
|
| 189 | 189 | return $this->groupManager->search($search); |
| 190 | 190 | } else { |
| 191 | 191 | $userObject = $this->userSession->getUser(); |
| 192 | - if($userObject !== null) { |
|
| 192 | + if ($userObject !== null) { |
|
| 193 | 193 | $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
| 194 | 194 | } else { |
| 195 | 195 | $groups = []; |
@@ -30,172 +30,172 @@ |
||
| 30 | 30 | use OCP\IUserSession; |
| 31 | 31 | |
| 32 | 32 | class MetaData { |
| 33 | - const SORT_NONE = 0; |
|
| 34 | - const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends |
|
| 35 | - const SORT_GROUPNAME = 2; |
|
| 36 | - |
|
| 37 | - /** @var string */ |
|
| 38 | - protected $user; |
|
| 39 | - /** @var bool */ |
|
| 40 | - protected $isAdmin; |
|
| 41 | - /** @var array */ |
|
| 42 | - protected $metaData = array(); |
|
| 43 | - /** @var \OCP\IGroupManager */ |
|
| 44 | - protected $groupManager; |
|
| 45 | - /** @var bool */ |
|
| 46 | - protected $sorting = false; |
|
| 47 | - /** @var IUserSession */ |
|
| 48 | - protected $userSession; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @param string $user the uid of the current user |
|
| 52 | - * @param bool $isAdmin whether the current users is an admin |
|
| 53 | - * @param \OCP\IGroupManager $groupManager |
|
| 54 | - * @param IUserSession $userSession |
|
| 55 | - */ |
|
| 56 | - public function __construct( |
|
| 57 | - $user, |
|
| 58 | - $isAdmin, |
|
| 59 | - \OCP\IGroupManager $groupManager, |
|
| 60 | - IUserSession $userSession |
|
| 61 | - ) { |
|
| 62 | - $this->user = $user; |
|
| 63 | - $this->isAdmin = (bool)$isAdmin; |
|
| 64 | - $this->groupManager = $groupManager; |
|
| 65 | - $this->userSession = $userSession; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * returns an array with meta data about all available groups |
|
| 70 | - * the array is structured as follows: |
|
| 71 | - * [0] array containing meta data about admin groups |
|
| 72 | - * [1] array containing meta data about unprivileged groups |
|
| 73 | - * @param string $groupSearch only effective when instance was created with |
|
| 74 | - * isAdmin being true |
|
| 75 | - * @param string $userSearch the pattern users are search for |
|
| 76 | - * @return array |
|
| 77 | - */ |
|
| 78 | - public function get($groupSearch = '', $userSearch = '') { |
|
| 79 | - $key = $groupSearch . '::' . $userSearch; |
|
| 80 | - if(isset($this->metaData[$key])) { |
|
| 81 | - return $this->metaData[$key]; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - $adminGroups = array(); |
|
| 85 | - $groups = array(); |
|
| 86 | - $sortGroupsIndex = 0; |
|
| 87 | - $sortGroupsKeys = array(); |
|
| 88 | - $sortAdminGroupsIndex = 0; |
|
| 89 | - $sortAdminGroupsKeys = array(); |
|
| 90 | - |
|
| 91 | - foreach($this->getGroups($groupSearch) as $group) { |
|
| 92 | - $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
|
| 93 | - if (strtolower($group->getGID()) !== 'admin') { |
|
| 94 | - $this->addEntry( |
|
| 95 | - $groups, |
|
| 96 | - $sortGroupsKeys, |
|
| 97 | - $sortGroupsIndex, |
|
| 98 | - $groupMetaData); |
|
| 99 | - } else { |
|
| 100 | - //admin group is hard coded to 'admin' for now. In future, |
|
| 101 | - //backends may define admin groups too. Then the if statement |
|
| 102 | - //has to be adjusted accordingly. |
|
| 103 | - $this->addEntry( |
|
| 104 | - $adminGroups, |
|
| 105 | - $sortAdminGroupsKeys, |
|
| 106 | - $sortAdminGroupsIndex, |
|
| 107 | - $groupMetaData); |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - //whether sorting is necessary is will be checked in sort() |
|
| 112 | - $this->sort($groups, $sortGroupsKeys); |
|
| 113 | - $this->sort($adminGroups, $sortAdminGroupsKeys); |
|
| 114 | - |
|
| 115 | - $this->metaData[$key] = array($adminGroups, $groups); |
|
| 116 | - return $this->metaData[$key]; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * sets the sort mode, see SORT_* constants for supported modes |
|
| 121 | - * |
|
| 122 | - * @param int $sortMode |
|
| 123 | - */ |
|
| 124 | - public function setSorting($sortMode) { |
|
| 125 | - switch ($sortMode) { |
|
| 126 | - case self::SORT_USERCOUNT: |
|
| 127 | - case self::SORT_GROUPNAME: |
|
| 128 | - $this->sorting = $sortMode; |
|
| 129 | - break; |
|
| 130 | - |
|
| 131 | - default: |
|
| 132 | - $this->sorting = self::SORT_NONE; |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * adds an group entry to the resulting array |
|
| 138 | - * @param array $entries the resulting array, by reference |
|
| 139 | - * @param array $sortKeys the sort key array, by reference |
|
| 140 | - * @param int $sortIndex the sort key index, by reference |
|
| 141 | - * @param array $data the group's meta data as returned by generateGroupMetaData() |
|
| 142 | - */ |
|
| 143 | - private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) { |
|
| 144 | - $entries[] = $data; |
|
| 145 | - if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 146 | - $sortKeys[$sortIndex] = $data['usercount']; |
|
| 147 | - $sortIndex++; |
|
| 148 | - } else if ($this->sorting === self::SORT_GROUPNAME) { |
|
| 149 | - $sortKeys[$sortIndex] = $data['name']; |
|
| 150 | - $sortIndex++; |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * creates an array containing the group meta data |
|
| 156 | - * @param \OCP\IGroup $group |
|
| 157 | - * @param string $userSearch |
|
| 158 | - * @return array with the keys 'id', 'name' and 'usercount' |
|
| 159 | - */ |
|
| 160 | - private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) { |
|
| 161 | - return array( |
|
| 162 | - 'id' => $group->getGID(), |
|
| 163 | - 'name' => $group->getDisplayName(), |
|
| 164 | - 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0, |
|
| 165 | - ); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * sorts the result array, if applicable |
|
| 170 | - * @param array $entries the result array, by reference |
|
| 171 | - * @param array $sortKeys the array containing the sort keys |
|
| 172 | - * @param return null |
|
| 173 | - */ |
|
| 174 | - private function sort(&$entries, $sortKeys) { |
|
| 175 | - if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 176 | - array_multisort($sortKeys, SORT_DESC, $entries); |
|
| 177 | - } else if ($this->sorting === self::SORT_GROUPNAME) { |
|
| 178 | - array_multisort($sortKeys, SORT_ASC, $entries); |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * returns the available groups |
|
| 184 | - * @param string $search a search string |
|
| 185 | - * @return \OCP\IGroup[] |
|
| 186 | - */ |
|
| 187 | - protected function getGroups($search = '') { |
|
| 188 | - if($this->isAdmin) { |
|
| 189 | - return $this->groupManager->search($search); |
|
| 190 | - } else { |
|
| 191 | - $userObject = $this->userSession->getUser(); |
|
| 192 | - if($userObject !== null) { |
|
| 193 | - $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
|
| 194 | - } else { |
|
| 195 | - $groups = []; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - return $groups; |
|
| 199 | - } |
|
| 200 | - } |
|
| 33 | + const SORT_NONE = 0; |
|
| 34 | + const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends |
|
| 35 | + const SORT_GROUPNAME = 2; |
|
| 36 | + |
|
| 37 | + /** @var string */ |
|
| 38 | + protected $user; |
|
| 39 | + /** @var bool */ |
|
| 40 | + protected $isAdmin; |
|
| 41 | + /** @var array */ |
|
| 42 | + protected $metaData = array(); |
|
| 43 | + /** @var \OCP\IGroupManager */ |
|
| 44 | + protected $groupManager; |
|
| 45 | + /** @var bool */ |
|
| 46 | + protected $sorting = false; |
|
| 47 | + /** @var IUserSession */ |
|
| 48 | + protected $userSession; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @param string $user the uid of the current user |
|
| 52 | + * @param bool $isAdmin whether the current users is an admin |
|
| 53 | + * @param \OCP\IGroupManager $groupManager |
|
| 54 | + * @param IUserSession $userSession |
|
| 55 | + */ |
|
| 56 | + public function __construct( |
|
| 57 | + $user, |
|
| 58 | + $isAdmin, |
|
| 59 | + \OCP\IGroupManager $groupManager, |
|
| 60 | + IUserSession $userSession |
|
| 61 | + ) { |
|
| 62 | + $this->user = $user; |
|
| 63 | + $this->isAdmin = (bool)$isAdmin; |
|
| 64 | + $this->groupManager = $groupManager; |
|
| 65 | + $this->userSession = $userSession; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * returns an array with meta data about all available groups |
|
| 70 | + * the array is structured as follows: |
|
| 71 | + * [0] array containing meta data about admin groups |
|
| 72 | + * [1] array containing meta data about unprivileged groups |
|
| 73 | + * @param string $groupSearch only effective when instance was created with |
|
| 74 | + * isAdmin being true |
|
| 75 | + * @param string $userSearch the pattern users are search for |
|
| 76 | + * @return array |
|
| 77 | + */ |
|
| 78 | + public function get($groupSearch = '', $userSearch = '') { |
|
| 79 | + $key = $groupSearch . '::' . $userSearch; |
|
| 80 | + if(isset($this->metaData[$key])) { |
|
| 81 | + return $this->metaData[$key]; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + $adminGroups = array(); |
|
| 85 | + $groups = array(); |
|
| 86 | + $sortGroupsIndex = 0; |
|
| 87 | + $sortGroupsKeys = array(); |
|
| 88 | + $sortAdminGroupsIndex = 0; |
|
| 89 | + $sortAdminGroupsKeys = array(); |
|
| 90 | + |
|
| 91 | + foreach($this->getGroups($groupSearch) as $group) { |
|
| 92 | + $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
|
| 93 | + if (strtolower($group->getGID()) !== 'admin') { |
|
| 94 | + $this->addEntry( |
|
| 95 | + $groups, |
|
| 96 | + $sortGroupsKeys, |
|
| 97 | + $sortGroupsIndex, |
|
| 98 | + $groupMetaData); |
|
| 99 | + } else { |
|
| 100 | + //admin group is hard coded to 'admin' for now. In future, |
|
| 101 | + //backends may define admin groups too. Then the if statement |
|
| 102 | + //has to be adjusted accordingly. |
|
| 103 | + $this->addEntry( |
|
| 104 | + $adminGroups, |
|
| 105 | + $sortAdminGroupsKeys, |
|
| 106 | + $sortAdminGroupsIndex, |
|
| 107 | + $groupMetaData); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + //whether sorting is necessary is will be checked in sort() |
|
| 112 | + $this->sort($groups, $sortGroupsKeys); |
|
| 113 | + $this->sort($adminGroups, $sortAdminGroupsKeys); |
|
| 114 | + |
|
| 115 | + $this->metaData[$key] = array($adminGroups, $groups); |
|
| 116 | + return $this->metaData[$key]; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * sets the sort mode, see SORT_* constants for supported modes |
|
| 121 | + * |
|
| 122 | + * @param int $sortMode |
|
| 123 | + */ |
|
| 124 | + public function setSorting($sortMode) { |
|
| 125 | + switch ($sortMode) { |
|
| 126 | + case self::SORT_USERCOUNT: |
|
| 127 | + case self::SORT_GROUPNAME: |
|
| 128 | + $this->sorting = $sortMode; |
|
| 129 | + break; |
|
| 130 | + |
|
| 131 | + default: |
|
| 132 | + $this->sorting = self::SORT_NONE; |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * adds an group entry to the resulting array |
|
| 138 | + * @param array $entries the resulting array, by reference |
|
| 139 | + * @param array $sortKeys the sort key array, by reference |
|
| 140 | + * @param int $sortIndex the sort key index, by reference |
|
| 141 | + * @param array $data the group's meta data as returned by generateGroupMetaData() |
|
| 142 | + */ |
|
| 143 | + private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) { |
|
| 144 | + $entries[] = $data; |
|
| 145 | + if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 146 | + $sortKeys[$sortIndex] = $data['usercount']; |
|
| 147 | + $sortIndex++; |
|
| 148 | + } else if ($this->sorting === self::SORT_GROUPNAME) { |
|
| 149 | + $sortKeys[$sortIndex] = $data['name']; |
|
| 150 | + $sortIndex++; |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * creates an array containing the group meta data |
|
| 156 | + * @param \OCP\IGroup $group |
|
| 157 | + * @param string $userSearch |
|
| 158 | + * @return array with the keys 'id', 'name' and 'usercount' |
|
| 159 | + */ |
|
| 160 | + private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) { |
|
| 161 | + return array( |
|
| 162 | + 'id' => $group->getGID(), |
|
| 163 | + 'name' => $group->getDisplayName(), |
|
| 164 | + 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0, |
|
| 165 | + ); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * sorts the result array, if applicable |
|
| 170 | + * @param array $entries the result array, by reference |
|
| 171 | + * @param array $sortKeys the array containing the sort keys |
|
| 172 | + * @param return null |
|
| 173 | + */ |
|
| 174 | + private function sort(&$entries, $sortKeys) { |
|
| 175 | + if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 176 | + array_multisort($sortKeys, SORT_DESC, $entries); |
|
| 177 | + } else if ($this->sorting === self::SORT_GROUPNAME) { |
|
| 178 | + array_multisort($sortKeys, SORT_ASC, $entries); |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * returns the available groups |
|
| 184 | + * @param string $search a search string |
|
| 185 | + * @return \OCP\IGroup[] |
|
| 186 | + */ |
|
| 187 | + protected function getGroups($search = '') { |
|
| 188 | + if($this->isAdmin) { |
|
| 189 | + return $this->groupManager->search($search); |
|
| 190 | + } else { |
|
| 191 | + $userObject = $this->userSession->getUser(); |
|
| 192 | + if($userObject !== null) { |
|
| 193 | + $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
|
| 194 | + } else { |
|
| 195 | + $groups = []; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + return $groups; |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | 201 | } |
@@ -49,8 +49,8 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | public function getSupportedActions() { |
| 51 | 51 | $actions = 0; |
| 52 | - foreach($this->possibleActions AS $action => $methodName) { |
|
| 53 | - if(method_exists($this, $methodName)) { |
|
| 52 | + foreach ($this->possibleActions AS $action => $methodName) { |
|
| 53 | + if (method_exists($this, $methodName)) { |
|
| 54 | 54 | $actions |= $action; |
| 55 | 55 | } |
| 56 | 56 | } |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | * compared with \OC\Group\Backend::CREATE_GROUP etc. |
| 68 | 68 | */ |
| 69 | 69 | public function implementsActions($actions) { |
| 70 | - return (bool)($this->getSupportedActions() & $actions); |
|
| 70 | + return (bool) ($this->getSupportedActions() & $actions); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
@@ -26,107 +26,107 @@ |
||
| 26 | 26 | * Abstract base class for user management |
| 27 | 27 | */ |
| 28 | 28 | abstract class Backend implements \OCP\GroupInterface { |
| 29 | - /** |
|
| 30 | - * error code for functions not provided by the group backend |
|
| 31 | - */ |
|
| 32 | - const NOT_IMPLEMENTED = -501; |
|
| 29 | + /** |
|
| 30 | + * error code for functions not provided by the group backend |
|
| 31 | + */ |
|
| 32 | + const NOT_IMPLEMENTED = -501; |
|
| 33 | 33 | |
| 34 | - protected $possibleActions = [ |
|
| 35 | - self::CREATE_GROUP => 'createGroup', |
|
| 36 | - self::DELETE_GROUP => 'deleteGroup', |
|
| 37 | - self::ADD_TO_GROUP => 'addToGroup', |
|
| 38 | - self::REMOVE_FROM_GOUP => 'removeFromGroup', |
|
| 39 | - self::COUNT_USERS => 'countUsersInGroup', |
|
| 40 | - self::GROUP_DETAILS => 'getGroupDetails', |
|
| 41 | - self::IS_ADMIN => 'isAdmin', |
|
| 42 | - ]; |
|
| 34 | + protected $possibleActions = [ |
|
| 35 | + self::CREATE_GROUP => 'createGroup', |
|
| 36 | + self::DELETE_GROUP => 'deleteGroup', |
|
| 37 | + self::ADD_TO_GROUP => 'addToGroup', |
|
| 38 | + self::REMOVE_FROM_GOUP => 'removeFromGroup', |
|
| 39 | + self::COUNT_USERS => 'countUsersInGroup', |
|
| 40 | + self::GROUP_DETAILS => 'getGroupDetails', |
|
| 41 | + self::IS_ADMIN => 'isAdmin', |
|
| 42 | + ]; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Get all supported actions |
|
| 46 | - * @return int bitwise-or'ed actions |
|
| 47 | - * |
|
| 48 | - * Returns the supported actions as int to be |
|
| 49 | - * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
| 50 | - */ |
|
| 51 | - public function getSupportedActions() { |
|
| 52 | - $actions = 0; |
|
| 53 | - foreach($this->possibleActions AS $action => $methodName) { |
|
| 54 | - if(method_exists($this, $methodName)) { |
|
| 55 | - $actions |= $action; |
|
| 56 | - } |
|
| 57 | - } |
|
| 44 | + /** |
|
| 45 | + * Get all supported actions |
|
| 46 | + * @return int bitwise-or'ed actions |
|
| 47 | + * |
|
| 48 | + * Returns the supported actions as int to be |
|
| 49 | + * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
| 50 | + */ |
|
| 51 | + public function getSupportedActions() { |
|
| 52 | + $actions = 0; |
|
| 53 | + foreach($this->possibleActions AS $action => $methodName) { |
|
| 54 | + if(method_exists($this, $methodName)) { |
|
| 55 | + $actions |= $action; |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - return $actions; |
|
| 60 | - } |
|
| 59 | + return $actions; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Check if backend implements actions |
|
| 64 | - * @param int $actions bitwise-or'ed actions |
|
| 65 | - * @return bool |
|
| 66 | - * |
|
| 67 | - * Returns the supported actions as int to be |
|
| 68 | - * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
| 69 | - */ |
|
| 70 | - public function implementsActions($actions) { |
|
| 71 | - return (bool)($this->getSupportedActions() & $actions); |
|
| 72 | - } |
|
| 62 | + /** |
|
| 63 | + * Check if backend implements actions |
|
| 64 | + * @param int $actions bitwise-or'ed actions |
|
| 65 | + * @return bool |
|
| 66 | + * |
|
| 67 | + * Returns the supported actions as int to be |
|
| 68 | + * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
| 69 | + */ |
|
| 70 | + public function implementsActions($actions) { |
|
| 71 | + return (bool)($this->getSupportedActions() & $actions); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * is user in group? |
|
| 76 | - * @param string $uid uid of the user |
|
| 77 | - * @param string $gid gid of the group |
|
| 78 | - * @return bool |
|
| 79 | - * |
|
| 80 | - * Checks whether the user is member of a group or not. |
|
| 81 | - */ |
|
| 82 | - public function inGroup($uid, $gid) { |
|
| 83 | - return in_array($gid, $this->getUserGroups($uid)); |
|
| 84 | - } |
|
| 74 | + /** |
|
| 75 | + * is user in group? |
|
| 76 | + * @param string $uid uid of the user |
|
| 77 | + * @param string $gid gid of the group |
|
| 78 | + * @return bool |
|
| 79 | + * |
|
| 80 | + * Checks whether the user is member of a group or not. |
|
| 81 | + */ |
|
| 82 | + public function inGroup($uid, $gid) { |
|
| 83 | + return in_array($gid, $this->getUserGroups($uid)); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Get all groups a user belongs to |
|
| 88 | - * @param string $uid Name of the user |
|
| 89 | - * @return array an array of group names |
|
| 90 | - * |
|
| 91 | - * This function fetches all groups a user belongs to. It does not check |
|
| 92 | - * if the user exists at all. |
|
| 93 | - */ |
|
| 94 | - public function getUserGroups($uid) { |
|
| 95 | - return array(); |
|
| 96 | - } |
|
| 86 | + /** |
|
| 87 | + * Get all groups a user belongs to |
|
| 88 | + * @param string $uid Name of the user |
|
| 89 | + * @return array an array of group names |
|
| 90 | + * |
|
| 91 | + * This function fetches all groups a user belongs to. It does not check |
|
| 92 | + * if the user exists at all. |
|
| 93 | + */ |
|
| 94 | + public function getUserGroups($uid) { |
|
| 95 | + return array(); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * get a list of all groups |
|
| 100 | - * @param string $search |
|
| 101 | - * @param int $limit |
|
| 102 | - * @param int $offset |
|
| 103 | - * @return array an array of group names |
|
| 104 | - * |
|
| 105 | - * Returns a list with all groups |
|
| 106 | - */ |
|
| 98 | + /** |
|
| 99 | + * get a list of all groups |
|
| 100 | + * @param string $search |
|
| 101 | + * @param int $limit |
|
| 102 | + * @param int $offset |
|
| 103 | + * @return array an array of group names |
|
| 104 | + * |
|
| 105 | + * Returns a list with all groups |
|
| 106 | + */ |
|
| 107 | 107 | |
| 108 | - public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
| 109 | - return array(); |
|
| 110 | - } |
|
| 108 | + public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
| 109 | + return array(); |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * check if a group exists |
|
| 114 | - * @param string $gid |
|
| 115 | - * @return bool |
|
| 116 | - */ |
|
| 117 | - public function groupExists($gid) { |
|
| 118 | - return in_array($gid, $this->getGroups($gid, 1)); |
|
| 119 | - } |
|
| 112 | + /** |
|
| 113 | + * check if a group exists |
|
| 114 | + * @param string $gid |
|
| 115 | + * @return bool |
|
| 116 | + */ |
|
| 117 | + public function groupExists($gid) { |
|
| 118 | + return in_array($gid, $this->getGroups($gid, 1)); |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * get a list of all users in a group |
|
| 123 | - * @param string $gid |
|
| 124 | - * @param string $search |
|
| 125 | - * @param int $limit |
|
| 126 | - * @param int $offset |
|
| 127 | - * @return array an array of user ids |
|
| 128 | - */ |
|
| 129 | - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 130 | - return array(); |
|
| 131 | - } |
|
| 121 | + /** |
|
| 122 | + * get a list of all users in a group |
|
| 123 | + * @param string $gid |
|
| 124 | + * @param string $search |
|
| 125 | + * @param int $limit |
|
| 126 | + * @param int $offset |
|
| 127 | + * @return array an array of user ids |
|
| 128 | + */ |
|
| 129 | + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 130 | + return array(); |
|
| 131 | + } |
|
| 132 | 132 | } |
@@ -20,4 +20,4 @@ |
||
| 20 | 20 | * |
| 21 | 21 | */ |
| 22 | 22 | |
| 23 | -require_once __DIR__ . '/v1.php'; |
|
| 23 | +require_once __DIR__.'/v1.php'; |
|
@@ -26,8 +26,8 @@ |
||
| 26 | 26 | $server = \OC::$server; |
| 27 | 27 | |
| 28 | 28 | $controller = new \OC\OCS\Provider( |
| 29 | - 'ocs_provider', |
|
| 30 | - $server->getRequest(), |
|
| 31 | - $server->getAppManager() |
|
| 29 | + 'ocs_provider', |
|
| 30 | + $server->getRequest(), |
|
| 31 | + $server->getAppManager() |
|
| 32 | 32 | ); |
| 33 | 33 | echo $controller->buildProviderList()->render(); |
@@ -19,7 +19,7 @@ |
||
| 19 | 19 | * |
| 20 | 20 | */ |
| 21 | 21 | |
| 22 | -require_once __DIR__ . '/../lib/base.php'; |
|
| 22 | +require_once __DIR__.'/../lib/base.php'; |
|
| 23 | 23 | |
| 24 | 24 | header('Content-Type: application/json'); |
| 25 | 25 | |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | <form method="post" name="login"> |
| 13 | 13 | <fieldset> |
| 14 | 14 | <?php if (!empty($_['redirect_url'])) { |
| 15 | - print_unescaped('<input type="hidden" name="redirect_url" value="' . \OCP\Util::sanitizeHTML($_['redirect_url']) . '">'); |
|
| 15 | + print_unescaped('<input type="hidden" name="redirect_url" value="'.\OCP\Util::sanitizeHTML($_['redirect_url']).'">'); |
|
| 16 | 16 | } ?> |
| 17 | 17 | <?php if (isset($_['apacheauthfailed']) && ($_['apacheauthfailed'])): ?> |
| 18 | 18 | <div class="warning"> |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | <small><?php p($l->t('Please contact your administrator.')); ?></small> |
| 21 | 21 | </div> |
| 22 | 22 | <?php endif; ?> |
| 23 | - <?php foreach($_['messages'] as $message): ?> |
|
| 23 | + <?php foreach ($_['messages'] as $message): ?> |
|
| 24 | 24 | <div class="warning"> |
| 25 | 25 | <?php p($message); ?><br> |
| 26 | 26 | </div> |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | <?php endif; ?> |
| 34 | 34 | <div id="message" class="hidden"> |
| 35 | 35 | <img class="float-spinner" alt="" |
| 36 | - src="<?php p(image_path('core', 'loading-dark.gif'));?>"> |
|
| 36 | + src="<?php p(image_path('core', 'loading-dark.gif')); ?>"> |
|
| 37 | 37 | <span id="messageText"></span> |
| 38 | 38 | <!-- the following div ensures that the spinner is always inside the #message div --> |
| 39 | 39 | <div style="clear: both;"></div> |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | <fieldset> |
| 89 | 89 | <legend><?php p($l->t('Alternative Logins')) ?></legend> |
| 90 | 90 | <ul> |
| 91 | - <?php foreach($_['alt_login'] as $login): ?> |
|
| 91 | + <?php foreach ($_['alt_login'] as $login): ?> |
|
| 92 | 92 | <li><a class="button" href="<?php print_unescaped($login['href']); ?>" ><?php p($login['name']); ?></a></li> |
| 93 | 93 | <?php endforeach; ?> |
| 94 | 94 | </ul> |
@@ -8,8 +8,8 @@ |
||
| 8 | 8 | <form method="post" name="login"> |
| 9 | 9 | <fieldset> |
| 10 | 10 | <?php if (!empty($_['redirect_url'])) { |
| 11 | - print_unescaped('<input type="hidden" name="redirect_url" value="' . \OCP\Util::sanitizeHTML($_['redirect_url']) . '">'); |
|
| 12 | - } ?> |
|
| 11 | + print_unescaped('<input type="hidden" name="redirect_url" value="' . \OCP\Util::sanitizeHTML($_['redirect_url']) . '">'); |
|
| 12 | + } ?> |
|
| 13 | 13 | <?php if (isset($_['apacheauthfailed']) && ($_['apacheauthfailed'])): ?> |
| 14 | 14 | <div class="warning"> |
| 15 | 15 | <?php p($l->t('Server side authentication failed!')); ?><br> |