-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(profile): Add public interface for profile manager so apps can check config #41055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you please educate me on the reason for this change?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was never intended to use functions of an indirect dependency of server 3rdparty (
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. Thank you for the explanation. |
||
| 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,15 +102,15 @@ 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) { | ||
| return $profileEnabledGlobally; | ||
| } | ||
|
|
||
| $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<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 | ||
|
|
@@ -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]; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @copyright Copyright (c) 2023 Joas Schilling <[email protected]> | ||
| * | ||
| * @author Joas Schilling <[email protected]> | ||
| * | ||
| * @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 <http://www.gnu.org/licenses/>. | ||
| * | ||
| */ | ||
|
|
||
| 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<array{id: string, icon: string, title: string, target: ?string}>} | ||
| * @since 28.0.0 | ||
| */ | ||
| public function getProfileFields(IUser $targetUser, ?IUser $visitingUser): array; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to know whether there is a standard in place for using safe functions in the codebase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will send a follow up adjusting all of them