Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UserUpdatedEvent #3504

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions modules/Core/classes/Events/UserUpdatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

class UserUpdatedEvent extends AbstractEvent implements HasWebhookParams {

public User $user;
public UserData $updated_data;

public function __construct(User $user) {
$this->user = $user;

$data = DB::getInstance()->get('users', ['id', $user->data()->id]);
$this->updated_data = new UserData($data->first());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if we could provide something like data_before, data_after and then a diff of them via data_changed

Copy link
Member Author

@partydragen partydragen May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well this PR did not really go as planned

I want to trigger certain functions when event is triggered but as the functions require User param it will get the outdated data :/

Like to solve that i need to directly update userData on update

foreach ($fields as $key => $value) {
    $this->data()->{$key} = $value;
}

But updating the userdata directly could have bad side effects idk

Another problem i also have is integrations cache is outdated if any integrations is added/removed

}

public static function name(): string {
return 'updatedUser';
}

public static function description(): string {
return (new Language())->get('admin', 'user_updated_hook_info');
}

public function webhookParams(): array {
return [
'user_id' => $this->user->data()->id,
'username' => $this->user->getDisplayname(),
'profile_url' => URL::getSelfURL() . ltrim($this->user->getProfileURL(), '/')
];
}
}
1 change: 1 addition & 0 deletions modules/Core/language/en_UK.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"admin/user_warned_webhook": "{{punished}} has been warned by {{punisher}}.",
"admin/user_group_added_hook_info": "User Group Added",
"admin/user_group_removed_hook_info": "User Group Removed",
"admin/user_updated_hook_info": "User updated",
"admin/acp_logins": "StaffCP Logins",
"admin/action": "Action",
"admin/action_info": "Action Info",
Expand Down
1 change: 1 addition & 0 deletions modules/Core/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ public function __construct(Language $language, Pages $pages, User $user, Naviga
EventHandler::registerEvent(UserProfilePostCreatedEvent::class);
EventHandler::registerEvent(UserProfilePostReplyCreatedEvent::class);
EventHandler::registerEvent(UserRegisteredEvent::class);
EventHandler::registerEvent(UserUpdatedEvent::class);
EventHandler::registerEvent(UserValidatedEvent::class);
EventHandler::registerEvent(UserWarnedEvent::class);

Expand Down
4 changes: 4 additions & 0 deletions modules/Core/pages/panel/users_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@
}
}

EventHandler::executeEvent(new UserUpdatedEvent(
$view_user,
));

Session::flash('edit_user_success', $language->get('admin', 'user_updated_successfully'));
Redirect::to(URL::build('/panel/users/edit/', 'id=' . urlencode($user_query->id)));
} catch (Exception $e) {
Expand Down
12 changes: 12 additions & 0 deletions modules/Core/pages/user/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
// Logout all other sessions for this user
$user->logoutAllOtherSessions();

EventHandler::executeEvent(new UserUpdatedEvent(
$user,
));

Session::delete('force_tfa_alert');
Session::flash('tfa_success', $language->get('user', 'tfa_successful'));
Redirect::to(URL::build('/user/settings'));
Expand Down Expand Up @@ -125,6 +129,10 @@
'tfa_complete' => false
]);

EventHandler::executeEvent(new UserUpdatedEvent(
$user,
));

Session::flash('settings_success', $language->get('user', 'tfa_disabled'));
Redirect::to(URL::build('/user/settings'));
}
Expand Down Expand Up @@ -330,6 +338,10 @@
}
}

EventHandler::executeEvent(new UserUpdatedEvent(
$user,
));

Session::flash('settings_success', $language->get('user', 'settings_updated_successfully'));
Redirect::to(URL::build('/user/settings'));

Expand Down
Loading