-
Notifications
You must be signed in to change notification settings - Fork 15
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 ImpersonateEvents for SnappyMail (and others) #180
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ | |
use OCP\ISession; | ||
use OCP\IUserManager; | ||
use OCP\IUserSession; | ||
use OCP\EventDispatcher\IEventDispatcher; | ||
use OCA\Impersonate\Events\EndImpersonateEvent; | ||
|
||
class LogoutController extends Controller { | ||
/** @var IUserManager */ | ||
|
@@ -20,6 +22,8 @@ class LogoutController extends Controller { | |
private $logger; | ||
/** @var ISession */ | ||
private $session; | ||
/** @var IEventDispatcher */ | ||
private $eventDispatcher; | ||
|
||
/** | ||
* @param string $appName | ||
|
@@ -34,23 +38,25 @@ public function __construct($appName, | |
IUserManager $userManager, | ||
IUserSession $userSession, | ||
ISession $session, | ||
LoggerInterface $logger) { | ||
LoggerInterface $logger, | ||
IEventDispatcher $eventDispatcher) { | ||
parent::__construct($appName, $request); | ||
$this->userManager = $userManager; | ||
$this->userSession = $userSession; | ||
$this->session = $session; | ||
$this->logger = $logger; | ||
$this->eventDispatcher = $eventDispatcher; | ||
} | ||
|
||
/** | ||
* @UseSession | ||
* @NoAdminRequired | ||
*/ | ||
public function logout(string $userId): JSONResponse { | ||
$user = $this->session->get('oldUserId'); | ||
$user = $this->userManager->get($user); | ||
$impersonator = $this->session->get('oldUserId'); | ||
$impersonator = $this->userManager->get($impersonator); | ||
|
||
if ($user === null) { | ||
if ($impersonator === null) { | ||
return new JSONResponse( | ||
sprintf( | ||
'No user found for %s', | ||
|
@@ -60,7 +66,10 @@ public function logout(string $userId): JSONResponse { | |
); | ||
} | ||
|
||
$this->userSession->setUser($user); | ||
$impersonatee = $this->userManager->get($userId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I love the more clearer distinction on the name of the Getter function on the event. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same gut feeling, 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, actually as i did in the Event class… =D |
||
$this->eventDispatcher->dispatchTyped(new EndImpersonateEvent($impersonator, $impersonatee)); | ||
|
||
$this->userSession->setUser($impersonator); | ||
|
||
$this->logger->info( | ||
sprintf( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2022 SnappyMail | ||
* | ||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de> | ||
* @author the-djmaze | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\Impersonate\Events; | ||
|
||
use OCP\EventDispatcher\Event; | ||
use OCP\IUser; | ||
|
||
abstract class AImpersonateEvent extends Event { | ||
private IUser $impersonator; | ||
|
||
private IUser $impersonatee; | ||
|
||
public function __construct(IUser $impersonator, IUser $impersonatee) { | ||
parent::__construct(); | ||
$this->impersonator = $impersonator; | ||
$this->impersonatee = $impersonatee; | ||
} | ||
|
||
public function getImpersonator(): IUser { | ||
return $this->impersonator; | ||
} | ||
|
||
public function getImpersonatedUser(): IUser { | ||
return $this->impersonatee; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2022 Arthur Schiwon <blizzz@arthur-schiwon.de> | ||
* | ||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\Impersonate\Events; | ||
|
||
class BeginImpersonateEvent extends AImpersonateEvent { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2022 Arthur Schiwon <blizzz@arthur-schiwon.de> | ||
* | ||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\Impersonate\Events; | ||
|
||
class EndImpersonateEvent extends AImpersonateEvent { | ||
} |
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.
Would recommend different variable names for the uid and the user object
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.
Had the same feeling and almost changed it back then. Well, almost. 👍