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 authored and skjnldsv committed Aug 9, 2021
1 parent c5c377f commit 6a99f21
Show file tree
Hide file tree
Showing 3 changed files with 128 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 @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 9 additions & 5 deletions apps/settings/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
}
104 changes: 104 additions & 0 deletions apps/settings/lib/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

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;
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 6a99f21

Please sign in to comment.