From 1c0f92c38232947b21dd736c56760e1378698211 Mon Sep 17 00:00:00 2001 From: Philipp Date: Fri, 8 Oct 2021 19:17:27 +0200 Subject: [PATCH 01/22] Move ProfileField from Model to Entity --- src/Factory/Api/Mastodon/Field.php | 5 +++-- src/Module/Settings/Profile/Index.php | 2 +- .../ProfileField/Entity}/ProfileField.php | 2 +- src/Repository/ProfileField.php | 18 +++++++++++------- 4 files changed, 16 insertions(+), 11 deletions(-) rename src/{Model => Profile/ProfileField/Entity}/ProfileField.php (98%) diff --git a/src/Factory/Api/Mastodon/Field.php b/src/Factory/Api/Mastodon/Field.php index 6ff63fb1b..2e3302242 100644 --- a/src/Factory/Api/Mastodon/Field.php +++ b/src/Factory/Api/Mastodon/Field.php @@ -25,13 +25,14 @@ use Friendica\BaseFactory; use Friendica\Collection\Api\Mastodon\Fields; use Friendica\Collection\ProfileFields; use Friendica\Content\Text\BBCode; -use Friendica\Model\ProfileField; +use Friendica\Profile\ProfileField\Entity\ProfileField; use Friendica\Network\HTTPException; class Field extends BaseFactory { /** - * @param ProfileField $profileField + * @param \Friendica\Profile\ProfileField\Entity\ProfileField $profileField + * * @return \Friendica\Object\Api\Mastodon\Field * @throws HTTPException\InternalServerErrorException */ diff --git a/src/Module/Settings/Profile/Index.php b/src/Module/Settings/Profile/Index.php index c51393c98..a7b71851d 100644 --- a/src/Module/Settings/Profile/Index.php +++ b/src/Module/Settings/Profile/Index.php @@ -30,7 +30,7 @@ use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\Profile; -use Friendica\Model\ProfileField; +use Friendica\Profile\ProfileField\Entity\ProfileField; use Friendica\Model\User; use Friendica\Module\BaseSettings; use Friendica\Module\Security\Login; diff --git a/src/Model/ProfileField.php b/src/Profile/ProfileField/Entity/ProfileField.php similarity index 98% rename from src/Model/ProfileField.php rename to src/Profile/ProfileField/Entity/ProfileField.php index c5905f934..6703615b0 100644 --- a/src/Model/ProfileField.php +++ b/src/Profile/ProfileField/Entity/ProfileField.php @@ -19,7 +19,7 @@ * */ -namespace Friendica\Model; +namespace Friendica\Profile\ProfileField\Entity; use Friendica\BaseModel; use Friendica\Database\Database; diff --git a/src/Repository/ProfileField.php b/src/Repository/ProfileField.php index 40417be48..75221933b 100644 --- a/src/Repository/ProfileField.php +++ b/src/Repository/ProfileField.php @@ -36,7 +36,7 @@ class ProfileField extends BaseRepository { protected static $table_name = 'profile_field'; - protected static $model_class = Model\ProfileField::class; + protected static $model_class = \Friendica\Profile\ProfileField\Entity\ProfileField::class; protected static $collection_class = Collection\ProfileFields::class; @@ -58,7 +58,8 @@ class ProfileField extends BaseRepository /** * @param array $data - * @return Model\ProfileField + * + * @return \Friendica\Profile\ProfileField\Entity\ProfileField */ protected function create(array $data) { @@ -67,7 +68,8 @@ class ProfileField extends BaseRepository /** * @param array $condition - * @return Model\ProfileField + * + * @return \Friendica\Profile\ProfileField\Entity\ProfileField * @throws \Friendica\Network\HTTPException\NotFoundException */ public function selectFirst(array $condition) @@ -138,7 +140,8 @@ class ProfileField extends BaseRepository /** * @param array $fields - * @return Model\ProfileField|bool + * + * @return \Friendica\Profile\ProfileField\Entity\ProfileField|bool * @throws \Exception */ public function insert(array $fields) @@ -150,7 +153,8 @@ class ProfileField extends BaseRepository } /** - * @param Model\ProfileField $model + * @param \Friendica\Profile\ProfileField\Entity\ProfileField $model + * * @return bool * @throws \Exception */ @@ -202,7 +206,7 @@ class ProfileField extends BaseRepository unset($profileFieldOrder['new']); // Prunes profile field whose label has been emptied - $profileFields = $profileFields->filter(function (Model\ProfileField $profileField) use (&$profileFieldInputs, &$profileFieldOrder) { + $profileFields = $profileFields->filter(function (\Friendica\Profile\ProfileField\Entity\ProfileField $profileField) use (&$profileFieldInputs, &$profileFieldOrder) { $keepModel = !isset($profileFieldInputs[$profileField->id]) || !empty($profileFieldInputs[$profileField->id]['label']); if (!$keepModel) { @@ -218,7 +222,7 @@ class ProfileField extends BaseRepository $profileFieldOrder = array_flip(array_keys($profileFieldOrder)); // Update existing profile fields from form values - $profileFields = $profileFields->map(function (Model\ProfileField $profileField) use ($uid, &$profileFieldInputs, &$profileFieldOrder) { + $profileFields = $profileFields->map(function (\Friendica\Profile\ProfileField\Entity\ProfileField $profileField) use ($uid, &$profileFieldInputs, &$profileFieldOrder) { if (isset($profileFieldInputs[$profileField->id]) && isset($profileFieldOrder[$profileField->id])) { $psid = $this->permissionSet->selectOrCreate($this->permissionSetFactory->createFromString( $uid, From a9981c792e6c7937dafd5eafbc66782d873b0dfd Mon Sep 17 00:00:00 2001 From: Philipp Date: Fri, 8 Oct 2021 19:45:20 +0200 Subject: [PATCH 02/22] Use ProfileField::selectPublicFieldsByUserId --- src/DI.php | 5 ++ src/Factory/Api/Mastodon/Field.php | 2 +- src/Module/Api/Friendica/Profile/Show.php | 4 +- .../Collection/ProfileFields.php | 2 +- .../ProfileField/Depository/ProfileField.php | 56 +++++++++++++++++ .../ProfileField/Entity/ProfileField.php | 63 ++++++++++++------- .../UnexpectedPermissionSetException.php | 7 +++ .../ProfileField/Factory/ProfileField.php | 39 ++++++++++++ src/Repository/ProfileField.php | 32 +++++----- 9 files changed, 170 insertions(+), 40 deletions(-) rename src/{ => Profile/ProfileField}/Collection/ProfileFields.php (95%) create mode 100644 src/Profile/ProfileField/Depository/ProfileField.php create mode 100644 src/Profile/ProfileField/Exception/UnexpectedPermissionSetException.php create mode 100644 src/Profile/ProfileField/Factory/ProfileField.php diff --git a/src/DI.php b/src/DI.php index 9025feb47..e64c5cef2 100644 --- a/src/DI.php +++ b/src/DI.php @@ -460,6 +460,11 @@ abstract class DI return self::$dice->create(Repository\ProfileField::class); } + public static function profileFieldNew(): Profile\ProfileField\Depository\ProfileField + { + return self::$dice->create(Profile\ProfileField\Depository\ProfileField::class); + } + public static function notification(): Navigation\Notifications\Depository\Notification { return self::$dice->create(Navigation\Notifications\Depository\Notification::class); diff --git a/src/Factory/Api/Mastodon/Field.php b/src/Factory/Api/Mastodon/Field.php index 2e3302242..aade421bb 100644 --- a/src/Factory/Api/Mastodon/Field.php +++ b/src/Factory/Api/Mastodon/Field.php @@ -23,7 +23,7 @@ namespace Friendica\Factory\Api\Mastodon; use Friendica\BaseFactory; use Friendica\Collection\Api\Mastodon\Fields; -use Friendica\Collection\ProfileFields; +use Friendica\Profile\ProfileField\Collection\ProfileFields; use Friendica\Content\Text\BBCode; use Friendica\Profile\ProfileField\Entity\ProfileField; use Friendica\Network\HTTPException; diff --git a/src/Module/Api/Friendica/Profile/Show.php b/src/Module/Api/Friendica/Profile/Show.php index b1d4314b7..c0f3a3f8d 100644 --- a/src/Module/Api/Friendica/Profile/Show.php +++ b/src/Module/Api/Friendica/Profile/Show.php @@ -21,7 +21,7 @@ namespace Friendica\Module\Api\Friendica\Profile; -use Friendica\Collection\ProfileFields; +use Friendica\Profile\ProfileField\Collection\ProfileFields; use Friendica\Content\Text\BBCode; use Friendica\DI; use Friendica\Model\Contact; @@ -45,7 +45,7 @@ class Show extends BaseApi $profile = Profile::getByUID($uid); - $profileFields = DI::profileField()->select(['uid' => $uid, 'psid' => PermissionSet::PUBLIC]); + $profileFields = DI::profileFieldNew()->selectPublicFieldsByUserId($uid); $profile = self::formatProfile($profile, $profileFields); diff --git a/src/Collection/ProfileFields.php b/src/Profile/ProfileField/Collection/ProfileFields.php similarity index 95% rename from src/Collection/ProfileFields.php rename to src/Profile/ProfileField/Collection/ProfileFields.php index 9e706da1f..b235ad2ec 100644 --- a/src/Collection/ProfileFields.php +++ b/src/Profile/ProfileField/Collection/ProfileFields.php @@ -19,7 +19,7 @@ * */ -namespace Friendica\Collection; +namespace Friendica\Profile\ProfileField\Collection; use Friendica\BaseCollection; diff --git a/src/Profile/ProfileField/Depository/ProfileField.php b/src/Profile/ProfileField/Depository/ProfileField.php new file mode 100644 index 000000000..47abcbd14 --- /dev/null +++ b/src/Profile/ProfileField/Depository/ProfileField.php @@ -0,0 +1,56 @@ +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, + ]); + } +} diff --git a/src/Profile/ProfileField/Entity/ProfileField.php b/src/Profile/ProfileField/Entity/ProfileField.php index 6703615b0..ad122344f 100644 --- a/src/Profile/ProfileField/Entity/ProfileField.php +++ b/src/Profile/ProfileField/Entity/ProfileField.php @@ -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; diff --git a/src/Profile/ProfileField/Exception/UnexpectedPermissionSetException.php b/src/Profile/ProfileField/Exception/UnexpectedPermissionSetException.php new file mode 100644 index 000000000..677a75bef --- /dev/null +++ b/src/Profile/ProfileField/Exception/UnexpectedPermissionSetException.php @@ -0,0 +1,7 @@ +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')) + ); + } +} diff --git a/src/Repository/ProfileField.php b/src/Repository/ProfileField.php index 75221933b..878c6b2b4 100644 --- a/src/Repository/ProfileField.php +++ b/src/Repository/ProfileField.php @@ -23,7 +23,6 @@ namespace Friendica\Repository; use Friendica\BaseModel; use Friendica\BaseRepository; -use Friendica\Collection; use Friendica\Core\L10n; use Friendica\Database\Database; use Friendica\Database\DBA; @@ -38,7 +37,7 @@ class ProfileField extends BaseRepository protected static $model_class = \Friendica\Profile\ProfileField\Entity\ProfileField::class; - protected static $collection_class = Collection\ProfileFields::class; + protected static $collection_class = \Friendica\Profile\ProfileField\Collection\ProfileFields::class; /** @var PermissionSet */ private $permissionSet; @@ -80,7 +79,8 @@ class ProfileField extends BaseRepository /** * @param array $condition * @param array $params - * @return Collection\ProfileFields + * + * @return \Friendica\Profile\ProfileField\Collection\ProfileFields * @throws \Exception */ public function select(array $condition = [], array $params = []) @@ -94,7 +94,8 @@ class ProfileField extends BaseRepository * @param int|null $min_id * @param int|null $max_id * @param int $limit - * @return Collection\ProfileFields + * + * @return \Friendica\Profile\ProfileField\Collection\ProfileFields * @throws \Exception */ public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT) @@ -104,7 +105,8 @@ class ProfileField extends BaseRepository /** * @param int $uid Field owner user Id - * @return Collection\ProfileFields + * + * @return \Friendica\Profile\ProfileField\Collection\ProfileFields * @throws \Exception */ public function selectByUserId(int $uid) @@ -120,7 +122,8 @@ class ProfileField extends BaseRepository * * @param int $cid Private contact id, must be owned by $uid * @param int $uid Field owner user id - * @return Collection\ProfileFields + * + * @return \Friendica\Profile\ProfileField\Collection\ProfileFields * @throws \Exception */ public function selectByContactId(int $cid, int $uid) @@ -166,14 +169,15 @@ class ProfileField extends BaseRepository } /** - * @param int $uid User Id - * @param Collection\ProfileFields $profileFields Collection of existing profile fields - * @param array $profileFieldInputs Array of profile field form inputs indexed by profile field id - * @param array $profileFieldOrder List of profile field id in order - * @return Collection\ProfileFields + * @param int $uid User Id + * @param \Friendica\Profile\ProfileField\Collection\ProfileFields $profileFields Collection of existing profile fields + * @param array $profileFieldInputs Array of profile field form inputs indexed by profile field id + * @param array $profileFieldOrder List of profile field id in order + * + * @return \Friendica\Profile\ProfileField\Collection\ProfileFields * @throws \Exception */ - public function updateCollectionFromForm(int $uid, Collection\ProfileFields $profileFields, array $profileFieldInputs, array $profileFieldOrder) + public function updateCollectionFromForm(int $uid, \Friendica\Profile\ProfileField\Collection\ProfileFields $profileFields, array $profileFieldInputs, array $profileFieldOrder) { // Returns an associative array of id => order values $profileFieldOrder = array_flip($profileFieldOrder); @@ -232,8 +236,8 @@ class ProfileField extends BaseRepository $profileFieldInputs[$profileField->id]['group_deny'] ?? '' ))->id; - $profileField->psid = $psid; - $profileField->label = $profileFieldInputs[$profileField->id]['label']; + $profileField->permissionSetId = $psid; + $profileField->label = $profileFieldInputs[$profileField->id]['label']; $profileField->value = $profileFieldInputs[$profileField->id]['value']; $profileField->order = $profileFieldOrder[$profileField->id]; From 6f692b857b06fb9f5ef05db9bb3d5f272632986b Mon Sep 17 00:00:00 2001 From: Philipp Date: Fri, 8 Oct 2021 20:01:09 +0200 Subject: [PATCH 03/22] Move ProfileField::selectByContactId() and ProfileField::selectByUserId() --- src/Module/Profile/Profile.php | 4 +- src/Module/Settings/Profile/Index.php | 4 +- .../ProfileField/Depository/ProfileField.php | 47 +++++++++++++++++-- src/Protocol/ActivityPub/Transmitter.php | 2 +- src/Repository/ProfileField.php | 36 -------------- 5 files changed, 49 insertions(+), 44 deletions(-) diff --git a/src/Module/Profile/Profile.php b/src/Module/Profile/Profile.php index 25b6eccc9..1b8ca3435 100644 --- a/src/Module/Profile/Profile.php +++ b/src/Module/Profile/Profile.php @@ -213,9 +213,9 @@ class Profile extends BaseProfile $contact_id = $view_as_contact_id ?: $remote_contact_id ?: 0; if ($is_owner && $contact_id === 0) { - $profile_fields = DI::profileField()->selectByUserId($profile['uid']); + $profile_fields = DI::profileFieldNew()->selectByUserId($profile['uid']); } else { - $profile_fields = DI::profileField()->selectByContactId($contact_id, $profile['uid']); + $profile_fields = DI::profileFieldNew()->selectByContactId($contact_id, $profile['uid']); } foreach ($profile_fields as $profile_field) { diff --git a/src/Module/Settings/Profile/Index.php b/src/Module/Settings/Profile/Index.php index a7b71851d..1ab3e32fc 100644 --- a/src/Module/Settings/Profile/Index.php +++ b/src/Module/Settings/Profile/Index.php @@ -100,7 +100,7 @@ class Index extends BaseSettings $homepage = 'http://' . $homepage; } - $profileFields = DI::profileField()->selectByUserId(local_user()); + $profileFields = DI::profileFieldNew()->selectByUserId(local_user()); $profileFields = DI::profileField()->updateCollectionFromForm( local_user(), @@ -159,7 +159,7 @@ class Index extends BaseSettings $custom_fields = []; - $profileFields = DI::profileField()->selectByUserId(local_user()); + $profileFields = DI::profileFieldNew()->selectByUserId(local_user()); foreach ($profileFields as $profileField) { /** @var ProfileField $profileField */ $defaultPermissions = $profileField->permissionSet->withAllowedContacts( diff --git a/src/Profile/ProfileField/Depository/ProfileField.php b/src/Profile/ProfileField/Depository/ProfileField.php index 47abcbd14..9ec2d4fb4 100644 --- a/src/Profile/ProfileField/Depository/ProfileField.php +++ b/src/Profile/ProfileField/Depository/ProfileField.php @@ -8,7 +8,7 @@ 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 Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository; use Psr\Log\LoggerInterface; class ProfileField extends BaseDepository @@ -18,9 +18,14 @@ class ProfileField extends BaseDepository protected static $table_name = 'profile_field'; - public function __construct(Database $database, LoggerInterface $logger, Factory\ProfileField $factory) + /** @var PermissionSetDepository */ + protected $permissionSetDepository; + + public function __construct(Database $database, LoggerInterface $logger, Factory\ProfileField $factory, PermissionSetDepository $permissionSetDepository) { parent::__construct($database, $logger, $factory); + + $this->permissionSetDepository = $this->permissionSetDepository; } /** @@ -50,7 +55,43 @@ class ProfileField extends BaseDepository { return $this->select([ 'uid' => $uid, - 'psid' => PermissionSet::PUBLIC, + 'psid' => PermissionSetDepository::PUBLIC, ]); } + + /** + * @param int $uid Field owner user Id + * + * @throws \Exception + */ + public function selectByUserId(int $uid): Collection\ProfileFields + { + return $this->select( + ['uid' => $uid], + ['order' => ['order']] + ); + } + + /** + * Retrieve all custom profile field a given contact is able to access to, including public profile fields. + * + * @param int $cid Private contact id, must be owned by $uid + * @param int $uid Field owner user id + * + * @throws \Exception + */ + public function selectByContactId(int $cid, int $uid): Collection\ProfileFields + { + $permissionSets = $this->permissionSetDepository->selectByContactId($cid, $uid); + + $permissionSetIds = $permissionSets->column('id'); + + // Includes public custom fields + $permissionSetIds[] = PermissionSetDepository::PUBLIC; + + return $this->select( + ['uid' => $uid, 'psid' => $permissionSetIds], + ['order' => ['order']] + ); + } } diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index e741789da..462460350 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -381,7 +381,7 @@ class Transmitter $custom_fields = []; - foreach (DI::profileField()->selectByContactId(0, $uid) as $profile_field) { + foreach (DI::profileFieldNew()->selectByContactId(0, $uid) as $profile_field) { $custom_fields[] = [ 'type' => 'PropertyValue', 'name' => $profile_field->label, diff --git a/src/Repository/ProfileField.php b/src/Repository/ProfileField.php index 878c6b2b4..c7fe74ea1 100644 --- a/src/Repository/ProfileField.php +++ b/src/Repository/ProfileField.php @@ -103,43 +103,7 @@ class ProfileField extends BaseRepository return parent::selectByBoundaries($condition, $params, $min_id, $max_id, $limit); } - /** - * @param int $uid Field owner user Id - * - * @return \Friendica\Profile\ProfileField\Collection\ProfileFields - * @throws \Exception - */ - public function selectByUserId(int $uid) - { - return $this->select( - ['uid' => $uid], - ['order' => ['order']] - ); - } - /** - * Retrieve all custom profile field a given contact is able to access to, including public profile fields. - * - * @param int $cid Private contact id, must be owned by $uid - * @param int $uid Field owner user id - * - * @return \Friendica\Profile\ProfileField\Collection\ProfileFields - * @throws \Exception - */ - public function selectByContactId(int $cid, int $uid) - { - $permissionSets = $this->permissionSet->selectByContactId($cid, $uid); - - $psids = $permissionSets->column('id'); - - // Includes public custom fields - $psids[] = 0; - - return $this->select( - ['uid' => $uid, 'psid' => $psids], - ['order' => ['order']] - ); - } /** * @param array $fields From f4038519464dd76be79ebb859b955efc849fce93 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 10 Oct 2021 20:39:35 +0200 Subject: [PATCH 04/22] Move ProfileFieldRepository::updateCollectionFromForm() --- src/DI.php | 5 + src/Module/Settings/Profile/Index.php | 55 +++++- .../ProfileField/Collection/ProfileFields.php | 10 +- .../ProfileField/Depository/ProfileField.php | 171 ++++++++++++++++-- .../ProfileField/Entity/ProfileField.php | 57 +++++- .../ProfileFieldNotFoundException.php | 14 ++ .../ProfileFieldPersistenceException.php | 13 ++ .../ProfileField/Factory/ProfileField.php | 25 ++- src/Repository/ProfileField.php | 147 --------------- 9 files changed, 320 insertions(+), 177 deletions(-) create mode 100644 src/Profile/ProfileField/Exception/ProfileFieldNotFoundException.php create mode 100644 src/Profile/ProfileField/Exception/ProfileFieldPersistenceException.php diff --git a/src/DI.php b/src/DI.php index e64c5cef2..562532f5c 100644 --- a/src/DI.php +++ b/src/DI.php @@ -465,6 +465,11 @@ abstract class DI return self::$dice->create(Profile\ProfileField\Depository\ProfileField::class); } + public static function profileFieldFactory(): Profile\ProfileField\Factory\ProfileField + { + return self::$dice->create(Profile\ProfileField\Factory\ProfileField::class); + } + public static function notification(): Navigation\Notifications\Depository\Notification { return self::$dice->create(Navigation\Notifications\Depository\Notification::class); diff --git a/src/Module/Settings/Profile/Index.php b/src/Module/Settings/Profile/Index.php index 1ab3e32fc..28fe695ba 100644 --- a/src/Module/Settings/Profile/Index.php +++ b/src/Module/Settings/Profile/Index.php @@ -30,6 +30,7 @@ use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\Profile; +use Friendica\Profile\ProfileField\Collection\ProfileFields; use Friendica\Profile\ProfileField\Entity\ProfileField; use Friendica\Model\User; use Friendica\Module\BaseSettings; @@ -100,16 +101,13 @@ class Index extends BaseSettings $homepage = 'http://' . $homepage; } - $profileFields = DI::profileFieldNew()->selectByUserId(local_user()); - - $profileFields = DI::profileField()->updateCollectionFromForm( + $profileFieldsNew = self::getProfileFieldsFromInput( local_user(), - $profileFields, $_REQUEST['profile_field'], $_REQUEST['profile_field_order'] ); - DI::profileField()->saveCollection($profileFields); + DI::profileFieldNew()->saveCollectionForUser(local_user(), $profileFieldsNew); $result = Profile::update( [ @@ -265,6 +263,53 @@ class Index extends BaseSettings return $o; } + private static function getProfileFieldsFromInput(int $uid, array $profileFieldInputs, array $profileFieldOrder): ProfileFields + { + $profileFields = new ProfileFields(); + + // Returns an associative array of id => order values + $profileFieldOrder = array_flip($profileFieldOrder); + + // Creation of the new field + if (!empty($profileFieldInputs['new']['label'])) { + $permissionSet = DI::permissionSet()->selectOrCreate(DI::permissionSetFactory()->createFromString( + $uid, + $profileFieldInputs['new']['contact_allow'] ?? '', + $profileFieldInputs['new']['group_allow'] ?? '', + $profileFieldInputs['new']['contact_deny'] ?? '', + $profileFieldInputs['new']['group_deny'] ?? '' + )); + + $profileFields->append(DI::profileFieldFactory()->createFromString( + $uid, + $profileFieldOrder['new'], + $profileFieldInputs['new']['label'], + $profileFieldInputs['new']['value'], + $permissionSet + )); + } + + foreach ($profileFieldInputs as $id => $profileFieldInput) { + $permissionSet = DI::permissionSet()->selectOrCreate(DI::permissionSetFactory()->createFromString( + $uid, + $profileFieldInput['contact_allow'] ?? '', + $profileFieldInput['group_allow'] ?? '', + $profileFieldInput['contact_deny'] ?? '', + $profileFieldInput['group_deny'] ?? '' + )); + + $profileFields->append(DI::profileFieldFactory()->createFromString( + $uid, + $profileFieldOrder[$id], + $profileFieldInput['label'], + $profileFieldInput['value'], + $permissionSet + )); + } + + return $profileFields; + } + private static function cleanKeywords($keywords) { $keywords = str_replace(',', ' ', $keywords); diff --git a/src/Profile/ProfileField/Collection/ProfileFields.php b/src/Profile/ProfileField/Collection/ProfileFields.php index b235ad2ec..b954e3ba3 100644 --- a/src/Profile/ProfileField/Collection/ProfileFields.php +++ b/src/Profile/ProfileField/Collection/ProfileFields.php @@ -22,14 +22,20 @@ namespace Friendica\Profile\ProfileField\Collection; use Friendica\BaseCollection; +use Friendica\Profile\ProfileField\Entity; class ProfileFields extends BaseCollection { + public function current(): Entity\ProfileField + { + return parent::current(); + } + /** * @param callable $callback * @return ProfileFields */ - public function map(callable $callback) + public function map(callable $callback): self { return parent::map($callback); } @@ -39,7 +45,7 @@ class ProfileFields extends BaseCollection * @param int $flag * @return ProfileFields */ - public function filter(callable $callback = null, int $flag = 0) + public function filter(callable $callback = null, int $flag = 0): self { return parent::filter($callback, $flag); } diff --git a/src/Profile/ProfileField/Depository/ProfileField.php b/src/Profile/ProfileField/Depository/ProfileField.php index 9ec2d4fb4..8ae74fefa 100644 --- a/src/Profile/ProfileField/Depository/ProfileField.php +++ b/src/Profile/ProfileField/Depository/ProfileField.php @@ -5,6 +5,8 @@ namespace Friendica\Profile\ProfileField\Depository; use Friendica\BaseDepository; use Friendica\Database\Database; use Friendica\Network\HTTPException\NotFoundException; +use Friendica\Profile\ProfileField\Exception\ProfileFieldNotFoundException; +use Friendica\Profile\ProfileField\Exception\ProfileFieldPersistenceException; use Friendica\Profile\ProfileField\Factory; use Friendica\Profile\ProfileField\Entity; use Friendica\Profile\ProfileField\Collection; @@ -25,23 +27,57 @@ class ProfileField extends BaseDepository { parent::__construct($database, $logger, $factory); - $this->permissionSetDepository = $this->permissionSetDepository; + $this->permissionSetDepository = permissionSetDepository; } /** * @param array $condition * @param array $params * @return Entity\ProfileField - * @throws NotFoundException + * @throws ProfileFieldNotFoundException */ private function selectOne(array $condition, array $params = []): Entity\ProfileField { - return parent::_selectOne($condition, $params); + try { + return parent::_selectOne($condition, $params); + } catch (NotFoundException $exception) { + throw new ProfileFieldNotFoundException($exception->getMessage()); + } } + /** + * @param array $condition + * @param array $params + * + * @return Collection\ProfileFields + * + * @throws ProfileFieldPersistenceException In case of underlying persistence exceptions + */ private function select(array $condition, array $params = []): Collection\ProfileFields { - return new Collection\ProfileFields(parent::_select($condition, $params)->getArrayCopy()); + try { + return new Collection\ProfileFields(parent::_select($condition, $params)->getArrayCopy()); + } catch (\Exception $exception) { + throw new ProfileFieldPersistenceException('Cannot select ProfileFields', $exception); + } + } + + /** + * Converts a given ProfileField into a DB compatible row array + * + * @param Entity\ProfileField $profileField + * + * @return array + */ + protected function convertToTableRow(Entity\ProfileField $profileField): array + { + return [ + 'label' => $profileField->label, + 'value' => $profileField->value, + 'order' => $profileField->order, + 'created' => $profileField->created, + 'edited' => $profileField->edited, + ]; } /** @@ -50,26 +86,36 @@ class ProfileField extends BaseDepository * @param int $uid the user id * * @return Collection\ProfileFields + * + * @throws ProfileFieldPersistenceException In case of underlying persistence exceptions */ public function selectPublicFieldsByUserId(int $uid): Collection\ProfileFields { - return $this->select([ - 'uid' => $uid, - 'psid' => PermissionSetDepository::PUBLIC, - ]); + try { + return $this->select([ + 'uid' => $uid, + 'psid' => PermissionSetDepository::PUBLIC, + ]); + } catch (\Exception $exception) { + throw new ProfileFieldPersistenceException(sprintf('Cannot select public ProfileField for user "%d"', $uid), $exception); + } } /** * @param int $uid Field owner user Id * - * @throws \Exception + * @throws ProfileFieldPersistenceException In case of underlying persistence exceptions */ public function selectByUserId(int $uid): Collection\ProfileFields { - return $this->select( - ['uid' => $uid], - ['order' => ['order']] - ); + try { + return $this->select( + ['uid' => $uid], + ['order' => ['order']] + ); + } catch (\Exception $exception) { + throw new ProfileFieldPersistenceException(sprintf('Cannot select ProfileField for user "%d"', $uid), $exception); + } } /** @@ -94,4 +140,103 @@ class ProfileField extends BaseDepository ['order' => ['order']] ); } + + /** + * @param int $id + * + * @return Entity\ProfileField + * + * @ProfileFieldNotFoundException In case there is no ProfileField found + */ + public function selectOnyById(int $id): Entity\ProfileField + { + try { + return $this->selectOne(['id' => $id]); + } catch (\Exception $exception) { + throw new ProfileFieldNotFoundException(sprintf('Cannot find Profile "%s"', $id), $exception); + } + } + + /** + * Delets a whole collection of ProfileFields + * + * @param Collection\ProfileFields $profileFields + * + * @return bool + * @throws ProfileFieldPersistenceException in case the persistence layer cannot delete the ProfileFields + */ + public function deleteCollection(Collection\ProfileFields $profileFields): bool + { + try { + return $this->db->delete(self::$table_name, ['id' => $profileFields->column('id')]); + } catch (\Exception $exception) { + throw new ProfileFieldPersistenceException('Cannot delete ProfileFields', $exception); + } + } + + /** + * @param Entity\ProfileField $profileField + * + * @return Entity\ProfileField + * @throws ProfileFieldPersistenceException in case the persistence layer cannot save the ProfileField + */ + public function save(Entity\ProfileField $profileField): Entity\ProfileField + { + $fields = $this->convertToTableRow($profileField); + + try { + if ($profileField->id) { + $this->db->update(self::$table_name, $fields, ['id' => $profileField]); + } else { + $this->db->insert(self::$table_name, $fields); + + $profileField = $this->selectOnyById($this->db->lastInsertId()); + } + } catch (\Exception $exception) { + throw new ProfileFieldPersistenceException(sprintf('Cannot save ProfileField with id "%d" and label "%s"', $profileField->id, $profileField->label), $exception); + } + + return $profileField; + } + + public function saveCollectionForUser(int $uid, Collection\ProfileFields $profileFields): Collection\ProfileFields + { + $savedProfileFields = new Collection\ProfileFields(); + + $profileFieldsOld = $this->selectByUserId($uid); + + // Prunes profile field whose label has been emptied + $labels = $profileFields->column('label'); + $prunedProfileFieldsOld = $profileFieldsOld->filter(function (Entity\ProfileField $profileFieldOld) use ($labels) { + return array_search($profileFieldOld->label, $labels) === false; + }); + $this->deleteCollection($prunedProfileFieldsOld); + + // Update the order based on the new Profile Field Collection + $order = 0; + $labelProfileFieldsOld = $profileFieldsOld->column('id', 'label'); + + foreach ($profileFields as $profileField) { + // Update existing field (preserve + if (array_key_exists($profileField->label, $labelProfileFieldsOld)) { + $profileFieldOldId = $labelProfileFieldsOld[$profileField->label]; + /** @var Entity\ProfileField $foundProfileFieldOld */ + $foundProfileFieldOld = $profileFieldsOld[$profileFieldOldId]; + $foundProfileFieldOld->update( + $profileField->value, + $order, + $profileField->permissionSet + ); + + $savedProfileFields->append($this->save($foundProfileFieldOld)); + } else { + $profileField->setOrder($order); + $savedProfileFields->append($this->save($profileField)); + } + + $order++; + } + + return $savedProfileFields; + } } diff --git a/src/Profile/ProfileField/Entity/ProfileField.php b/src/Profile/ProfileField/Entity/ProfileField.php index ad122344f..deab955d3 100644 --- a/src/Profile/ProfileField/Entity/ProfileField.php +++ b/src/Profile/ProfileField/Entity/ProfileField.php @@ -22,6 +22,9 @@ 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; @@ -65,9 +68,10 @@ 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) + 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) { $this->permissionSetDepository = $permissionSetDepository; + $this->permissionSet = $permissionSet; $this->uid = $uid; $this->order = $order; @@ -79,26 +83,65 @@ class ProfileField extends BaseEntity $this->id = $id; } + /** + * @throws ProfileFieldNotFoundException + * @throws UnexpectedPermissionSetException + */ public function __get($name) { switch ($name) { case 'permissionSet': if (empty($this->permissionSet)) { - $permissionSet = $this->permissionSetDepository->selectOneById($this->psid, $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)); - } + try { + $permissionSet = $this->permissionSetDepository->selectOneById($this->psid, $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; + $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: - $return = parent::__get($name); + try { + $return = parent::__get($name); + } catch (InternalServerErrorException $exception) { + throw new ProfileFieldNotFoundException($exception->getMessage()); + } break; } return $return; } + + /** + * Updates a ProfileField + * + * @param string $value The current or changed value + * @param int $order The current or changed order + * @param PermissionSet $permissionSet The current or changed PermissionSet + */ + public function update(string $value, int $order, PermissionSet $permissionSet) + { + $this->value = $value; + $this->order = $order; + $this->permissionSet = $permissionSet; + $this->psid = $permissionSet->id; + $this->edited = new \DateTime('now', new \DateTimeZone('UTC')); + } + + /** + * Sets the order of the ProfileField + * + * @param int $order + */ + public function setOrder(int $order) + { + $this->order = $order; + $this->edited = new \DateTime('now', new \DateTimeZone('UTC')); + } } diff --git a/src/Profile/ProfileField/Exception/ProfileFieldNotFoundException.php b/src/Profile/ProfileField/Exception/ProfileFieldNotFoundException.php new file mode 100644 index 000000000..b204d9e54 --- /dev/null +++ b/src/Profile/ProfileField/Exception/ProfileFieldNotFoundException.php @@ -0,0 +1,14 @@ +permissionSetDepository, @@ -32,8 +33,26 @@ class ProfileField extends BaseFactory implements ICanCreateFromTableRow $row['psid'], $row['label'], $row['value'], - new \DateTime($row['created'], new \DateTimeZone('UTC')), - new \DateTime($row['edited'] ?? 'now', new \DateTimeZone('UTC')) + new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC')), + new \DateTime($row['edited'] ?? 'now', new \DateTimeZone('UTC')), + $row['id'], + $permissionSet ); } + + public function createFromString( + int $uid, + int $order, + string $label, + string $value, + PermissionSet $permissionSet + ): Entity\ProfileField { + return $this->createFromTableRow([ + 'uid' => $uid, + 'order' => $order, + 'psid' => $permissionSet->id, + 'label' => $label, + 'value' => $value, + ], $permissionSet); + } } diff --git a/src/Repository/ProfileField.php b/src/Repository/ProfileField.php index c7fe74ea1..3bb4a0b44 100644 --- a/src/Repository/ProfileField.php +++ b/src/Repository/ProfileField.php @@ -21,7 +21,6 @@ namespace Friendica\Repository; -use Friendica\BaseModel; use Friendica\BaseRepository; use Friendica\Core\L10n; use Friendica\Database\Database; @@ -55,56 +54,6 @@ class ProfileField extends BaseRepository $this->l10n = $l10n; } - /** - * @param array $data - * - * @return \Friendica\Profile\ProfileField\Entity\ProfileField - */ - protected function create(array $data) - { - return new Model\ProfileField($this->dba, $this->logger, $this->permissionSet, $data); - } - - /** - * @param array $condition - * - * @return \Friendica\Profile\ProfileField\Entity\ProfileField - * @throws \Friendica\Network\HTTPException\NotFoundException - */ - public function selectFirst(array $condition) - { - return parent::selectFirst($condition); - } - - /** - * @param array $condition - * @param array $params - * - * @return \Friendica\Profile\ProfileField\Collection\ProfileFields - * @throws \Exception - */ - public function select(array $condition = [], array $params = []) - { - return parent::select($condition, $params); - } - - /** - * @param array $condition - * @param array $params - * @param int|null $min_id - * @param int|null $max_id - * @param int $limit - * - * @return \Friendica\Profile\ProfileField\Collection\ProfileFields - * @throws \Exception - */ - public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT) - { - return parent::selectByBoundaries($condition, $params, $min_id, $max_id, $limit); - } - - - /** * @param array $fields * @@ -119,102 +68,6 @@ class ProfileField extends BaseRepository return parent::insert($fields); } - /** - * @param \Friendica\Profile\ProfileField\Entity\ProfileField $model - * - * @return bool - * @throws \Exception - */ - public function update(BaseModel $model) - { - $model->edited = DateTimeFormat::utcNow(); - - return parent::update($model); - } - - /** - * @param int $uid User Id - * @param \Friendica\Profile\ProfileField\Collection\ProfileFields $profileFields Collection of existing profile fields - * @param array $profileFieldInputs Array of profile field form inputs indexed by profile field id - * @param array $profileFieldOrder List of profile field id in order - * - * @return \Friendica\Profile\ProfileField\Collection\ProfileFields - * @throws \Exception - */ - public function updateCollectionFromForm(int $uid, \Friendica\Profile\ProfileField\Collection\ProfileFields $profileFields, array $profileFieldInputs, array $profileFieldOrder) - { - // Returns an associative array of id => order values - $profileFieldOrder = array_flip($profileFieldOrder); - - // Creation of the new field - if (!empty($profileFieldInputs['new']['label'])) { - $psid = $this->permissionSet->selectOrCreate($this->permissionSetFactory->createFromString( - $uid, - $profileFieldInputs['new']['contact_allow'] ?? '', - $profileFieldInputs['new']['group_allow'] ?? '', - $profileFieldInputs['new']['contact_deny'] ?? '', - $profileFieldInputs['new']['group_deny'] ?? '' - ))->id; - - $newProfileField = $this->insert([ - 'uid' => $uid, - 'label' => $profileFieldInputs['new']['label'], - 'value' => $profileFieldInputs['new']['value'], - 'psid' => $psid, - 'order' => $profileFieldOrder['new'], - ]); - - $profileFieldInputs[$newProfileField->id] = $profileFieldInputs['new']; - $profileFieldOrder[$newProfileField->id] = $profileFieldOrder['new']; - - $profileFields[] = $newProfileField; - } - - unset($profileFieldInputs['new']); - unset($profileFieldOrder['new']); - - // Prunes profile field whose label has been emptied - $profileFields = $profileFields->filter(function (\Friendica\Profile\ProfileField\Entity\ProfileField $profileField) use (&$profileFieldInputs, &$profileFieldOrder) { - $keepModel = !isset($profileFieldInputs[$profileField->id]) || !empty($profileFieldInputs[$profileField->id]['label']); - - if (!$keepModel) { - unset($profileFieldInputs[$profileField->id]); - unset($profileFieldOrder[$profileField->id]); - $this->delete($profileField); - } - - return $keepModel; - }); - - // Regenerates the order values if items were deleted - $profileFieldOrder = array_flip(array_keys($profileFieldOrder)); - - // Update existing profile fields from form values - $profileFields = $profileFields->map(function (\Friendica\Profile\ProfileField\Entity\ProfileField $profileField) use ($uid, &$profileFieldInputs, &$profileFieldOrder) { - if (isset($profileFieldInputs[$profileField->id]) && isset($profileFieldOrder[$profileField->id])) { - $psid = $this->permissionSet->selectOrCreate($this->permissionSetFactory->createFromString( - $uid, - $profileFieldInputs[$profileField->id]['contact_allow'] ?? '', - $profileFieldInputs[$profileField->id]['group_allow'] ?? '', - $profileFieldInputs[$profileField->id]['contact_deny'] ?? '', - $profileFieldInputs[$profileField->id]['group_deny'] ?? '' - ))->id; - - $profileField->permissionSetId = $psid; - $profileField->label = $profileFieldInputs[$profileField->id]['label']; - $profileField->value = $profileFieldInputs[$profileField->id]['value']; - $profileField->order = $profileFieldOrder[$profileField->id]; - - unset($profileFieldInputs[$profileField->id]); - unset($profileFieldOrder[$profileField->id]); - } - - return $profileField; - }); - - return $profileFields; - } - /** * Migrates a legacy profile to the new slimmer profile with extra custom fields. * Multi profiles are converted to ACl-protected custom fields and deleted. From 838cdac5d17cf71e7103215c65bd0b2a878254f9 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 10 Oct 2021 20:54:29 +0200 Subject: [PATCH 05/22] Move ProfileFieldRepository::migrateFromLegacyProfile() & delete old repository --- src/Core/UserImport.php | 3 +- src/DI.php | 10 +- src/Model/Profile.php | 83 ++++++++++++ src/Module/Api/Friendica/Profile/Show.php | 2 +- src/Module/Profile/Profile.php | 4 +- src/Module/Settings/Profile/Index.php | 4 +- src/Protocol/ActivityPub/Transmitter.php | 2 +- src/Repository/ProfileField.php | 149 ---------------------- update.php | 2 +- 9 files changed, 93 insertions(+), 166 deletions(-) delete mode 100644 src/Repository/ProfileField.php diff --git a/src/Core/UserImport.php b/src/Core/UserImport.php index c0725bee8..309e5c2a9 100644 --- a/src/Core/UserImport.php +++ b/src/Core/UserImport.php @@ -25,6 +25,7 @@ use Friendica\Database\DBA; use Friendica\Database\DBStructure; use Friendica\DI; use Friendica\Model\Photo; +use Friendica\Model\Profile; use Friendica\Object\Image; use Friendica\Security\PermissionSet\Depository\PermissionSet; use Friendica\Util\Strings; @@ -278,7 +279,7 @@ class UserImport $profile['id'] = DBA::lastInsertId(); } - DI::profileField()->migrateFromLegacyProfile($profile); + Profile::migrateFromLegacyProfile($profile); } $permissionSet = DI::permissionSet()->selectDefaultForUser($newuid); diff --git a/src/DI.php b/src/DI.php index 562532f5c..31542e3d5 100644 --- a/src/DI.php +++ b/src/DI.php @@ -452,15 +452,7 @@ abstract class DI return self::$dice->create(Security\PermissionSet\Factory\PermissionSet::class); } - /** - * @return Repository\ProfileField - */ - public static function profileField() - { - return self::$dice->create(Repository\ProfileField::class); - } - - public static function profileFieldNew(): Profile\ProfileField\Depository\ProfileField + public static function profileField(): Profile\ProfileField\Depository\ProfileField { return self::$dice->create(Profile\ProfileField\Depository\ProfileField::class); } diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 9987f8676..2baf455cf 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -38,6 +38,7 @@ use Friendica\DI; use Friendica\Network\HTTPException; use Friendica\Protocol\Activity; use Friendica\Protocol\Diaspora; +use Friendica\Security\PermissionSet\Entity\PermissionSet; use Friendica\Util\DateTimeFormat; use Friendica\Util\HTTPSignature; use Friendica\Util\Network; @@ -936,4 +937,86 @@ class Profile return ['total' => $total, 'entries' => $profiles]; } + + /** + * Migrates a legacy profile to the new slimmer profile with extra custom fields. + * Multi profiles are converted to ACl-protected custom fields and deleted. + * + * @param array $profile One profile array + * @throws \Exception + */ + public static function migrateFromLegacyProfile(array $profile) + { + // Already processed, aborting + if ($profile['is-default'] === null) { + return; + } + + $contacts = []; + + if (!$profile['is-default']) { + $contacts = Contact::selectToArray(['id'], [ + 'uid' => $profile['uid'], + 'profile-id' => $profile['id'] + ]); + if (!count($contacts)) { + // No contact visibility selected defaults to user-only permission + $contacts = Contact::selectToArray(['id'], ['uid' => $profile['uid'], 'self' => true]); + } + } + + $permissionSet = DI::permissionSet()->selectOrCreate( + new PermissionSet( + $profile['uid'], + array_column($contacts, 'id') ?? [] + ) + ); + + $order = 1; + + $custom_fields = [ + 'hometown' => DI::l10n()->t('Hometown:'), + 'marital' => DI::l10n()->t('Marital Status:'), + 'with' => DI::l10n()->t('With:'), + 'howlong' => DI::l10n()->t('Since:'), + 'sexual' => DI::l10n()->t('Sexual Preference:'), + 'politic' => DI::l10n()->t('Political Views:'), + 'religion' => DI::l10n()->t('Religious Views:'), + 'likes' => DI::l10n()->t('Likes:'), + 'dislikes' => DI::l10n()->t('Dislikes:'), + 'pdesc' => DI::l10n()->t('Title/Description:'), + 'summary' => DI::l10n()->t('Summary'), + 'music' => DI::l10n()->t('Musical interests'), + 'book' => DI::l10n()->t('Books, literature'), + 'tv' => DI::l10n()->t('Television'), + 'film' => DI::l10n()->t('Film/dance/culture/entertainment'), + 'interest' => DI::l10n()->t('Hobbies/Interests'), + 'romance' => DI::l10n()->t('Love/romance'), + 'work' => DI::l10n()->t('Work/employment'), + 'education' => DI::l10n()->t('School/education'), + 'contact' => DI::l10n()->t('Contact information and Social Networks'), + ]; + + foreach ($custom_fields as $field => $label) { + if (!empty($profile[$field]) && $profile[$field] > DBA::NULL_DATE && $profile[$field] > DBA::NULL_DATETIME) { + DI::profileField()->save(DI::profileFieldFactory()->createFromString( + $profile['uid'], + $order, + trim($label, ':'), + $profile[$field], + $permissionSet + )); + } + + $profile[$field] = null; + } + + if ($profile['is-default']) { + $profile['profile-name'] = null; + $profile['is-default'] = null; + DBA::update('profile', $profile, ['id' => $profile['id']]); + } else if (!empty($profile['id'])) { + DBA::delete('profile', ['id' => $profile['id']]); + } + } } diff --git a/src/Module/Api/Friendica/Profile/Show.php b/src/Module/Api/Friendica/Profile/Show.php index c0f3a3f8d..8102ac4bc 100644 --- a/src/Module/Api/Friendica/Profile/Show.php +++ b/src/Module/Api/Friendica/Profile/Show.php @@ -45,7 +45,7 @@ class Show extends BaseApi $profile = Profile::getByUID($uid); - $profileFields = DI::profileFieldNew()->selectPublicFieldsByUserId($uid); + $profileFields = DI::profileField()->selectPublicFieldsByUserId($uid); $profile = self::formatProfile($profile, $profileFields); diff --git a/src/Module/Profile/Profile.php b/src/Module/Profile/Profile.php index 1b8ca3435..25b6eccc9 100644 --- a/src/Module/Profile/Profile.php +++ b/src/Module/Profile/Profile.php @@ -213,9 +213,9 @@ class Profile extends BaseProfile $contact_id = $view_as_contact_id ?: $remote_contact_id ?: 0; if ($is_owner && $contact_id === 0) { - $profile_fields = DI::profileFieldNew()->selectByUserId($profile['uid']); + $profile_fields = DI::profileField()->selectByUserId($profile['uid']); } else { - $profile_fields = DI::profileFieldNew()->selectByContactId($contact_id, $profile['uid']); + $profile_fields = DI::profileField()->selectByContactId($contact_id, $profile['uid']); } foreach ($profile_fields as $profile_field) { diff --git a/src/Module/Settings/Profile/Index.php b/src/Module/Settings/Profile/Index.php index 28fe695ba..92d6356b8 100644 --- a/src/Module/Settings/Profile/Index.php +++ b/src/Module/Settings/Profile/Index.php @@ -107,7 +107,7 @@ class Index extends BaseSettings $_REQUEST['profile_field_order'] ); - DI::profileFieldNew()->saveCollectionForUser(local_user(), $profileFieldsNew); + DI::profileField()->saveCollectionForUser(local_user(), $profileFieldsNew); $result = Profile::update( [ @@ -157,7 +157,7 @@ class Index extends BaseSettings $custom_fields = []; - $profileFields = DI::profileFieldNew()->selectByUserId(local_user()); + $profileFields = DI::profileField()->selectByUserId(local_user()); foreach ($profileFields as $profileField) { /** @var ProfileField $profileField */ $defaultPermissions = $profileField->permissionSet->withAllowedContacts( diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 462460350..e741789da 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -381,7 +381,7 @@ class Transmitter $custom_fields = []; - foreach (DI::profileFieldNew()->selectByContactId(0, $uid) as $profile_field) { + foreach (DI::profileField()->selectByContactId(0, $uid) as $profile_field) { $custom_fields[] = [ 'type' => 'PropertyValue', 'name' => $profile_field->label, diff --git a/src/Repository/ProfileField.php b/src/Repository/ProfileField.php deleted file mode 100644 index 3bb4a0b44..000000000 --- a/src/Repository/ProfileField.php +++ /dev/null @@ -1,149 +0,0 @@ -. - * - */ - -namespace Friendica\Repository; - -use Friendica\BaseRepository; -use Friendica\Core\L10n; -use Friendica\Database\Database; -use Friendica\Database\DBA; -use Friendica\Model; -use Friendica\Security\PermissionSet\Depository\PermissionSet; -use Friendica\Util\DateTimeFormat; -use Psr\Log\LoggerInterface; - -class ProfileField extends BaseRepository -{ - protected static $table_name = 'profile_field'; - - protected static $model_class = \Friendica\Profile\ProfileField\Entity\ProfileField::class; - - protected static $collection_class = \Friendica\Profile\ProfileField\Collection\ProfileFields::class; - - /** @var PermissionSet */ - private $permissionSet; - /** @var \Friendica\Security\PermissionSet\Factory\PermissionSet */ - private $permissionSetFactory; - /** @var L10n */ - private $l10n; - - public function __construct(Database $dba, LoggerInterface $logger, PermissionSet $permissionSet, \Friendica\Security\PermissionSet\Factory\PermissionSet $permissionSetFactory, L10n $l10n) - { - parent::__construct($dba, $logger); - - $this->permissionSet = $permissionSet; - $this->permissionSetFactory = $permissionSetFactory; - $this->l10n = $l10n; - } - - /** - * @param array $fields - * - * @return \Friendica\Profile\ProfileField\Entity\ProfileField|bool - * @throws \Exception - */ - public function insert(array $fields) - { - $fields['created'] = DateTimeFormat::utcNow(); - $fields['edited'] = DateTimeFormat::utcNow(); - - return parent::insert($fields); - } - - /** - * Migrates a legacy profile to the new slimmer profile with extra custom fields. - * Multi profiles are converted to ACl-protected custom fields and deleted. - * - * @param array $profile Profile table row - * @throws \Exception - */ - public function migrateFromLegacyProfile(array $profile) - { - // Already processed, aborting - if ($profile['is-default'] === null) { - return; - } - - $contacts = []; - - if (!$profile['is-default']) { - $contacts = Model\Contact::selectToArray(['id'], ['uid' => $profile['uid'], 'profile-id' => $profile['id']]); - if (!count($contacts)) { - // No contact visibility selected defaults to user-only permission - $contacts = Model\Contact::selectToArray(['id'], ['uid' => $profile['uid'], 'self' => true]); - } - } - - $psid = $this->permissionSet->selectOrCreate( - new \Friendica\Security\PermissionSet\Entity\PermissionSet( - $profile['uid'], - array_column($contacts, 'id') ?? [] - ) - )->id; - - $order = 1; - - $custom_fields = [ - 'hometown' => $this->l10n->t('Hometown:'), - 'marital' => $this->l10n->t('Marital Status:'), - 'with' => $this->l10n->t('With:'), - 'howlong' => $this->l10n->t('Since:'), - 'sexual' => $this->l10n->t('Sexual Preference:'), - 'politic' => $this->l10n->t('Political Views:'), - 'religion' => $this->l10n->t('Religious Views:'), - 'likes' => $this->l10n->t('Likes:'), - 'dislikes' => $this->l10n->t('Dislikes:'), - 'pdesc' => $this->l10n->t('Title/Description:'), - 'summary' => $this->l10n->t('Summary'), - 'music' => $this->l10n->t('Musical interests'), - 'book' => $this->l10n->t('Books, literature'), - 'tv' => $this->l10n->t('Television'), - 'film' => $this->l10n->t('Film/dance/culture/entertainment'), - 'interest' => $this->l10n->t('Hobbies/Interests'), - 'romance' => $this->l10n->t('Love/romance'), - 'work' => $this->l10n->t('Work/employment'), - 'education' => $this->l10n->t('School/education'), - 'contact' => $this->l10n->t('Contact information and Social Networks'), - ]; - - foreach ($custom_fields as $field => $label) { - if (!empty($profile[$field]) && $profile[$field] > DBA::NULL_DATE && $profile[$field] > DBA::NULL_DATETIME) { - $this->insert([ - 'uid' => $profile['uid'], - 'psid' => $psid, - 'order' => $order++, - 'label' => trim($label, ':'), - 'value' => $profile[$field], - ]); - } - - $profile[$field] = null; - } - - if ($profile['is-default']) { - $profile['profile-name'] = null; - $profile['is-default'] = null; - $this->dba->update('profile', $profile, ['id' => $profile['id']]); - } elseif (!empty($profile['id'])) { - $this->dba->delete('profile', ['id' => $profile['id']]); - } - } -} diff --git a/update.php b/update.php index 6a53c83f1..d7598a33f 100644 --- a/update.php +++ b/update.php @@ -206,7 +206,7 @@ function update_1332() $profiles = DBA::select('profile', [], $condition); while ($profile = DBA::fetch($profiles)) { - DI::profileField()->migrateFromLegacyProfile($profile); + Profile::migrateFromLegacyProfile($profile); } DBA::close($profiles); From 7061e16b271e2b2127bad6a90f90e1b1a55eefc9 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 10 Oct 2021 20:57:23 +0200 Subject: [PATCH 06/22] Add copyright --- .../ProfileField/Depository/ProfileField.php | 19 +++++++++++++++++++ .../ProfileFieldNotFoundException.php | 19 +++++++++++++++++++ .../ProfileFieldPersistenceException.php | 19 +++++++++++++++++++ .../UnexpectedPermissionSetException.php | 19 +++++++++++++++++++ .../ProfileField/Factory/ProfileField.php | 19 +++++++++++++++++++ 5 files changed, 95 insertions(+) diff --git a/src/Profile/ProfileField/Depository/ProfileField.php b/src/Profile/ProfileField/Depository/ProfileField.php index 8ae74fefa..228fc7760 100644 --- a/src/Profile/ProfileField/Depository/ProfileField.php +++ b/src/Profile/ProfileField/Depository/ProfileField.php @@ -1,4 +1,23 @@ . + * + */ namespace Friendica\Profile\ProfileField\Depository; diff --git a/src/Profile/ProfileField/Exception/ProfileFieldNotFoundException.php b/src/Profile/ProfileField/Exception/ProfileFieldNotFoundException.php index b204d9e54..3ee209cd7 100644 --- a/src/Profile/ProfileField/Exception/ProfileFieldNotFoundException.php +++ b/src/Profile/ProfileField/Exception/ProfileFieldNotFoundException.php @@ -1,4 +1,23 @@ . + * + */ namespace Friendica\Profile\ProfileField\Exception; diff --git a/src/Profile/ProfileField/Exception/ProfileFieldPersistenceException.php b/src/Profile/ProfileField/Exception/ProfileFieldPersistenceException.php index fef4f76e6..f14e17a08 100644 --- a/src/Profile/ProfileField/Exception/ProfileFieldPersistenceException.php +++ b/src/Profile/ProfileField/Exception/ProfileFieldPersistenceException.php @@ -1,4 +1,23 @@ . + * + */ namespace Friendica\Profile\ProfileField\Exception; diff --git a/src/Profile/ProfileField/Exception/UnexpectedPermissionSetException.php b/src/Profile/ProfileField/Exception/UnexpectedPermissionSetException.php index 677a75bef..91220def6 100644 --- a/src/Profile/ProfileField/Exception/UnexpectedPermissionSetException.php +++ b/src/Profile/ProfileField/Exception/UnexpectedPermissionSetException.php @@ -1,4 +1,23 @@ . + * + */ namespace Friendica\Profile\ProfileField\Exception; diff --git a/src/Profile/ProfileField/Factory/ProfileField.php b/src/Profile/ProfileField/Factory/ProfileField.php index 77de7908a..caec34af0 100644 --- a/src/Profile/ProfileField/Factory/ProfileField.php +++ b/src/Profile/ProfileField/Factory/ProfileField.php @@ -1,4 +1,23 @@ . + * + */ namespace Friendica\Profile\ProfileField\Factory; From f273c27e3b662644a572ec7b45984e6be7957193 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 10 Oct 2021 22:45:25 +0200 Subject: [PATCH 07/22] Fixups --- src/BaseCollection.php | 19 +++++++++++++- src/Module/Settings/Profile/Index.php | 3 +++ .../ProfileField/Depository/ProfileField.php | 13 ++++++---- .../ProfileField/Entity/ProfileField.php | 15 ++++++++--- tests/Util/CollectionDouble.php | 9 +++++++ tests/Util/EntityDouble.php | 26 +++++++++++++++++++ tests/src/CollectionTest.php | 24 +++++++++++++++++ 7 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 tests/Util/CollectionDouble.php create mode 100644 tests/Util/EntityDouble.php create mode 100644 tests/src/CollectionTest.php diff --git a/src/BaseCollection.php b/src/BaseCollection.php index 1aa13ae96..d2212e244 100644 --- a/src/BaseCollection.php +++ b/src/BaseCollection.php @@ -87,7 +87,7 @@ class BaseCollection extends \ArrayIterator */ public function column($column, $index_key = null) { - return array_column($this->getArrayCopy(), $column, $index_key); + return array_column($this->getArrayCopy(true), $column, $index_key); } /** @@ -124,4 +124,21 @@ class BaseCollection extends \ArrayIterator { return new static(array_reverse($this->getArrayCopy()), $this->getTotalCount()); } + + /** + * @inheritDoc + * + * includes recursion for entity::toArray() function + * @see BaseEntity::toArray() + */ + public function getArrayCopy(bool $recursive = false): array + { + if (!$recursive) { + return parent::getArrayCopy(); + } + + return array_map(function ($item) { + return is_object($item) ? $item->toArray() : $item; + }, parent::getArrayCopy()); + } } diff --git a/src/Module/Settings/Profile/Index.php b/src/Module/Settings/Profile/Index.php index 92d6356b8..efb3ac9d3 100644 --- a/src/Module/Settings/Profile/Index.php +++ b/src/Module/Settings/Profile/Index.php @@ -287,6 +287,9 @@ class Index extends BaseSettings $profileFieldInputs['new']['value'], $permissionSet )); + + unset($profileFieldInputs['new']); + unset($profileFieldOrder['new']); } foreach ($profileFieldInputs as $id => $profileFieldInput) { diff --git a/src/Profile/ProfileField/Depository/ProfileField.php b/src/Profile/ProfileField/Depository/ProfileField.php index 228fc7760..590e3f45a 100644 --- a/src/Profile/ProfileField/Depository/ProfileField.php +++ b/src/Profile/ProfileField/Depository/ProfileField.php @@ -30,6 +30,7 @@ use Friendica\Profile\ProfileField\Factory; use Friendica\Profile\ProfileField\Entity; use Friendica\Profile\ProfileField\Collection; use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository; +use Friendica\Util\DateTimeFormat; use Psr\Log\LoggerInterface; class ProfileField extends BaseDepository @@ -46,7 +47,7 @@ class ProfileField extends BaseDepository { parent::__construct($database, $logger, $factory); - $this->permissionSetDepository = permissionSetDepository; + $this->permissionSetDepository = $permissionSetDepository; } /** @@ -91,11 +92,13 @@ class ProfileField extends BaseDepository protected function convertToTableRow(Entity\ProfileField $profileField): array { return [ + 'uid' => $profileField->uid, 'label' => $profileField->label, 'value' => $profileField->value, 'order' => $profileField->order, - 'created' => $profileField->created, - 'edited' => $profileField->edited, + 'created' => $profileField->created->format(DateTimeFormat::MYSQL), + 'edited' => $profileField->edited->format(DateTimeFormat::MYSQL), + 'psid' => $profileField->permissionSetId ]; } @@ -205,7 +208,7 @@ class ProfileField extends BaseDepository try { if ($profileField->id) { - $this->db->update(self::$table_name, $fields, ['id' => $profileField]); + $this->db->update(self::$table_name, $fields, ['id' => $profileField->id]); } else { $this->db->insert(self::$table_name, $fields); @@ -233,7 +236,7 @@ class ProfileField extends BaseDepository // Update the order based on the new Profile Field Collection $order = 0; - $labelProfileFieldsOld = $profileFieldsOld->column('id', 'label'); + $labelProfileFieldsOld = array_flip($profileFieldsOld->column('label')); foreach ($profileFields as $profileField) { // Update existing field (preserve diff --git a/src/Profile/ProfileField/Entity/ProfileField.php b/src/Profile/ProfileField/Entity/ProfileField.php index deab955d3..c46ad12de 100644 --- a/src/Profile/ProfileField/Entity/ProfileField.php +++ b/src/Profile/ProfileField/Entity/ProfileField.php @@ -58,7 +58,7 @@ class ProfileField extends BaseEntity /** @var int */ protected $order; /** @var int */ - protected $psid; + protected $permissionSetId; /** @var string */ protected $label; /** @var string */ @@ -68,6 +68,9 @@ class ProfileField extends BaseEntity /** @var \DateTime */ protected $edited; + /** + * @throws UnexpectedPermissionSetException In case no Permission Set can be retrieved + */ 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) { $this->permissionSetDepository = $permissionSetDepository; @@ -75,12 +78,16 @@ class ProfileField extends BaseEntity $this->uid = $uid; $this->order = $order; - $this->psid = $permissionSetId; + $this->permissionSetId = $permissionSetId ?? ($permissionSet ? $permissionSet->id : null); $this->label = $label; $this->value = $value; $this->created = $created; $this->edited = $edited; $this->id = $id; + + if (is_null($this->permissionSetId)) { + throw new UnexpectedPermissionSetException('Either set the permission set ID or the permission set itself'); + } } /** @@ -93,7 +100,7 @@ class ProfileField extends BaseEntity case 'permissionSet': if (empty($this->permissionSet)) { try { - $permissionSet = $this->permissionSetDepository->selectOneById($this->psid, $this->uid); + $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)); } @@ -130,7 +137,7 @@ class ProfileField extends BaseEntity $this->value = $value; $this->order = $order; $this->permissionSet = $permissionSet; - $this->psid = $permissionSet->id; + $this->permissionSetId = $permissionSet->id; $this->edited = new \DateTime('now', new \DateTimeZone('UTC')); } diff --git a/tests/Util/CollectionDouble.php b/tests/Util/CollectionDouble.php new file mode 100644 index 000000000..ed5e371e4 --- /dev/null +++ b/tests/Util/CollectionDouble.php @@ -0,0 +1,9 @@ +protString = $protString; + $this->protInt = $protInt; + $this->protDateTime = $protDateTime; + $this->privString = $privString; + } +} diff --git a/tests/src/CollectionTest.php b/tests/src/CollectionTest.php new file mode 100644 index 000000000..9e198c985 --- /dev/null +++ b/tests/src/CollectionTest.php @@ -0,0 +1,24 @@ +append(new EntityDouble('test', 23, new \DateTime('now', new \DateTimeZone('UTC')), 'privTest')); + $collection->append(new EntityDouble('test2', 25, new \DateTime('now', new \DateTimeZone('UTC')), 'privTest23')); + + self::assertEquals(['test', 'test2'], $collection->column('protString')); + self::assertEmpty($collection->column('privString')); + self::assertEquals([23,25], $collection->column('protInt')); + } +} From 7c0c102f9fd04ee1c6b29a6499ed01d87c39ebae Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 10 Oct 2021 23:30:21 +0200 Subject: [PATCH 08/22] Add tests --- .../ProfileField/Entity/ProfileField.php | 35 ++- .../ProfileField/Factory/ProfileField.php | 9 +- .../ProfileField/Entity/ProfileFieldTest.php | 223 ++++++++++++++++++ 3 files changed, 244 insertions(+), 23 deletions(-) create mode 100644 tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php diff --git a/src/Profile/ProfileField/Entity/ProfileField.php b/src/Profile/ProfileField/Entity/ProfileField.php index c46ad12de..f3cb33ae9 100644 --- a/src/Profile/ProfileField/Entity/ProfileField.php +++ b/src/Profile/ProfileField/Entity/ProfileField.php @@ -30,7 +30,7 @@ use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDe use Friendica\Security\PermissionSet\Entity\PermissionSet; /** - * Custom profile field model class. + * Custom profile field entity class. * * 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. @@ -68,26 +68,19 @@ class ProfileField extends BaseEntity /** @var \DateTime */ protected $edited; - /** - * @throws UnexpectedPermissionSetException In case no Permission Set can be retrieved - */ 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) { $this->permissionSetDepository = $permissionSetDepository; $this->permissionSet = $permissionSet; - $this->uid = $uid; - $this->order = $order; - $this->permissionSetId = $permissionSetId ?? ($permissionSet ? $permissionSet->id : null); - $this->label = $label; - $this->value = $value; - $this->created = $created; - $this->edited = $edited; - $this->id = $id; - - if (is_null($this->permissionSetId)) { - throw new UnexpectedPermissionSetException('Either set the permission set ID or the permission set itself'); - } + $this->uid = $uid; + $this->order = $order; + $this->permissionSetId = $permissionSetId; + $this->label = $label; + $this->value = $value; + $this->created = $created; + $this->edited = $edited; + $this->id = $id; } /** @@ -134,11 +127,11 @@ 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->permissionSetId = $permissionSet->id; + $this->edited = new \DateTime('now', new \DateTimeZone('UTC')); } /** diff --git a/src/Profile/ProfileField/Factory/ProfileField.php b/src/Profile/ProfileField/Factory/ProfileField.php index caec34af0..e7016ec28 100644 --- a/src/Profile/ProfileField/Factory/ProfileField.php +++ b/src/Profile/ProfileField/Factory/ProfileField.php @@ -22,6 +22,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\Profile\ProfileField\Entity; use Friendica\Capabilities\ICanCreateFromTableRow; @@ -45,16 +46,20 @@ class ProfileField extends BaseFactory implements ICanCreateFromTableRow */ 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'); + } + return new Entity\ProfileField( $this->permissionSetDepository, $row['uid'], $row['order'], - $row['psid'], + $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'], + $row['id'] ?? null, $permissionSet ); } diff --git a/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php b/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php new file mode 100644 index 000000000..40f06a089 --- /dev/null +++ b/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php @@ -0,0 +1,223 @@ +permissionSetDepository = \Mockery::mock(PermissionSetDepository::class); + $this->profileFieldFactory = new ProfileFieldFactory(new VoidLogger(), $this->permissionSetDepository); + $this->permissionSetFactory = new PermissionSetFactory(new VoidLogger(), new ACLFormatter()); + } + + public function dataEntity() + { + return [ + 'default' => [ + 'uid' => 23, + 'order' => 1, + 'psid' => 2, + 'label' => 'test', + 'value' => 'more', + 'created' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), + 'edited' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), + ], + 'withId' => [ + 'uid' => 23, + 'order' => 1, + 'psid' => 2, + 'label' => 'test', + 'value' => 'more', + 'created' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), + 'edited' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), + 'id' => 54, + ], + ]; + } + + /** + * @dataProvider dataEntity + */ + public function testEntity(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); + + self::assertEquals($uid, $entity->uid); + self::assertEquals($order, $entity->order); + self::assertEquals($psid, $entity->permissionSetId); + self::assertEquals($label, $entity->label); + self::assertEquals($value, $entity->value); + self::assertEquals($created, $entity->created); + self::assertEquals($edited, $entity->edited); + self::assertEquals($id, $entity->id); + } + + /** + * @dataProvider dataEntity + */ + public function testUpdate(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'id' => $psid]); + + $entity = $this->profileFieldFactory->createFromTableRow([ + 'uid' => $uid, + 'order' => $order, + 'psid' => $psid, + 'label' => $label, + 'value' => $value, + 'created' => $created->format(DateTimeFormat::MYSQL), + 'edited' => $edited->format(DateTimeFormat::MYSQL), + 'id' => $id, + ], $permissionSet); + + $permissionSetNew = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'allow_cid' => '<1>', + 'id' => 23]); + + $entity->update('updatedValue', 2345, $permissionSetNew); + + self::assertEquals($uid, $entity->uid); + self::assertEquals(2345, $entity->order); + self::assertEquals(23, $entity->permissionSetId); + self::assertEquals($label, $entity->label); + self::assertEquals('updatedValue', $entity->value); + self::assertEquals($created, $entity->created); + self::assertGreaterThan($edited, $entity->edited); + self::assertEquals($id, $entity->id); + } + + /** + * @dataProvider dataEntity + */ + public function testSetOrder(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'id' => $psid]); + + $entity = $this->profileFieldFactory->createFromTableRow([ + 'uid' => $uid, + 'order' => $order, + 'psid' => $psid, + 'label' => $label, + 'value' => $value, + 'created' => $created->format(DateTimeFormat::MYSQL), + 'edited' => $edited->format(DateTimeFormat::MYSQL), + 'id' => $id, + ], $permissionSet); + + $entity->setOrder(2345); + + self::assertEquals($uid, $entity->uid); + self::assertEquals(2345, $entity->order); + self::assertEquals($psid, $entity->permissionSetId); + self::assertEquals($label, $entity->label); + self::assertEquals($value, $entity->value); + self::assertEquals($created, $entity->created); + self::assertGreaterThan($edited, $entity->edited); + self::assertEquals($id, $entity->id); + } + + /** + * Test the exception because of a wrong property + * + * @dataProvider dataEntity + */ + public function testWrongGet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); + + self::expectException(ProfileFieldNotFoundException::class); + $entity->wrong; + } + + /** + * Test gathering the permissionset + * + * @dataProvider dataEntity + */ + public function testPermissionSet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); + $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => $uid, 'id' => $psid]); + + $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid)->andReturns($permissionSet); + + self::assertEquals($psid, $entity->permissionSet->id); + } + + /** + * Test the exception because of incompatible user id + * + * @dataProvider dataEntity + */ + public function testWrongPermissionSet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); + $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 12345, 'id' => $psid]); + + $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid)->andReturns($permissionSet); + + self::expectException(UnexpectedPermissionSetException::class); + self::expectExceptionMessage(sprintf('PermissionSet %d (user-id: %d) for ProfileField %d (user-id: %d) is invalid.', $psid, 12345, $id, $uid)); + $entity->permissionSet; + } + + /** + * Test the exception because of missing permission set + * + * @dataProvider dataEntity + */ + public function testMissingPermissionSet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); + + $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid) + ->andThrow(new NotFoundException('test')); + + self::expectException(UnexpectedPermissionSetException::class); + self::expectExceptionMessage(sprintf('No PermissionSet found for ProfileField %d (user-id: %d).', $id, $uid)); + $entity->permissionSet; + } + + /** + * Test the exception because the factory cannot find a permissionSet ID, nor the permissionSet itself + * + * @dataProvider dataEntity + */ + public function testMissingPermissionFactory(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + { + self::expectException(UnexpectedPermissionSetException::class); + self::expectExceptionMessage('Either set the permission set ID or the permission set itself'); + + $entity = $this->profileFieldFactory->createFromTableRow([ + 'uid' => $uid, + 'order' => $order, + 'label' => $label, + 'value' => $value, + 'created' => $created->format(DateTimeFormat::MYSQL), + 'edited' => $edited->format(DateTimeFormat::MYSQL), + 'id' => $id, + ]); + } +} From a22c373347adb100a03768a76b33a1b7cfc2768c Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 10 Oct 2021 23:32:16 +0200 Subject: [PATCH 09/22] make PHP-CS happy :-) --- .../src/Profile/ProfileField/Entity/ProfileFieldTest.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php b/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php index 40f06a089..ff52c7f08 100644 --- a/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php +++ b/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php @@ -45,7 +45,7 @@ class ProfileFieldTest extends MockedTest 'created' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), 'edited' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), ], - 'withId' => [ + 'withId' => [ 'uid' => 23, 'order' => 1, 'psid' => 2, @@ -93,8 +93,11 @@ class ProfileFieldTest extends MockedTest 'id' => $id, ], $permissionSet); - $permissionSetNew = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'allow_cid' => '<1>', - 'id' => 23]); + $permissionSetNew = $this->permissionSetFactory->createFromTableRow([ + 'uid' => 2, + 'allow_cid' => '<1>', + 'id' => 23 + ]); $entity->update('updatedValue', 2345, $permissionSetNew); From 8a354dac8269d788f3f8ea90732530eeacf53d98 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 17 Oct 2021 12:53:26 +0200 Subject: [PATCH 10/22] Fixup ProfileField Test (because of PermissionSet fix) --- tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php b/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php index ff52c7f08..845c89cc6 100644 --- a/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php +++ b/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php @@ -164,7 +164,7 @@ class ProfileFieldTest extends MockedTest $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => $uid, 'id' => $psid]); - $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid)->andReturns($permissionSet); + $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid, $uid)->andReturns($permissionSet); self::assertEquals($psid, $entity->permissionSet->id); } @@ -179,7 +179,7 @@ class ProfileFieldTest extends MockedTest $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 12345, 'id' => $psid]); - $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid)->andReturns($permissionSet); + $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid, $uid)->andReturns($permissionSet); self::expectException(UnexpectedPermissionSetException::class); self::expectExceptionMessage(sprintf('PermissionSet %d (user-id: %d) for ProfileField %d (user-id: %d) is invalid.', $psid, 12345, $id, $uid)); @@ -195,7 +195,7 @@ class ProfileFieldTest extends MockedTest { $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); - $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid) + $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid, $uid) ->andThrow(new NotFoundException('test')); self::expectException(UnexpectedPermissionSetException::class); From 5fecc9ecf7e837f8e952c48d447fc8c5e6709adf Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 17 Oct 2021 15:32:50 +0200 Subject: [PATCH 11/22] Adapt ProfileField & Fix current PermissionSet Usage at ProfileFields --- src/Core/UserImport.php | 2 +- src/Model/Profile.php | 4 +- src/Module/Settings/Profile/Index.php | 20 ++--- .../ProfileField/Collection/ProfileFields.php | 4 +- .../ProfileField/Depository/ProfileField.php | 36 +++++--- .../ProfileField/Entity/ProfileField.php | 69 ++++------------ .../ProfileField/Factory/ProfileField.php | 50 ++++++++--- .../PermissionSet/Factory/PermissionSet.php | 4 +- static/dbview.config.php | 16 ++++ .../ProfileField/Entity/ProfileFieldTest.php | 82 ++++++++----------- update.php | 2 +- 11 files changed, 145 insertions(+), 144 deletions(-) diff --git a/src/Core/UserImport.php b/src/Core/UserImport.php index 309e5c2a9..4ac59d14c 100644 --- a/src/Core/UserImport.php +++ b/src/Core/UserImport.php @@ -279,7 +279,7 @@ class UserImport $profile['id'] = DBA::lastInsertId(); } - Profile::migrateFromLegacyProfile($profile); + Profile::migrate($profile); } $permissionSet = DI::permissionSet()->selectDefaultForUser($newuid); diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 2baf455cf..d089ada3c 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -945,7 +945,7 @@ class Profile * @param array $profile One profile array * @throws \Exception */ - public static function migrateFromLegacyProfile(array $profile) + public static function migrate(array $profile) { // Already processed, aborting if ($profile['is-default'] === null) { @@ -999,7 +999,7 @@ class Profile foreach ($custom_fields as $field => $label) { if (!empty($profile[$field]) && $profile[$field] > DBA::NULL_DATE && $profile[$field] > DBA::NULL_DATETIME) { - DI::profileField()->save(DI::profileFieldFactory()->createFromString( + DI::profileField()->save(DI::profileFieldFactory()->createFromValues( $profile['uid'], $order, trim($label, ':'), diff --git a/src/Module/Settings/Profile/Index.php b/src/Module/Settings/Profile/Index.php index efb3ac9d3..7ebf5b712 100644 --- a/src/Module/Settings/Profile/Index.php +++ b/src/Module/Settings/Profile/Index.php @@ -274,13 +274,13 @@ class Index extends BaseSettings if (!empty($profileFieldInputs['new']['label'])) { $permissionSet = DI::permissionSet()->selectOrCreate(DI::permissionSetFactory()->createFromString( $uid, - $profileFieldInputs['new']['contact_allow'] ?? '', - $profileFieldInputs['new']['group_allow'] ?? '', - $profileFieldInputs['new']['contact_deny'] ?? '', - $profileFieldInputs['new']['group_deny'] ?? '' + DI::aclFormatter()->toString($profileFieldInputs['new']['contact_allow'] ?? ''), + DI::aclFormatter()->toString($profileFieldInputs['new']['group_allow'] ?? ''), + DI::aclFormatter()->toString($profileFieldInputs['new']['contact_deny'] ?? ''), + DI::aclFormatter()->toString($profileFieldInputs['new']['group_deny'] ?? '') )); - $profileFields->append(DI::profileFieldFactory()->createFromString( + $profileFields->append(DI::profileFieldFactory()->createFromValues( $uid, $profileFieldOrder['new'], $profileFieldInputs['new']['label'], @@ -295,13 +295,13 @@ class Index extends BaseSettings foreach ($profileFieldInputs as $id => $profileFieldInput) { $permissionSet = DI::permissionSet()->selectOrCreate(DI::permissionSetFactory()->createFromString( $uid, - $profileFieldInput['contact_allow'] ?? '', - $profileFieldInput['group_allow'] ?? '', - $profileFieldInput['contact_deny'] ?? '', - $profileFieldInput['group_deny'] ?? '' + DI::aclFormatter()->toString($profileFieldInput['contact_allow'] ?? ''), + DI::aclFormatter()->toString($profileFieldInput['group_allow'] ?? ''), + DI::aclFormatter()->toString($profileFieldInput['contact_deny'] ?? ''), + DI::aclFormatter()->toString($profileFieldInput['group_deny'] ?? '') )); - $profileFields->append(DI::profileFieldFactory()->createFromString( + $profileFields->append(DI::profileFieldFactory()->createFromValues( $uid, $profileFieldOrder[$id], $profileFieldInput['label'], diff --git a/src/Profile/ProfileField/Collection/ProfileFields.php b/src/Profile/ProfileField/Collection/ProfileFields.php index b954e3ba3..21af7397c 100644 --- a/src/Profile/ProfileField/Collection/ProfileFields.php +++ b/src/Profile/ProfileField/Collection/ProfileFields.php @@ -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); } diff --git a/src/Profile/ProfileField/Depository/ProfileField.php b/src/Profile/ProfileField/Depository/ProfileField.php index 590e3f45a..0ca427a1a 100644 --- a/src/Profile/ProfileField/Depository/ProfileField.php +++ b/src/Profile/ProfileField/Depository/ProfileField.php @@ -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); diff --git a/src/Profile/ProfileField/Entity/ProfileField.php b/src/Profile/ProfileField/Entity/ProfileField.php index f3cb33ae9..b81437d9a 100644 --- a/src/Profile/ProfileField/Entity/ProfileField.php +++ b/src/Profile/ProfileField/Entity/ProfileField.php @@ -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')); } /** diff --git a/src/Profile/ProfileField/Factory/ProfileField.php b/src/Profile/ProfileField/Factory/ProfileField.php index e7016ec28..4253d93b9 100644 --- a/src/Profile/ProfileField/Factory/ProfileField.php +++ b/src/Profile/ProfileField/Factory/ProfileField.php @@ -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); } } diff --git a/src/Security/PermissionSet/Factory/PermissionSet.php b/src/Security/PermissionSet/Factory/PermissionSet.php index 84c7d5838..bde6cd73f 100644 --- a/src/Security/PermissionSet/Factory/PermissionSet.php +++ b/src/Security/PermissionSet/Factory/PermissionSet.php @@ -51,7 +51,8 @@ class PermissionSet extends BaseFactory implements ICanCreateFromTableRow string $allow_cid = '', string $allow_gid = '', string $deny_cid = '', - string $deny_gid = ''): Entity\PermissionSet + string $deny_gid = '', + int $id = null): Entity\PermissionSet { return $this->createFromTableRow([ 'uid' => $uid, @@ -59,6 +60,7 @@ class PermissionSet extends BaseFactory implements ICanCreateFromTableRow 'allow_gid' => $allow_gid, 'deny_cid' => $deny_cid, 'deny_gid' => $deny_gid, + 'id' => $id, ]); } } diff --git a/static/dbview.config.php b/static/dbview.config.php index 662425b02..a373f4c28 100644 --- a/static/dbview.config.php +++ b/static/dbview.config.php @@ -1047,5 +1047,21 @@ INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` WHERE NOT `workerqueue`.`done`" ], + "profile_field-view" => [ + "fields" => [ + "id" => ["profile_field", "id"], + "uid" => ["profile_field", "uid"], + "label" => ["profile_field", "label"], + "value" => ["profile_field", "value"], + "order" => ["profile_field", "order"], + "psid"=> ["profile_field", "psid"], + "allow_cid" => ["permissionset", "allow_cid"], + "allow_gid" => ["permissionset", "allow_gid"], + "deny_cid" => ["permissionset", "deny_cid"], + "deny_gid" => ["permissionset", "deny_gid"] + ], + "query" => "FROM `profile_field` + INNER JOIN `permissionset` ON `permissionset`.`id` = `profile_field`.`psid`" + ], ]; diff --git a/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php b/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php index 845c89cc6..e4f29d686 100644 --- a/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php +++ b/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php @@ -2,7 +2,6 @@ namespace Friendica\Test\src\Profile\ProfileField\Entity; -use Friendica\Network\HTTPException\NotFoundException; use Friendica\Profile\ProfileField\Entity\ProfileField; use Friendica\Profile\ProfileField\Exception\ProfileFieldNotFoundException; use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException; @@ -21,7 +20,7 @@ class ProfileFieldTest extends MockedTest protected $permissionSetDepository; /** @var ProfileFieldFactory */ protected $profileFieldFactory; - /** @var PermissionSetFactory */ + /** @var MockInterface|PermissionSetFactory */ protected $permissionSetFactory; protected function setUp(): void @@ -29,8 +28,8 @@ class ProfileFieldTest extends MockedTest parent::setUp(); $this->permissionSetDepository = \Mockery::mock(PermissionSetDepository::class); - $this->profileFieldFactory = new ProfileFieldFactory(new VoidLogger(), $this->permissionSetDepository); $this->permissionSetFactory = new PermissionSetFactory(new VoidLogger(), new ACLFormatter()); + $this->profileFieldFactory = new ProfileFieldFactory(new VoidLogger(), $this->permissionSetFactory); } public function dataEntity() @@ -44,6 +43,14 @@ class ProfileFieldTest extends MockedTest 'value' => 'more', 'created' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), 'edited' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), + 'permissionSet' => [ + 'uid' => 23, + 'allow_cid' => "<1>", + 'allow_gid' => "<~>", + 'deny_cid' => '<2>', + 'deny_gid' => '<3>', + 'id' => 2, + ] ], 'withId' => [ 'uid' => 23, @@ -53,6 +60,14 @@ class ProfileFieldTest extends MockedTest 'value' => 'more', 'created' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), 'edited' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), + 'permissionSet' => [ + 'uid' => 23, + 'allow_cid' => "<1>", + 'allow_gid' => "<~>", + 'deny_cid' => '<2>', + 'deny_gid' => '<3>', + 'id' => 2, + ], 'id' => 54, ], ]; @@ -61,13 +76,13 @@ class ProfileFieldTest extends MockedTest /** * @dataProvider dataEntity */ - public function testEntity(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + public function testEntity(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null) { - $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); + $entity = new ProfileField($uid, $order, $label, $value, $created, $edited, $this->permissionSetFactory->createFromTableRow($permissionSet), $id); self::assertEquals($uid, $entity->uid); self::assertEquals($order, $entity->order); - self::assertEquals($psid, $entity->permissionSetId); + self::assertEquals($psid, $entity->permissionSet->id); self::assertEquals($label, $entity->label); self::assertEquals($value, $entity->value); self::assertEquals($created, $entity->created); @@ -78,7 +93,7 @@ class ProfileFieldTest extends MockedTest /** * @dataProvider dataEntity */ - public function testUpdate(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + public function testUpdate(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null) { $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'id' => $psid]); @@ -103,7 +118,7 @@ class ProfileFieldTest extends MockedTest self::assertEquals($uid, $entity->uid); self::assertEquals(2345, $entity->order); - self::assertEquals(23, $entity->permissionSetId); + self::assertEquals(23, $entity->permissionSet->id); self::assertEquals($label, $entity->label); self::assertEquals('updatedValue', $entity->value); self::assertEquals($created, $entity->created); @@ -114,7 +129,7 @@ class ProfileFieldTest extends MockedTest /** * @dataProvider dataEntity */ - public function testSetOrder(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + public function testSetOrder(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null) { $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 2, 'id' => $psid]); @@ -133,7 +148,7 @@ class ProfileFieldTest extends MockedTest self::assertEquals($uid, $entity->uid); self::assertEquals(2345, $entity->order); - self::assertEquals($psid, $entity->permissionSetId); + self::assertEquals($psid, $entity->permissionSet->id); self::assertEquals($label, $entity->label); self::assertEquals($value, $entity->value); self::assertEquals($created, $entity->created); @@ -146,9 +161,9 @@ class ProfileFieldTest extends MockedTest * * @dataProvider dataEntity */ - public function testWrongGet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + public function testWrongGet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null) { - $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); + $entity = new ProfileField($uid, $order, $label, $value, $created, $edited, $this->permissionSetFactory->createFromTableRow($permissionSet), $id); self::expectException(ProfileFieldNotFoundException::class); $entity->wrong; @@ -159,9 +174,10 @@ class ProfileFieldTest extends MockedTest * * @dataProvider dataEntity */ - public function testPermissionSet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + public function testPermissionSet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null) { - $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); + $entity = new ProfileField($uid, $order, $label, $value, $created, $edited, $this->permissionSetFactory->createFromTableRow($permissionSet), $id); + $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => $uid, 'id' => $psid]); $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid, $uid)->andReturns($permissionSet); @@ -169,49 +185,15 @@ class ProfileFieldTest extends MockedTest self::assertEquals($psid, $entity->permissionSet->id); } - /** - * Test the exception because of incompatible user id - * - * @dataProvider dataEntity - */ - public function testWrongPermissionSet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) - { - $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); - $permissionSet = $this->permissionSetFactory->createFromTableRow(['uid' => 12345, 'id' => $psid]); - - $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid, $uid)->andReturns($permissionSet); - - self::expectException(UnexpectedPermissionSetException::class); - self::expectExceptionMessage(sprintf('PermissionSet %d (user-id: %d) for ProfileField %d (user-id: %d) is invalid.', $psid, 12345, $id, $uid)); - $entity->permissionSet; - } - - /** - * Test the exception because of missing permission set - * - * @dataProvider dataEntity - */ - public function testMissingPermissionSet(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) - { - $entity = new ProfileField($this->permissionSetDepository, $uid, $order, $psid, $label, $value, $created, $edited, $id); - - $this->permissionSetDepository->shouldReceive('selectOneById')->with($psid, $uid) - ->andThrow(new NotFoundException('test')); - - self::expectException(UnexpectedPermissionSetException::class); - self::expectExceptionMessage(sprintf('No PermissionSet found for ProfileField %d (user-id: %d).', $id, $uid)); - $entity->permissionSet; - } - /** * Test the exception because the factory cannot find a permissionSet ID, nor the permissionSet itself * * @dataProvider dataEntity */ - public function testMissingPermissionFactory(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, $id = null) + public function testMissingPermissionFactory(int $uid, int $order, int $psid, string $label, string $value, \DateTime $created, \DateTime $edited, array $permissionSet, $id = null) { self::expectException(UnexpectedPermissionSetException::class); - self::expectExceptionMessage('Either set the permission set ID or the permission set itself'); + self::expectExceptionMessage('Either set the PermissionSet fields (join) or the PermissionSet itself'); $entity = $this->profileFieldFactory->createFromTableRow([ 'uid' => $uid, diff --git a/update.php b/update.php index d7598a33f..f5d310830 100644 --- a/update.php +++ b/update.php @@ -206,7 +206,7 @@ function update_1332() $profiles = DBA::select('profile', [], $condition); while ($profile = DBA::fetch($profiles)) { - Profile::migrateFromLegacyProfile($profile); + Profile::migrate($profile); } DBA::close($profiles); From a07acac46303609947b5197527d9c81a819df2e9 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 17 Oct 2021 15:38:24 +0200 Subject: [PATCH 12/22] Fix PHP-CS --- .../ProfileField/Depository/ProfileField.php | 1 - .../ProfileField/Entity/ProfileFieldTest.php | 30 +++++++++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Profile/ProfileField/Depository/ProfileField.php b/src/Profile/ProfileField/Depository/ProfileField.php index 0ca427a1a..dafe08f95 100644 --- a/src/Profile/ProfileField/Depository/ProfileField.php +++ b/src/Profile/ProfileField/Depository/ProfileField.php @@ -23,7 +23,6 @@ namespace Friendica\Profile\ProfileField\Depository; use Friendica\BaseDepository; use Friendica\Database\Database; -use Friendica\Database\DBA; use Friendica\Profile\ProfileField\Exception\ProfileFieldNotFoundException; use Friendica\Profile\ProfileField\Exception\ProfileFieldPersistenceException; use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException; diff --git a/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php b/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php index e4f29d686..8acd1d94c 100644 --- a/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php +++ b/tests/src/Profile/ProfileField/Entity/ProfileFieldTest.php @@ -36,13 +36,13 @@ class ProfileFieldTest extends MockedTest { return [ 'default' => [ - 'uid' => 23, - 'order' => 1, - 'psid' => 2, - 'label' => 'test', - 'value' => 'more', - 'created' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), - 'edited' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), + 'uid' => 23, + 'order' => 1, + 'psid' => 2, + 'label' => 'test', + 'value' => 'more', + 'created' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), + 'edited' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), 'permissionSet' => [ 'uid' => 23, 'allow_cid' => "<1>", @@ -53,13 +53,13 @@ class ProfileFieldTest extends MockedTest ] ], 'withId' => [ - 'uid' => 23, - 'order' => 1, - 'psid' => 2, - 'label' => 'test', - 'value' => 'more', - 'created' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), - 'edited' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), + 'uid' => 23, + 'order' => 1, + 'psid' => 2, + 'label' => 'test', + 'value' => 'more', + 'created' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), + 'edited' => new \DateTime('2021-10-10T21:12:00.000000+0000', new \DateTimeZone('UTC')), 'permissionSet' => [ 'uid' => 23, 'allow_cid' => "<1>", @@ -68,7 +68,7 @@ class ProfileFieldTest extends MockedTest 'deny_gid' => '<3>', 'id' => 2, ], - 'id' => 54, + 'id' => 54, ], ]; } From b5d994394e2c67d37d9a9360731d08d7112d18fe Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 17 Oct 2021 23:10:10 +0200 Subject: [PATCH 13/22] Fixing PUBLIC usage, Fixing DB-View, Creating tests --- database.sql | 20 +++ src/BaseEntity.php | 14 ++ src/Factory/Api/Mastodon/Account.php | 15 +- src/Factory/Api/Mastodon/Field.php | 2 +- src/Module/ActivityPub/Objects.php | 2 +- src/Module/Profile/Status.php | 2 +- src/Module/Settings/Profile/Index.php | 6 +- .../ProfileField/Depository/ProfileField.php | 10 +- .../ProfileField/Factory/ProfileField.php | 2 +- src/Protocol/ActivityPub/Transmitter.php | 2 +- .../Depository/PermissionSet.php | 62 ++++++-- static/dbview.config.php | 4 +- .../Depository/ProfileFieldTest.php | 148 ++++++++++++++++++ .../Depository/PermissionSetTest.php | 60 +++++-- 14 files changed, 304 insertions(+), 45 deletions(-) create mode 100644 tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php diff --git a/database.sql b/database.sql index 793aae386..4fd6ba39f 100644 --- a/database.sql +++ b/database.sql @@ -2587,3 +2587,23 @@ CREATE VIEW `workerqueue-view` AS SELECT FROM `process` INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` WHERE NOT `workerqueue`.`done`; + +-- +-- VIEW profile_field-view +-- +DROP VIEW IF EXISTS `profile_field-view`; +CREATE VIEW `profile_field-view` AS SELECT + `profile_field`.`id` AS `id`, + `profile_field`.`uid` AS `uid`, + `profile_field`.`label` AS `label`, + `profile_field`.`value` AS `value`, + `profile_field`.`order` AS `order`, + `profile_field`.`psid` AS `psid`, + `permissionset`.`allow_cid` AS `allow_cid`, + `permissionset`.`allow_gid` AS `allow_gid`, + `permissionset`.`deny_cid` AS `deny_cid`, + `permissionset`.`deny_gid` AS `deny_gid`, + `profile_field`.`created` AS `created`, + `profile_field`.`edited` AS `edited` + FROM `profile_field` + INNER JOIN `permissionset` ON `permissionset`.`id` = `profile_field`.`psid`; diff --git a/src/BaseEntity.php b/src/BaseEntity.php index a5b968397..2ee22b1bb 100644 --- a/src/BaseEntity.php +++ b/src/BaseEntity.php @@ -53,4 +53,18 @@ abstract class BaseEntity extends BaseDataTransferObject return $this->$name; } + + /** + * @param $name + * @return bool + * @throws HTTPException\InternalServerErrorException + */ + public function __isset($name) + { + if (!property_exists($this, $name)) { + throw new HTTPException\InternalServerErrorException('Unknown property ' . $name . ' in Entity ' . static::class); + } + + return !empty($this->$name); + } } diff --git a/src/Factory/Api/Mastodon/Account.php b/src/Factory/Api/Mastodon/Account.php index 1d067352a..a987e6874 100644 --- a/src/Factory/Api/Mastodon/Account.php +++ b/src/Factory/Api/Mastodon/Account.php @@ -27,8 +27,7 @@ use Friendica\Collection\Api\Mastodon\Fields; use Friendica\Model\APContact; use Friendica\Model\Contact; use Friendica\Network\HTTPException; -use Friendica\Repository\ProfileField; -use Friendica\Security\PermissionSet\Depository\PermissionSet; +use Friendica\Profile\ProfileField\Depository\ProfileField as ProfileFieldDepository; use ImagickException; use Psr\Log\LoggerInterface; @@ -36,17 +35,17 @@ class Account extends BaseFactory { /** @var BaseURL */ private $baseUrl; - /** @var ProfileField */ - private $profileFieldRepo; + /** @var ProfileFieldDepository */ + private $profileFieldDepo; /** @var Field */ private $mstdnFieldFactory; - public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileFieldRepo, Field $mstdnFieldFactory) + public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileFieldDepository $profileFieldDepo, Field $mstdnFieldFactory) { parent::__construct($logger); $this->baseUrl = $baseURL; - $this->profileFieldRepo = $profileFieldRepo; + $this->profileFieldDepo = $profileFieldDepo; $this->mstdnFieldFactory = $mstdnFieldFactory; } @@ -77,7 +76,7 @@ class Account extends BaseFactory $self_contact = Contact::selectFirst(['uid'], ['nurl' => $publicContact['nurl'], 'self' => true]); if (!empty($self_contact['uid'])) { - $profileFields = $this->profileFieldRepo->select(['uid' => $self_contact['uid'], 'psid' => PermissionSet::PUBLIC]); + $profileFields = $this->profileFieldDepo->selectPublicFieldsByUserId($self_contact['uid']); $fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields); } else { $fields = new Fields(); @@ -95,7 +94,7 @@ class Account extends BaseFactory { $publicContact = Contact::selectFirst([], ['uid' => $userId, 'self' => true]); - $profileFields = $this->profileFieldRepo->select(['uid' => $userId, 'psid' => PermissionSet::PUBLIC]); + $profileFields = $this->profileFieldDepo->selectPublicFieldsByUserId($userId); $fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields); $apContact = APContact::getByURL($publicContact['url'], false); diff --git a/src/Factory/Api/Mastodon/Field.php b/src/Factory/Api/Mastodon/Field.php index aade421bb..397ca57ad 100644 --- a/src/Factory/Api/Mastodon/Field.php +++ b/src/Factory/Api/Mastodon/Field.php @@ -31,7 +31,7 @@ use Friendica\Network\HTTPException; class Field extends BaseFactory { /** - * @param \Friendica\Profile\ProfileField\Entity\ProfileField $profileField + * @param ProfileField $profileField * * @return \Friendica\Object\Api\Mastodon\Field * @throws HTTPException\InternalServerErrorException diff --git a/src/Module/ActivityPub/Objects.php b/src/Module/ActivityPub/Objects.php index 7e2ceb8b0..47d5409d5 100644 --- a/src/Module/ActivityPub/Objects.php +++ b/src/Module/ActivityPub/Objects.php @@ -86,7 +86,7 @@ class Objects extends BaseModule $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $item['uid']); if (!empty($permissionSets)) { $psid = array_merge($permissionSets->column('id'), - [DI::permissionSet()->selectEmptyForUser($item['uid'])]); + [DI::permissionSet()->selectPublic($item['uid'])]); $validated = in_array($item['psid'], $psid); } } diff --git a/src/Module/Profile/Status.php b/src/Module/Profile/Status.php index 4cf097120..c2d691f1c 100644 --- a/src/Module/Profile/Status.php +++ b/src/Module/Profile/Status.php @@ -213,7 +213,7 @@ class Status extends BaseProfile $permissionSets = DI::permissionSet()->selectByContactId($remote_user, $profile['uid']); if (!empty($permissionSets)) { $condition = ['psid' => array_merge($permissionSets->column('id'), - [DI::permissionSet()->selectEmptyForUser($profile['uid'])->id])]; + [DI::permissionSet()->selectPublic($profile['uid'])->id])]; } } elseif ($profile['uid'] == local_user()) { $condition = []; diff --git a/src/Module/Settings/Profile/Index.php b/src/Module/Settings/Profile/Index.php index 7ebf5b712..05b5fd3ea 100644 --- a/src/Module/Settings/Profile/Index.php +++ b/src/Module/Settings/Profile/Index.php @@ -287,11 +287,11 @@ class Index extends BaseSettings $profileFieldInputs['new']['value'], $permissionSet )); - - unset($profileFieldInputs['new']); - unset($profileFieldOrder['new']); } + unset($profileFieldInputs['new']); + unset($profileFieldOrder['new']); + foreach ($profileFieldInputs as $id => $profileFieldInput) { $permissionSet = DI::permissionSet()->selectOrCreate(DI::permissionSetFactory()->createFromString( $uid, diff --git a/src/Profile/ProfileField/Depository/ProfileField.php b/src/Profile/ProfileField/Depository/ProfileField.php index dafe08f95..79bf21b49 100644 --- a/src/Profile/ProfileField/Depository/ProfileField.php +++ b/src/Profile/ProfileField/Depository/ProfileField.php @@ -125,9 +125,11 @@ class ProfileField extends BaseDepository public function selectPublicFieldsByUserId(int $uid): Collection\ProfileFields { try { + $publicPermissionSet = $this->permissionSetDepository->selectPublic($uid); + return $this->select([ 'uid' => $uid, - 'psid' => PermissionSetDepository::PUBLIC, + 'psid' => $publicPermissionSet->id ]); } catch (\Exception $exception) { throw new ProfileFieldPersistenceException(sprintf('Cannot select public ProfileField for user "%d"', $uid), $exception); @@ -166,7 +168,7 @@ class ProfileField extends BaseDepository $permissionSetIds = $permissionSets->column('id'); // Includes public custom fields - $permissionSetIds[] = PermissionSetDepository::PUBLIC; + $permissionSetIds[] = $this->permissionSetDepository->selectPublic($uid)->id; return $this->select( ['uid' => $uid, 'psid' => $permissionSetIds], @@ -215,6 +217,10 @@ class ProfileField extends BaseDepository */ public function save(Entity\ProfileField $profileField): Entity\ProfileField { + if ($profileField->permissionSet->id === null) { + throw new ProfileFieldPersistenceException('PermissionSet needs to be saved first.'); + } + $fields = $this->convertToTableRow($profileField); try { diff --git a/src/Profile/ProfileField/Factory/ProfileField.php b/src/Profile/ProfileField/Factory/ProfileField.php index 4253d93b9..80a8f5502 100644 --- a/src/Profile/ProfileField/Factory/ProfileField.php +++ b/src/Profile/ProfileField/Factory/ProfileField.php @@ -49,7 +49,7 @@ class ProfileField extends BaseFactory implements ICanCreateFromTableRow public function createFromTableRow(array $row, PermissionSet $permissionSet = null): Entity\ProfileField { 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)) + (!array_key_exists('psid', $row) || !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'); } diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index e741789da..f0ed0cf5a 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -229,7 +229,7 @@ class Transmitter $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $owner['uid']); if (!empty($permissionSets)) { $condition = ['psid' => array_merge($permissionSets->column('id'), - [DI::permissionSet()->selectEmptyForUser($owner['uid'])])]; + [DI::permissionSet()->selectPublic($owner['uid'])])]; } } } diff --git a/src/Security/PermissionSet/Depository/PermissionSet.php b/src/Security/PermissionSet/Depository/PermissionSet.php index b4a918b6d..b65a933b4 100644 --- a/src/Security/PermissionSet/Depository/PermissionSet.php +++ b/src/Security/PermissionSet/Depository/PermissionSet.php @@ -53,6 +53,22 @@ class PermissionSet extends BaseDepository $this->aclFormatter = $aclFormatter; } + /** + * replaces the PUBLIC id for the public permissionSet + * (no need to create the default permission set over and over again) + * + * @param $condition + */ + private function checkPublicSelect(&$condition) + { + if (empty($condition['allow_cid']) && + empty($condition['allow_gid']) && + empty($condition['deny_cid']) && + empty($condition['deny_gid'])) { + $condition['uid'] = self::PUBLIC; + } + } + /** * @param array $condition * @param array $params @@ -70,6 +86,16 @@ class PermissionSet extends BaseDepository return new Collection\PermissionSets(parent::_select($condition, $params)->getArrayCopy()); } + private function checkPublic(Entity\PermissionSet $permissionSet): bool + { + return (($permissionSet->id === self::PUBLIC) || + (is_null($permissionSet->id) && + empty($permissionSet->allow_cid) && + empty($permissionSet->allow_gid) && + empty($permissionSet->deny_cid) && + empty($permissionSet->deny_gid))); + } + /** * Converts a given PermissionSet into a DB compatible row array * @@ -89,22 +115,18 @@ class PermissionSet extends BaseDepository } /** - * @param int $id A permissionset table row id or self::PUBLIC - * @param int|null $uid Should be provided when id can be self::PUBLIC + * @param int $id A PermissionSet table row id or self::PUBLIC + * @param int $uid The owner of the PermissionSet * @return Entity\PermissionSet * @throws NotFoundException */ - public function selectOneById(int $id, int $uid = null): Entity\PermissionSet + public function selectOneById(int $id, int $uid): Entity\PermissionSet { if ($id === self::PUBLIC) { - if (empty($uid)) { - throw new \InvalidArgumentException('Missing uid for Public permission set instantiation'); - } - return $this->factory->createFromString($uid); } - return $this->selectOne(['id' => $id]); + return $this->selectOne(['id' => $id, 'uid' => $uid]); } /** @@ -174,15 +196,15 @@ class PermissionSet extends BaseDepository } /** - * Fetch the empty PermissionSet for a given user, create it if it doesn't exist + * Fetch the public PermissionSet * * @param int $uid * * @return Entity\PermissionSet */ - public function selectEmptyForUser(int $uid): Entity\PermissionSet + public function selectPublic(int $uid): Entity\PermissionSet { - return $this->selectOrCreate($this->factory->createFromString($uid)); + return $this->factory->createFromString($uid, '', '', '', '', self::PUBLIC); } /** @@ -198,6 +220,11 @@ class PermissionSet extends BaseDepository return $permissionSet; } + // Don't select/update Public permission sets + if ($this->checkPublic($permissionSet)) { + return $this->selectPublic($permissionSet->uid); + } + try { return $this->selectOne($this->convertToTableRow($permissionSet)); } catch (NotFoundException $exception) { @@ -205,8 +232,19 @@ class PermissionSet extends BaseDepository } } + /** + * @param Entity\PermissionSet $permissionSet + * + * @return Entity\PermissionSet + * @throws NotFoundException + */ public function save(Entity\PermissionSet $permissionSet): Entity\PermissionSet { + // Don't save/update the common public PermissionSet + if ($this->checkPublic($permissionSet)) { + return $this->selectPublic($permissionSet->uid); + } + $fields = $this->convertToTableRow($permissionSet); if ($permissionSet->id) { @@ -214,7 +252,7 @@ class PermissionSet extends BaseDepository } else { $this->db->insert(self::$table_name, $fields); - $permissionSet = $this->selectOneById($this->db->lastInsertId()); + $permissionSet = $this->selectOneById($this->db->lastInsertId(), $permissionSet->uid); } return $permissionSet; diff --git a/static/dbview.config.php b/static/dbview.config.php index a373f4c28..f8d11955e 100644 --- a/static/dbview.config.php +++ b/static/dbview.config.php @@ -1058,7 +1058,9 @@ "allow_cid" => ["permissionset", "allow_cid"], "allow_gid" => ["permissionset", "allow_gid"], "deny_cid" => ["permissionset", "deny_cid"], - "deny_gid" => ["permissionset", "deny_gid"] + "deny_gid" => ["permissionset", "deny_gid"], + "created" => ["profile_field", "created"], + "updated" => ["profile_field", "updated"], ], "query" => "FROM `profile_field` INNER JOIN `permissionset` ON `permissionset`.`id` = `profile_field`.`psid`" diff --git a/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php b/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php new file mode 100644 index 000000000..ecd4014fa --- /dev/null +++ b/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php @@ -0,0 +1,148 @@ +addRules(include __DIR__ . '/../../../../../static/dependencies.config.php') + ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]); + + $this->depository = $dice->create(ProfileFieldDepository::class); + $this->factory = $dice->create(ProfileFieldFactory::class); + $this->permissionSetFactory = $dice->create(PermissionSetFactory::class); + $this->permissionSetDepository = $dice->create(PermissionSetDepository::class); + $this->dba = $dice->create(Database::class); + } + + /** + * Test create ProfileField without a valid PermissionSet + */ + public function testSavingWithoutPermissionSet() + { + self::expectExceptionMessage('PermissionSet needs to be saved first.'); + self::expectException(ProfileFieldPersistenceException::class); + + $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', DI::dba()); + + $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetFactory->createFromString(42, '', '<~>')); + + self::assertEquals($profileField->uid, $profileField->permissionSet->uid); + + $this->depository->save($profileField); + } + + /** + * Test saving a new entity + */ + public function testSaveNew() + { + $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', DI::dba()); + + $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetDepository->save($this->permissionSetFactory->createFromString(42, '', '<~>'))); + + self::assertEquals($profileField->uid, $profileField->permissionSet->uid); + + $savedProfileField = $this->depository->save($profileField); + + self::assertNotNull($savedProfileField->id); + self::assertNull($profileField->id); + + /** @var ProfileField $selectedProfileField */ + $selectedProfileField = $this->depository->selectOneById($savedProfileField->id); + + self::assertEquals($savedProfileField, $selectedProfileField); + } + + /** + * Test updating the order of a ProfileField + */ + public function testUpdateOrder() + { + $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', DI::dba()); + + $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetDepository->save($this->permissionSetFactory->createFromString(42, '', '<~>'))); + + self::assertEquals($profileField->uid, $profileField->permissionSet->uid); + + $savedProfileField = $this->depository->save($profileField); + + self::assertNotNull($savedProfileField->id); + self::assertNull($profileField->id); + + /** @var ProfileField $selectedProfileField */ + $selectedProfileField = $this->depository->selectOneById($savedProfileField->id); + + self::assertEquals($savedProfileField, $selectedProfileField); + + $selectedProfileField->setOrder(66); + + $updatedOrderProfileField = $this->depository->save($selectedProfileField); + + self::assertEquals($selectedProfileField->id, $updatedOrderProfileField->id); + self::assertEquals(66, $updatedOrderProfileField->order); + + // Even using the ID of the old, saved ProfileField returns the right instance + $updatedFromOldProfileField = $this->depository->selectOneById($savedProfileField->id); + self::assertEquals(66, $updatedFromOldProfileField->order); + } + + /** + * Test updating a whole entity + */ + public function testUpdate() + { + $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', DI::dba()); + + $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetDepository->save($this->permissionSetFactory->createFromString(42, '', '<~>'))); + + self::assertEquals($profileField->uid, $profileField->permissionSet->uid); + + $savedProfileField = $this->depository->save($profileField); + + self::assertNotNull($savedProfileField->id); + self::assertNull($profileField->id); + + /** @var ProfileField $selectedProfileField */ + $selectedProfileField = $this->depository->selectOneById($savedProfileField->id); + + self::assertEquals($savedProfileField, $selectedProfileField); + + $savedProfileField->update('another', 5, $this->permissionSetDepository->selectPublic(42)); + self::assertEquals(PermissionSet::PUBLIC, $savedProfileField->permissionSet->id); + + $publicProfileField = $this->depository->save($savedProfileField); + + self::assertEquals($this->permissionSetDepository->selectPublic(42), $publicProfileField->permissionSet); + self::assertEquals('another', $publicProfileField->value); + self::assertEquals(5, $publicProfileField->order); + } +} diff --git a/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php b/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php index 305769411..a136ff84c 100644 --- a/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php +++ b/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php @@ -5,36 +5,68 @@ namespace Friendica\Test\src\Security\PermissionSet\Depository; use Dice\Dice; use Friendica\Database\Database; use Friendica\DI; -use Friendica\Security\PermissionSet\Depository\PermissionSet; -use Friendica\Test\MockedTest; +use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository; +use Friendica\Security\PermissionSet\Factory\PermissionSet as PermissionSetFactory; +use Friendica\Test\DatabaseTest; use Friendica\Test\Util\Database\StaticDatabase; -class PermissionSetTest extends MockedTest +class PermissionSetTest extends DatabaseTest { - /** @var PermissionSet */ + /** @var PermissionSetDepository */ private $depository; + /** @var PermissionSetFactory */ + private $factory; public function setUp(): void { + parent::setUp(); + $dice = (new Dice()) ->addRules(include __DIR__ . '/../../../../../static/dependencies.config.php') ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]); - DI::init($dice); - $this->depository = DI::permissionSet(); - } - - public function testSelectOneByIdPublicMissingUid() - { - $this->expectException(\InvalidArgumentException::class); - - $this->depository->selectOneById(PermissionSet::PUBLIC); + $this->depository = $dice->create(PermissionSetDepository::class); + $this->factory = $dice->create(PermissionSetFactory::class); } public function testSelectOneByIdPublic() { - $permissionSet = $this->depository->selectOneById(PermissionSet::PUBLIC, 1); + $permissionSet = $this->depository->selectPublic(1); $this->assertInstanceOf(\Friendica\Security\PermissionSet\Entity\PermissionSet::class, $permissionSet); + self::assertEmpty($permissionSet->allow_cid); + self::assertEmpty($permissionSet->allow_gid); + self::assertEmpty($permissionSet->deny_cid); + self::assertEmpty($permissionSet->deny_gid); + self::assertEmpty(PermissionSetDepository::PUBLIC, $permissionSet->id); + self::assertEquals(1, $permissionSet->uid); + } + + /** + * Test create/update PermissionSets + */ + public function testSaving() + { + $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', DI::dba()); + + $permissionSet = $this->factory->createFromString(42, '', '<~>'); + + $permissionSet = $this->depository->selectOrCreate($permissionSet); + + self::assertNotNull($permissionSet->id); + + $permissionSetSelected = $this->depository->selectOneById($permissionSet->id, 42); + + self::assertEquals($permissionSet, $permissionSetSelected); + + $newPermissionSet = $permissionSet->withAllowedContacts(['1', '2']); + $savedPermissionSet = $this->depository->save($newPermissionSet); + + self::assertNotNull($savedPermissionSet->id); + self::assertNull($newPermissionSet->id); + + $permissionSetSavedSelected = $this->depository->selectOneById($savedPermissionSet->id, 42); + + self::assertEquals($savedPermissionSet, $permissionSetSavedSelected); } } From 64a336ee8de35fbea9de668e2497b2dd1220fdaf Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 17 Oct 2021 23:12:26 +0200 Subject: [PATCH 14/22] Rename method again --- src/Module/ActivityPub/Objects.php | 2 +- src/Module/Profile/Status.php | 2 +- src/Profile/ProfileField/Depository/ProfileField.php | 4 ++-- src/Protocol/ActivityPub/Transmitter.php | 2 +- src/Security/PermissionSet/Depository/PermissionSet.php | 6 +++--- .../Profile/ProfileField/Depository/ProfileFieldTest.php | 4 ++-- .../Security/PermissionSet/Depository/PermissionSetTest.php | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Module/ActivityPub/Objects.php b/src/Module/ActivityPub/Objects.php index 47d5409d5..5ee1b65d8 100644 --- a/src/Module/ActivityPub/Objects.php +++ b/src/Module/ActivityPub/Objects.php @@ -86,7 +86,7 @@ class Objects extends BaseModule $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $item['uid']); if (!empty($permissionSets)) { $psid = array_merge($permissionSets->column('id'), - [DI::permissionSet()->selectPublic($item['uid'])]); + [DI::permissionSet()->selectPublicForUser($item['uid'])]); $validated = in_array($item['psid'], $psid); } } diff --git a/src/Module/Profile/Status.php b/src/Module/Profile/Status.php index c2d691f1c..e93fc5699 100644 --- a/src/Module/Profile/Status.php +++ b/src/Module/Profile/Status.php @@ -213,7 +213,7 @@ class Status extends BaseProfile $permissionSets = DI::permissionSet()->selectByContactId($remote_user, $profile['uid']); if (!empty($permissionSets)) { $condition = ['psid' => array_merge($permissionSets->column('id'), - [DI::permissionSet()->selectPublic($profile['uid'])->id])]; + [DI::permissionSet()->selectPublicForUser($profile['uid'])->id])]; } } elseif ($profile['uid'] == local_user()) { $condition = []; diff --git a/src/Profile/ProfileField/Depository/ProfileField.php b/src/Profile/ProfileField/Depository/ProfileField.php index 79bf21b49..b792edf8f 100644 --- a/src/Profile/ProfileField/Depository/ProfileField.php +++ b/src/Profile/ProfileField/Depository/ProfileField.php @@ -125,7 +125,7 @@ class ProfileField extends BaseDepository public function selectPublicFieldsByUserId(int $uid): Collection\ProfileFields { try { - $publicPermissionSet = $this->permissionSetDepository->selectPublic($uid); + $publicPermissionSet = $this->permissionSetDepository->selectPublicForUser($uid); return $this->select([ 'uid' => $uid, @@ -168,7 +168,7 @@ class ProfileField extends BaseDepository $permissionSetIds = $permissionSets->column('id'); // Includes public custom fields - $permissionSetIds[] = $this->permissionSetDepository->selectPublic($uid)->id; + $permissionSetIds[] = $this->permissionSetDepository->selectPublicForUser($uid)->id; return $this->select( ['uid' => $uid, 'psid' => $permissionSetIds], diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index f0ed0cf5a..ab7b63866 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -229,7 +229,7 @@ class Transmitter $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $owner['uid']); if (!empty($permissionSets)) { $condition = ['psid' => array_merge($permissionSets->column('id'), - [DI::permissionSet()->selectPublic($owner['uid'])])]; + [DI::permissionSet()->selectPublicForUser($owner['uid'])])]; } } } diff --git a/src/Security/PermissionSet/Depository/PermissionSet.php b/src/Security/PermissionSet/Depository/PermissionSet.php index b65a933b4..09ecceaf0 100644 --- a/src/Security/PermissionSet/Depository/PermissionSet.php +++ b/src/Security/PermissionSet/Depository/PermissionSet.php @@ -202,7 +202,7 @@ class PermissionSet extends BaseDepository * * @return Entity\PermissionSet */ - public function selectPublic(int $uid): Entity\PermissionSet + public function selectPublicForUser(int $uid): Entity\PermissionSet { return $this->factory->createFromString($uid, '', '', '', '', self::PUBLIC); } @@ -222,7 +222,7 @@ class PermissionSet extends BaseDepository // Don't select/update Public permission sets if ($this->checkPublic($permissionSet)) { - return $this->selectPublic($permissionSet->uid); + return $this->selectPublicForUser($permissionSet->uid); } try { @@ -242,7 +242,7 @@ class PermissionSet extends BaseDepository { // Don't save/update the common public PermissionSet if ($this->checkPublic($permissionSet)) { - return $this->selectPublic($permissionSet->uid); + return $this->selectPublicForUser($permissionSet->uid); } $fields = $this->convertToTableRow($permissionSet); diff --git a/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php b/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php index ecd4014fa..2218deebb 100644 --- a/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php +++ b/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php @@ -136,12 +136,12 @@ class ProfileFieldTest extends DatabaseTest self::assertEquals($savedProfileField, $selectedProfileField); - $savedProfileField->update('another', 5, $this->permissionSetDepository->selectPublic(42)); + $savedProfileField->update('another', 5, $this->permissionSetDepository->selectPublicForUser(42)); self::assertEquals(PermissionSet::PUBLIC, $savedProfileField->permissionSet->id); $publicProfileField = $this->depository->save($savedProfileField); - self::assertEquals($this->permissionSetDepository->selectPublic(42), $publicProfileField->permissionSet); + self::assertEquals($this->permissionSetDepository->selectPublicForUser(42), $publicProfileField->permissionSet); self::assertEquals('another', $publicProfileField->value); self::assertEquals(5, $publicProfileField->order); } diff --git a/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php b/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php index a136ff84c..f5d2e22c6 100644 --- a/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php +++ b/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php @@ -31,7 +31,7 @@ class PermissionSetTest extends DatabaseTest public function testSelectOneByIdPublic() { - $permissionSet = $this->depository->selectPublic(1); + $permissionSet = $this->depository->selectPublicForUser(1); $this->assertInstanceOf(\Friendica\Security\PermissionSet\Entity\PermissionSet::class, $permissionSet); self::assertEmpty($permissionSet->allow_cid); From caa977c24ea3195d48c2a5816620147eb9b90255 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 17 Oct 2021 23:15:24 +0200 Subject: [PATCH 15/22] adapt fixture --- tests/datasets/api.fixture.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php index 86b2bd564..6743dbf11 100644 --- a/tests/datasets/api.fixture.php +++ b/tests/datasets/api.fixture.php @@ -26,6 +26,8 @@ use Friendica\Model\Notification; return [ // Empty these tables + 'profile_field', + 'permissionset', 'cache', 'conversation', 'pconfig', From bf224b58f8824315ef431ed42b4a1e70ad84b71a Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 17 Oct 2021 23:39:18 +0200 Subject: [PATCH 16/22] Move to PermissionSet::isPublic() --- .../PermissionSet/Depository/PermissionSet.php | 14 ++------------ .../PermissionSet/Entity/PermissionSet.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Security/PermissionSet/Depository/PermissionSet.php b/src/Security/PermissionSet/Depository/PermissionSet.php index 09ecceaf0..b91f45218 100644 --- a/src/Security/PermissionSet/Depository/PermissionSet.php +++ b/src/Security/PermissionSet/Depository/PermissionSet.php @@ -86,16 +86,6 @@ class PermissionSet extends BaseDepository return new Collection\PermissionSets(parent::_select($condition, $params)->getArrayCopy()); } - private function checkPublic(Entity\PermissionSet $permissionSet): bool - { - return (($permissionSet->id === self::PUBLIC) || - (is_null($permissionSet->id) && - empty($permissionSet->allow_cid) && - empty($permissionSet->allow_gid) && - empty($permissionSet->deny_cid) && - empty($permissionSet->deny_gid))); - } - /** * Converts a given PermissionSet into a DB compatible row array * @@ -221,7 +211,7 @@ class PermissionSet extends BaseDepository } // Don't select/update Public permission sets - if ($this->checkPublic($permissionSet)) { + if ($permissionSet->isPublic()) { return $this->selectPublicForUser($permissionSet->uid); } @@ -241,7 +231,7 @@ class PermissionSet extends BaseDepository public function save(Entity\PermissionSet $permissionSet): Entity\PermissionSet { // Don't save/update the common public PermissionSet - if ($this->checkPublic($permissionSet)) { + if ($permissionSet->isPublic()) { return $this->selectPublicForUser($permissionSet->uid); } diff --git a/src/Security/PermissionSet/Entity/PermissionSet.php b/src/Security/PermissionSet/Entity/PermissionSet.php index 849871647..6a0409331 100644 --- a/src/Security/PermissionSet/Entity/PermissionSet.php +++ b/src/Security/PermissionSet/Entity/PermissionSet.php @@ -3,6 +3,7 @@ namespace Friendica\Security\PermissionSet\Entity; use Friendica\BaseEntity; +use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository; /** * @property-read int|null $id @@ -47,6 +48,21 @@ class PermissionSet extends BaseEntity $this->deny_gid = $deny_gid; } + /** + * Checks, if the current PermissionSet is a/the public PermissionSet + * + * @return bool + */ + public function isPublic(): bool + { + return (($this->id === PermissionSetDepository::PUBLIC) || + (is_null($this->id) && + empty($this->allow_cid) && + empty($this->allow_gid) && + empty($this->deny_cid) && + empty($this->deny_gid))); + } + /** * Creates a new Entity with a new allowed_cid list (wipes the id because it isn't the same entity anymore) * From 0f752ff91bcb382a82a3c6c5158fd3b7ba6f94fd Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 17 Oct 2021 23:42:13 +0200 Subject: [PATCH 17/22] Fix tests --- .../Profile/ProfileField/Depository/ProfileFieldTest.php | 9 ++++----- .../PermissionSet/Depository/PermissionSetTest.php | 6 ++++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php b/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php index 2218deebb..c5350d0fe 100644 --- a/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php +++ b/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php @@ -4,7 +4,6 @@ namespace Friendica\Test\src\Profile\ProfileField\Depository; use Dice\Dice; use Friendica\Database\Database; -use Friendica\DI; use Friendica\Profile\ProfileField\Depository\ProfileField as ProfileFieldDepository; use Friendica\Profile\ProfileField\Entity\ProfileField; use Friendica\Profile\ProfileField\Exception\ProfileFieldPersistenceException; @@ -51,7 +50,7 @@ class ProfileFieldTest extends DatabaseTest self::expectExceptionMessage('PermissionSet needs to be saved first.'); self::expectException(ProfileFieldPersistenceException::class); - $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', DI::dba()); + $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', $this->dba); $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetFactory->createFromString(42, '', '<~>')); @@ -65,7 +64,7 @@ class ProfileFieldTest extends DatabaseTest */ public function testSaveNew() { - $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', DI::dba()); + $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', $this->dba); $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetDepository->save($this->permissionSetFactory->createFromString(42, '', '<~>'))); @@ -87,7 +86,7 @@ class ProfileFieldTest extends DatabaseTest */ public function testUpdateOrder() { - $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', DI::dba()); + $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', $this->dba); $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetDepository->save($this->permissionSetFactory->createFromString(42, '', '<~>'))); @@ -120,7 +119,7 @@ class ProfileFieldTest extends DatabaseTest */ public function testUpdate() { - $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', DI::dba()); + $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', $this->dba); $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetDepository->save($this->permissionSetFactory->createFromString(42, '', '<~>'))); diff --git a/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php b/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php index f5d2e22c6..3f36e3bca 100644 --- a/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php +++ b/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php @@ -4,7 +4,6 @@ namespace Friendica\Test\src\Security\PermissionSet\Depository; use Dice\Dice; use Friendica\Database\Database; -use Friendica\DI; use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository; use Friendica\Security\PermissionSet\Factory\PermissionSet as PermissionSetFactory; use Friendica\Test\DatabaseTest; @@ -16,6 +15,8 @@ class PermissionSetTest extends DatabaseTest private $depository; /** @var PermissionSetFactory */ private $factory; + /** @var Database */ + private $dba; public function setUp(): void { @@ -27,6 +28,7 @@ class PermissionSetTest extends DatabaseTest $this->depository = $dice->create(PermissionSetDepository::class); $this->factory = $dice->create(PermissionSetFactory::class); + $this->dba = $dice->create(Database::class); } public function testSelectOneByIdPublic() @@ -47,7 +49,7 @@ class PermissionSetTest extends DatabaseTest */ public function testSaving() { - $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', DI::dba()); + $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', $this->dba); $permissionSet = $this->factory->createFromString(42, '', '<~>'); From 5d2170990b99b31429fd32b3658de36c3bfaa123 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 18 Oct 2021 00:07:56 +0200 Subject: [PATCH 18/22] Update messages.po --- view/lang/C/messages.po | 336 ++++++++++++++++++++-------------------- 1 file changed, 168 insertions(+), 168 deletions(-) diff --git a/view/lang/C/messages.po b/view/lang/C/messages.po index a2829de9f..52412965f 100644 --- a/view/lang/C/messages.po +++ b/view/lang/C/messages.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: 2021.12-dev\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2021-10-16 19:31-0400\n" +"POT-Creation-Date: 2021-10-18 00:07+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -331,7 +331,7 @@ msgid "Access denied." msgstr "" #: mod/cal.php:61 mod/cal.php:78 mod/photos.php:69 mod/photos.php:143 -#: mod/photos.php:815 src/Model/Profile.php:228 src/Module/HCard.php:52 +#: mod/photos.php:815 src/Model/Profile.php:229 src/Module/HCard.php:52 #: src/Module/Profile/Common.php:41 src/Module/Profile/Common.php:52 #: src/Module/Profile/Contacts.php:40 src/Module/Profile/Contacts.php:50 #: src/Module/Profile/Media.php:38 src/Module/Profile/Status.php:58 @@ -509,7 +509,7 @@ msgstr "" msgid "Permission settings" msgstr "" -#: mod/editpost.php:116 src/Core/ACL.php:327 +#: mod/editpost.php:116 src/Core/ACL.php:325 msgid "CC: email addresses" msgstr "" @@ -527,7 +527,7 @@ msgstr "" msgid "Categories (comma-separated list)" msgstr "" -#: mod/editpost.php:123 src/Core/ACL.php:328 +#: mod/editpost.php:123 src/Core/ACL.php:326 msgid "Example: bob@example.com, mary@example.com" msgstr "" @@ -545,7 +545,7 @@ msgid "Cancel" msgstr "" #: mod/editpost.php:134 src/Content/Conversation.php:380 -#: src/Content/Widget/VCard.php:107 src/Model/Profile.php:459 +#: src/Content/Widget/VCard.php:107 src/Model/Profile.php:460 #: src/Module/Admin/Logs/View.php:92 msgid "Message" msgstr "" @@ -615,13 +615,13 @@ msgid "Event Finishes:" msgstr "" #: mod/events.php:506 src/Module/Profile/Profile.php:172 -#: src/Module/Settings/Profile/Index.php:239 +#: src/Module/Settings/Profile/Index.php:237 msgid "Description:" msgstr "" #: mod/events.php:508 src/Content/Widget/VCard.php:98 src/Model/Event.php:80 #: src/Model/Event.php:107 src/Model/Event.php:466 src/Model/Event.php:915 -#: src/Model/Profile.php:367 src/Module/Contact.php:565 +#: src/Model/Profile.php:368 src/Module/Contact.php:565 #: src/Module/Directory.php:150 src/Module/Notifications/Introductions.php:165 #: src/Module/Profile/Profile.php:194 msgid "Location:" @@ -647,7 +647,7 @@ msgstr "" #: src/Module/Install.php:245 src/Module/Install.php:287 #: src/Module/Install.php:324 src/Module/Invite.php:177 #: src/Module/Item/Compose.php:150 src/Module/Profile/Profile.php:247 -#: src/Module/Settings/Profile/Index.php:223 src/Object/Post.php:963 +#: src/Module/Settings/Profile/Index.php:221 src/Object/Post.php:963 #: view/theme/duepuntozero/config.php:69 view/theme/frio/config.php:160 #: view/theme/quattro/config.php:71 view/theme/vier/config.php:119 msgid "Submit" @@ -2374,7 +2374,7 @@ msgstr "" msgid "All contacts" msgstr "" -#: src/BaseModule.php:212 src/Content/Widget.php:231 src/Core/ACL.php:195 +#: src/BaseModule.php:212 src/Content/Widget.php:231 src/Core/ACL.php:193 #: src/Module/Contact.php:756 src/Module/PermissionTooltip.php:75 #: src/Module/PermissionTooltip.php:97 msgid "Followers" @@ -2988,7 +2988,7 @@ msgstr "" #: src/Content/Item.php:444 src/Content/Item.php:466 src/Model/Contact.php:1010 #: src/Model/Contact.php:1068 src/Model/Contact.php:1077 -#: src/Module/Directory.php:160 src/Module/Settings/Profile/Index.php:226 +#: src/Module/Directory.php:160 src/Module/Settings/Profile/Index.php:224 msgid "View Profile" msgstr "" @@ -3357,7 +3357,7 @@ msgid "The end" msgstr "" #: src/Content/Text/HTML.php:885 src/Content/Widget/VCard.php:103 -#: src/Model/Profile.php:453 +#: src/Model/Profile.php:454 msgid "Follow" msgstr "" @@ -3533,68 +3533,68 @@ msgstr[1] "" msgid "More Trending Tags" msgstr "" -#: src/Content/Widget/VCard.php:96 src/Model/Profile.php:372 +#: src/Content/Widget/VCard.php:96 src/Model/Profile.php:373 #: src/Module/Contact.php:567 src/Module/Profile/Profile.php:176 msgid "XMPP:" msgstr "" -#: src/Content/Widget/VCard.php:97 src/Model/Profile.php:373 +#: src/Content/Widget/VCard.php:97 src/Model/Profile.php:374 #: src/Module/Contact.php:569 src/Module/Profile/Profile.php:180 msgid "Matrix:" msgstr "" -#: src/Content/Widget/VCard.php:101 src/Model/Profile.php:465 +#: src/Content/Widget/VCard.php:101 src/Model/Profile.php:466 #: src/Module/Notifications/Introductions.php:179 msgid "Network:" msgstr "" -#: src/Content/Widget/VCard.php:105 src/Model/Profile.php:455 +#: src/Content/Widget/VCard.php:105 src/Model/Profile.php:456 msgid "Unfollow" msgstr "" -#: src/Core/ACL.php:166 src/Module/Profile/Profile.php:242 +#: src/Core/ACL.php:164 src/Module/Profile/Profile.php:242 msgid "Yourself" msgstr "" -#: src/Core/ACL.php:202 src/Module/PermissionTooltip.php:81 +#: src/Core/ACL.php:200 src/Module/PermissionTooltip.php:81 #: src/Module/PermissionTooltip.php:103 msgid "Mutuals" msgstr "" -#: src/Core/ACL.php:294 +#: src/Core/ACL.php:292 msgid "Post to Email" msgstr "" -#: src/Core/ACL.php:321 +#: src/Core/ACL.php:319 msgid "Public" msgstr "" -#: src/Core/ACL.php:322 +#: src/Core/ACL.php:320 msgid "" "This content will be shown to all your followers and can be seen in the " "community pages and by anyone with its link." msgstr "" -#: src/Core/ACL.php:323 +#: src/Core/ACL.php:321 msgid "Limited/Private" msgstr "" -#: src/Core/ACL.php:324 +#: src/Core/ACL.php:322 msgid "" "This content will be shown only to the people in the first box, to the " "exception of the people mentioned in the second box. It won't appear " "anywhere public." msgstr "" -#: src/Core/ACL.php:325 +#: src/Core/ACL.php:323 msgid "Show to:" msgstr "" -#: src/Core/ACL.php:326 +#: src/Core/ACL.php:324 msgid "Except to:" msgstr "" -#: src/Core/ACL.php:329 +#: src/Core/ACL.php:327 msgid "Connectors" msgstr "" @@ -4178,35 +4178,35 @@ msgid "" "\t\t\t\t\tThe friendica database was successfully updated from %s to %s." msgstr "" -#: src/Core/UserImport.php:124 +#: src/Core/UserImport.php:125 msgid "Error decoding account file" msgstr "" -#: src/Core/UserImport.php:130 +#: src/Core/UserImport.php:131 msgid "Error! No version data in file! This is not a Friendica account file?" msgstr "" -#: src/Core/UserImport.php:138 +#: src/Core/UserImport.php:139 #, php-format msgid "User '%s' already exists on this server!" msgstr "" -#: src/Core/UserImport.php:174 +#: src/Core/UserImport.php:175 msgid "User creation error" msgstr "" -#: src/Core/UserImport.php:219 +#: src/Core/UserImport.php:220 #, php-format msgid "%d contact not imported" msgid_plural "%d contacts not imported" msgstr[0] "" msgstr[1] "" -#: src/Core/UserImport.php:272 +#: src/Core/UserImport.php:273 msgid "User profile creation error" msgstr "" -#: src/Core/UserImport.php:325 +#: src/Core/UserImport.php:326 msgid "Done. You can now login with your username and password" msgstr "" @@ -4515,66 +4515,146 @@ msgstr "" msgid "[no subject]" msgstr "" -#: src/Model/Profile.php:355 src/Module/Profile/Profile.php:256 +#: src/Model/Profile.php:356 src/Module/Profile/Profile.php:256 #: src/Module/Profile/Profile.php:258 msgid "Edit profile" msgstr "" -#: src/Model/Profile.php:357 +#: src/Model/Profile.php:358 msgid "Change profile photo" msgstr "" -#: src/Model/Profile.php:370 src/Module/Directory.php:155 +#: src/Model/Profile.php:371 src/Module/Directory.php:155 #: src/Module/Profile/Profile.php:184 msgid "Homepage:" msgstr "" -#: src/Model/Profile.php:371 src/Module/Contact.php:571 +#: src/Model/Profile.php:372 src/Module/Contact.php:571 #: src/Module/Notifications/Introductions.php:167 msgid "About:" msgstr "" -#: src/Model/Profile.php:457 +#: src/Model/Profile.php:458 msgid "Atom feed" msgstr "" -#: src/Model/Profile.php:495 src/Model/Profile.php:592 +#: src/Model/Profile.php:496 src/Model/Profile.php:593 msgid "g A l F d" msgstr "" -#: src/Model/Profile.php:496 +#: src/Model/Profile.php:497 msgid "F d" msgstr "" -#: src/Model/Profile.php:558 src/Model/Profile.php:643 +#: src/Model/Profile.php:559 src/Model/Profile.php:644 msgid "[today]" msgstr "" -#: src/Model/Profile.php:568 +#: src/Model/Profile.php:569 msgid "Birthday Reminders" msgstr "" -#: src/Model/Profile.php:569 +#: src/Model/Profile.php:570 msgid "Birthdays this week:" msgstr "" -#: src/Model/Profile.php:630 +#: src/Model/Profile.php:631 msgid "[No description]" msgstr "" -#: src/Model/Profile.php:656 +#: src/Model/Profile.php:657 msgid "Event Reminders" msgstr "" -#: src/Model/Profile.php:657 +#: src/Model/Profile.php:658 msgid "Upcoming events the next 7 days:" msgstr "" -#: src/Model/Profile.php:845 +#: src/Model/Profile.php:846 #, php-format msgid "OpenWebAuth: %1$s welcomes %2$s" msgstr "" +#: src/Model/Profile.php:978 +msgid "Hometown:" +msgstr "" + +#: src/Model/Profile.php:979 +msgid "Marital Status:" +msgstr "" + +#: src/Model/Profile.php:980 +msgid "With:" +msgstr "" + +#: src/Model/Profile.php:981 +msgid "Since:" +msgstr "" + +#: src/Model/Profile.php:982 +msgid "Sexual Preference:" +msgstr "" + +#: src/Model/Profile.php:983 +msgid "Political Views:" +msgstr "" + +#: src/Model/Profile.php:984 +msgid "Religious Views:" +msgstr "" + +#: src/Model/Profile.php:985 +msgid "Likes:" +msgstr "" + +#: src/Model/Profile.php:986 +msgid "Dislikes:" +msgstr "" + +#: src/Model/Profile.php:987 +msgid "Title/Description:" +msgstr "" + +#: src/Model/Profile.php:988 src/Module/Admin/Summary.php:234 +msgid "Summary" +msgstr "" + +#: src/Model/Profile.php:989 +msgid "Musical interests" +msgstr "" + +#: src/Model/Profile.php:990 +msgid "Books, literature" +msgstr "" + +#: src/Model/Profile.php:991 +msgid "Television" +msgstr "" + +#: src/Model/Profile.php:992 +msgid "Film/dance/culture/entertainment" +msgstr "" + +#: src/Model/Profile.php:993 +msgid "Hobbies/Interests" +msgstr "" + +#: src/Model/Profile.php:994 +msgid "Love/romance" +msgstr "" + +#: src/Model/Profile.php:995 +msgid "Work/employment" +msgstr "" + +#: src/Model/Profile.php:996 +msgid "School/education" +msgstr "" + +#: src/Model/Profile.php:997 +msgid "Contact information and Social Networks" +msgstr "" + #: src/Model/Storage/FilesystemConfig.php:77 msgid "Storage base path" msgstr "" @@ -6607,10 +6687,6 @@ msgstr "" msgid "Server Settings" msgstr "" -#: src/Module/Admin/Summary.php:234 src/Repository/ProfileField.php:290 -msgid "Summary" -msgstr "" - #: src/Module/Admin/Summary.php:236 msgid "Registered users" msgstr "" @@ -7731,7 +7807,7 @@ msgid "Sort by post received date" msgstr "" #: src/Module/Conversation/Network.php:250 -#: src/Module/Settings/Profile/Index.php:228 +#: src/Module/Settings/Profile/Index.php:226 msgid "Personal" msgstr "" @@ -7955,7 +8031,7 @@ msgid "Twitter Source / Tweet URL (requires API key)" msgstr "" #: src/Module/Debug/Feed.php:38 src/Module/Filer/SaveTag.php:40 -#: src/Module/Settings/Profile/Index.php:142 +#: src/Module/Settings/Profile/Index.php:140 msgid "You must be logged in to use this module" msgstr "" @@ -8718,12 +8794,12 @@ msgstr "" msgid "Birthday:" msgstr "" -#: src/Module/Profile/Profile.php:167 src/Module/Settings/Profile/Index.php:246 +#: src/Module/Profile/Profile.php:167 src/Module/Settings/Profile/Index.php:244 #: src/Util/Temporal.php:165 msgid "Age: " msgstr "" -#: src/Module/Profile/Profile.php:167 src/Module/Settings/Profile/Index.php:246 +#: src/Module/Profile/Profile.php:167 src/Module/Settings/Profile/Index.php:244 #: src/Util/Temporal.php:165 #, php-format msgid "%d year old" @@ -9298,133 +9374,133 @@ msgstr "" msgid "Beginning of week:" msgstr "" -#: src/Module/Settings/Profile/Index.php:83 +#: src/Module/Settings/Profile/Index.php:84 msgid "Profile Name is required." msgstr "" -#: src/Module/Settings/Profile/Index.php:134 +#: src/Module/Settings/Profile/Index.php:132 msgid "Profile couldn't be updated." msgstr "" -#: src/Module/Settings/Profile/Index.php:173 -#: src/Module/Settings/Profile/Index.php:193 +#: src/Module/Settings/Profile/Index.php:171 +#: src/Module/Settings/Profile/Index.php:191 msgid "Label:" msgstr "" -#: src/Module/Settings/Profile/Index.php:174 -#: src/Module/Settings/Profile/Index.php:194 +#: src/Module/Settings/Profile/Index.php:172 +#: src/Module/Settings/Profile/Index.php:192 msgid "Value:" msgstr "" -#: src/Module/Settings/Profile/Index.php:184 -#: src/Module/Settings/Profile/Index.php:204 +#: src/Module/Settings/Profile/Index.php:182 +#: src/Module/Settings/Profile/Index.php:202 msgid "Field Permissions" msgstr "" -#: src/Module/Settings/Profile/Index.php:185 -#: src/Module/Settings/Profile/Index.php:205 +#: src/Module/Settings/Profile/Index.php:183 +#: src/Module/Settings/Profile/Index.php:203 msgid "(click to open/close)" msgstr "" -#: src/Module/Settings/Profile/Index.php:191 +#: src/Module/Settings/Profile/Index.php:189 msgid "Add a new profile field" msgstr "" -#: src/Module/Settings/Profile/Index.php:221 +#: src/Module/Settings/Profile/Index.php:219 msgid "Profile Actions" msgstr "" -#: src/Module/Settings/Profile/Index.php:222 +#: src/Module/Settings/Profile/Index.php:220 msgid "Edit Profile Details" msgstr "" -#: src/Module/Settings/Profile/Index.php:224 +#: src/Module/Settings/Profile/Index.php:222 msgid "Change Profile Photo" msgstr "" -#: src/Module/Settings/Profile/Index.php:229 +#: src/Module/Settings/Profile/Index.php:227 msgid "Profile picture" msgstr "" -#: src/Module/Settings/Profile/Index.php:230 +#: src/Module/Settings/Profile/Index.php:228 msgid "Location" msgstr "" -#: src/Module/Settings/Profile/Index.php:231 src/Util/Temporal.php:93 +#: src/Module/Settings/Profile/Index.php:229 src/Util/Temporal.php:93 #: src/Util/Temporal.php:95 msgid "Miscellaneous" msgstr "" -#: src/Module/Settings/Profile/Index.php:232 +#: src/Module/Settings/Profile/Index.php:230 msgid "Custom Profile Fields" msgstr "" -#: src/Module/Settings/Profile/Index.php:234 src/Module/Welcome.php:58 +#: src/Module/Settings/Profile/Index.php:232 src/Module/Welcome.php:58 msgid "Upload Profile Photo" msgstr "" -#: src/Module/Settings/Profile/Index.php:238 +#: src/Module/Settings/Profile/Index.php:236 msgid "Display name:" msgstr "" -#: src/Module/Settings/Profile/Index.php:241 +#: src/Module/Settings/Profile/Index.php:239 msgid "Street Address:" msgstr "" -#: src/Module/Settings/Profile/Index.php:242 +#: src/Module/Settings/Profile/Index.php:240 msgid "Locality/City:" msgstr "" -#: src/Module/Settings/Profile/Index.php:243 +#: src/Module/Settings/Profile/Index.php:241 msgid "Region/State:" msgstr "" -#: src/Module/Settings/Profile/Index.php:244 +#: src/Module/Settings/Profile/Index.php:242 msgid "Postal/Zip Code:" msgstr "" -#: src/Module/Settings/Profile/Index.php:245 +#: src/Module/Settings/Profile/Index.php:243 msgid "Country:" msgstr "" -#: src/Module/Settings/Profile/Index.php:247 +#: src/Module/Settings/Profile/Index.php:245 msgid "XMPP (Jabber) address:" msgstr "" -#: src/Module/Settings/Profile/Index.php:247 +#: src/Module/Settings/Profile/Index.php:245 msgid "The XMPP address will be published so that people can follow you there." msgstr "" -#: src/Module/Settings/Profile/Index.php:248 +#: src/Module/Settings/Profile/Index.php:246 msgid "Matrix (Element) address:" msgstr "" -#: src/Module/Settings/Profile/Index.php:248 +#: src/Module/Settings/Profile/Index.php:246 msgid "" "The Matrix address will be published so that people can follow you there." msgstr "" -#: src/Module/Settings/Profile/Index.php:249 +#: src/Module/Settings/Profile/Index.php:247 msgid "Homepage URL:" msgstr "" -#: src/Module/Settings/Profile/Index.php:250 +#: src/Module/Settings/Profile/Index.php:248 msgid "Public Keywords:" msgstr "" -#: src/Module/Settings/Profile/Index.php:250 +#: src/Module/Settings/Profile/Index.php:248 msgid "(Used for suggesting potential friends, can be seen by others)" msgstr "" -#: src/Module/Settings/Profile/Index.php:251 +#: src/Module/Settings/Profile/Index.php:249 msgid "Private Keywords:" msgstr "" -#: src/Module/Settings/Profile/Index.php:251 +#: src/Module/Settings/Profile/Index.php:249 msgid "(Used for searching profiles, never shown to others)" msgstr "" -#: src/Module/Settings/Profile/Index.php:252 +#: src/Module/Settings/Profile/Index.php:250 #, php-format msgid "" "

Custom fields appear on your profile page.

\n" @@ -10124,15 +10200,15 @@ msgstr "" msgid "%s created a new post" msgstr "" -#: src/Navigation/Notifications/Factory/Introduction.php:134 +#: src/Navigation/Notifications/Factory/Introduction.php:133 msgid "Friend Suggestion" msgstr "" -#: src/Navigation/Notifications/Factory/Introduction.php:160 +#: src/Navigation/Notifications/Factory/Introduction.php:159 msgid "Friend/Connect Request" msgstr "" -#: src/Navigation/Notifications/Factory/Introduction.php:160 +#: src/Navigation/Notifications/Factory/Introduction.php:159 msgid "New Follower" msgstr "" @@ -10483,82 +10559,6 @@ msgstr "" msgid "The folder view/smarty3/ must be writable by webserver." msgstr "" -#: src/Repository/ProfileField.php:280 -msgid "Hometown:" -msgstr "" - -#: src/Repository/ProfileField.php:281 -msgid "Marital Status:" -msgstr "" - -#: src/Repository/ProfileField.php:282 -msgid "With:" -msgstr "" - -#: src/Repository/ProfileField.php:283 -msgid "Since:" -msgstr "" - -#: src/Repository/ProfileField.php:284 -msgid "Sexual Preference:" -msgstr "" - -#: src/Repository/ProfileField.php:285 -msgid "Political Views:" -msgstr "" - -#: src/Repository/ProfileField.php:286 -msgid "Religious Views:" -msgstr "" - -#: src/Repository/ProfileField.php:287 -msgid "Likes:" -msgstr "" - -#: src/Repository/ProfileField.php:288 -msgid "Dislikes:" -msgstr "" - -#: src/Repository/ProfileField.php:289 -msgid "Title/Description:" -msgstr "" - -#: src/Repository/ProfileField.php:291 -msgid "Musical interests" -msgstr "" - -#: src/Repository/ProfileField.php:292 -msgid "Books, literature" -msgstr "" - -#: src/Repository/ProfileField.php:293 -msgid "Television" -msgstr "" - -#: src/Repository/ProfileField.php:294 -msgid "Film/dance/culture/entertainment" -msgstr "" - -#: src/Repository/ProfileField.php:295 -msgid "Hobbies/Interests" -msgstr "" - -#: src/Repository/ProfileField.php:296 -msgid "Love/romance" -msgstr "" - -#: src/Repository/ProfileField.php:297 -msgid "Work/employment" -msgstr "" - -#: src/Repository/ProfileField.php:298 -msgid "School/education" -msgstr "" - -#: src/Repository/ProfileField.php:299 -msgid "Contact information and Social Networks" -msgstr "" - #: src/Security/Authentication.php:209 msgid "Login failed." msgstr "" From f7603b0b6aad54d1bebd8efa249ccb36c69c5e3d Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 18 Oct 2021 00:35:06 +0200 Subject: [PATCH 19/22] Try to delete all data at the end of the test .. --- .../ProfileField/Depository/ProfileFieldTest.php | 10 ++++++++++ .../PermissionSet/Depository/PermissionSetTest.php | 2 ++ 2 files changed, 12 insertions(+) diff --git a/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php b/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php index c5350d0fe..2194bc523 100644 --- a/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php +++ b/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php @@ -4,6 +4,7 @@ namespace Friendica\Test\src\Profile\ProfileField\Depository; use Dice\Dice; use Friendica\Database\Database; +use Friendica\Profile\ProfileField\Collection\ProfileFields; use Friendica\Profile\ProfileField\Depository\ProfileField as ProfileFieldDepository; use Friendica\Profile\ProfileField\Entity\ProfileField; use Friendica\Profile\ProfileField\Exception\ProfileFieldPersistenceException; @@ -79,6 +80,9 @@ class ProfileFieldTest extends DatabaseTest $selectedProfileField = $this->depository->selectOneById($savedProfileField->id); self::assertEquals($savedProfileField, $selectedProfileField); + + $profileFields = new ProfileFields([$selectedProfileField]); + $this->depository->deleteCollection($profileFields); } /** @@ -112,6 +116,9 @@ class ProfileFieldTest extends DatabaseTest // Even using the ID of the old, saved ProfileField returns the right instance $updatedFromOldProfileField = $this->depository->selectOneById($savedProfileField->id); self::assertEquals(66, $updatedFromOldProfileField->order); + + $profileFields = new ProfileFields([$updatedFromOldProfileField]); + $this->depository->deleteCollection($profileFields); } /** @@ -143,5 +150,8 @@ class ProfileFieldTest extends DatabaseTest self::assertEquals($this->permissionSetDepository->selectPublicForUser(42), $publicProfileField->permissionSet); self::assertEquals('another', $publicProfileField->value); self::assertEquals(5, $publicProfileField->order); + + $profileFields = new ProfileFields([$publicProfileField]); + $this->depository->deleteCollection($profileFields); } } diff --git a/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php b/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php index 3f36e3bca..51ed99390 100644 --- a/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php +++ b/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php @@ -70,5 +70,7 @@ class PermissionSetTest extends DatabaseTest $permissionSetSavedSelected = $this->depository->selectOneById($savedPermissionSet->id, 42); self::assertEquals($savedPermissionSet, $permissionSetSavedSelected); + + $this->dba->delete('permissionset', ['1=1']); } } From 81122bfc3baf69b3e96179867aee166aaf571f5f Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 18 Oct 2021 20:50:53 +0200 Subject: [PATCH 20/22] Fix tests --- .../Depository/ProfileFieldTest.php | 35 ++++--------------- .../Depository/PermissionSetTest.php | 26 ++++---------- 2 files changed, 14 insertions(+), 47 deletions(-) diff --git a/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php b/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php index 2194bc523..87ea6ca9c 100644 --- a/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php +++ b/tests/src/Profile/ProfileField/Depository/ProfileFieldTest.php @@ -2,20 +2,17 @@ namespace Friendica\Test\src\Profile\ProfileField\Depository; -use Dice\Dice; -use Friendica\Database\Database; use Friendica\Profile\ProfileField\Collection\ProfileFields; use Friendica\Profile\ProfileField\Depository\ProfileField as ProfileFieldDepository; -use Friendica\Profile\ProfileField\Entity\ProfileField; use Friendica\Profile\ProfileField\Exception\ProfileFieldPersistenceException; use Friendica\Profile\ProfileField\Factory\ProfileField as ProfileFieldFactory; use Friendica\Security\PermissionSet\Depository\PermissionSet; use Friendica\Security\PermissionSet\Factory\PermissionSet as PermissionSetFactory; use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository; -use Friendica\Test\DatabaseTest; -use Friendica\Test\Util\Database\StaticDatabase; +use Friendica\Test\FixtureTest; +use Friendica\DI; -class ProfileFieldTest extends DatabaseTest +class ProfileFieldTest extends FixtureTest { /** @var ProfileFieldDepository */ private $depository; @@ -25,22 +22,15 @@ class ProfileFieldTest extends DatabaseTest private $permissionSetFactory; /** @var PermissionSetDepository */ private $permissionSetDepository; - /** @var Database */ - private $dba; public function setUp(): void { parent::setUp(); - $dice = (new Dice()) - ->addRules(include __DIR__ . '/../../../../../static/dependencies.config.php') - ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]); - - $this->depository = $dice->create(ProfileFieldDepository::class); - $this->factory = $dice->create(ProfileFieldFactory::class); - $this->permissionSetFactory = $dice->create(PermissionSetFactory::class); - $this->permissionSetDepository = $dice->create(PermissionSetDepository::class); - $this->dba = $dice->create(Database::class); + $this->depository = DI::profileField(); + $this->factory = DI::profileFieldFactory(); + $this->permissionSetFactory = DI::permissionSetFactory(); + $this->permissionSetDepository = DI::permissionSet(); } /** @@ -51,8 +41,6 @@ class ProfileFieldTest extends DatabaseTest self::expectExceptionMessage('PermissionSet needs to be saved first.'); self::expectException(ProfileFieldPersistenceException::class); - $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', $this->dba); - $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetFactory->createFromString(42, '', '<~>')); self::assertEquals($profileField->uid, $profileField->permissionSet->uid); @@ -65,8 +53,6 @@ class ProfileFieldTest extends DatabaseTest */ public function testSaveNew() { - $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', $this->dba); - $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetDepository->save($this->permissionSetFactory->createFromString(42, '', '<~>'))); self::assertEquals($profileField->uid, $profileField->permissionSet->uid); @@ -76,7 +62,6 @@ class ProfileFieldTest extends DatabaseTest self::assertNotNull($savedProfileField->id); self::assertNull($profileField->id); - /** @var ProfileField $selectedProfileField */ $selectedProfileField = $this->depository->selectOneById($savedProfileField->id); self::assertEquals($savedProfileField, $selectedProfileField); @@ -90,8 +75,6 @@ class ProfileFieldTest extends DatabaseTest */ public function testUpdateOrder() { - $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', $this->dba); - $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetDepository->save($this->permissionSetFactory->createFromString(42, '', '<~>'))); self::assertEquals($profileField->uid, $profileField->permissionSet->uid); @@ -101,7 +84,6 @@ class ProfileFieldTest extends DatabaseTest self::assertNotNull($savedProfileField->id); self::assertNull($profileField->id); - /** @var ProfileField $selectedProfileField */ $selectedProfileField = $this->depository->selectOneById($savedProfileField->id); self::assertEquals($savedProfileField, $selectedProfileField); @@ -126,8 +108,6 @@ class ProfileFieldTest extends DatabaseTest */ public function testUpdate() { - $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', $this->dba); - $profileField = $this->factory->createFromValues(42, 0, 'public', 'value', $this->permissionSetDepository->save($this->permissionSetFactory->createFromString(42, '', '<~>'))); self::assertEquals($profileField->uid, $profileField->permissionSet->uid); @@ -137,7 +117,6 @@ class ProfileFieldTest extends DatabaseTest self::assertNotNull($savedProfileField->id); self::assertNull($profileField->id); - /** @var ProfileField $selectedProfileField */ $selectedProfileField = $this->depository->selectOneById($savedProfileField->id); self::assertEquals($savedProfileField, $selectedProfileField); diff --git a/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php b/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php index 51ed99390..596b1e6c1 100644 --- a/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php +++ b/tests/src/Security/PermissionSet/Depository/PermissionSetTest.php @@ -2,40 +2,32 @@ namespace Friendica\Test\src\Security\PermissionSet\Depository; -use Dice\Dice; -use Friendica\Database\Database; use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository; +use Friendica\Security\PermissionSet\Entity\PermissionSet; use Friendica\Security\PermissionSet\Factory\PermissionSet as PermissionSetFactory; -use Friendica\Test\DatabaseTest; -use Friendica\Test\Util\Database\StaticDatabase; +use Friendica\Test\FixtureTest; +use Friendica\DI; -class PermissionSetTest extends DatabaseTest +class PermissionSetTest extends FixtureTest { /** @var PermissionSetDepository */ private $depository; /** @var PermissionSetFactory */ private $factory; - /** @var Database */ - private $dba; public function setUp(): void { parent::setUp(); - $dice = (new Dice()) - ->addRules(include __DIR__ . '/../../../../../static/dependencies.config.php') - ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true]); - - $this->depository = $dice->create(PermissionSetDepository::class); - $this->factory = $dice->create(PermissionSetFactory::class); - $this->dba = $dice->create(Database::class); + $this->depository = DI::permissionSet(); + $this->factory = DI::permissionSetFactory(); } public function testSelectOneByIdPublic() { $permissionSet = $this->depository->selectPublicForUser(1); - $this->assertInstanceOf(\Friendica\Security\PermissionSet\Entity\PermissionSet::class, $permissionSet); + $this->assertInstanceOf(PermissionSet::class, $permissionSet); self::assertEmpty($permissionSet->allow_cid); self::assertEmpty($permissionSet->allow_gid); self::assertEmpty($permissionSet->deny_cid); @@ -49,8 +41,6 @@ class PermissionSetTest extends DatabaseTest */ public function testSaving() { - $this->loadFixture(__DIR__ . '/../../../../datasets/api.fixture.php', $this->dba); - $permissionSet = $this->factory->createFromString(42, '', '<~>'); $permissionSet = $this->depository->selectOrCreate($permissionSet); @@ -70,7 +60,5 @@ class PermissionSetTest extends DatabaseTest $permissionSetSavedSelected = $this->depository->selectOneById($savedPermissionSet->id, 42); self::assertEquals($savedPermissionSet, $permissionSetSavedSelected); - - $this->dba->delete('permissionset', ['1=1']); } } From d470a6a9818d97f5b48a4b29b0e32ba3e89a0223 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 18 Oct 2021 23:17:40 +0200 Subject: [PATCH 21/22] Push DB Version --- database.sql | 2 +- static/dbstructure.config.php | 2 +- static/dbview.config.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/database.sql b/database.sql index 4fd6ba39f..933502ef8 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2021.12-dev (Siberian Iris) --- DB_UPDATE_VERSION 1439 +-- DB_UPDATE_VERSION 1440 -- ------------------------------------------ diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 6f1986507..ec7c0e3ef 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -55,7 +55,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1439); + define('DB_UPDATE_VERSION', 1440); } return [ diff --git a/static/dbview.config.php b/static/dbview.config.php index f8d11955e..a12d5747a 100644 --- a/static/dbview.config.php +++ b/static/dbview.config.php @@ -1060,7 +1060,7 @@ "deny_cid" => ["permissionset", "deny_cid"], "deny_gid" => ["permissionset", "deny_gid"], "created" => ["profile_field", "created"], - "updated" => ["profile_field", "updated"], + "edited" => ["profile_field", "edited"], ], "query" => "FROM `profile_field` INNER JOIN `permissionset` ON `permissionset`.`id` = `profile_field`.`psid`" From cf38b62993b52a9b9952ae94898d78ed856901e4 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 18 Oct 2021 23:31:33 +0200 Subject: [PATCH 22/22] public fields fix --- update.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/update.php b/update.php index f5d310830..64bd7a9c3 100644 --- a/update.php +++ b/update.php @@ -55,6 +55,7 @@ use Friendica\Model\Photo; use Friendica\Model\Post; use Friendica\Model\Profile; use Friendica\Model\Storage; +use Friendica\Security\PermissionSet\Depository\PermissionSet; use Friendica\Worker\Delivery; // Post-update script of PR 5751 @@ -1028,3 +1029,12 @@ function update_1439() } DBA::close($intros); } + +function update_1440() +{ + // Fix wrong public permissionset + DBA::p("UPDATE `profile_field` SET `psid` = ? WHERE psid IN (SELECT `id` FROM `permissionset` WHERE `id` != ? AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '')", PermissionSet::PUBLIC, PermissionSet::PUBLIC); + DBA::delete('permissionset', ["`id` != ? AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = ''", PermissionSet::PUBLIC]); + + return Update::SUCCESS; +}