3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Security\PermissionSet\Repository;
25 use Friendica\BaseRepository;
26 use Friendica\Database\Database;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Group;
29 use Friendica\Network\HTTPException\NotFoundException;
30 use Friendica\Security\PermissionSet\Factory;
31 use Friendica\Security\PermissionSet\Collection;
32 use Friendica\Security\PermissionSet\Entity;
33 use Friendica\Util\ACLFormatter;
34 use Psr\Log\LoggerInterface;
36 class PermissionSet extends BaseRepository
38 /** @var int Virtual permission set id for public permission */
41 /** @var Factory\PermissionSet */
44 protected static $table_name = 'permissionset';
46 /** @var ACLFormatter */
47 private $aclFormatter;
49 public function __construct(Database $database, LoggerInterface $logger, Factory\PermissionSet $factory, ACLFormatter $aclFormatter)
51 parent::__construct($database, $logger, $factory);
53 $this->aclFormatter = $aclFormatter;
57 * replaces the PUBLIC id for the public permissionSet
58 * (no need to create the default permission set over and over again)
62 private function checkPublicSelect(&$condition)
64 if (empty($condition['allow_cid']) &&
65 empty($condition['allow_gid']) &&
66 empty($condition['deny_cid']) &&
67 empty($condition['deny_gid'])) {
68 $condition['uid'] = self::PUBLIC;
73 * @param array $condition
74 * @param array $params
76 * @return Entity\PermissionSet
77 * @throws NotFoundException
79 private function selectOne(array $condition, array $params = []): Entity\PermissionSet
81 return parent::_selectOne($condition, $params);
84 private function select(array $condition, array $params = []): Collection\PermissionSets
86 return new Collection\PermissionSets(parent::_select($condition, $params)->getArrayCopy());
90 * Converts a given PermissionSet into a DB compatible row array
92 * @param Entity\PermissionSet $permissionSet
96 protected function convertToTableRow(Entity\PermissionSet $permissionSet): array
99 'uid' => $permissionSet->uid,
100 'allow_cid' => $this->aclFormatter->toString($permissionSet->allow_cid),
101 'allow_gid' => $this->aclFormatter->toString($permissionSet->allow_gid),
102 'deny_cid' => $this->aclFormatter->toString($permissionSet->deny_cid),
103 'deny_gid' => $this->aclFormatter->toString($permissionSet->deny_gid),
108 * @param int $id A PermissionSet table row id or self::PUBLIC
109 * @param int $uid The owner of the PermissionSet
110 * @return Entity\PermissionSet
111 * @throws NotFoundException
113 public function selectOneById(int $id, int $uid): Entity\PermissionSet
115 if ($id === self::PUBLIC) {
116 return $this->factory->createFromString($uid);
119 return $this->selectOne(['id' => $id, 'uid' => $uid]);
123 * Returns a permission set collection for a given contact
125 * @param int $cid Contact id of the visitor
126 * @param int $uid User id whom the items belong, used for ownership check.
128 * @return Collection\PermissionSets
130 public function selectByContactId(int $cid, int $uid): Collection\PermissionSets
132 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
133 if (!empty($cdata)) {
134 $public_contact_str = $this->aclFormatter->toString($cdata['public']);
135 $user_contact_str = $this->aclFormatter->toString($cdata['user']);
136 $cid = $cdata['user'];
138 $public_contact_str = $this->aclFormatter->toString($cid);
139 $user_contact_str = '';
143 if (!empty($user_contact_str) && $this->db->exists('contact', [
148 $groups = Group::getIdsByContactId($cid);
151 $group_str = '<<>>'; // should be impossible to match
152 foreach ($groups as $group_id) {
153 $group_str .= '|<' . preg_quote($group_id) . '>';
156 if (!empty($user_contact_str)) {
157 $condition = ["`uid` = ? AND (NOT (`deny_cid` REGEXP ? OR `deny_cid` REGEXP ? OR deny_gid REGEXP ?)
158 AND (allow_cid REGEXP ? OR allow_cid REGEXP ? OR allow_gid REGEXP ? OR (allow_cid = '' AND allow_gid = '')))",
159 $uid, $user_contact_str, $public_contact_str, $group_str,
160 $user_contact_str, $public_contact_str, $group_str];
162 $condition = ["`uid` = ? AND (NOT (`deny_cid` REGEXP ? OR deny_gid REGEXP ?)
163 AND (allow_cid REGEXP ? OR allow_gid REGEXP ? OR (allow_cid = '' AND allow_gid = '')))",
164 $uid, $public_contact_str, $group_str, $public_contact_str, $group_str];
167 return $this->select($condition);
171 * Fetch the default PermissionSet for a given user, create it if it doesn't exist
175 * @return Entity\PermissionSet
178 public function selectDefaultForUser(int $uid): Entity\PermissionSet
180 $self_contact = Contact::selectFirst(['id'], ['uid' => $uid, 'self' => true]);
182 return $this->selectOrCreate($this->factory->createFromString(
184 $this->aclFormatter->toString($self_contact['id'])
189 * Fetch the public PermissionSet
193 * @return Entity\PermissionSet
195 public function selectPublicForUser(int $uid): Entity\PermissionSet
197 return $this->factory->createFromString($uid, '', '', '', '', self::PUBLIC);
201 * Selects or creates a PermissionSet based on it's fields
203 * @param Entity\PermissionSet $permissionSet
205 * @return Entity\PermissionSet
207 public function selectOrCreate(Entity\PermissionSet $permissionSet): Entity\PermissionSet
209 if ($permissionSet->id) {
210 return $permissionSet;
213 // Don't select/update Public permission sets
214 if ($permissionSet->isPublic()) {
215 return $this->selectPublicForUser($permissionSet->uid);
219 return $this->selectOne($this->convertToTableRow($permissionSet));
220 } catch (NotFoundException $exception) {
221 return $this->save($permissionSet);
226 * @param Entity\PermissionSet $permissionSet
228 * @return Entity\PermissionSet
229 * @throws NotFoundException
231 public function save(Entity\PermissionSet $permissionSet): Entity\PermissionSet
233 // Don't save/update the common public PermissionSet
234 if ($permissionSet->isPublic()) {
235 return $this->selectPublicForUser($permissionSet->uid);
238 $fields = $this->convertToTableRow($permissionSet);
240 if ($permissionSet->id) {
241 $this->db->update(self::$table_name, $fields, ['id' => $permissionSet->id]);
243 $this->db->insert(self::$table_name, $fields);
245 $permissionSet = $this->selectOneById($this->db->lastInsertId(), $permissionSet->uid);
248 return $permissionSet;