-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 logout prehooks #29329
Add logout prehooks #29329
Conversation
lib/private/Server.php
Outdated
@@ -322,6 +323,10 @@ public function __construct($webRoot, \OC\Config $config) { | |||
/** @var $user \OC\User\User */ | |||
\OC_Hook::emit('OC_User', 'post_login', ['run' => true, 'uid' => $user->getUID(), 'password' => $password]); | |||
}); | |||
$userSession->listen('\OC\User', 'preLogout', function () { | |||
$event = new GenericEvent(null, []); | |||
\OC::$server->getEventDispatcher()->dispatch('prelogoutHook', $event); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optional, but we can consider following the naming convention of EventDispatcher.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please do
lib/private/User/Session.php
Outdated
if ($this->preLogout() === true) { | ||
return; | ||
} | ||
$this->manager->emit('\OC\User', 'logout'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation
31ac4bf
to
79c47cb
Compare
Codecov Report
@@ Coverage Diff @@
## master #29329 +/- ##
============================================
+ Coverage 60.21% 60.21% +<.01%
- Complexity 17182 17183 +1
============================================
Files 1030 1030
Lines 57271 57280 +9
============================================
+ Hits 34485 34493 +8
- Misses 22786 22787 +1
Continue to review full report at Codecov.
|
79c47cb
to
f75b7ef
Compare
lib/private/User/Session.php
Outdated
@@ -823,6 +823,13 @@ public function loginWithCookie($uid, $currentToken) { | |||
* logout the user from the session | |||
*/ | |||
public function logout() { | |||
$this->manager->emit('\OC\User', 'preLogout'); | |||
|
|||
if ($this->getSession()->get('switchedToNewUser') === 'yes') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't abuse the session to pass around variables. There is an existing way to receive values from a symfony event, just use it. Here's an example: https://github.com/owncloud/guests/blob/master/controller/userscontroller.php#L130
f75b7ef
to
1037226
Compare
@PVince81 No more sessions. Data handled via Symfony events. |
lib/private/User/Session.php
Outdated
|
||
$eventDispatcher = \OC::$server->getEventDispatcher(); | ||
$eventDispatcher->addListener('OC\User\Session::logout', function ($event) use (&$called) { | ||
if ($event['switchedToNewUser'] === 'true') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be called "cancel".
Also why not use booleans instead of strings containing booleans ?
685b513
to
4fb0892
Compare
@PVince81 Updated the new patch by making 'cancel' and used booleans instead of string containing booleans. Thanks for the feedback. |
lib/private/User/Session.php
Outdated
|
||
$eventDispatcher = \OC::$server->getEventDispatcher(); | ||
$eventDispatcher->addListener('OC\User\Session::logout', function ($event) use (&$cancelLogout) { | ||
$cancelLogout = $event['cancel']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tested this ? You are not even firing the event with $eventDispatcher
Please look again at the example I posted and use the same approach
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that's a misunderstanding. Core must fire the event and apps must listen to it to react, not the other way around.
1e536bf
to
32039d5
Compare
@PVince81 Updated the change to trigger the event from core. And the impersonate app reacts to it. Thanks for the feedback. |
42b213c
to
370ae0f
Compare
@PVince81 I have used naming convention for the event names. Let me know if this change is ok. |
lib/private/Server.php
Outdated
@@ -322,6 +323,10 @@ public function __construct($webRoot, \OC\Config $config) { | |||
/** @var $user \OC\User\User */ | |||
\OC_Hook::emit('OC_User', 'post_login', ['run' => true, 'uid' => $user->getUID(), 'password' => $password]); | |||
}); | |||
$userSession->listen('\OC\User', 'preLogout', function () { | |||
$event = new GenericEvent(null, []); | |||
\OC::$server->getEventDispatcher()->dispatch('pre_logoutHook', $event); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"user.pre_logout_hook" is better i guess. According to the naming convention:
-Use only lowercase letters, numbers, dots (.) and underscores (_);
-Prefix names with a namespace followed by a dot (e.g. order., user.*);
-End names with a verb that indicates what action has been taken (e.g. order.placed).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
370ae0f
to
8988368
Compare
lib/private/Server.php
Outdated
@@ -322,6 +323,10 @@ public function __construct($webRoot, \OC\Config $config) { | |||
/** @var $user \OC\User\User */ | |||
\OC_Hook::emit('OC_User', 'post_login', ['run' => true, 'uid' => $user->getUID(), 'password' => $password]); | |||
}); | |||
$userSession->listen('\OC\User', 'preLogout', function () { | |||
$event = new GenericEvent(null, []); | |||
\OC::$server->getEventDispatcher()->dispatch('user.pre_logout_hook', $event); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm, is this the official Symfony naming convention ? for all other Symfony events in OC we used a namespaced name, please look it up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://symfony.com/doc/current/components/event_dispatcher.html#naming-conventions
Yes, this is the official Symfony naming convention, but we don't have to follow that of course. I checked quickly the other Symfony events in OC. Generally, we used a namespaced name, but 3rdparty libraries are following this convention, like:
core/apps/files_external/3rdparty/aws-sdk-php/Aws/Common/Model/MultipartUpload/AbstractTransfer.php
Line 33 in 5ce66c4
const BEFORE_UPLOAD = 'multipart_upload.before_upload'; |
or
core/apps/files_external/3rdparty/aws-sdk-php/Guzzle/Plugin/Backoff/BackoffPlugin.php
Line 19 in 5ce66c4
const RETRY_PARAM = 'plugins.backoff.retry_count'; |
All the events related to a class should be constant class property and we should use these constants when dispatching an event. IMHO it is cleaner.
By the way, core has some codes that follow the convention:
core/apps/files_sharing/lib/API/Remote.php
Line 116 in baf07a7
$dispatcher->dispatch('remoteshare.declined', $event); |
or
https://github.com/owncloud/core/blob/master/lib/public/Http/HttpEvents.php#L32
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For code consistency I am modifying the event name to use namespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DeepDiver1975 any comment on this ? I assume we want to continue using the namespace format like we did for loadAdditionalScripts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, we are still in the adaptation phase to EventDispatcher. IMHO, we can add event strings as constant property to the related class, we can track it under #29174 and follow the convention.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please double check namespace format and add unit tests to make codecov happy.
13c4c61
to
3af8477
Compare
lib/private/User/Session.php
Outdated
|
||
$event = new GenericEvent(null, ['cancel' => false]); | ||
$eventDispatcher = \OC::$server->getEventDispatcher(); | ||
$eventDispatcher->dispatch('\OC\User\Session::cancel_logout', $event); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the event should be \OC\User\Session::pre_logout
. We are informing every listener that we are about to logout, not about the cancel.
lib/private/Server.php
Outdated
@@ -322,6 +323,10 @@ public function __construct($webRoot, \OC\Config $config) { | |||
/** @var $user \OC\User\User */ | |||
\OC_Hook::emit('OC_User', 'post_login', ['run' => true, 'uid' => $user->getUID(), 'password' => $password]); | |||
}); | |||
$userSession->listen('\OC\User', 'preLogout', function () { | |||
$event = new GenericEvent(null, []); | |||
\OC::$server->getEventDispatcher()->dispatch('\OC\User\Session::pre_logouthook', $event); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"pre_logout", remove the "hook" from the name
3af8477
to
eb1384d
Compare
@PVince81 Hope the code looks better. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 thanks, please backport
Backport PR: #29352 |
d4ba906
to
d859408
Compare
This change will help to do actions in logout prehooks. Any changes to be made before logout action can be done here. Signed-off-by: Sujith H <sharidasan@owncloud.com>
d859408
to
44b511c
Compare
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This change will help to do actions
in logout prehooks. Any changes to
be made before logout action can be
done here.
Signed-off-by: Sujith H sharidasan@owncloud.com
Description
Add logout prehooks
Related Issue
Motivation and Context
Adding logout prehooks
How Has This Been Tested?
Screenshots (if appropriate):
Types of changes
Checklist: