Skip to content

Commit

Permalink
Profile backend
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Ng <chrng8@gmail.com>
  • Loading branch information
Pytal committed Oct 19, 2021
1 parent 7215148 commit 3093548
Show file tree
Hide file tree
Showing 38 changed files with 1,921 additions and 81 deletions.
9 changes: 2 additions & 7 deletions apps/provisioning_api/lib/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,11 +721,11 @@ public function editUserMultiValue(
*
* @param string $userId
* @param string $key
* @param string|bool $value
* @param string $value
* @return DataResponse
* @throws OCSException
*/
public function editUser(string $userId, string $key, $value): DataResponse {
public function editUser(string $userId, string $key, string $value): DataResponse {
$currentLoggedInUser = $this->userSession->getUser();

$targetUser = $this->userManager->get($userId);
Expand Down Expand Up @@ -961,11 +961,6 @@ public function editUser(string $userId, string $key, $value): DataResponse {
$this->accountManager->updateAccount($userAccount);
break;
case IAccountManager::PROPERTY_PROFILE_ENABLED:
if (!is_bool($value)) {
throw new OCSException('Invalid value, value must be a boolean', 102);
}
$value = $value === true ? '1' : '0';

$userAccount = $this->accountManager->getAccount($targetUser);
try {
$userProperty = $userAccount->getProperty($key);
Expand Down
143 changes: 114 additions & 29 deletions apps/settings/lib/Settings/Personal/PersonalInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Settings\Settings\Personal;

use OCA\FederatedFileSharing\FederatedShareProvider;
use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\IAccountProperty;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\Files\FileInfo;
use OCP\IConfig;
use OCP\IGroup;
Expand All @@ -48,26 +51,36 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OC\Profile\ProfileManager;
use OCP\Settings\ISettings;
use OCP\Accounts\IAccountProperty;
use OCP\AppFramework\Services\IInitialState;

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

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

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

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

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

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

/** @var IAppManager */
private $appManager;

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

/** @var IL10N */
private $l;

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

Expand All @@ -76,6 +89,7 @@ public function __construct(
IUserManager $userManager,
IGroupManager $groupManager,
IAccountManager $accountManager,
ProfileManager $profileManager,
IAppManager $appManager,
IFactory $l10nFactory,
IL10N $l,
Expand All @@ -84,6 +98,7 @@ public function __construct(
$this->config = $config;
$this->userManager = $userManager;
$this->accountManager = $accountManager;
$this->profileManager = $profileManager;
$this->groupManager = $groupManager;
$this->appManager = $appManager;
$this->l10nFactory = $l10nFactory;
Expand Down Expand Up @@ -114,7 +129,7 @@ public function getForm(): TemplateResponse {
$totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
}

$languageParameters = $this->getLanguages($user);
$languageParameters = $this->getLanguageMap($user);
$localeParameters = $this->getLocales($user);
$messageParameters = $this->getMessageParameters($account);

Expand Down Expand Up @@ -146,24 +161,107 @@ public function getForm(): TemplateResponse {
] + $messageParameters + $languageParameters + $localeParameters;

$personalInfoParameters = [
'displayNames' => $this->getDisplayNames($account),
'emails' => $this->getEmails($account),
'languages' => $this->getLanguages($user),
'userId' => $uid,
'displayNameMap' => $this->getDisplayNameMap($account),
'emailMap' => $this->getEmailMap($account),
'languageMap' => $this->getLanguageMap($user),
'profileEnabled' => $this->isProfileEnabled($account),
'organisationMap' => $this->getOrganisationMap($account),
'roleMap' => $this->getRoleMap($account),
'headlineMap' => $this->getHeadlineMap($account),
'biographyMap' => $this->getBiographyMap($account),
];

$accountParameters = [
'displayNameChangeSupported' => $user->canChangeDisplayName(),
'lookupServerUploadEnabled' => $lookupServerUploadEnabled,
];

$profileParameters = [
'profileConfig' => $this->profileManager->getProfileConfig($user, $user),
];

$this->initialStateService->provideInitialState('personalInfoParameters', $personalInfoParameters);
$this->initialStateService->provideInitialState('accountParameters', $accountParameters);
$this->initialStateService->provideInitialState('profileParameters', $profileParameters);

return new TemplateResponse('settings', 'settings/personal/personal.info', $parameters, '');
}

/**
* @return string the section ID, e.g. 'sharing'
* returns the primary biography in an
* associative array
*/
private function getBiographyMap(IAccount $account): array {
$primaryBiography = [
'value' => $account->getProperty(IAccountManager::PROPERTY_BIOGRAPHY)->getValue(),
'scope' => $account->getProperty(IAccountManager::PROPERTY_BIOGRAPHY)->getScope(),
'verified' => $account->getProperty(IAccountManager::PROPERTY_BIOGRAPHY)->getVerified(),
];

$biographyMap = [
'primaryBiography' => $primaryBiography,
];

return $biographyMap;
}

/**
* returns the primary organisation in an
* associative array
*/
private function getOrganisationMap(IAccount $account): array {
$primaryOrganisation = [
'value' => $account->getProperty(IAccountManager::PROPERTY_ORGANISATION)->getValue(),
'scope' => $account->getProperty(IAccountManager::PROPERTY_ORGANISATION)->getScope(),
'verified' => $account->getProperty(IAccountManager::PROPERTY_ORGANISATION)->getVerified(),
];

$organisationMap = [
'primaryOrganisation' => $primaryOrganisation,
];

return $organisationMap;
}

/**
* returns the primary headline in an
* associative array
*/
private function getHeadlineMap(IAccount $account): array {
$primaryHeadline = [
'value' => $account->getProperty(IAccountManager::PROPERTY_HEADLINE)->getValue(),
'scope' => $account->getProperty(IAccountManager::PROPERTY_HEADLINE)->getScope(),
'verified' => $account->getProperty(IAccountManager::PROPERTY_HEADLINE)->getVerified(),
];

$headlineMap = [
'primaryHeadline' => $primaryHeadline,
];

return $headlineMap;
}

/**
* returns the primary role in an
* associative array
*/
private function getRoleMap(IAccount $account): array {
$primaryRole = [
'value' => $account->getProperty(IAccountManager::PROPERTY_ROLE)->getValue(),
'scope' => $account->getProperty(IAccountManager::PROPERTY_ROLE)->getScope(),
'verified' => $account->getProperty(IAccountManager::PROPERTY_ROLE)->getVerified(),
];

$roleMap = [
'primaryRole' => $primaryRole,
];

return $roleMap;
}

/**
* returns the section ID string, e.g. 'sharing'
* @since 9.1
*/
public function getSection(): string {
Expand All @@ -184,9 +282,6 @@ public function getPriority(): int {

/**
* returns a sorted list of the user's group GIDs
*
* @param IUser $user
* @return array
*/
private function getGroups(IUser $user): array {
$groups = array_map(
Expand All @@ -205,32 +300,26 @@ static function (IGroup $group) {
* associative array
*
* NOTE may be extended to provide additional display names (i.e. aliases) in the future
*
* @param IAccount $account
* @return array
*/
private function getDisplayNames(IAccount $account): array {
private function getDisplayNameMap(IAccount $account): array {
$primaryDisplayName = [
'value' => $account->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getValue(),
'scope' => $account->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getScope(),
'verified' => $account->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getVerified(),
];

$displayNames = [
$displayNameMap = [
'primaryDisplayName' => $primaryDisplayName,
];

return $displayNames;
return $displayNameMap;
}

/**
* returns the primary email and additional emails in an
* associative array
*
* @param IAccount $account
* @return array
*/
private function getEmails(IAccount $account): array {
private function getEmailMap(IAccount $account): array {
$systemEmail = [
'value' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getValue(),
'scope' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getScope(),
Expand All @@ -246,26 +335,23 @@ function (IAccountProperty $property) {
'locallyVerified' => $property->getLocallyVerified(),
];
},
$account->getPropertyCollection(IAccountManager::COLLECTION_EMAIL)->getProperties()
$account->getPropertyCollection(IAccountManager::COLLECTION_EMAIL)->getProperties(),
);

$emails = [
$emailMap = [
'primaryEmail' => $systemEmail,
'additionalEmails' => $additionalEmails,
'notificationEmail' => (string)$account->getUser()->getPrimaryEMailAddress(),
];

return $emails;
return $emailMap;
}

/**
* returns the user's active language, common languages, and other languages in an
* associative array
*
* @param IUser $user
* @return array
*/
private function getLanguages(IUser $user): array {
private function getLanguageMap(IUser $user): array {
$forceLanguage = $this->config->getSystemValue('force_language', false);
if ($forceLanguage !== false) {
return [];
Expand Down Expand Up @@ -340,8 +426,7 @@ private function getLocales(IUser $user): array {
}

/**
* @param IAccount $account
* @return array
* returns the message parameters
*/
private function getMessageParameters(IAccount $account): array {
$needVerifyMessage = [IAccountManager::PROPERTY_EMAIL, IAccountManager::PROPERTY_WEBSITE, IAccountManager::PROPERTY_TWITTER];
Expand Down
32 changes: 30 additions & 2 deletions apps/user_status/lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,27 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\UserStatus\Listener;

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;
use OCP\EventDispatcher\IEventListener;
use OCP\IInitialStateService;
use OCP\IUserSession;

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

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

/** @var IUserSession */
private $userSession;

/** @var IInitialStateService */
private $initialState;
Expand All @@ -45,11 +55,19 @@ class BeforeTemplateRenderedListener implements IEventListener {
/**
* BeforeTemplateRenderedListener constructor.
*
* @param IAccountManager $accountManager
* @param IUserSession $userSession
* @param IInitialStateService $initialState
* @param JSDataService $jsDataService
*/
public function __construct(IInitialStateService $initialState,
JSDataService $jsDataService) {
public function __construct(
IAccountManager $accountManager,
IUserSession $userSession,
IInitialStateService $initialState,
JSDataService $jsDataService
) {
$this->accountManager = $accountManager;
$this->userSession = $userSession;
$this->initialState = $initialState;
$this->jsDataService = $jsDataService;
}
Expand All @@ -58,6 +76,12 @@ public function __construct(IInitialStateService $initialState,
* @inheritDoc
*/
public function handle(Event $event): void {
$user = $this->userSession->getUser();
if ($user === null) {
return;
}
$account = $this->accountManager->getAccount($user);

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

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

\OCP\Util::addScript('user_status', 'user-status-menu');
\OCP\Util::addStyle('user_status', 'user-status-menu');
}
Expand Down
Loading

0 comments on commit 3093548

Please sign in to comment.