diff --git a/core/Controller/ProfilePageController.php b/core/Controller/ProfilePageController.php index 5034a665684ff..eacdd14c932ee 100644 --- a/core/Controller/ProfilePageController.php +++ b/core/Controller/ProfilePageController.php @@ -101,7 +101,7 @@ public function index(string $targetUserId): TemplateResponse { $this->initialStateService->provideInitialState( 'profileParameters', - $this->profileManager->getProfileParams($targetUser, $visitingUser), + $this->profileManager->getProfileFields($targetUser, $visitingUser), ); $this->eventDispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($targetUserId)); diff --git a/core/Db/ProfileConfig.php b/core/Db/ProfileConfig.php index 3ebfe02ca79fa..b399874adf48e 100644 --- a/core/Db/ProfileConfig.php +++ b/core/Db/ProfileConfig.php @@ -29,7 +29,6 @@ use function Safe\json_decode; use function Safe\json_encode; use \JsonSerializable; -use OCP\Accounts\IAccountManager; use OCP\AppFramework\Db\Entity; use OCP\Profile\ParameterDoesNotExistException; @@ -40,53 +39,6 @@ * @method void setConfig(string $config) */ class ProfileConfig extends Entity implements JsonSerializable { - /** - * Visible to users, guests, and public access - * - * @since 23.0.0 - */ - public const VISIBILITY_SHOW = 'show'; - - /** - * Visible to users and guests - * - * @since 23.0.0 - */ - public const VISIBILITY_SHOW_USERS_ONLY = 'show_users_only'; - - /** - * Visible to nobody - * - * @since 23.0.0 - */ - public const VISIBILITY_HIDE = 'hide'; - - /** - * Default account property visibility - * - * @since 23.0.0 - */ - public const DEFAULT_PROPERTY_VISIBILITY = [ - IAccountManager::PROPERTY_ADDRESS => self::VISIBILITY_SHOW_USERS_ONLY, - IAccountManager::PROPERTY_AVATAR => self::VISIBILITY_SHOW, - IAccountManager::PROPERTY_BIOGRAPHY => self::VISIBILITY_SHOW, - IAccountManager::PROPERTY_DISPLAYNAME => self::VISIBILITY_SHOW, - IAccountManager::PROPERTY_HEADLINE => self::VISIBILITY_SHOW, - IAccountManager::PROPERTY_ORGANISATION => self::VISIBILITY_SHOW, - IAccountManager::PROPERTY_ROLE => self::VISIBILITY_SHOW, - IAccountManager::PROPERTY_EMAIL => self::VISIBILITY_SHOW_USERS_ONLY, - IAccountManager::PROPERTY_PHONE => self::VISIBILITY_SHOW_USERS_ONLY, - IAccountManager::PROPERTY_TWITTER => self::VISIBILITY_SHOW, - IAccountManager::PROPERTY_WEBSITE => self::VISIBILITY_SHOW, - ]; - - /** - * Default visibility - * - * @since 23.0.0 - */ - public const DEFAULT_VISIBILITY = self::VISIBILITY_SHOW_USERS_ONLY; - /** @var string */ protected $userId; diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 9d0570d349c6d..401d240da0638 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -552,6 +552,7 @@ 'OCP\\Preview\\IVersionedPreviewFile' => $baseDir . '/lib/public/Preview/IVersionedPreviewFile.php', 'OCP\\Profile\\BeforeTemplateRenderedEvent' => $baseDir . '/lib/public/Profile/BeforeTemplateRenderedEvent.php', 'OCP\\Profile\\ILinkAction' => $baseDir . '/lib/public/Profile/ILinkAction.php', + 'OCP\\Profile\\IProfileManager' => $baseDir . '/lib/public/Profile/IProfileManager.php', 'OCP\\Profile\\ParameterDoesNotExistException' => $baseDir . '/lib/public/Profile/ParameterDoesNotExistException.php', 'OCP\\Profiler\\IProfile' => $baseDir . '/lib/public/Profiler/IProfile.php', 'OCP\\Profiler\\IProfiler' => $baseDir . '/lib/public/Profiler/IProfiler.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 63ad21216a4a6..7be62a3cf62cd 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -585,6 +585,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Preview\\IVersionedPreviewFile' => __DIR__ . '/../../..' . '/lib/public/Preview/IVersionedPreviewFile.php', 'OCP\\Profile\\BeforeTemplateRenderedEvent' => __DIR__ . '/../../..' . '/lib/public/Profile/BeforeTemplateRenderedEvent.php', 'OCP\\Profile\\ILinkAction' => __DIR__ . '/../../..' . '/lib/public/Profile/ILinkAction.php', + 'OCP\\Profile\\IProfileManager' => __DIR__ . '/../../..' . '/lib/public/Profile/IProfileManager.php', 'OCP\\Profile\\ParameterDoesNotExistException' => __DIR__ . '/../../..' . '/lib/public/Profile/ParameterDoesNotExistException.php', 'OCP\\Profiler\\IProfile' => __DIR__ . '/../../..' . '/lib/public/Profiler/IProfile.php', 'OCP\\Profiler\\IProfiler' => __DIR__ . '/../../..' . '/lib/public/Profiler/IProfiler.php', diff --git a/lib/private/Profile/ProfileManager.php b/lib/private/Profile/ProfileManager.php index 8fa652712055e..39c51ea0e7795 100644 --- a/lib/private/Profile/ProfileManager.php +++ b/lib/private/Profile/ProfileManager.php @@ -26,8 +26,9 @@ namespace OC\Profile; -use function Safe\array_flip; -use function Safe\usort; +use OCP\Profile\IProfileManager; +use function array_flip; +use function usort; use OC\AppFramework\Bootstrap\Coordinator; use OC\Core\Db\ProfileConfig; use OC\Core\Db\ProfileConfigMapper; @@ -49,7 +50,7 @@ use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; -class ProfileManager { +class ProfileManager implements IProfileManager { /** @var ILinkAction[] */ private array $actions = []; @@ -101,7 +102,7 @@ public function __construct( /** * 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) { @@ -109,7 +110,7 @@ public function isProfileEnabled(?IUser $user = null): ?bool { } $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, @@ -193,18 +194,18 @@ private function getActions(IUser $targetUser, ?IUser $visitingUser): array { * 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 - if ($visibility === ProfileConfig::VISIBILITY_SHOW_USERS_ONLY) { + if ($visibility === self::VISIBILITY_SHOW_USERS_ONLY) { if (empty($scope)) { return $visitingUser !== null; } @@ -218,10 +219,10 @@ private function isParameterVisible(string $paramId, IUser $targetUser, ?IUser $ }; } - if ($visibility === ProfileConfig::VISIBILITY_SHOW) { + if ($visibility === self::VISIBILITY_SHOW) { if (empty($scope)) { return true; - }; + } return match ($scope) { IAccountManager::SCOPE_PRIVATE => $visitingUser !== null && $this->knownUserService->isKnownToUser($targetUser->getUID(), $visitingUser->getUID()), @@ -238,8 +239,9 @@ private function isParameterVisible(string $paramId, IUser $targetUser, ?IUser $ /** * 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} */ - 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 @@ -257,14 +259,14 @@ public function getProfileParams(IUser $targetUser, ?IUser $visitingUser): array 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; } } @@ -284,7 +286,7 @@ function (ILinkAction $action) { 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); } ), ) @@ -316,12 +318,12 @@ private function getDefaultProfileConfig(IUser $targetUser, ?IUser $visitingUser // 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]; } diff --git a/lib/private/Server.php b/lib/private/Server.php index b8004670634ab..37b7669f62477 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -130,6 +130,7 @@ use OC\Preview\GeneratorHelper; use OC\Preview\IMagickSupport; use OC\Preview\MimeIconProvider; +use OC\Profile\ProfileManager; use OC\Remote\Api\ApiFactory; use OC\Remote\InstanceFactory; use OC\RichObjectStrings\Validator; @@ -235,6 +236,7 @@ use OCP\Mail\IMailer; use OCP\OCM\IOCMDiscoveryService; use OCP\OCM\IOCMProvider; +use OCP\Profile\IProfileManager; use OCP\Remote\Api\IApiFactory; use OCP\Remote\IInstanceFactory; use OCP\RichObjectStrings\IValidator; @@ -1434,6 +1436,8 @@ public function __construct($webRoot, \OC\Config $config) { $this->registerAlias(ISetupCheckManager::class, SetupCheckManager::class); + $this->registerAlias(IProfileManager::class, ProfileManager::class); + $this->connectDispatcher(); } diff --git a/lib/public/Profile/IProfileManager.php b/lib/public/Profile/IProfileManager.php new file mode 100644 index 0000000000000..996e49d116e7a --- /dev/null +++ b/lib/public/Profile/IProfileManager.php @@ -0,0 +1,106 @@ + + * + * @author Joas Schilling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCP\Profile; + +use OCP\Accounts\IAccountManager; +use OCP\IUser; + +/** + * @since 28.0.0 + */ +interface IProfileManager { + /** + * Visible to users, guests, and public access + * + * @since 28.0.0 + */ + public const VISIBILITY_SHOW = 'show'; + + /** + * Visible to users and guests + * + * @since 28.0.0 + */ + public const VISIBILITY_SHOW_USERS_ONLY = 'show_users_only'; + + /** + * Visible to nobody + * + * @since 28.0.0 + */ + public const VISIBILITY_HIDE = 'hide'; + + /** + * Default account property visibility + * + * @since 28.0.0 + */ + public const DEFAULT_PROPERTY_VISIBILITY = [ + IAccountManager::PROPERTY_ADDRESS => self::VISIBILITY_SHOW_USERS_ONLY, + IAccountManager::PROPERTY_AVATAR => self::VISIBILITY_SHOW, + IAccountManager::PROPERTY_BIOGRAPHY => self::VISIBILITY_SHOW, + IAccountManager::PROPERTY_DISPLAYNAME => self::VISIBILITY_SHOW, + IAccountManager::PROPERTY_HEADLINE => self::VISIBILITY_SHOW, + IAccountManager::PROPERTY_ORGANISATION => self::VISIBILITY_SHOW, + IAccountManager::PROPERTY_ROLE => self::VISIBILITY_SHOW, + IAccountManager::PROPERTY_EMAIL => self::VISIBILITY_SHOW_USERS_ONLY, + IAccountManager::PROPERTY_PHONE => self::VISIBILITY_SHOW_USERS_ONLY, + IAccountManager::PROPERTY_TWITTER => self::VISIBILITY_SHOW, + IAccountManager::PROPERTY_WEBSITE => self::VISIBILITY_SHOW, + ]; + + /** + * Default visibility + * + * @since 28.0.0 + */ + public const DEFAULT_VISIBILITY = self::VISIBILITY_SHOW_USERS_ONLY; + + /** + * If no user is passed as an argument return whether profile is enabled globally in `config.php` + * + * @since 28.0.0 + */ + public function isProfileEnabled(?IUser $user = null): bool; + + /** + * Return whether the profile parameter of the target user + * is visible to the visiting user + * + * @since 28.0.0 + */ + public function isProfileFieldVisible(string $profileField, IUser $targetUser, ?IUser $visitingUser): bool; + + /** + * 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, biography?: ?string, displayname?: ?string, headline?: ?string, isUserAvatarVisible?: bool, organisation?: ?string, role?: ?string, actions: list} + * @since 28.0.0 + */ + public function getProfileFields(IUser $targetUser, ?IUser $visitingUser): array; +}