3 * @copyright Copyright (C) 2020, Friendica
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\Repository;
24 use Friendica\BaseRepository;
25 use Friendica\Collection;
26 use Friendica\Database\Database;
28 use Friendica\Model\Group;
29 use Friendica\Network\HTTPException;
30 use Friendica\Util\ACLFormatter;
31 use Psr\Log\LoggerInterface;
33 class PermissionSet extends BaseRepository
35 /** @var int Virtual permission set id for public permission */
38 protected static $table_name = 'permissionset';
40 protected static $model_class = Model\PermissionSet::class;
42 protected static $collection_class = Collection\PermissionSets::class;
44 /** @var ACLFormatter */
45 private $aclFormatter;
47 public function __construct(Database $dba, LoggerInterface $logger, ACLFormatter $aclFormatter)
49 parent::__construct($dba, $logger);
51 $this->aclFormatter = $aclFormatter;
56 * @return Model\PermissionSet
58 protected function create(array $data)
60 return new Model\PermissionSet($this->dba, $this->logger, $data);
64 * @param array $condition
65 * @return Model\PermissionSet
66 * @throws \Friendica\Network\HTTPException\NotFoundException
68 public function selectFirst(array $condition)
70 if (isset($condition['id']) && !$condition['id']) {
71 return $this->create([
73 'uid' => $condition['uid'] ?? 0,
81 return parent::selectFirst($condition);
85 * @param array $condition
86 * @param array $params
87 * @return Collection\PermissionSets
90 public function select(array $condition = [], array $params = [])
92 return parent::select($condition, $params);
96 * @param array $condition
97 * @param array $params
98 * @param int|null $max_id
99 * @param int|null $since_id
101 * @return Collection\PermissionSets
104 public function selectByBoundaries(array $condition = [], array $params = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT)
106 return parent::selectByBoundaries($condition, $params, $max_id, $since_id, $limit);
110 * Fetch the id of a given permission set. Generate a new one when needed
113 * @param string|null $allow_cid Allowed contact IDs - empty = everyone
114 * @param string|null $allow_gid Allowed group IDs - empty = everyone
115 * @param string|null $deny_cid Disallowed contact IDs - empty = no one
116 * @param string|null $deny_gid Disallowed group IDs - empty = no one
120 public function getIdFromACL(
122 string $allow_cid = null,
123 string $allow_gid = null,
124 string $deny_cid = null,
125 string $deny_gid = null
127 $allow_cid = $this->aclFormatter->sanitize($allow_cid);
128 $allow_gid = $this->aclFormatter->sanitize($allow_gid);
129 $deny_cid = $this->aclFormatter->sanitize($deny_cid);
130 $deny_gid = $this->aclFormatter->sanitize($deny_gid);
133 if (!$allow_cid && !$allow_gid && !$deny_cid && !$deny_gid) {
139 'allow_cid' => $allow_cid,
140 'allow_gid' => $allow_gid,
141 'deny_cid' => $deny_cid,
142 'deny_gid' => $deny_gid
146 $permissionset = $this->selectFirst($condition);
147 } catch(HTTPException\NotFoundException $exception) {
148 $permissionset = $this->insert($condition);
151 return $permissionset->id;
155 * Returns a permission set collection for a given contact
157 * @param integer $contact_id Contact id of the visitor
158 * @param integer $uid User id whom the items belong, used for ownership check.
160 * @return Collection\PermissionSets
163 public function selectByContactId($contact_id, $uid)
166 if ($this->dba->exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) {
167 $groups = Group::getIdsByContactId($contact_id);
170 $group_str = '<<>>'; // should be impossible to match
171 foreach ($groups as $group_id) {
172 $group_str .= '|<' . preg_quote($group_id) . '>';
175 $contact_str = '<' . $contact_id . '>';
177 $condition = ["`uid` = ? AND (NOT (`deny_cid` REGEXP ? OR deny_gid REGEXP ?)
178 AND (allow_cid REGEXP ? OR allow_gid REGEXP ? OR (allow_cid = '' AND allow_gid = '')))",
179 $uid, $contact_str, $group_str, $contact_str, $group_str];
181 return $this->select($condition);