Skip to content

Commit

Permalink
Merge pull request nextcloud#41055 from nextcloud/feat/noid/profile-m…
Browse files Browse the repository at this point in the history
…anager-ocp

feat(profile): Add public interface for profile manager so apps can check config
  • Loading branch information
nickvergessen authored Oct 23, 2023
2 parents 06b3580 + fd9f7a7 commit 834c9a2
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 66 deletions.
2 changes: 1 addition & 1 deletion core/Controller/ProfilePageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
48 changes: 0 additions & 48 deletions core/Db/ProfileConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
36 changes: 19 additions & 17 deletions lib/private/Profile/ProfileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,7 +50,7 @@
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

class ProfileManager {
class ProfileManager implements IProfileManager {
/** @var ILinkAction[] */
private array $actions = [];

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand All @@ -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()),
Expand All @@ -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
Expand All @@ -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;
}
}
Expand All @@ -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);
}
),
)
Expand Down Expand Up @@ -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];
}

Expand Down
4 changes: 4 additions & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
106 changes: 106 additions & 0 deletions lib/public/Profile/IProfileManager.php
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 <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @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;
}

0 comments on commit 834c9a2

Please sign in to comment.