mirror of
https://github.com/friendica/friendica
synced 2025-05-12 07:04:09 +02:00
Use ProfileField::selectPublicFieldsByUserId
This commit is contained in:
parent
1c0f92c382
commit
a9981c792e
9 changed files with 170 additions and 40 deletions
46
src/Profile/ProfileField/Collection/ProfileFields.php
Normal file
46
src/Profile/ProfileField/Collection/ProfileFields.php
Normal file
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Profile\ProfileField\Collection;
|
||||
|
||||
use Friendica\BaseCollection;
|
||||
|
||||
class ProfileFields extends BaseCollection
|
||||
{
|
||||
/**
|
||||
* @param callable $callback
|
||||
* @return ProfileFields
|
||||
*/
|
||||
public function map(callable $callback)
|
||||
{
|
||||
return parent::map($callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable|null $callback
|
||||
* @param int $flag
|
||||
* @return ProfileFields
|
||||
*/
|
||||
public function filter(callable $callback = null, int $flag = 0)
|
||||
{
|
||||
return parent::filter($callback, $flag);
|
||||
}
|
||||
}
|
56
src/Profile/ProfileField/Depository/ProfileField.php
Normal file
56
src/Profile/ProfileField/Depository/ProfileField.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Profile\ProfileField\Depository;
|
||||
|
||||
use Friendica\BaseDepository;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Network\HTTPException\NotFoundException;
|
||||
use Friendica\Profile\ProfileField\Factory;
|
||||
use Friendica\Profile\ProfileField\Entity;
|
||||
use Friendica\Profile\ProfileField\Collection;
|
||||
use Friendica\Security\PermissionSet\Depository\PermissionSet;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class ProfileField extends BaseDepository
|
||||
{
|
||||
/** @var Factory\ProfileField */
|
||||
protected $factory;
|
||||
|
||||
protected static $table_name = 'profile_field';
|
||||
|
||||
public function __construct(Database $database, LoggerInterface $logger, Factory\ProfileField $factory)
|
||||
{
|
||||
parent::__construct($database, $logger, $factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $condition
|
||||
* @param array $params
|
||||
* @return Entity\ProfileField
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
private function selectOne(array $condition, array $params = []): Entity\ProfileField
|
||||
{
|
||||
return parent::_selectOne($condition, $params);
|
||||
}
|
||||
|
||||
private function select(array $condition, array $params = []): Collection\ProfileFields
|
||||
{
|
||||
return new Collection\ProfileFields(parent::_select($condition, $params)->getArrayCopy());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all public available ProfileFields for a specific user
|
||||
*
|
||||
* @param int $uid the user id
|
||||
*
|
||||
* @return Collection\ProfileFields
|
||||
*/
|
||||
public function selectPublicFieldsByUserId(int $uid): Collection\ProfileFields
|
||||
{
|
||||
return $this->select([
|
||||
'uid' => $uid,
|
||||
'psid' => PermissionSet::PUBLIC,
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -21,12 +21,10 @@
|
|||
|
||||
namespace Friendica\Profile\ProfileField\Entity;
|
||||
|
||||
use Friendica\BaseModel;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Network\HTTPException\NotFoundException;
|
||||
use Friendica\BaseEntity;
|
||||
use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException;
|
||||
use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository;
|
||||
use Friendica\Security\PermissionSet\Entity\PermissionSet;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Custom profile field model class.
|
||||
|
@ -34,40 +32,61 @@ use Psr\Log\LoggerInterface;
|
|||
* Custom profile fields are user-created arbitrary profile fields that can be assigned a permission set to restrict its
|
||||
* display to specific Friendica contacts as it requires magic authentication to work.
|
||||
*
|
||||
* @property int uid
|
||||
* @property int order
|
||||
* @property int psid
|
||||
* @property string label
|
||||
* @property string value
|
||||
* @property string created
|
||||
* @property string edited
|
||||
* @property PermissionSet 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
|
||||
* @property-read \DateTime $edited
|
||||
* @property PermissionSet $permissionSet
|
||||
*/
|
||||
class ProfileField extends BaseModel
|
||||
class ProfileField extends BaseEntity
|
||||
{
|
||||
/** @var int|null */
|
||||
protected $id;
|
||||
/** @var PermissionSet */
|
||||
private $permissionSet;
|
||||
|
||||
protected $permissionSet;
|
||||
/** @var PermissionSetDepository */
|
||||
private $permissionSetDepository;
|
||||
protected $permissionSetDepository;
|
||||
/** @var int */
|
||||
protected $uid;
|
||||
/** @var int */
|
||||
protected $order;
|
||||
/** @var int */
|
||||
protected $psid;
|
||||
/** @var string */
|
||||
protected $label;
|
||||
/** @var string */
|
||||
protected $value;
|
||||
/** @var \DateTime */
|
||||
protected $created;
|
||||
/** @var \DateTime */
|
||||
protected $edited;
|
||||
|
||||
public function __construct(Database $dba, LoggerInterface $logger, PermissionSetDepository $permissionSetDepository, array $data = [])
|
||||
public function __construct(PermissionSetDepository $permissionSetDepository, int $uid, int $order, int $permissionSetId, string $label, string $value, \DateTime $created, \DateTime $edited, int $id = null)
|
||||
{
|
||||
parent::__construct($dba, $logger, $data);
|
||||
|
||||
$this->permissionSetDepository = $permissionSetDepository;
|
||||
|
||||
$this->uid = $uid;
|
||||
$this->order = $order;
|
||||
$this->psid = $permissionSetId;
|
||||
$this->label = $label;
|
||||
$this->value = $value;
|
||||
$this->created = $created;
|
||||
$this->edited = $edited;
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function __get($name)
|
||||
{
|
||||
$this->checkValid();
|
||||
|
||||
switch ($name) {
|
||||
case 'permissionSet':
|
||||
if (empty($this->permissionSet)) {
|
||||
$permissionSet = $this->permissionSetDepository->selectOneById($this->psid, $this->uid);
|
||||
if ($permissionSet->uid !== $this->uid) {
|
||||
throw new NotFoundException(sprintf('PermissionSet %d (user-id: %d) for ProfileField %d (user-id: %d) is invalid.', $permissionSet->id, $permissionSet->uid, $this->id, $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;
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Profile\ProfileField\Exception;
|
||||
|
||||
class UnexpectedPermissionSetException extends \Exception
|
||||
{
|
||||
}
|
39
src/Profile/ProfileField/Factory/ProfileField.php
Normal file
39
src/Profile/ProfileField/Factory/ProfileField.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Profile\ProfileField\Factory;
|
||||
|
||||
use Friendica\BaseFactory;
|
||||
use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository;
|
||||
use Friendica\Profile\ProfileField\Entity;
|
||||
use Friendica\Capabilities\ICanCreateFromTableRow;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class ProfileField extends BaseFactory implements ICanCreateFromTableRow
|
||||
{
|
||||
/** @var PermissionSetDepository */
|
||||
private $permissionSetDepository;
|
||||
|
||||
public function __construct(LoggerInterface $logger, PermissionSetDepository $permissionSetDepository)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
|
||||
$this->permissionSetDepository = $permissionSetDepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function createFromTableRow(array $row): Entity\ProfileField
|
||||
{
|
||||
return new Entity\ProfileField(
|
||||
$this->permissionSetDepository,
|
||||
$row['uid'],
|
||||
$row['order'],
|
||||
$row['psid'],
|
||||
$row['label'],
|
||||
$row['value'],
|
||||
new \DateTime($row['created'], new \DateTimeZone('UTC')),
|
||||
new \DateTime($row['edited'] ?? 'now', new \DateTimeZone('UTC'))
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue