Separate Frontend "User" and Backend "Admin" on Yii2 Advanced

Separate Frontend "User" and Backend Admin on Yii2 Advanced

Login to your database and duplicate or copy the "user" table naming it as "admin".

Example /common/models/User.php and name it Admin.php. Open it up, and change the name of the class from 'User' to 'Admin'. You also need to change the tableName() function to return '{{%admin}}'.

example of /common/models/Admin.php:

class Admin extends ActiveRecord implements IdentityInterface
{

    public static function tableName()

    {

        return '{{%admin}}';

    }

}


Duplicate /common/models/LoginForm.php and name it AdminLoginForm.php. Open it up, and change the name of the class from 'LoginForm' to 'AdminLoginForm'. At the bottom of the class, there is the getUser() function. It references the User class. Change User::findByUsername($this->username) to Admin::findByUsername($this->username). Save the file.

example of /common/models/AdminLoginForm.php:

class AdminLoginForm extends Model
{
    public function getUser()
    {
        if ($this->_user === false) {
            $this->_user = Admin::findByUsername($this->username);
        }
        return $this->_user;
    }
}


Now you need to update /backend/controllers/SiteController.php to use these new models. You need to change all references of 'User' to 'Admin' and 'LoginForm' to 'AdminLoginForm', in the namespaces and the code!

example of /backend/controllers/SiteController.php:

namespace backend\controllers;
    use Yii;
    use yii\filters\AccessControl;
    use yii\web\Controller;
    use common\models\AdminLoginForm;
    use yii\filters\VerbFilter;
    class SiteController extends Controller
    {
        public function actionLogin()
        {
            if (!\Yii::$app->user->isGuest) {
                return $this->goHome();
            }


            $model = new AdminLoginForm();
            if ($model->load(Yii::$app->request->post()) && $model->login()) {
                return $this->goBack();
            } else {
                return $this->render('login', [
                    'model' => $model,
                ]);
            }
        }
    }


example of /frontend/config/main.php:

'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => [
                'name' => '_frontendUser', // unique for frontend
            ]
        ],
        'session' => [
            'name' => 'PHPFRONTSESSID',
            'savePath' => sys_get_temp_dir(),
        ],
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => '[RANDOM KEY HERE]',
            'csrfParam' => '_frontendCSRF',
        ],
    ],


example of /backend/config/main.php:

'components' => [
        'user' => [
            'identityClass' => 'common\models\Admin',
            'enableAutoLogin' => true,
            'identityCookie' => [
                'name' => '_backendUser', // unique for backend
            ]
        ],
        'session' => [
            'name' => 'PHPBACKSESSID',
            'savePath' => sys_get_temp_dir(),
        ],

        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => '[DIFFERENT UNIQUE KEY]',
            'csrfParam' => '_backendCSRF',
        ],
    ],


No comments:

Powered by Blogger.