streams/Code/Access/AccessControl.php

171 lines
6 KiB
PHP
Raw Normal View History

<?php
2022-02-16 04:08:28 +00:00
namespace Code\Access;
/**
2019-02-15 00:01:40 +00:00
* @brief AccessControl class which represents individual content ACLs.
*
2019-02-15 00:01:40 +00:00
* A class to hold an AccessControl object with allowed and denied contacts and
* groups.
*
2022-02-16 04:08:28 +00:00
* After evaluating @ref ::Code::Access::PermissionLimits "PermissionLimits"
* and @ref ::Code::Lib::Permcat "Permcat"s individual content ACLs are evaluated.
* These answer the question "Can Joe view *this* album/photo?".
*/
2021-12-02 23:02:31 +00:00
class AccessControl
{
/**
* @brief Allow contacts
* @var string
*/
2022-08-27 22:54:42 +00:00
protected string $allow_cid;
2021-12-02 23:02:31 +00:00
/**
* @brief Allow groups
* @var string
*/
2022-08-27 22:54:42 +00:00
protected string $allow_gid;
2021-12-02 23:02:31 +00:00
/**
* @brief Deny contacts
* @var string
*/
2022-08-27 22:54:42 +00:00
protected string $deny_cid;
2021-12-02 23:02:31 +00:00
/**
* @brief Deny groups
* @var string
*/
2022-08-27 22:54:42 +00:00
protected string $deny_gid;
2021-12-02 23:02:31 +00:00
/**
* @brief Indicates if we are using the default constructor values or
* values that have been set explicitly.
* @var bool
*/
2022-08-27 22:54:42 +00:00
protected bool $explicit;
2021-12-02 23:02:31 +00:00
/**
* @brief Constructor for AccessList class.
*
* @note The array to pass to the constructor is different from the array
* that you provide to the set() or set_from_array() functions.
*
* @param array $channel A channel array, where these entries are evaluated:
2022-08-27 22:54:42 +00:00
* * \e string \b channel_allow_cid => string of allowed xchan_hash
* * \e string \b channel_allow_gid => string of allowed group_id
* * \e string \b channel_deny_cid => string of denied xchan_hash
* * \e string \b channel_deny_gid => string of denied group_id
2021-12-02 23:02:31 +00:00
*/
public function __construct(mixed $channel)
2021-12-02 23:02:31 +00:00
{
if ($channel) {
$this->allow_cid = $channel['channel_allow_cid'];
$this->allow_gid = $channel['channel_allow_gid'];
$this->deny_cid = $channel['channel_deny_cid'];
$this->deny_gid = $channel['channel_deny_gid'];
} else {
$this->allow_cid = '';
$this->allow_gid = '';
$this->deny_cid = '';
$this->deny_gid = '';
}
2021-12-02 23:02:31 +00:00
$this->explicit = false;
}
2021-12-02 23:02:31 +00:00
/**
2022-09-04 22:39:58 +00:00
* @brief Determine if we are using the default constructor values
2021-12-02 23:02:31 +00:00
* or values that have been set explicitly.
*
* @return bool
2022-10-23 05:00:42 +00:00
* @noinspection PhpUnused
2021-12-02 23:02:31 +00:00
*/
2022-08-27 22:54:42 +00:00
public function get_explicit(): bool
2021-12-02 23:02:31 +00:00
{
return $this->explicit;
}
2021-12-02 23:02:31 +00:00
/**
* @brief Set access list from strings such as those in already
* existing stored data items.
*
* @note The array to pass to this set function is different from the array
* that you provide to the constructor or set_from_array().
*
* @param array $arr
2022-08-27 22:54:42 +00:00
* * \e string \b allow_cid => string of allowed xchan_hash
* * \e string \b allow_gid => string of allowed group_id
* * \e string \b deny_cid => string of denied xchan_hash
* * \e string \b deny_gid => string of denied group_id
2021-12-02 23:02:31 +00:00
* @param bool $explicit (optional) default true
*/
2022-08-27 22:54:42 +00:00
public function set(array $arr, bool $explicit = true): void
2021-12-02 23:02:31 +00:00
{
2022-09-04 02:29:05 +00:00
$this->allow_cid = (array_key_exists('allow_cid', $arr)) ? $arr['allow_cid'] : '';
$this->allow_gid = (array_key_exists('allow_gid', $arr)) ? $arr['allow_gid'] : '';
$this->deny_cid = (array_key_exists('deny_cid', $arr)) ? $arr['deny_cid'] : '';
$this->deny_gid = (array_key_exists('deny_gid', $arr)) ? $arr['deny_gid'] : '';
2021-12-02 23:02:31 +00:00
$this->explicit = $explicit;
}
2021-12-02 23:02:31 +00:00
/**
* @brief Return an array consisting of the current access list components
* where the elements are directly storable.
*
* @return array An associative array with:
2022-08-27 22:54:42 +00:00
* * \e string \b allow_cid => string of allowed xchan_hash
* * \e string \b allow_gid => string of allowed group_id
* * \e string \b deny_cid => string of denied xchan_hash
* * \e string \b deny_gid => string of denied group_id
2021-12-02 23:02:31 +00:00
*/
2022-08-27 22:54:42 +00:00
public function get(): array
2021-12-02 23:02:31 +00:00
{
return [
'allow_cid' => $this->allow_cid,
'allow_gid' => $this->allow_gid,
'deny_cid' => $this->deny_cid,
'deny_gid' => $this->deny_gid,
];
}
2021-12-02 23:02:31 +00:00
/**
* @brief Set access list components from arrays, such as those provided by
* acl_selector().
*
* For convenience, a string (or non-array) input is assumed to be a
* comma-separated list and auto-converted into an array.
*
* @note The array to pass to this set function is different from the array
* that you provide to the constructor or set().
*
* @param array $arr An associative array with:
2022-08-27 22:54:42 +00:00
* * \e array|string \b contact_allow => array of xchan_hash or comma-seperated string
* * \e array|string \b group_allow => array of group_id or comma-seperated string
* * \e array|string \b contact_deny => array of xchan_hash or comma-seperated string
* * \e array|string \b group_deny => array of group_id or comma-seperated string
2021-12-02 23:02:31 +00:00
* @param bool $explicit (optional) default true
*/
2022-08-27 22:54:42 +00:00
public function set_from_array(array $arr, bool $explicit = true): void
2021-12-02 23:02:31 +00:00
{
$this->allow_cid = perms2str((is_array($arr['contact_allow']))
? $arr['contact_allow'] : explode(',', $arr['contact_allow']));
$this->allow_gid = perms2str((is_array($arr['group_allow']))
? $arr['group_allow'] : explode(',', $arr['group_allow']));
$this->deny_cid = perms2str((is_array($arr['contact_deny']))
? $arr['contact_deny'] : explode(',', $arr['contact_deny']));
$this->deny_gid = perms2str((is_array($arr['group_deny']))
? $arr['group_deny'] : explode(',', $arr['group_deny']));
2021-12-02 23:02:31 +00:00
$this->explicit = $explicit;
}
2021-12-02 23:02:31 +00:00
/**
* @brief Returns true if any access lists component is set.
*
* @return bool Return true if any of allow_* deny_* values is set.
*/
2022-08-27 22:54:42 +00:00
public function is_private(): bool
2021-12-02 23:02:31 +00:00
{
2022-08-15 10:33:19 +00:00
return $this->allow_cid || $this->allow_gid || $this->deny_cid || $this->deny_gid;
2021-12-02 23:02:31 +00:00
}
}