diff options
author | John Molakvoæ <skjnldsv@users.noreply.github.com> | 2024-03-15 13:03:34 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-15 13:03:34 +0100 |
commit | 9338ef36ded767f2c35b7ec575b351859420ed09 (patch) | |
tree | 65c53c6a36f300859dc22b2d423275bcf2911367 /lib/private/Profile | |
parent | 6b09a79227a5dc98aa4620c6e5e15b610a06c806 (diff) | |
parent | df1cd1ba7e6e1f6e66a2b3229b5c082f1b81162e (diff) | |
download | nextcloud-server-9338ef36ded767f2c35b7ec575b351859420ed09.tar.gz nextcloud-server-9338ef36ded767f2c35b7ec575b351859420ed09.zip |
Merge branch 'master' into refactor/OC-Server-getShareManager
Signed-off-by: John Molakvoæ <skjnldsv@users.noreply.github.com>
Diffstat (limited to 'lib/private/Profile')
-rw-r--r-- | lib/private/Profile/Actions/EmailAction.php | 21 | ||||
-rw-r--r-- | lib/private/Profile/Actions/FediverseAction.php | 16 | ||||
-rw-r--r-- | lib/private/Profile/Actions/PhoneAction.php | 21 | ||||
-rw-r--r-- | lib/private/Profile/Actions/TwitterAction.php | 23 | ||||
-rw-r--r-- | lib/private/Profile/Actions/WebsiteAction.php | 21 | ||||
-rw-r--r-- | lib/private/Profile/ProfileManager.php | 150 |
6 files changed, 78 insertions, 174 deletions
diff --git a/lib/private/Profile/Actions/EmailAction.php b/lib/private/Profile/Actions/EmailAction.php index 8ab4939b515..a676f6e228e 100644 --- a/lib/private/Profile/Actions/EmailAction.php +++ b/lib/private/Profile/Actions/EmailAction.php @@ -33,26 +33,13 @@ use OCP\L10N\IFactory; use OCP\Profile\ILinkAction; class EmailAction implements ILinkAction { - /** @var string */ - private $value; - - /** @var IAccountManager */ - private $accountManager; - - /** @var IFactory */ - private $l10nFactory; - - /** @var IUrlGenerator */ - private $urlGenerator; + private string $value = ''; public function __construct( - IAccountManager $accountManager, - IFactory $l10nFactory, - IURLGenerator $urlGenerator + private IAccountManager $accountManager, + private IFactory $l10nFactory, + private IURLGenerator $urlGenerator, ) { - $this->accountManager = $accountManager; - $this->l10nFactory = $l10nFactory; - $this->urlGenerator = $urlGenerator; } public function preload(IUser $targetUser): void { diff --git a/lib/private/Profile/Actions/FediverseAction.php b/lib/private/Profile/Actions/FediverseAction.php index ed3fcd80b52..4c73f785dd0 100644 --- a/lib/private/Profile/Actions/FediverseAction.php +++ b/lib/private/Profile/Actions/FediverseAction.php @@ -26,27 +26,21 @@ declare(strict_types=1); namespace OC\Profile\Actions; -use function Safe\substr; use OCP\Accounts\IAccountManager; use OCP\IURLGenerator; use OCP\IUser; use OCP\L10N\IFactory; use OCP\Profile\ILinkAction; +use function substr; class FediverseAction implements ILinkAction { - private ?string $value = null; - private IAccountManager $accountManager; - private IFactory $l10nFactory; - private IURLGenerator $urlGenerator; + private string $value = ''; public function __construct( - IAccountManager $accountManager, - IFactory $l10nFactory, - IURLGenerator $urlGenerator + private IAccountManager $accountManager, + private IFactory $l10nFactory, + private IURLGenerator $urlGenerator, ) { - $this->accountManager = $accountManager; - $this->l10nFactory = $l10nFactory; - $this->urlGenerator = $urlGenerator; } public function preload(IUser $targetUser): void { diff --git a/lib/private/Profile/Actions/PhoneAction.php b/lib/private/Profile/Actions/PhoneAction.php index 6081a04ad7e..6a4b2dd49d4 100644 --- a/lib/private/Profile/Actions/PhoneAction.php +++ b/lib/private/Profile/Actions/PhoneAction.php @@ -33,26 +33,13 @@ use OCP\L10N\IFactory; use OCP\Profile\ILinkAction; class PhoneAction implements ILinkAction { - /** @var string */ - private $value; - - /** @var IAccountManager */ - private $accountManager; - - /** @var IFactory */ - private $l10nFactory; - - /** @var IUrlGenerator */ - private $urlGenerator; + private string $value = ''; public function __construct( - IAccountManager $accountManager, - IFactory $l10nFactory, - IURLGenerator $urlGenerator + private IAccountManager $accountManager, + private IFactory $l10nFactory, + private IURLGenerator $urlGenerator, ) { - $this->accountManager = $accountManager; - $this->l10nFactory = $l10nFactory; - $this->urlGenerator = $urlGenerator; } public function preload(IUser $targetUser): void { diff --git a/lib/private/Profile/Actions/TwitterAction.php b/lib/private/Profile/Actions/TwitterAction.php index 041da42e539..f7f57d4c6d1 100644 --- a/lib/private/Profile/Actions/TwitterAction.php +++ b/lib/private/Profile/Actions/TwitterAction.php @@ -26,34 +26,21 @@ declare(strict_types=1); namespace OC\Profile\Actions; -use function Safe\substr; use OCP\Accounts\IAccountManager; use OCP\IURLGenerator; use OCP\IUser; use OCP\L10N\IFactory; use OCP\Profile\ILinkAction; +use function substr; class TwitterAction implements ILinkAction { - /** @var string */ - private $value; - - /** @var IAccountManager */ - private $accountManager; - - /** @var IFactory */ - private $l10nFactory; - - /** @var IUrlGenerator */ - private $urlGenerator; + private string $value = ''; public function __construct( - IAccountManager $accountManager, - IFactory $l10nFactory, - IURLGenerator $urlGenerator + private IAccountManager $accountManager, + private IFactory $l10nFactory, + private IURLGenerator $urlGenerator, ) { - $this->accountManager = $accountManager; - $this->l10nFactory = $l10nFactory; - $this->urlGenerator = $urlGenerator; } public function preload(IUser $targetUser): void { diff --git a/lib/private/Profile/Actions/WebsiteAction.php b/lib/private/Profile/Actions/WebsiteAction.php index 6b052be57bd..22e2692c4c5 100644 --- a/lib/private/Profile/Actions/WebsiteAction.php +++ b/lib/private/Profile/Actions/WebsiteAction.php @@ -33,26 +33,13 @@ use OCP\L10N\IFactory; use OCP\Profile\ILinkAction; class WebsiteAction implements ILinkAction { - /** @var string */ - private $value; - - /** @var IAccountManager */ - private $accountManager; - - /** @var IFactory */ - private $l10nFactory; - - /** @var IUrlGenerator */ - private $urlGenerator; + private string $value = ''; public function __construct( - IAccountManager $accountManager, - IFactory $l10nFactory, - IURLGenerator $urlGenerator + private IAccountManager $accountManager, + private IFactory $l10nFactory, + private IURLGenerator $urlGenerator, ) { - $this->accountManager = $accountManager; - $this->l10nFactory = $l10nFactory; - $this->urlGenerator = $urlGenerator; } public function preload(IUser $targetUser): void { diff --git a/lib/private/Profile/ProfileManager.php b/lib/private/Profile/ProfileManager.php index f20ae74768e..c8fb780bbe8 100644 --- a/lib/private/Profile/ProfileManager.php +++ b/lib/private/Profile/ProfileManager.php @@ -26,62 +26,36 @@ declare(strict_types=1); namespace OC\Profile; -use function Safe\array_flip; -use function Safe\usort; use OC\AppFramework\Bootstrap\Coordinator; use OC\Core\Db\ProfileConfig; use OC\Core\Db\ProfileConfigMapper; use OC\KnownUser\KnownUserService; use OC\Profile\Actions\EmailAction; +use OC\Profile\Actions\FediverseAction; use OC\Profile\Actions\PhoneAction; use OC\Profile\Actions\TwitterAction; -use OC\Profile\Actions\FediverseAction; use OC\Profile\Actions\WebsiteAction; use OCP\Accounts\IAccountManager; use OCP\Accounts\PropertyDoesNotExistException; use OCP\App\IAppManager; use OCP\AppFramework\Db\DoesNotExistException; +use OCP\Cache\CappedMemoryCache; use OCP\IConfig; use OCP\IUser; use OCP\L10N\IFactory; use OCP\Profile\ILinkAction; -use OCP\Cache\CappedMemoryCache; +use OCP\Profile\IProfileManager; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; +use function array_flip; +use function usort; -class ProfileManager { - /** @var IAccountManager */ - private $accountManager; - - /** @var IAppManager */ - private $appManager; - - /** @var IConfig */ - private $config; - - /** @var ProfileConfigMapper */ - private $configMapper; - - /** @var ContainerInterface */ - private $container; - - /** @var KnownUserService */ - private $knownUserService; - - /** @var IFactory */ - private $l10nFactory; - - /** @var LoggerInterface */ - private $logger; - - /** @var Coordinator */ - private $coordinator; - +class ProfileManager implements IProfileManager { /** @var ILinkAction[] */ - private $actions = []; + private array $actions = []; /** @var null|ILinkAction[] */ - private $sortedActions = null; + private ?array $sortedActions = null; /** @var CappedMemoryCache<ProfileConfig> */ private CappedMemoryCache $configCache; @@ -112,32 +86,23 @@ class ProfileManager { ]; public function __construct( - IAccountManager $accountManager, - IAppManager $appManager, - IConfig $config, - ProfileConfigMapper $configMapper, - ContainerInterface $container, - KnownUserService $knownUserService, - IFactory $l10nFactory, - LoggerInterface $logger, - Coordinator $coordinator + private IAccountManager $accountManager, + private IAppManager $appManager, + private IConfig $config, + private ProfileConfigMapper $configMapper, + private ContainerInterface $container, + private KnownUserService $knownUserService, + private IFactory $l10nFactory, + private LoggerInterface $logger, + private Coordinator $coordinator, ) { - $this->accountManager = $accountManager; - $this->appManager = $appManager; - $this->config = $config; - $this->configMapper = $configMapper; - $this->container = $container; - $this->knownUserService = $knownUserService; - $this->l10nFactory = $l10nFactory; - $this->logger = $logger; - $this->coordinator = $coordinator; $this->configCache = new CappedMemoryCache(); } /** * If no user is passed as an argument return whether profile is enabled globally in `config.php` */ - public function isProfileEnabled(?IUser $user = null): ?bool { + public function isProfileEnabled(?IUser $user = null): bool { $profileEnabledGlobally = $this->config->getSystemValueBool('profile.enabled', true); if (empty($user) || !$profileEnabledGlobally) { @@ -145,7 +110,7 @@ class ProfileManager { } $account = $this->accountManager->getAccount($user); - return filter_var( + return (bool) filter_var( $account->getProperty(IAccountManager::PROPERTY_PROFILE_ENABLED)->getValue(), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE, @@ -229,57 +194,54 @@ class ProfileManager { * Return whether the profile parameter of the target user * is visible to the visiting user */ - private function isParameterVisible(string $paramId, IUser $targetUser, ?IUser $visitingUser): bool { + public function isProfileFieldVisible(string $profileField, IUser $targetUser, ?IUser $visitingUser): bool { try { $account = $this->accountManager->getAccount($targetUser); - $scope = $account->getProperty($paramId)->getScope(); + $scope = $account->getProperty($profileField)->getScope(); } catch (PropertyDoesNotExistException $e) { // Allow the exception as not all profile parameters are account properties } - $visibility = $this->getProfileConfig($targetUser, $visitingUser)[$paramId]['visibility']; + $visibility = $this->getProfileConfig($targetUser, $visitingUser)[$profileField]['visibility']; // Handle profile visibility and account property scope - switch ($visibility) { - case ProfileConfig::VISIBILITY_HIDE: - return false; - case ProfileConfig::VISIBILITY_SHOW_USERS_ONLY: - if (!empty($scope)) { - switch ($scope) { - case IAccountManager::SCOPE_PRIVATE: - return $visitingUser !== null && $this->knownUserService->isKnownToUser($targetUser->getUID(), $visitingUser->getUID()); - case IAccountManager::SCOPE_LOCAL: - case IAccountManager::SCOPE_FEDERATED: - case IAccountManager::SCOPE_PUBLISHED: - return $visitingUser !== null; - default: - return false; - } - } + + if ($visibility === self::VISIBILITY_SHOW_USERS_ONLY) { + if (empty($scope)) { return $visitingUser !== null; - case ProfileConfig::VISIBILITY_SHOW: - if (!empty($scope)) { - switch ($scope) { - case IAccountManager::SCOPE_PRIVATE: - return $visitingUser !== null && $this->knownUserService->isKnownToUser($targetUser->getUID(), $visitingUser->getUID()); - case IAccountManager::SCOPE_LOCAL: - case IAccountManager::SCOPE_FEDERATED: - case IAccountManager::SCOPE_PUBLISHED: - return true; - default: - return false; - } - } + } + + return match ($scope) { + IAccountManager::SCOPE_PRIVATE => $visitingUser !== null && $this->knownUserService->isKnownToUser($targetUser->getUID(), $visitingUser->getUID()), + IAccountManager::SCOPE_LOCAL, + IAccountManager::SCOPE_FEDERATED, + IAccountManager::SCOPE_PUBLISHED => $visitingUser !== null, + default => false, + }; + } + + if ($visibility === self::VISIBILITY_SHOW) { + if (empty($scope)) { return true; - default: - return false; + } + + return match ($scope) { + IAccountManager::SCOPE_PRIVATE => $visitingUser !== null && $this->knownUserService->isKnownToUser($targetUser->getUID(), $visitingUser->getUID()), + IAccountManager::SCOPE_LOCAL, + IAccountManager::SCOPE_FEDERATED, + IAccountManager::SCOPE_PUBLISHED => true, + default => false, + }; } + + return false; } /** * Return the profile parameters of the target user that are visible to the visiting user * in an associative array + * @return array{userId: string, address?: string|null, biography?: string|null, displayname?: string|null, headline?: string|null, isUserAvatarVisible?: bool, organisation?: string|null, role?: string|null, actions: list<array{id: string, icon: string, title: string, target: ?string}>} */ - public function getProfileParams(IUser $targetUser, ?IUser $visitingUser): array { + public function getProfileFields(IUser $targetUser, ?IUser $visitingUser): array { $account = $this->accountManager->getAccount($targetUser); // Initialize associative array of profile parameters @@ -297,14 +259,14 @@ class ProfileManager { case IAccountManager::PROPERTY_ORGANISATION: case IAccountManager::PROPERTY_ROLE: $profileParameters[$property] = - $this->isParameterVisible($property, $targetUser, $visitingUser) + $this->isProfileFieldVisible($property, $targetUser, $visitingUser) // Explicitly set to null when value is empty string ? ($account->getProperty($property)->getValue() ?: null) : null; break; case IAccountManager::PROPERTY_AVATAR: // Add avatar visibility - $profileParameters['isUserAvatarVisible'] = $this->isParameterVisible($property, $targetUser, $visitingUser); + $profileParameters['isUserAvatarVisible'] = $this->isProfileFieldVisible($property, $targetUser, $visitingUser); break; } } @@ -324,7 +286,7 @@ class ProfileManager { array_filter( $this->getActions($targetUser, $visitingUser), function (ILinkAction $action) use ($targetUser, $visitingUser) { - return $this->isParameterVisible($action->getId(), $targetUser, $visitingUser); + return $this->isProfileFieldVisible($action->getId(), $targetUser, $visitingUser); } ), ) @@ -356,12 +318,12 @@ class ProfileManager { // Construct the default config for actions $actionsConfig = []; foreach ($this->getActions($targetUser, $visitingUser) as $action) { - $actionsConfig[$action->getId()] = ['visibility' => ProfileConfig::DEFAULT_VISIBILITY]; + $actionsConfig[$action->getId()] = ['visibility' => self::DEFAULT_VISIBILITY]; } // Construct the default config for account properties $propertiesConfig = []; - foreach (ProfileConfig::DEFAULT_PROPERTY_VISIBILITY as $property => $visibility) { + foreach (self::DEFAULT_PROPERTY_VISIBILITY as $property => $visibility) { $propertiesConfig[$property] = ['visibility' => $visibility]; } |