-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3893d52
commit ae4e2ed
Showing
3 changed files
with
198 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,27 @@ | |
use CWebUser; | ||
|
||
/** | ||
* WebUser extends the standard Yii class {@see \CWebUser}, providing handlers for the following events: | ||
* WebUser extends the standard Yii class {@see \CWebUser}, providing handlers for the authentication flow events. | ||
* | ||
* - {@see onAfterRestore} - raises after user data restoration; | ||
* - {@see onBeforeLogin} - raises before user logs in; | ||
* - {@see onAfterLogin} - raises after user successfully logged in; | ||
* - {@see onBeforeLogout} - raises before user logs out; | ||
* - {@see onAfterLogout} - raises after user successfully logged out; | ||
* Application configuration example: | ||
* | ||
* ```php | ||
* return [ | ||
* 'components' => [ | ||
* 'user' => [ | ||
* 'class' => yii1tech\web\user\WebUser::class, | ||
* ], | ||
* // ... | ||
* ], | ||
* // ... | ||
* ]; | ||
* ``` | ||
* | ||
* @property callable|\CList $onAfterRestore raises after user data restoration. | ||
* @property callable|\CList $onBeforeLogin raises before user logs in. | ||
* @property callable|\CList $onAfterLogin raises after user successfully logged in. | ||
* @property callable|\CList $onBeforeLogout raises before user logs out. | ||
* @property callable|\CList $onAfterLogout raises after user successfully logged out. | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
|
@@ -26,7 +40,9 @@ public function init() | |
{ | ||
parent::init(); | ||
|
||
$this->afterRestore(); | ||
if (!$this->getIsGuest()) { | ||
$this->afterRestore(); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -58,18 +74,19 @@ public function onAfterRestore($event): void | |
*/ | ||
protected function beforeLogin($id, $states, $fromCookie) | ||
{ | ||
$allowLogin = true; | ||
|
||
if ($this->hasEventHandler('onBeforeLogin')) { | ||
$this->onBeforeLogin(new CEvent($this, [ | ||
'allowLogin' => &$allowLogin, | ||
'id' => $id, | ||
'states' => $states, | ||
'fromCookie' => $fromCookie, | ||
])); | ||
if (!$this->hasEventHandler('onBeforeLogin')) { | ||
return true; | ||
} | ||
|
||
return $allowLogin; | ||
$event = new CEvent($this, [ | ||
'allowLogin' => true, | ||
'id' => $id, | ||
'states' => $states, | ||
'fromCookie' => $fromCookie, | ||
]); | ||
$this->onBeforeLogin($event); | ||
|
||
return $event->params['allowLogin']; | ||
} | ||
|
||
/** | ||
|
@@ -107,15 +124,17 @@ public function onAfterLogin($event): void | |
*/ | ||
protected function beforeLogout() | ||
{ | ||
$allowLogout = true; | ||
|
||
if ($this->hasEventHandler('onBeforeLogout')) { | ||
$this->onBeforeLogout(new CEvent($this, [ | ||
'allowLogout' => &$allowLogout, | ||
])); | ||
if (!$this->hasEventHandler('onBeforeLogout')) { | ||
return true; | ||
} | ||
|
||
return $allowLogout; | ||
$event = new CEvent($this, [ | ||
'allowLogout' => true, | ||
]); | ||
|
||
$this->onBeforeLogout($event); | ||
|
||
return $event->params['allowLogout']; | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
|
||
namespace yii1tech\web\user\test; | ||
|
||
use CEvent; | ||
use CUserIdentity; | ||
use Yii; | ||
use yii1tech\web\user\WebUser; | ||
|
||
class WebUserTest extends TestCase | ||
{ | ||
protected function createWebUser(): WebUser | ||
{ | ||
return Yii::createComponent([ | ||
'class' => WebUser::class, | ||
'stateKeyPrefix' => '', | ||
]); | ||
} | ||
|
||
public function testBeforeLoginEvent(): void | ||
{ | ||
$webUser = $this->createWebUser(); | ||
|
||
$event = null; | ||
$webUser->onBeforeLogin = function (CEvent $raisedEvent) use (&$event) { | ||
$event = $raisedEvent; | ||
}; | ||
|
||
$identity = new CUserIdentity('username', 'password'); | ||
$identity->setPersistentStates(['foo' => 'bar']); | ||
|
||
$webUser->login($identity); | ||
|
||
$this->assertFalse($webUser->getIsGuest()); | ||
|
||
$this->assertTrue($event instanceof CEvent); | ||
$this->assertEquals(true, $event->params['allowLogin']); | ||
$this->assertEquals(false, $event->params['fromCookie']); | ||
$this->assertEquals($identity->getId(), $event->params['id']); | ||
$this->assertEquals($identity->getPersistentStates(), $event->params['states']); | ||
} | ||
|
||
/** | ||
* @depends testBeforeLoginEvent | ||
*/ | ||
public function testBeforeLoginEventBlocksLogin(): void | ||
{ | ||
$webUser = $this->createWebUser(); | ||
|
||
$webUser->onBeforeLogin = function (CEvent $event) { | ||
$event->params['allowLogin'] = false; | ||
}; | ||
|
||
$identity = new CUserIdentity('username', 'password'); | ||
|
||
$webUser->login($identity); | ||
|
||
$this->assertTrue($webUser->getIsGuest()); | ||
} | ||
|
||
public function testAfterLoginEvent(): void | ||
{ | ||
$webUser = $this->createWebUser(); | ||
|
||
$event = null; | ||
$webUser->onAfterLogin = function (CEvent $raisedEvent) use (&$event) { | ||
$event = $raisedEvent; | ||
}; | ||
|
||
$identity = new CUserIdentity('username', 'password'); | ||
|
||
$webUser->login($identity); | ||
|
||
$this->assertFalse($webUser->getIsGuest()); | ||
|
||
$this->assertTrue($event instanceof CEvent); | ||
$this->assertEquals(false, $event->params['fromCookie']); | ||
} | ||
|
||
public function testBeforeLogoutEvent(): void | ||
{ | ||
$webUser = $this->createWebUser(); | ||
|
||
$event = null; | ||
$webUser->onBeforeLogout = function (CEvent $raisedEvent) use (&$event) { | ||
$event = $raisedEvent; | ||
}; | ||
|
||
$identity = new CUserIdentity('username', 'password'); | ||
|
||
$webUser->login($identity); | ||
$webUser->logout(); | ||
|
||
$this->assertTrue($webUser->getIsGuest()); | ||
|
||
$this->assertTrue($event instanceof CEvent); | ||
$this->assertEquals(true, $event->params['allowLogout']); | ||
} | ||
|
||
/** | ||
* @depends testBeforeLogoutEvent | ||
*/ | ||
public function testBeforeLogoutEventBlocksLogout(): void | ||
{ | ||
$webUser = $this->createWebUser(); | ||
|
||
$webUser->onBeforeLogout = function (CEvent $event) { | ||
$event->params['allowLogout'] = false; | ||
}; | ||
|
||
$identity = new CUserIdentity('username', 'password'); | ||
|
||
$webUser->login($identity); | ||
$webUser->logout(); | ||
|
||
$this->assertFalse($webUser->getIsGuest()); | ||
} | ||
|
||
public function testAfterLogoutEvent(): void | ||
{ | ||
$webUser = $this->createWebUser(); | ||
|
||
$event = null; | ||
$webUser->onAfterLogout = function (CEvent $raisedEvent) use (&$event) { | ||
$event = $raisedEvent; | ||
}; | ||
|
||
$identity = new CUserIdentity('username', 'password'); | ||
|
||
$webUser->login($identity); | ||
$webUser->logout(); | ||
|
||
$this->assertTrue($webUser->getIsGuest()); | ||
|
||
$this->assertTrue($event instanceof CEvent); | ||
} | ||
|
||
public function testAfterRestoreEvent(): void | ||
{ | ||
$webUser = $this->createWebUser(); | ||
|
||
$event = null; | ||
$webUser->onAfterRestore = function (CEvent $raisedEvent) use (&$event) { | ||
$event = $raisedEvent; | ||
}; | ||
|
||
$_SESSION['__id'] = 'username'; | ||
$_SESSION['__name'] = 'username'; | ||
|
||
$webUser->init(); | ||
|
||
$this->assertTrue($event instanceof CEvent); | ||
} | ||
} |