Yii - Module with its UserIdentity component

I started using the Yii framework, and I have a question-question about using the UserIdentity component .

My application has an admin module that will act as a backoffice for content displayed on a real site. This module has its own table for administrators tbl_admin_user.

I want to split the login method (to check instead of this table instead tbl_user) of the module from one of the common sites. For this, I suggested that I need to implement the UserIdentity authentication method only for the module, and not for the full application, correct mine if I am mistaken.

When I override this method, I get nothing. The admin module uses the UserIdentity located in * protected \ components * instead of its own.

Any suggestion?

+3
source share
3 answers

You do not have to import your module identification class. To do this, simply change the init function in the class of your module, extending the CWebModule, as shown below:

public function init()
{
    $this->setImport(array(
        '#moduleName#.models.*',
        '#moduleName#.components.*',
    ));
}

Thus, all your module components (and models as well) will be imported, i.e. your module identification class.

: 2 UserIdentity. , - include_path. , CUserIdentity , . AdminUserIdentity. AdminUserIdentity ($ username, $password) UserIdentity ($ username, $password). .

+6

Yii2 User, :

    public function init() {
    // this overrides the User Identity class
    // and uses the one provided by the admin module
    \Yii::$app->set("user", [
         'class' => 'yii\web\User',
         'identityClass' => 'app\modules\admin\models\User',
         'enableAutoLogin' => true,
    ]);
}
0

GiiModule :

/**
 * Initializes the gii module.
 */
public function init()
{
    parent::init();
    Yii::setPathOfAlias('gii',dirname(__FILE__));
    Yii::app()->setComponents(array(
        'errorHandler'=>array(
            'class'=>'CErrorHandler',
            'errorAction'=>$this->getId().'/default/error',
        ),
        'user'=>array(
            'class'=>'CWebUser',
            'stateKeyPrefix'=>'gii',
            'loginUrl'=>Yii::app()->createUrl($this->getId().'/default/login'),
        ),
        'widgetFactory' => array(
            'class'=>'CWidgetFactory',
            'widgets' => array()
        )
    ), false);
    $this->generatorPaths[]='gii.generators';
    $this->controllerMap=$this->findGenerators();
}

, , , .

0

Source: https://habr.com/ru/post/1792588/


All Articles