Skip to content

Commit

Permalink
Adjust for SessionTrait moved to atk4/ui
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Feb 8, 2022
1 parent 35dfaf3 commit 0b22239
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
3 changes: 1 addition & 2 deletions demos/_includes/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ protected function init(): void

public function initAuth(bool $check = true): void
{
$this->auth = new Auth(['check' => $check, 'pageDashboard' => 'index']);
$this->auth->setApp($this);
$this->auth = new Auth($this, ['check' => $check, 'pageDashboard' => 'index']);

// Can not setmodel at this stage :(
$m = new \Atk4\Login\Model\User($this->db);
Expand Down
6 changes: 4 additions & 2 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Atk4\Login\Cache\Session;
use Atk4\Login\Layout\Narrow;
use Atk4\Login\Model\User;
use Atk4\Ui\App;
use Atk4\Ui\Layout\Admin;
use Atk4\Ui\VirtualPage;

Expand Down Expand Up @@ -103,12 +104,13 @@ class Auth
/**
* @param array $options
*/
public function __construct($options = [])
public function __construct(App $app, $options = [])
{
$this->setApp($app);
$this->setDefaults($options);

if ($this->cacheEnabled) {
$this->cache = Factory::factory($this->cacheClass, $this->cacheOptions);
$this->cache = Factory::factory($this->cacheClass, array_merge([1 => $this->getApp()], $this->cacheOptions));
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/Cache/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

namespace Atk4\Login\Cache;

use Atk4\Core\AppScopeTrait;
use Atk4\Core\DiContainerTrait;
use Atk4\Core\NameTrait;
use Atk4\Core\SessionTrait;
use Atk4\Ui\App;
use Atk4\Ui\SessionTrait;

/**
* Session cache for authentication controller.
*/
class Session // implements CacheInterface
{
use AppScopeTrait;
use DiContainerTrait;
use NameTrait;
use SessionTrait;
Expand All @@ -23,8 +26,10 @@ class Session // implements CacheInterface
/** @var string|null Cache key. Set this if you want to use multiple cache objects at same time. */
public $key;

public function __construct(array $options = [])
public function __construct(App $app, array $options = [])
{
$this->setApp($app);

$this->setDefaults($options);
}

Expand All @@ -34,7 +39,7 @@ public function __construct(array $options = [])
public function init(): void
{
if (\PHP_SAPI !== 'cli') { // helps with unit tests
$this->startSession();
$this->getApp()->session->startSession(); // TODO use SessionTrait methods instead
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/AclTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function setupDefaultDb(): void

protected function createAuthAndLogin(string $user): Auth
{
$auth = new Auth(['check' => false]);
$auth = new Auth($this->createAppForSession(), ['check' => false]);

$auth->setModel($this->createUserModel());
$auth->tryLogin($user, $user === 'admin' ? 'admin' : 'user');
Expand Down
11 changes: 7 additions & 4 deletions tests/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public function _testAuth(bool $cacheEnabled): void
$this->setupDefaultDb();

$createAuthFx = function (array $options = []) use ($cacheEnabled) {
$auth = new Auth(array_merge(['check' => false, 'cacheEnabled' => $cacheEnabled], $options));
$auth = new Auth(
$this->createAppForSession(),
array_merge(['check' => false, 'cacheEnabled' => $cacheEnabled], $options)
);
$auth->setModel($this->createUserModel());

return $auth;
Expand Down Expand Up @@ -121,17 +124,17 @@ public function testAuthCustomLoginAndPasswordFieldName(): void
{
$this->setupDefaultDb();

$auth = new Auth(['check' => false]);
$auth = new Auth($this->createAppForSession(), ['check' => false]);
$auth->setModel($this->createUserModel(), 'name', null);
$auth->tryLogin('admin', 'admin'); // there is no "name" = 'admin'
$this->assertFalse($auth->isLoggedIn());

$auth = new Auth(['check' => false]);
$auth = new Auth($this->createAppForSession(), ['check' => false]);
$auth->setModel($this->createUserModel(), 'name', null);
$auth->tryLogin('Administrator', 'admin');
$this->assertTrue($auth->isLoggedIn());

$auth = new Auth(['check' => false]);
$auth = new Auth($this->createAppForSession(), ['check' => false]);
$auth->setModel($this->createUserModel(), null, 'name');
$this->expectException(\Exception::class);
$auth->tryLogin('admin', 'admin'); // wrong password field
Expand Down
9 changes: 9 additions & 0 deletions tests/GenericTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Atk4\Login\Model\AccessRule;
use Atk4\Login\Model\Role;
use Atk4\Login\Model\User;
use Atk4\Ui\App;

abstract class GenericTestCase extends BaseTestCase
{
Expand All @@ -24,6 +25,14 @@ protected function tearDown(): void
parent::tearDown();
}

protected function createAppForSession(): App
{
return new App([
'catch_exceptions' => false,
'always_run' => false,
]);
}

protected function setupDefaultDb(): void
{
$this->setDb([
Expand Down

0 comments on commit 0b22239

Please sign in to comment.