diff options
author | Joas Schilling <coding@schilljs.com> | 2021-03-09 21:22:59 +0100 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2021-03-10 17:19:55 +0100 |
commit | c7be18c0d6cf6a5af2251abdfa18cdccd11da33b (patch) | |
tree | 13dcd637d5cc1437ec3febf8ba5f809ce11f7c63 /lib/private/KnownUser | |
parent | 7baefcfc74d12da8a3850b6747da7c055c56e522 (diff) | |
download | nextcloud-server-c7be18c0d6cf6a5af2251abdfa18cdccd11da33b.tar.gz nextcloud-server-c7be18c0d6cf6a5af2251abdfa18cdccd11da33b.zip |
Add a service to find out if a user knows another user
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/private/KnownUser')
-rw-r--r-- | lib/private/KnownUser/KnownUserMapper.php | 13 | ||||
-rw-r--r-- | lib/private/KnownUser/KnownUserService.php | 62 |
2 files changed, 75 insertions, 0 deletions
diff --git a/lib/private/KnownUser/KnownUserMapper.php b/lib/private/KnownUser/KnownUserMapper.php index 86e13a2e2f0..1144e2fd212 100644 --- a/lib/private/KnownUser/KnownUserMapper.php +++ b/lib/private/KnownUser/KnownUserMapper.php @@ -62,6 +62,19 @@ class KnownUserMapper extends QBMapper { return (int) $query->execute(); } + /** + * @param string $knownTo + * @return KnownUser[] + */ + public function getKnownTo(string $knownTo): array { + $query = $this->db->getQueryBuilder(); + $query->select('*') + ->from($this->getTableName()) + ->where($query->expr()->eq('known_to', $query->createNamedParameter($knownTo))); + + return $this->findEntities($query); + } + public function createKnownUserFromRow(array $row): KnownUser { return $this->mapRowToEntity([ 'id' => $row['s_id'], diff --git a/lib/private/KnownUser/KnownUserService.php b/lib/private/KnownUser/KnownUserService.php new file mode 100644 index 00000000000..3a97b967c3a --- /dev/null +++ b/lib/private/KnownUser/KnownUserService.php @@ -0,0 +1,62 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2021 Joas Schilling <coding@schilljs.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\KnownUser; + +class KnownUserService { + /** @var KnownUserMapper */ + protected $mapper; + /** @var array */ + protected $knownUsers = []; + + public function __construct(KnownUserMapper $mapper) { + $this->mapper = $mapper; + } + + public function deleteKnownTo(string $knownTo): int { + return $this->mapper->deleteKnownTo($knownTo); + } + + public function deleteKnownUser(string $knownUser): int { + return $this->mapper->deleteKnownUser($knownUser); + } + + public function storeIsKnownToUser(string $knownTo, string $knownUser): void { + $entity = new KnownUser(); + $entity->setKnownTo($knownTo); + $entity->setKnownUser($knownUser); + $this->mapper->insert($entity); + } + + public function isKnownToUser(string $knownTo, string $user): bool { + if (!isset($this->knownUsers[$knownTo])) { + $entities = $this->mapper->getKnownTo($knownTo); + $this->knownUsers[$knownTo] = []; + foreach ($entities as $entity) { + $this->knownUsers[$knownTo][$entity->getKnownUser()] = true; + } + } + + return isset($this->knownUsers[$knownTo][$user]); + } +} |