From 6a99f21c5317bc2231ee6efa857420cf09983027 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Fri, 15 Jan 2021 13:10:30 +0100 Subject: [PATCH] Add activity for display name change Signed-off-by: Morris Jobke --- apps/settings/lib/Activity/Provider.php | 15 ++++ apps/settings/lib/AppInfo/Application.php | 14 +-- apps/settings/lib/Hooks.php | 104 ++++++++++++++++++++++ 3 files changed, 128 insertions(+), 5 deletions(-) diff --git a/apps/settings/lib/Activity/Provider.php b/apps/settings/lib/Activity/Provider.php index 2d5c858f5e83b..d91ae3b5f573c 100644 --- a/apps/settings/lib/Activity/Provider.php +++ b/apps/settings/lib/Activity/Provider.php @@ -45,6 +45,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'; @@ -114,6 +117,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 the system'); } elseif ($event->getSubject() === self::APP_TOKEN_CREATED) { $subject = $this->l->t('You created app password "{token}"'); } elseif ($event->getSubject() === self::APP_TOKEN_DELETED) { @@ -149,12 +158,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: diff --git a/apps/settings/lib/AppInfo/Application.php b/apps/settings/lib/AppInfo/Application.php index 64bb42e065231..bae82cbadcf3f 100644 --- a/apps/settings/lib/AppInfo/Application.php +++ b/apps/settings/lib/AppInfo/Application.php @@ -156,12 +156,16 @@ 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']); } - /** @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']); + } } } diff --git a/apps/settings/lib/Hooks.php b/apps/settings/lib/Hooks.php index 4f005272b93a6..0fb64c41c4508 100644 --- a/apps/settings/lib/Hooks.php +++ b/apps/settings/lib/Hooks.php @@ -24,11 +24,14 @@ * along with this program. If not, see . * */ + namespace OCA\Settings; +use OCA\Settings\Activity\GroupProvider; use OCA\Settings\Activity\Provider; use OCP\Activity\IManager as IActivityManager; use OCP\IConfig; +use OCP\IGroup; use OCP\IGroupManager; use OCP\IURLGenerator; use OCP\IUser; @@ -211,4 +214,105 @@ public function onChangeEmail(IUser $user, $oldMailAddress) { $this->mailer->send($message); } } + + 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 + * @throws \InvalidArgumentException + * @throws \BadMethodCallException + */ + public function addUserToGroup(IGroup $group, IUser $user): void { + $subAdminManager = $this->groupManager->getSubAdmin(); + $usersToNotify = $subAdminManager->getGroupsSubAdmins($group); + $usersToNotify[] = $user; + + + $event = $this->activityManager->generateEvent(); + $event->setApp('settings') + ->setType('group_settings'); + + $actor = $this->userSession->getUser(); + if ($actor instanceof IUser) { + $event->setAuthor($actor->getUID()) + ->setSubject(GroupProvider::ADDED_TO_GROUP, [ + 'user' => $user->getUID(), + 'group' => $group->getGID(), + 'actor' => $actor->getUID(), + ]); + } else { + $event->setSubject(GroupProvider::ADDED_TO_GROUP, [ + 'user' => $user->getUID(), + 'group' => $group->getGID(), + ]); + } + + foreach ($usersToNotify as $userToNotify) { + $event->setAffectedUser($userToNotify->getUID()); + $this->activityManager->publish($event); + } + } + + /** + * @param IGroup $group + * @param IUser $user + * @throws \InvalidArgumentException + * @throws \BadMethodCallException + */ + public function removeUserFromGroup(IGroup $group, IUser $user): void { + $subAdminManager = $this->groupManager->getSubAdmin(); + $usersToNotify = $subAdminManager->getGroupsSubAdmins($group); + $usersToNotify[] = $user; + + + $event = $this->activityManager->generateEvent(); + $event->setApp('settings') + ->setType('group_settings'); + + $actor = $this->userSession->getUser(); + if ($actor instanceof IUser) { + $event->setAuthor($actor->getUID()) + ->setSubject(GroupProvider::REMOVED_FROM_GROUP, [ + 'user' => $user->getUID(), + 'group' => $group->getGID(), + 'actor' => $actor->getUID(), + ]); + } else { + $event->setSubject(GroupProvider::REMOVED_FROM_GROUP, [ + 'user' => $user->getUID(), + 'group' => $group->getGID(), + ]); + } + + foreach ($usersToNotify as $userToNotify) { + $event->setAffectedUser($userToNotify->getUID()); + $this->activityManager->publish($event); + } + } }