Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
So before we just blindly did out own thing with rebuilding the UserModel and setting the Auth. Now we check to see if they are using a custom UserModal and/or finder and support login using the finder defined in the Auth Config.
  • Loading branch information
challgren committed Jan 3, 2019
1 parent 61de1da commit 2e928f6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
5 changes: 1 addition & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ini name="apc.enable_cli" value="1"/>
<!-- E_ALL => 32767 -->
<!-- E_ALL & ~E_USER_DEPRECATED => 16383 -->
<ini name="error_reporting" value="16383"/>
<ini name="error_reporting" value="32767"/>
</php>

<!-- Add any additional test suites you want to run here -->
Expand All @@ -36,8 +36,5 @@
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
<blacklist>
<directory suffix=".ctp">src/Template/</directory>
</blacklist>
</filter>
</phpunit>
57 changes: 46 additions & 11 deletions src/Controller/Component/ImpersonateComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace CakeImpersonate\Controller\Component;

use Cake\Controller\Component;
use Cake\ORM\Entity;
use Cake\Event\Event;

/**
* Impersonate component
Expand All @@ -23,24 +23,29 @@ class ImpersonateComponent extends Component
*
* @var array
*/
protected $_defaultConfig = [];
protected $_defaultConfig = [
'userModel' => 'Users',
'finder' => 'all',
];

/**
* Function impersonate
*
* @param mixed $id
* @param mixed $id ID of user to impersonate
* @return bool
* @throws \Exception If userModal is not loaded in the Controller
*/
public function login($id)
{
$this->getController()->loadModel('Users');
$userModel = $this->getConfig('userModal', 'Users');
$this->getController()->loadModel($userModel);

$originalAuth = $this->getController()->getRequest()->getSession()->read('Auth');

/** @var Entity $users */
$users = $this->getController()->Users->get($id);
$this->getController()->Auth->setUser($users->toArray());
$this->getController()->getRequest()->getSession()->write('OriginalAuth', $originalAuth);
$finder = $this->getConfig('finder');
/** @var \Cake\ORM\Table $userTable */
$userTable = $this->getController()->{$userModel};
$userArray = $userTable->find($finder)->where([$userTable->getAlias() . '.id' => $id])->firstOrFail()->toArray();
$this->getController()->Auth->setUser($userArray);
$this->getController()->getRequest()->getSession()->write('OriginalAuth', $this->getController()->getRequest()->getSession()->read('Auth'));

return true;
}
Expand All @@ -54,7 +59,6 @@ public function login($id)
public function isImpersonate()
{
if ($this->getController()->getRequest()->getSession()->read('OriginalAuth')) {

return true;
}

Expand All @@ -78,4 +82,35 @@ public function logout()

return true;
}

/**
* {@inheritdoc}
*/
public function implementedEvents()
{
$eventMap = [
'Controller.initialize' => 'updateConfig',
'Controller.startup' => 'updateConfig',
];
$events = [];
foreach ($eventMap as $event => $method) {
if (method_exists($this, $method)) {
$events[$event] = $method;
}
}

return $events;
}

/**
* Updates the userModel and finder based on the AuthComponent.
*
* @param Event $event Event that started the update.
* @return void
*/
public function updateConfig(Event $event)
{
$this->setConfig('userModel', $this->getController()->Auth->getConfig('authorize.all.userModel', $this->getConfig('userModel')));
$this->setConfig('finder', $this->getController()->Auth->getConfig('authorize.all.finder', $this->getConfig('finder')));
}
}
13 changes: 13 additions & 0 deletions tests/TestCase/Controller/Component/ImpersonateComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ public function testLogout()
*/
public function testLogin()
{
$this->Impersonate->getRequest()->getSession()->write('Auth', $this->Auth);
$this->assertTrue($this->Impersonate->Impersonate->login(1));

$this->assertEquals($this->Auth, $this->Impersonate->getRequest()->getSession()->read('OriginalAuth'));
}

/**
* @return void
* @expectedException \Exception
*/
public function testLoginException()
{
$this->Impersonate->Impersonate->setConfig('userModal', 'UserNotFound');
$this->Impersonate->getRequest()->getSession()->write('Auth', $this->Auth);
$this->assertTrue($this->Impersonate->Impersonate->login(2));

Expand Down
2 changes: 2 additions & 0 deletions tests/test_app/Controller/ImpersonateTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Use Controller instead of AppController to avoid conflicts
*
* @property \CakeImpersonate\Controller\Component\ImpersonateComponent $Impersonate
* @property \App\Model\Table\UsersTable $Users
*/
class ImpersonateTestController extends Controller
{
Expand All @@ -24,6 +25,7 @@ class ImpersonateTestController extends Controller
public function initialize()
{
$this->loadComponent('Auth');
$this->loadModel('Users');
$this->loadComponent('CakeImpersonate.Impersonate');
parent::initialize();
}
Expand Down

0 comments on commit 2e928f6

Please sign in to comment.