Yii2 - 使用多个后端会话和cookie

时间:2017-12-28 12:09:30

标签: php yii yii2

我告诉你这个案子。 基本上我在yii2高级模板中有单独的后端。 为什么?这就是原因

我的办公室在一个国家有很多分支机构,每个分支机构都有很多部门。

这些部分,我将它们解释为模块。 部门名称相同但有时候,它们有很多不同 行为。

例如,总部的管理员可以删除分支中的员工姓名 办公室,但管理员分公司,他们不能。

所以,我选择将它们分成后端文件夹,如下所示:

backend  (which is portal branch and also super-admin backend)
  -modules
    -human_resource

backend-jkt (which is Jakarta Indonesia backend)
  -modules
    -human_resource

enter image description here

我的问题是:

当用户成功登录后端时,我创建了一个指向backend-jkt的链接,它也会自动登录。

反之亦然,

当人们直接后端-jkt但没有登录到后端时,它会自动重定向到后端的登录,

现在我的情况是:当用户登录到后端,然后点击图片中的链接“雅加达”时,用户必须再次登录。

这是我在后端的配置

<?php
$params = array_merge(
    require __DIR__ . '/../../common/config/params.php',
    require __DIR__ . '/../../common/config/params-local.php',
    require __DIR__ . '/params.php',
    require __DIR__ . '/params-local.php'
);

return [
    'id' => 'app-backend',
    'name' => 'Backend System',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'backend\controllers',
    'bootstrap' => ['log'],
    'modules' => [
        'mimin' => [
            'class' => '\hscstudio\mimin\Module',
        ],
        'SuperAdmin' => [
            'class' => 'backend\modules\super_admin\SuperAdmin',
        ],
    ],
    'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => [
                'name' => '_identity-backend',
                'httpOnly' => true
            ],
        ],
        'session' => [
            // this is the name of the session cookie used for login on the backend
            'name' => 'advanced-backend',
            'savePath' => sys_get_temp_dir(),
        ],
        'request' => [
            'cookieValidationKey' => 'IkR77lm93Rcb9TCoYTAZ',
            'csrfParam' => '_csrf-backend',
        ],

        'assetManager' => [
            'bundles' => [
                'dmstr\web\AdminLteAsset' => [

                ],
            ],
        ],

        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'urlManager' => [
            'suffix' => '.html',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],

        'urlManagerBackendJkt' => [
            'class' => 'yii\web\urlManager',
            'baseUrl' => '/backend-jkt/web/',

            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                'http://jkt.tresnamuda.local/' => '@app/index',
            ],
        ],
        'authManager' => [
            'class' => 'yii\rbac\DbManager', // only support DbManager
        ],

    ],
    'as access' => [
        'class' => '\hscstudio\mimin\components\AccessControl',
        'allowActions' => [
            // add wildcard allowed action here!
            'site/*',
            'debug/*',
            // 'mimin/*', // only in dev mode
        ],
    ],
    'params' => $params,
];

这是后端-jkt

<?php
$params = array_merge(
    require __DIR__ . '/../../backend/config/params.php',
    require __DIR__ . '/../../backend/config/params-local.php',
    require __DIR__ . '/params.php',
    require __DIR__ . '/params-local.php'
);

return [
    'id' => 'app-backend_jkt',
    'name' => 'Jkt Backend System',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'backend_jkt\controllers',
    'bootstrap' => ['log'],
    'modules' => [
        'mimin' => [
            'class' => '\hscstudio\mimin\Module',
        ],
    ],
    'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => [
                'name' => '_identity-backend',
                'httpOnly' => true
            ],
        ],
        'session' => [
            // this is the name of the session cookie used for login on the backend
            'name' => 'advanced-backend',
            'savePath' => sys_get_temp_dir(),
        ],
        'request' => [
            'cookieValidationKey' => 'IkR77lm93Rcb9TCoYTAZ',
            'csrfParam' => '_csrf-backend',
        ],

        'assetManager' => [
            'bundles' => [
                'dmstr\web\AdminLteAsset' => [

                ],
            ],
        ],

        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'urlManager' => [
            'suffix' => '.html',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],

        'authManager' => [
            'class' => 'yii\rbac\DbManager', // only support DbManager
        ],

    ],
    'as access' => [
        'class' => '\hscstudio\mimin\components\AccessControl',
        'allowActions' => [
            // add wildcard allowed action here!
            'site/*',
            'debug/*',
            // 'mimin/*', // only in dev mode
        ],
    ],
    'params' => $params,
];

1 个答案:

答案 0 :(得分:0)

您对用户浏览器中的Cookie按域和路径分类的问题,因此您必须将其存储到下一个域路径,我建议您在单击雅加达后将用户ID和私钥发送到雅加达并通过简单命令强制登录user-id:

if(private-key is Okey and you get $user-id by POST ) {

$user = User::findOne($user-id);
Yii::$app->getUser()->login($user);

}

私钥很简单或提前为什么你可以增加你的安全性,你可以保留它,只是检查是否有用户ID!