2018-07-19 15:52:05 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file src/Model/PermissionSet.php
|
|
|
|
*/
|
2019-11-05 14:27:22 +01:00
|
|
|
|
2018-07-19 15:52:05 +02:00
|
|
|
namespace Friendica\Model;
|
|
|
|
|
2019-11-05 14:27:22 +01:00
|
|
|
use Friendica\Core\L10n;
|
2018-07-20 14:19:26 +02:00
|
|
|
use Friendica\Database\DBA;
|
2019-11-05 14:27:22 +01:00
|
|
|
use Friendica\DI;
|
|
|
|
use Friendica\Network\HTTPException;
|
2018-07-19 15:52:05 +02:00
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* functions for interacting with the permission set of an object (item, photo, event, ...)
|
2018-07-19 15:52:05 +02:00
|
|
|
*/
|
2019-12-15 23:28:01 +01:00
|
|
|
class PermissionSet
|
2018-07-19 15:52:05 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Fetch the id of a given permission set. Generate a new one when needed
|
|
|
|
*
|
2019-11-05 14:27:22 +01:00
|
|
|
* @param int $uid
|
|
|
|
* @param string|null $allow_cid Allowed contact IDs - empty = everyone
|
|
|
|
* @param string|null $allow_gid Allowed group IDs - empty = everyone
|
|
|
|
* @param string|null $deny_cid Disallowed contact IDs - empty = no one
|
|
|
|
* @param string|null $deny_gid Disallowed group IDs - empty = no one
|
2019-01-06 22:06:53 +01:00
|
|
|
* @return int id
|
2019-11-05 14:27:22 +01:00
|
|
|
* @throws HTTPException\InternalServerErrorException
|
2018-07-19 15:52:05 +02:00
|
|
|
*/
|
2019-11-05 14:27:22 +01:00
|
|
|
public static function getIdFromACL(
|
|
|
|
int $uid,
|
|
|
|
string $allow_cid = null,
|
|
|
|
string $allow_gid = null,
|
|
|
|
string $deny_cid = null,
|
|
|
|
string $deny_gid = null
|
|
|
|
) {
|
|
|
|
$ACLFormatter = DI::aclFormatter();
|
2018-07-19 15:52:05 +02:00
|
|
|
|
2019-11-05 14:27:22 +01:00
|
|
|
$allow_cid = $ACLFormatter->sanitize($allow_cid);
|
|
|
|
$allow_gid = $ACLFormatter->sanitize($allow_gid);
|
|
|
|
$deny_cid = $ACLFormatter->sanitize($deny_cid);
|
|
|
|
$deny_gid = $ACLFormatter->sanitize($deny_gid);
|
2018-07-19 15:52:05 +02:00
|
|
|
|
2019-11-05 14:27:22 +01:00
|
|
|
// Public permission
|
|
|
|
if (!$allow_cid && !$allow_gid && !$deny_cid && !$deny_gid) {
|
|
|
|
return 0;
|
2018-07-19 15:52:05 +02:00
|
|
|
}
|
2018-07-26 01:14:55 +02:00
|
|
|
|
2019-11-05 14:27:22 +01:00
|
|
|
$condition = [
|
|
|
|
'uid' => $uid,
|
|
|
|
'allow_cid' => $allow_cid,
|
|
|
|
'allow_gid' => $allow_gid,
|
|
|
|
'deny_cid' => $deny_cid,
|
|
|
|
'deny_gid' => $deny_gid
|
|
|
|
];
|
|
|
|
$permissionset = DBA::selectFirst('permissionset', ['id'], $condition);
|
2018-07-19 15:52:05 +02:00
|
|
|
|
2019-11-05 14:27:22 +01:00
|
|
|
if (DBA::isResult($permissionset)) {
|
|
|
|
$psid = $permissionset['id'];
|
|
|
|
} else {
|
|
|
|
if (DBA::insert('permissionset', $condition, true)) {
|
|
|
|
$psid = DBA::lastInsertId();
|
|
|
|
} else {
|
|
|
|
throw new HTTPException\InternalServerErrorException(L10n::t('Unable to create a new permission set.'));
|
|
|
|
}
|
2018-07-19 15:52:05 +02:00
|
|
|
}
|
|
|
|
|
2019-11-05 14:27:22 +01:00
|
|
|
return $psid;
|
2018-07-19 15:52:05 +02:00
|
|
|
}
|
2018-07-26 01:14:55 +02:00
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Returns a permission set for a given contact
|
2018-07-26 01:14:55 +02:00
|
|
|
*
|
|
|
|
* @param integer $uid User id whom the items belong
|
|
|
|
* @param integer $contact_id Contact id of the visitor
|
|
|
|
*
|
|
|
|
* @return array of permission set ids.
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Exception
|
2018-07-26 01:14:55 +02:00
|
|
|
*/
|
2019-09-28 07:37:24 +02:00
|
|
|
static public function get($uid, $contact_id)
|
2018-07-26 01:14:55 +02:00
|
|
|
{
|
2019-09-28 07:37:24 +02:00
|
|
|
if (DBA::exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) {
|
2018-07-26 01:14:55 +02:00
|
|
|
$groups = Group::getIdsByContactId($contact_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($groups) || !is_array($groups)) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-09-28 07:37:24 +02:00
|
|
|
|
2018-07-26 01:14:55 +02:00
|
|
|
$group_str = '<<>>'; // should be impossible to match
|
|
|
|
|
|
|
|
foreach ($groups as $g) {
|
|
|
|
$group_str .= '|<' . intval($g) . '>';
|
|
|
|
}
|
|
|
|
|
|
|
|
$contact_str = '<' . $contact_id . '>';
|
|
|
|
|
2019-09-28 22:42:33 +02:00
|
|
|
$condition = ["`uid` = ? AND (NOT (`deny_cid` REGEXP ? OR deny_gid REGEXP ?)
|
|
|
|
AND (allow_cid REGEXP ? OR allow_gid REGEXP ? OR (allow_cid = '' AND allow_gid = '')))",
|
|
|
|
$uid, $contact_str, $group_str, $contact_str, $group_str];
|
2018-07-26 01:14:55 +02:00
|
|
|
|
|
|
|
$ret = DBA::select('permissionset', ['id'], $condition);
|
|
|
|
$set = [];
|
|
|
|
while ($permission = DBA::fetch($ret)) {
|
|
|
|
$set[] = $permission['id'];
|
|
|
|
}
|
|
|
|
DBA::close($ret);
|
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
2018-07-19 15:52:05 +02:00
|
|
|
}
|