mirror of
https://github.com/friendica/friendica
synced 2025-05-12 07:04:09 +02:00
Adapt ProfileField & Fix current PermissionSet Usage at ProfileFields
This commit is contained in:
parent
8a354dac82
commit
5fecc9ecf7
11 changed files with 145 additions and 144 deletions
|
@ -35,7 +35,7 @@ class ProfileFields extends BaseCollection
|
|||
* @param callable $callback
|
||||
* @return ProfileFields
|
||||
*/
|
||||
public function map(callable $callback): self
|
||||
public function map(callable $callback): ProfileFields
|
||||
{
|
||||
return parent::map($callback);
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ class ProfileFields extends BaseCollection
|
|||
* @param int $flag
|
||||
* @return ProfileFields
|
||||
*/
|
||||
public function filter(callable $callback = null, int $flag = 0): self
|
||||
public function filter(callable $callback = null, int $flag = 0): ProfileFields
|
||||
{
|
||||
return parent::filter($callback, $flag);
|
||||
}
|
||||
|
|
|
@ -23,9 +23,10 @@ namespace Friendica\Profile\ProfileField\Depository;
|
|||
|
||||
use Friendica\BaseDepository;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Network\HTTPException\NotFoundException;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Profile\ProfileField\Exception\ProfileFieldNotFoundException;
|
||||
use Friendica\Profile\ProfileField\Exception\ProfileFieldPersistenceException;
|
||||
use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException;
|
||||
use Friendica\Profile\ProfileField\Factory;
|
||||
use Friendica\Profile\ProfileField\Entity;
|
||||
use Friendica\Profile\ProfileField\Collection;
|
||||
|
@ -40,6 +41,8 @@ class ProfileField extends BaseDepository
|
|||
|
||||
protected static $table_name = 'profile_field';
|
||||
|
||||
protected static $view_name = 'profile_field-view';
|
||||
|
||||
/** @var PermissionSetDepository */
|
||||
protected $permissionSetDepository;
|
||||
|
||||
|
@ -53,16 +56,20 @@ class ProfileField extends BaseDepository
|
|||
/**
|
||||
* @param array $condition
|
||||
* @param array $params
|
||||
*
|
||||
* @return Entity\ProfileField
|
||||
*
|
||||
* @throws ProfileFieldNotFoundException
|
||||
* @throws UnexpectedPermissionSetException
|
||||
*/
|
||||
private function selectOne(array $condition, array $params = []): Entity\ProfileField
|
||||
{
|
||||
try {
|
||||
return parent::_selectOne($condition, $params);
|
||||
} catch (NotFoundException $exception) {
|
||||
throw new ProfileFieldNotFoundException($exception->getMessage());
|
||||
$fields = $this->db->selectFirst(static::$view_name, [], $condition, $params);
|
||||
if (!$this->db->isResult($fields)) {
|
||||
throw new ProfileFieldNotFoundException();
|
||||
}
|
||||
|
||||
return $this->factory->createFromTableRow($fields);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -72,14 +79,19 @@ class ProfileField extends BaseDepository
|
|||
* @return Collection\ProfileFields
|
||||
*
|
||||
* @throws ProfileFieldPersistenceException In case of underlying persistence exceptions
|
||||
* @throws UnexpectedPermissionSetException
|
||||
*/
|
||||
private function select(array $condition, array $params = []): Collection\ProfileFields
|
||||
{
|
||||
try {
|
||||
return new Collection\ProfileFields(parent::_select($condition, $params)->getArrayCopy());
|
||||
} catch (\Exception $exception) {
|
||||
throw new ProfileFieldPersistenceException('Cannot select ProfileFields', $exception);
|
||||
$rows = $this->db->selectToArray(static::$view_name, [], $condition, $params);
|
||||
|
||||
$Entities = new Collection\ProfileFields();
|
||||
foreach ($rows as $fields) {
|
||||
$this->logger->warning('row', ['row' => $fields]);
|
||||
$Entities[] = $this->factory->createFromTableRow($fields);
|
||||
}
|
||||
|
||||
return $Entities;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -98,7 +110,7 @@ class ProfileField extends BaseDepository
|
|||
'order' => $profileField->order,
|
||||
'created' => $profileField->created->format(DateTimeFormat::MYSQL),
|
||||
'edited' => $profileField->edited->format(DateTimeFormat::MYSQL),
|
||||
'psid' => $profileField->permissionSetId
|
||||
'psid' => $profileField->permissionSet->id
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -170,7 +182,7 @@ class ProfileField extends BaseDepository
|
|||
*
|
||||
* @ProfileFieldNotFoundException In case there is no ProfileField found
|
||||
*/
|
||||
public function selectOnyById(int $id): Entity\ProfileField
|
||||
public function selectOneById(int $id): Entity\ProfileField
|
||||
{
|
||||
try {
|
||||
return $this->selectOne(['id' => $id]);
|
||||
|
@ -212,7 +224,7 @@ class ProfileField extends BaseDepository
|
|||
} else {
|
||||
$this->db->insert(self::$table_name, $fields);
|
||||
|
||||
$profileField = $this->selectOnyById($this->db->lastInsertId());
|
||||
$profileField = $this->selectOneById($this->db->lastInsertId());
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
throw new ProfileFieldPersistenceException(sprintf('Cannot save ProfileField with id "%d" and label "%s"', $profileField->id, $profileField->label), $exception);
|
||||
|
|
|
@ -23,10 +23,7 @@ namespace Friendica\Profile\ProfileField\Entity;
|
|||
|
||||
use Friendica\BaseEntity;
|
||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||
use Friendica\Network\HTTPException\NotFoundException;
|
||||
use Friendica\Profile\ProfileField\Exception\ProfileFieldNotFoundException;
|
||||
use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException;
|
||||
use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository;
|
||||
use Friendica\Security\PermissionSet\Entity\PermissionSet;
|
||||
|
||||
/**
|
||||
|
@ -38,7 +35,6 @@ use Friendica\Security\PermissionSet\Entity\PermissionSet;
|
|||
* @property-read int|null $id
|
||||
* @property-read int $uid
|
||||
* @property-read int $order
|
||||
* @property-read int $permissionSetId
|
||||
* @property-read string $label
|
||||
* @property-read string $value
|
||||
* @property-read \DateTime $created
|
||||
|
@ -51,14 +47,10 @@ class ProfileField extends BaseEntity
|
|||
protected $id;
|
||||
/** @var PermissionSet */
|
||||
protected $permissionSet;
|
||||
/** @var PermissionSetDepository */
|
||||
protected $permissionSetDepository;
|
||||
/** @var int */
|
||||
protected $uid;
|
||||
/** @var int */
|
||||
protected $order;
|
||||
/** @var int */
|
||||
protected $permissionSetId;
|
||||
/** @var string */
|
||||
protected $label;
|
||||
/** @var string */
|
||||
|
@ -68,54 +60,28 @@ class ProfileField extends BaseEntity
|
|||
/** @var \DateTime */
|
||||
protected $edited;
|
||||
|
||||
public function __construct(PermissionSetDepository $permissionSetDepository, int $uid, int $order, int $permissionSetId, string $label, string $value, \DateTime $created, \DateTime $edited, int $id = null, PermissionSet $permissionSet = null)
|
||||
public function __construct(int $uid, int $order, string $label, string $value, \DateTime $created, \DateTime $edited, PermissionSet $permissionSet, int $id = null)
|
||||
{
|
||||
$this->permissionSetDepository = $permissionSetDepository;
|
||||
$this->permissionSet = $permissionSet;
|
||||
|
||||
$this->uid = $uid;
|
||||
$this->order = $order;
|
||||
$this->permissionSetId = $permissionSetId;
|
||||
$this->label = $label;
|
||||
$this->value = $value;
|
||||
$this->created = $created;
|
||||
$this->edited = $edited;
|
||||
$this->id = $id;
|
||||
$this->permissionSet = $permissionSet;
|
||||
$this->uid = $uid;
|
||||
$this->order = $order;
|
||||
$this->label = $label;
|
||||
$this->value = $value;
|
||||
$this->created = $created;
|
||||
$this->edited = $edited;
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ProfileFieldNotFoundException
|
||||
* @throws UnexpectedPermissionSetException
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
switch ($name) {
|
||||
case 'permissionSet':
|
||||
if (empty($this->permissionSet)) {
|
||||
try {
|
||||
$permissionSet = $this->permissionSetDepository->selectOneById($this->permissionSetId, $this->uid);
|
||||
if ($permissionSet->uid !== $this->uid) {
|
||||
throw new UnexpectedPermissionSetException(sprintf('PermissionSet %d (user-id: %d) for ProfileField %d (user-id: %d) is invalid.', $permissionSet->id, $permissionSet->uid, $this->id, $this->uid));
|
||||
}
|
||||
|
||||
$this->permissionSet = $permissionSet;
|
||||
} catch (NotFoundException $exception) {
|
||||
throw new UnexpectedPermissionSetException(sprintf('No PermissionSet found for ProfileField %d (user-id: %d).', $this->id, $this->uid));
|
||||
}
|
||||
}
|
||||
|
||||
$return = $this->permissionSet;
|
||||
break;
|
||||
default:
|
||||
try {
|
||||
$return = parent::__get($name);
|
||||
} catch (InternalServerErrorException $exception) {
|
||||
throw new ProfileFieldNotFoundException($exception->getMessage());
|
||||
}
|
||||
break;
|
||||
try {
|
||||
return parent::__get($name);
|
||||
} catch (InternalServerErrorException $exception) {
|
||||
throw new ProfileFieldNotFoundException($exception->getMessage());
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -127,11 +93,10 @@ class ProfileField extends BaseEntity
|
|||
*/
|
||||
public function update(string $value, int $order, PermissionSet $permissionSet)
|
||||
{
|
||||
$this->value = $value;
|
||||
$this->order = $order;
|
||||
$this->permissionSet = $permissionSet;
|
||||
$this->permissionSetId = $permissionSet->id;
|
||||
$this->edited = new \DateTime('now', new \DateTimeZone('UTC'));
|
||||
$this->value = $value;
|
||||
$this->order = $order;
|
||||
$this->permissionSet = $permissionSet;
|
||||
$this->edited = new \DateTime('now', new \DateTimeZone('UTC'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -23,7 +23,7 @@ namespace Friendica\Profile\ProfileField\Factory;
|
|||
|
||||
use Friendica\BaseFactory;
|
||||
use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException;
|
||||
use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository;
|
||||
use Friendica\Security\PermissionSet\Factory\PermissionSet as PermissionSetFactory;
|
||||
use Friendica\Profile\ProfileField\Entity;
|
||||
use Friendica\Capabilities\ICanCreateFromTableRow;
|
||||
use Friendica\Security\PermissionSet\Entity\PermissionSet;
|
||||
|
@ -31,45 +31,68 @@ use Psr\Log\LoggerInterface;
|
|||
|
||||
class ProfileField extends BaseFactory implements ICanCreateFromTableRow
|
||||
{
|
||||
/** @var PermissionSetDepository */
|
||||
private $permissionSetDepository;
|
||||
/** @var PermissionSetFactory */
|
||||
private $permissionSetFactory;
|
||||
|
||||
public function __construct(LoggerInterface $logger, PermissionSetDepository $permissionSetDepository)
|
||||
public function __construct(LoggerInterface $logger, PermissionSetFactory $permissionSetFactory)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
|
||||
$this->permissionSetDepository = $permissionSetDepository;
|
||||
$this->permissionSetFactory = $permissionSetFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @throws UnexpectedPermissionSetException
|
||||
*/
|
||||
public function createFromTableRow(array $row, PermissionSet $permissionSet = null): Entity\ProfileField
|
||||
{
|
||||
if (empty($permissionSet) && empty($row['psid'])) {
|
||||
throw new UnexpectedPermissionSetException('Either set the permission set ID or the permission set itself');
|
||||
if (empty($permissionSet) &&
|
||||
(empty($row['psid']) || !array_key_exists('allow_cid', $row) || !array_key_exists('allow_gid', $row) || !array_key_exists('deny_cid', $row) || !array_key_exists('deny_gid', $row))
|
||||
) {
|
||||
throw new UnexpectedPermissionSetException('Either set the PermissionSet fields (join) or the PermissionSet itself');
|
||||
}
|
||||
|
||||
return new Entity\ProfileField(
|
||||
$this->permissionSetDepository,
|
||||
$row['uid'],
|
||||
$row['order'],
|
||||
$row['psid'] ?? $permissionSet->id,
|
||||
$row['label'],
|
||||
$row['value'],
|
||||
new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC')),
|
||||
new \DateTime($row['edited'] ?? 'now', new \DateTimeZone('UTC')),
|
||||
$row['id'] ?? null,
|
||||
$permissionSet
|
||||
$permissionSet ?? $this->permissionSetFactory->createFromString(
|
||||
$row['uid'],
|
||||
$row['allow_cid'],
|
||||
$row['allow_gid'],
|
||||
$row['deny_cid'],
|
||||
$row['deny_gid'],
|
||||
$row['psid']
|
||||
),
|
||||
$row['id'] ?? null
|
||||
);
|
||||
}
|
||||
|
||||
public function createFromString(
|
||||
/**
|
||||
* Creates a ProfileField instance based on it's values
|
||||
*
|
||||
* @param int $uid
|
||||
* @param int $order
|
||||
* @param string $label
|
||||
* @param string $value
|
||||
* @param PermissionSet $permissionSet
|
||||
* @param int|null $id
|
||||
*
|
||||
* @return Entity\ProfileField
|
||||
* @throws UnexpectedPermissionSetException
|
||||
*/
|
||||
public function createFromValues(
|
||||
int $uid,
|
||||
int $order,
|
||||
string $label,
|
||||
string $value,
|
||||
PermissionSet $permissionSet
|
||||
PermissionSet $permissionSet,
|
||||
int $id = null
|
||||
): Entity\ProfileField {
|
||||
return $this->createFromTableRow([
|
||||
'uid' => $uid,
|
||||
|
@ -77,6 +100,7 @@ class ProfileField extends BaseFactory implements ICanCreateFromTableRow
|
|||
'psid' => $permissionSet->id,
|
||||
'label' => $label,
|
||||
'value' => $value,
|
||||
'id' => $id,
|
||||
], $permissionSet);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue