Skip to content

Commit

Permalink
Toggle profile globally
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Ng <chrng8@gmail.com>
  • Loading branch information
Pytal committed Dec 10, 2021
1 parent c019e33 commit 99f8fe7
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 105 deletions.
7 changes: 4 additions & 3 deletions apps/settings/lib/Settings/Personal/PersonalInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
use OCP\Settings\ISettings;

class PersonalInfo implements ISettings {
use \OC\Profile\TProfileHelper;

/** @var IConfig */
private $config;
Expand Down Expand Up @@ -166,15 +165,17 @@ public function getForm(): TemplateResponse {
'twitterScope' => $account->getProperty(IAccountManager::PROPERTY_TWITTER)->getScope(),
'twitterVerification' => $account->getProperty(IAccountManager::PROPERTY_TWITTER)->getVerified(),
'groups' => $this->getGroups($user),
'isFairUseOfFreePushService' => $this->isFairUseOfFreePushService()
'isFairUseOfFreePushService' => $this->isFairUseOfFreePushService(),
'globalProfileEnabled' => $this->profileManager->isProfileEnabled(),
] + $messageParameters + $languageParameters + $localeParameters;

$personalInfoParameters = [
'userId' => $uid,
'displayNameMap' => $this->getDisplayNameMap($account),
'emailMap' => $this->getEmailMap($account),
'languageMap' => $this->getLanguageMap($user),
'profileEnabled' => $this->isProfileEnabled($account),
'globalProfileEnabled' => $this->profileManager->isProfileEnabled(),
'profileEnabled' => $this->profileManager->isProfileEnabled($user),
'organisationMap' => $this->getOrganisationMap($account),
'roleMap' => $this->getRoleMap($account),
'headlineMap' => $this->getHeadlineMap($account),
Expand Down
2 changes: 2 additions & 0 deletions apps/settings/templates/settings/admin/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,5 @@
</fieldset>
</form>
</div>

<div class="vue-global-profile-settings"></div>
26 changes: 14 additions & 12 deletions apps/settings/templates/settings/personal/personal.info.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,20 @@
<input type="hidden" id="twitterscope" value="<?php p($_['twitterScope']) ?>">
</form>
</div>
<div class="personal-settings-setting-box">
<div id="vue-organisation-section"></div>
</div>
<div class="personal-settings-setting-box">
<div id="vue-role-section"></div>
</div>
<div class="personal-settings-setting-box">
<div id="vue-headline-section"></div>
</div>
<div class="personal-settings-setting-box">
<div id="vue-biography-section"></div>
</div>
<?php if ($_['globalProfileEnabled']) : ?>
<div class="personal-settings-setting-box">
<div id="vue-organisation-section"></div>
</div>
<div class="personal-settings-setting-box">
<div id="vue-role-section"></div>
</div>
<div class="personal-settings-setting-box">
<div id="vue-headline-section"></div>
</div>
<div class="personal-settings-setting-box">
<div id="vue-biography-section"></div>
</div>
<?php endif; ?>
</div>

<div class="profile-settings-container">
Expand Down
18 changes: 8 additions & 10 deletions apps/user_status/lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

namespace OCA\UserStatus\Listener;

use OC\Profile\ProfileManager;
use OCA\UserStatus\AppInfo\Application;
use OCA\UserStatus\Service\JSDataService;
use OCP\Accounts\IAccountManager;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\EventDispatcher\Event;
Expand All @@ -38,10 +38,9 @@
use OCP\IUserSession;

class BeforeTemplateRenderedListener implements IEventListener {
use \OC\Profile\TProfileHelper;

/** @var IAccountManager */
private $accountManager;
/** @var ProfileManager */
private $profileManager;

/** @var IUserSession */
private $userSession;
Expand All @@ -55,18 +54,18 @@ class BeforeTemplateRenderedListener implements IEventListener {
/**
* BeforeTemplateRenderedListener constructor.
*
* @param IAccountManager $accountManager
* @param ProfileManager $profileManager
* @param IUserSession $userSession
* @param IInitialStateService $initialState
* @param JSDataService $jsDataService
*/
public function __construct(
IAccountManager $accountManager,
ProfileManager $profileManager,
IUserSession $userSession,
IInitialStateService $initialState,
JSDataService $jsDataService
) {
$this->accountManager = $accountManager;
$this->profileManager = $profileManager;
$this->userSession = $userSession;
$this->initialState = $initialState;
$this->jsDataService = $jsDataService;
Expand All @@ -80,7 +79,6 @@ public function handle(Event $event): void {
if ($user === null) {
return;
}
$account = $this->accountManager->getAccount($user);

if (!($event instanceof BeforeTemplateRenderedEvent)) {
// Unrelated
Expand All @@ -95,8 +93,8 @@ public function handle(Event $event): void {
return $this->jsDataService;
});

$this->initialState->provideLazyInitialState(Application::APP_ID, 'profileEnabled', function () use ($account) {
return ['profileEnabled' => $this->isProfileEnabled($account)];
$this->initialState->provideLazyInitialState(Application::APP_ID, 'profileEnabled', function () use ($user) {
return ['profileEnabled' => $this->profileManager->isProfileEnabled($user)];
});

\OCP\Util::addScript('user_status', 'user-status-menu');
Expand Down
10 changes: 10 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -2009,4 +2009,14 @@
* Also, it might log sensitive data into a plain text file.
*/
'ldap_log_file' => '',


/**
* Globally enable/disable the profile feature
*
* When globally enabled, users may individually enable/disable their profiles
*
* Defaults to ``true``
*/
'profile.enabled' => true,
];
13 changes: 0 additions & 13 deletions core/Controller/ProfilePageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

use OC\KnownUser\KnownUserService;
use OC\Profile\ProfileManager;
use OCP\Accounts\IAccountManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
Expand All @@ -41,26 +40,16 @@
use OCP\UserStatus\IManager as IUserStatusManager;

class ProfilePageController extends Controller {
use \OC\Profile\TProfileHelper;

/** @var IInitialState */
private $initialStateService;

/** @var IAccountManager */
private $accountManager;

/** @var ProfileManager */
private $profileManager;

/** @var IShareManager */
private $shareManager;

/** @var IGroupManager */
private $groupManager;

/** @var KnownUserService */
private $knownUserService;

/** @var IUserManager */
private $userManager;

Expand All @@ -74,7 +63,6 @@ public function __construct(
$appName,
IRequest $request,
IInitialState $initialStateService,
IAccountManager $accountManager,
ProfileManager $profileManager,
IShareManager $shareManager,
IGroupManager $groupManager,
Expand All @@ -85,7 +73,6 @@ public function __construct(
) {
parent::__construct($appName, $request);
$this->initialStateService = $initialStateService;
$this->accountManager = $accountManager;
$this->profileManager = $profileManager;
$this->shareManager = $shareManager;
$this->groupManager = $groupManager;
Expand Down
20 changes: 9 additions & 11 deletions lib/private/Contacts/ContactsMenu/ContactsStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
namespace OC\Contacts\ContactsMenu;

use OC\KnownUser\KnownUserService;
use OCP\Accounts\IAccountManager;
use OC\Profile\ProfileManager;
use OCP\Contacts\ContactsMenu\IContactsStore;
use OCP\Contacts\ContactsMenu\IEntry;
use OCP\Contacts\IManager;
Expand All @@ -44,17 +44,16 @@
use OCP\L10N\IFactory as IL10NFactory;

class ContactsStore implements IContactsStore {
use \OC\Profile\TProfileHelper;

/** @var IAccountManager */
private $accountManager;

/** @var IManager */
private $contactsManager;

/** @var IConfig */
private $config;

/** @var ProfileManager */
private $profileManager;

/** @var IUserManager */
private $userManager;

Expand All @@ -71,18 +70,18 @@ class ContactsStore implements IContactsStore {
private $l10nFactory;

public function __construct(
IAccountManager $accountManager,
IManager $contactsManager,
IConfig $config,
ProfileManager $profileManager,
IUserManager $userManager,
IURLGenerator $urlGenerator,
IGroupManager $groupManager,
KnownUserService $knownUserService,
IL10NFactory $l10nFactory
) {
$this->accountManager = $accountManager;
$this->contactsManager = $contactsManager;
$this->config = $config;
$this->profileManager = $profileManager;
$this->userManager = $userManager;
$this->urlGenerator = $urlGenerator;
$this->groupManager = $groupManager;
Expand Down Expand Up @@ -335,10 +334,9 @@ private function contactArrayToEntry(array $contact) {
// Provide profile parameters for core/src/OC/contactsmenu/contact.handlebars template
if (isset($contact['UID']) && isset($contact['FN'])) {
$targetUserId = $contact['UID'];
$user = $this->userManager->get($targetUserId);
if (!empty($user)) {
$account = $this->accountManager->getAccount($user);
if ($this->isProfileEnabled($account)) {
$targetUser = $this->userManager->get($targetUserId);
if (!empty($targetUser)) {
if ($this->profileManager->isProfileEnabled($targetUser)) {
$entry->setProfileTitle($this->l10nFactory->get('lib')->t('View profile'));
$entry->setProfileUrl($this->urlGenerator->linkToRouteAbsolute('core.ProfilePage.index', ['targetUserId' => $targetUserId]));
}
Expand Down
18 changes: 8 additions & 10 deletions lib/private/Contacts/ContactsMenu/Providers/ProfileProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace OC\Contacts\ContactsMenu\Providers;

use OCP\Accounts\IAccountManager;
use OC\Profile\ProfileManager;
use OCP\Contacts\ContactsMenu\IActionFactory;
use OCP\Contacts\ContactsMenu\IEntry;
use OCP\Contacts\ContactsMenu\IProvider;
Expand All @@ -33,14 +33,13 @@
use OCP\L10N\IFactory as IL10NFactory;

class ProfileProvider implements IProvider {
use \OC\Profile\TProfileHelper;

/** @var IAccountManager */
private $accountManager;

/** @var IActionFactory */
private $actionFactory;

/** @var ProfileManager */
private $profileManager;

/** @var IL10NFactory */
private $l10nFactory;

Expand All @@ -51,21 +50,21 @@ class ProfileProvider implements IProvider {
private $userManager;

/**
* @param IAccountManager $accountManager
* @param IActionFactory $actionFactory
* @param ProfileManager $profileManager
* @param IL10NFactory $l10nFactory
* @param IURLGenerator $urlGenerator
* @param IUserManager $userManager
*/
public function __construct(
IAccountManager $accountManager,
IActionFactory $actionFactory,
ProfileManager $profileManager,
IL10NFactory $l10nFactory,
IURLGenerator $urlGenerator,
IUserManager $userManager
) {
$this->accountManager = $accountManager;
$this->actionFactory = $actionFactory;
$this->profileManager = $profileManager;
$this->l10nFactory = $l10nFactory;
$this->urlGenerator = $urlGenerator;
$this->userManager = $userManager;
Expand All @@ -78,8 +77,7 @@ public function process(IEntry $entry) {
$targetUserId = $entry->getProperty('UID');
$targetUser = $this->userManager->get($targetUserId);
if (!empty($targetUser)) {
$account = $this->accountManager->getAccount($targetUser);
if ($this->isProfileEnabled($account)) {
if ($this->profileManager->isProfileEnabled($targetUser)) {
$iconUrl = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/profile.svg'));
$profileActionText = $this->l10nFactory->get('lib')->t('View profile');
$profileUrl = $this->urlGenerator->linkToRouteAbsolute('core.ProfilePage.index', ['targetUserId' => $targetUserId]);
Expand Down
25 changes: 25 additions & 0 deletions lib/private/Profile/ProfileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use OCP\Accounts\PropertyDoesNotExistException;
use OCP\App\IAppManager;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IConfig;
use OCP\IUser;
use OCP\L10N\IFactory;
use OCP\Profile\ILinkAction;
Expand All @@ -54,6 +55,9 @@ class ProfileManager {
/** @var IAppManager */
private $appManager;

/** @var IConfig */
private $config;

/** @var ProfileConfigMapper */
private $configMapper;

Expand Down Expand Up @@ -106,6 +110,7 @@ class ProfileManager {
public function __construct(
IAccountManager $accountManager,
IAppManager $appManager,
IConfig $config,
ProfileConfigMapper $configMapper,
ContainerInterface $container,
KnownUserService $knownUserService,
Expand All @@ -115,6 +120,7 @@ public function __construct(
) {
$this->accountManager = $accountManager;
$this->appManager = $appManager;
$this->config = $config;
$this->configMapper = $configMapper;
$this->container = $container;
$this->knownUserService = $knownUserService;
Expand All @@ -123,6 +129,25 @@ public function __construct(
$this->coordinator = $coordinator;
}

/**
* Return whether profile is enabled for the user,
* if no user is passed returns whether profile is enabled globally in `config.php`
*/
public function isProfileEnabled(?IUser $user = null): ?bool {
$globalProfileEnabled = $this->config->getSystemValueBool('profile.enabled', true);

if ($globalProfileEnabled && !empty($user)) {
$account = $this->accountManager->getAccount($user);
return filter_var(
$account->getProperty(IAccountManager::PROPERTY_PROFILE_ENABLED)->getValue(),
FILTER_VALIDATE_BOOLEAN,
FILTER_NULL_ON_FAILURE,
);
}

return $globalProfileEnabled;
}

/**
* Register an action for the user
*/
Expand Down
Loading

0 comments on commit 99f8fe7

Please sign in to comment.