Skip to content

Commit

Permalink
Add activity for display name change
Browse files Browse the repository at this point in the history
Signed-off-by: Morris Jobke <[email protected]>
  • Loading branch information
MorrisJobke committed Jan 15, 2021
1 parent d3ab26c commit 9dfd938
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
15 changes: 15 additions & 0 deletions apps/settings/lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class Provider implements IProvider {
public const EMAIL_CHANGED_BY = 'email_changed_by';
public const EMAIL_CHANGED_SELF = 'email_changed_self';
public const EMAIL_CHANGED = 'email_changed';
public const DISPLAY_NAME_CHANGED_BY = 'displayname_changed_by';
public const DISPLAY_NAME_CHANGED_SELF = 'displayname_changed_self';
public const DISPLAY_NAME_CHANGED = 'displayname_changed';
public const APP_TOKEN_CREATED = 'app_token_created';
public const APP_TOKEN_DELETED = 'app_token_deleted';
public const APP_TOKEN_RENAMED = 'app_token_renamed';
Expand Down Expand Up @@ -115,6 +118,12 @@ public function parse($language, IEvent $event, IEvent $previousEvent = null): I
$subject = $this->l->t('You changed your email address');
} elseif ($event->getSubject() === self::EMAIL_CHANGED) {
$subject = $this->l->t('Your email address was changed by an administrator');
} elseif ($event->getSubject() === self::DISPLAY_NAME_CHANGED_BY) {
$subject = $this->l->t('{actor} changed your display name');
} elseif ($event->getSubject() === self::DISPLAY_NAME_CHANGED_SELF) {
$subject = $this->l->t('You changed your display name');
} elseif ($event->getSubject() === self::DISPLAY_NAME_CHANGED) {
$subject = $this->l->t('Your display name was changed by an administrator');
} elseif ($event->getSubject() === self::APP_TOKEN_CREATED) {
$subject = $this->l->t('You created app password "{token}"');
} elseif ($event->getSubject() === self::APP_TOKEN_DELETED) {
Expand Down Expand Up @@ -150,12 +159,18 @@ protected function getParameters(IEvent $event): array {
case self::PASSWORD_RESET_SELF:
case self::EMAIL_CHANGED_SELF:
case self::EMAIL_CHANGED:
case self::DISPLAY_NAME_CHANGED_SELF:
case self::DISPLAY_NAME_CHANGED:
return [];
case self::PASSWORD_CHANGED_BY:
case self::EMAIL_CHANGED_BY:
return [
'actor' => $this->generateUserParameter($parameters[0]),
];
case self::DISPLAY_NAME_CHANGED_BY:
return [
'actor' => $this->generateUserParameter($parameters['actor']),
];
case self::APP_TOKEN_CREATED:
case self::APP_TOKEN_DELETED:
case self::APP_TOKEN_FILESYSTEM_GRANTED:
Expand Down
15 changes: 10 additions & 5 deletions apps/settings/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,17 @@ public function onChangePassword(array $parameters) {
* @throws \OCP\AppFramework\QueryException
*/
public function onChangeInfo(array $parameters) {
if ($parameters['feature'] !== 'eMailAddress') {
return;
if ($parameters['feature'] === 'eMailAddress') {
/** @var Hooks $hooks */
$hooks = $this->getContainer()->query(Hooks::class);
$hooks->onChangeEmail($parameters['user'], $parameters['old_value']);
}

if ($parameters['feature'] === 'displayName') {
/** @var Hooks $hooks */
$hooks = $this->getContainer()->query(Hooks::class);
$hooks->onChangeDisplayName($parameters['user'], $parameters['old_value']);
}

/** @var Hooks $hooks */
$hooks = $this->getContainer()->query(Hooks::class);
$hooks->onChangeEmail($parameters['user'], $parameters['old_value']);
}
}
27 changes: 27 additions & 0 deletions apps/settings/lib/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,33 @@ public function onChangeEmail(IUser $user, $oldMailAddress) {
}
}

public function onChangeDisplayName(IUser $user, string $oldDisplayName) {
if ($oldDisplayName === $user->getDisplayName() ||
$user->getLastLogin() === 0) {
// display name didn't really change or user didn't login,
// so don't create activities.
return;
}

$event = $this->activityManager->generateEvent();
$event->setApp('settings')
->setType('personal_settings')
->setAffectedUser($user->getUID());

$actor = $this->userSession->getUser();
if ($actor instanceof IUser) {
$subject = Provider::DISPLAY_NAME_CHANGED_SELF;
if ($actor->getUID() !== $user->getUID()) {
$subject = Provider::DISPLAY_NAME_CHANGED_BY;
}
$event->setAuthor($actor->getUID())
->setSubject($subject, ['actor' => $actor->getUID()]);
} else {
$event->setSubject(Provider::DISPLAY_NAME_CHANGED);
}
$this->activityManager->publish($event);
}

/**
* @param IGroup $group
* @param IUser $user
Expand Down

0 comments on commit 9dfd938

Please sign in to comment.