using auth in code with Yii2-usuario
This commit is contained in:
@ -34,6 +34,14 @@ Put this in your migration:
|
|||||||
$administratorRole->description = 'Administrator';
|
$administratorRole->description = 'Administrator';
|
||||||
$auth->add($administratorRole);
|
$auth->add($administratorRole);
|
||||||
|
|
||||||
|
// create permission for certain tasks
|
||||||
|
$permission = $auth->createPermission('user-management');
|
||||||
|
$permission->description = 'User Management';
|
||||||
|
$auth->add($permission);
|
||||||
|
|
||||||
|
// let administrators do user management
|
||||||
|
$auth->addChild($administratorRole, $auth->getPermission('user-management'));
|
||||||
|
|
||||||
// create user "admin" with password "verysecret"
|
// create user "admin" with password "verysecret"
|
||||||
$user = new \Da\User\Model\User([
|
$user = new \Da\User\Model\User([
|
||||||
'scenario' => 'create',
|
'scenario' => 'create',
|
||||||
@ -52,11 +60,15 @@ Put this in your migration:
|
|||||||
{
|
{
|
||||||
$auth = Yii::$app->authManager;
|
$auth = Yii::$app->authManager;
|
||||||
|
|
||||||
|
// delete permission
|
||||||
|
$auth->remove($auth->getPermission('user-management'));
|
||||||
|
|
||||||
// delete admin-user and administrator role
|
// delete admin-user and administrator role
|
||||||
$administratorRole = $auth->getRole("xw-administrator");
|
$administratorRole = $auth->getRole("administrator");
|
||||||
$user = \Da\User\Model\User::findOne(['name'=>"admin"]);
|
$user = \Da\User\Model\User::findOne(['name'=>"admin"]);
|
||||||
$auth->revoke($administratorRole, $user->id);
|
$auth->revoke($administratorRole, $user->id);
|
||||||
$user->delete();
|
$user->delete();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
## User Management
|
## User Management
|
||||||
@ -67,6 +79,29 @@ Having setup the ```admin``` user you can start using user management at
|
|||||||
|
|
||||||
You should be prompted a login screen and the enter ```admin/verysecret```.
|
You should be prompted a login screen and the enter ```admin/verysecret```.
|
||||||
|
|
||||||
|
## Working with authentication
|
||||||
|
|
||||||
|
Usually access restrictions to controller actions care specified in
|
||||||
|
[```behaviors()```](http://stuff.cebe.cc/yii2docs/guide-security-authorization.html).
|
||||||
|
|
||||||
|
Additionally, in your code you can directly use permission checks. This is
|
||||||
|
helpful e.g. in ```./views/layouts/main.php```.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
// Is current user a guest (not signed in?)
|
||||||
|
if (Yii::$app->user->isGuest) {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get roles of user
|
||||||
|
$roles = Yii::$app->authManager->getRolesByUser(Yii::$app->user->getId());
|
||||||
|
|
||||||
|
// Does current user have permission to do "user-management"?
|
||||||
|
if (Yii::$app->user->can("user-management")) {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
|
||||||
### Recommended Reading
|
### Recommended Reading
|
||||||
|
|
||||||
It is helpful to basically understand how Yii2 does authantication. The you can get in Yii2-usuario more quickly.
|
It is helpful to basically understand how Yii2 does authantication. The you can get in Yii2-usuario more quickly.
|
||||||
|
|||||||
Reference in New Issue
Block a user