PHP code example of elisdn / yii2-hybrid-authmanager

1. Go to this page and download the library: Download elisdn/yii2-hybrid-authmanager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

elisdn / yii2-hybrid-authmanager example snippets


'components' => [
    ...
    'authManager' => [
        'class' => 'elisdn\hybrid\AuthManager',
        'modelClass' => 'app\modules\User',
    ],
],

namespace app\models;

use elisdn\hybrid\AuthRoleModelInterface;

class User extends ActiveRecord implements IdentityInterface, AuthRoleModelInterface
{
    ...

    public static function findAuthRoleIdentity($id)
    {
        return static::findOne($id);
    }

    public static function findAuthIdsByRoleName($roleName)
    {
        return static::find()->where(['role' => $roleName])->select('id')->column();
    }

    public function getAuthRoleNames()
    {
        return (array)$this->role;
    }

    public function addAuthRoleName($roleName)
    {
        $this->updateAttributes(['role' => $this->role = $roleName]);
    }

    public function removeAuthRoleName($roleName)
    {
        $this->updateAttributes(['role' => $this->role = null]);
    }

    public function clearAuthRoleNames()
    {
        $this->updateAttributes(['role' => $this->role = null]);
    }

    ...
}

class User extends ActiveRecord implements IdentityInterface, AuthRoleModelInterface
{
    ...

    public function getAuthRoleNames()
    {
        return $this->roles;
    }

    public function addAuthRoleName($roleName)
    {
        $this->updateAttributes(['roles' => $this->roles = array_merge($this->roles, [$roleName])]);
    }

    public function removeAuthRoleName($roleName)
    {
        $this->updateAttributes(['roles' => $this->roles = array_diff($this->roles, [$roleName])]);
    }

    public function clearAuthRoleNames()
    {
        $this->updateAttributes(['roles' => $this->roles = []]);
    }

    ...
}

class User extends ActiveRecord implements IdentityInterface, AuthRoleModelInterface
{
    ...

    public function getAuthRoleNames()
    {
        return (array)Json::decode($this->roles);
    }

    public function addAuthRoleName($roleName)
    {
        $roles = (array)Json::decode($this->roles);
        $this->roles[] = $roleName;
        $this->updateAttributes(['role' => $this->roles = Json::encode($this->roles)]);
    }

    public function removeAuthRoleName($roleName)
    {
        $roles = (array)Json::decode($this->roles);
        $this->roles = array_diff($this->roles, [$roleName])
        $this->updateAttributes(['role' => $this->roles = Json::encode($this->roles)]);
    }

    public function clearAuthRoleNames()
    {
        $this->updateAttributes(['role' => $this->roles = Json::encode([])]);
    }

    ...
}

class User extends ActiveRecord implements IdentityInterface, AuthRoleModelInterface
{
    ...

    public static function onRenameRole(RenameRoleEvent $event)
    {
        self::updateAll(['role' => $event->newRoleName], ['role' => $event->oldRoleName]);
    }

    public static function onRemoveRole(RemoveRoleEvent $event)
    {
        self::updateAll(['role' => null], ['role' => $event->roleName]);
    }

    public static function onRemoveAll(RemoveAllEvent $event)
    {
        self::updateAll(['role' => null]);
    }

    public static function onRemoveAllAssignments(RemoveAllAssignmentsEvent $event)
    {
        self::updateAll(['role' => null]);
    }
}

'authManager' => [
    'class' => 'elisdn\hybrid\AuthManager',
    'modelClass' => 'app\models\User',
    'on renameRole' => ['app\models\User', 'onRenameRole'],
    'on removeRole' => ['app\models\User', 'onRemoveRole'],
    'on removeAll' => ['app\models\User', 'onRemoveAll'],
    'on removeAllAssignments' => ['app\models\User', 'onRemoveAllAssignments'],
],