(1)配置组件,并实现组件对应的接口类
比较好的做法:frontend/config/main.php 配置app\models\User,backend/config/main.php 配置app\models\Adminuser
//frontend/config/main.php
return [
'components' => [
'user' => [
'identityClass' => 'app\models\User',
],
],
];
//app\models\User
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class User extends ActiveRecord implements IdentityInterface
{
public static function tableName()
{
return 'user';
}
/**
* 根据给到的ID查询身份。
*
* @param string|integer $id 被查询的ID
* @return IdentityInterface|null 通过ID匹配到的身份对象
*/
public static function findIdentity($id)
{
return static::findOne($id);
}
/**
* 根据 token 查询身份。
*
* @param string $token 被查询的 token
* @return IdentityInterface|null 通过 token 得到的身份对象
*/
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
/**
* @return int|string 当前用户ID
*/
public function getId()
{
return $this->id;
}
/**
* @return string 当前用户的(cookie)认证密钥
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* @param string $authKey
* @return boolean if auth key is valid for current user
*/
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
}
(2)使用
$isGuest = Yii::$app->user->isGuest;// 判断当前用户是否是游客(未认证的)
$id = Yii::$app->user->id;// 当前用户的ID。 未认证用户则为 Null 。
$identity = Yii::$app->user->identity;//当前用户的身份实例。未认证用户则为 Null 。
$identity = User::findOne(['username' => $username]);// 使用指定用户名获取用户身份实例。请注意,如果需要的话您可能要检验密码
Yii::$app->user->login($identity);// 登录用户
Yii::$app->user->logout();//注销用户
二、用户授权
两种方式:ACF(Access Controller Filter,存取控制过滤器)和RBAC(Role-Based Access Control,基于角色的访问访问控制)
(1)ACF(Access Controller Filter,存取控制过滤器)
(2)RBAC(Role-Based Access Control,基于角色的访问访问控制)
auth_item表:角色和权限
(name,type,description) type=1表示角色,type=2表示权限
auth_item_child表:给角色赋权限
(parent,child) parent是角色,child是权限
auth_assignment表:将角色指派给用户
(item_name,user_id) item_name是角色,user_id是用户
1.1 common/config/main.php
//common/config/main.php
'components' => [
'authManager' => [
'class' =>'yii\rbac\DbManager',
],
],
1.2 迁移rbac的数据库的4张表,迁移文件:vendor/yiisoft/yii2/rbac/migrations
php yii migrate —migrationPath=@yii/rbac/migrations
1.3 console/下创建 RbacControler.php,
namespace console\controllers;
use Yii;
use yii\console\Controller;
class RbacController extends Controller
{
public function actionInit()
{
$auth = Yii::$app->authManager;
// 添加 "createPost" 权限
$createPost = $auth->createPermission('createPost');
$createPost->description = '新增文章';
$auth->add($createPost);
// 添加 "updatePost" 权限
$updatePost = $auth->createPermission('updatePost');
$updatePost->description = '修改文章';
$auth->add($updatePost);
// 添加 "deletePost" 权限
$deletePost = $auth->createPermission('deletePost');
$deletePost->description = '删除文章';
$auth->add($deletePost);
// 添加 "approveComment" 权限
$approveComment = $auth->createPermission('approveComment');
$approveComment->description = '审核评论';
$auth->add($approveComment);
// 添加 "postadmin" 角色并赋予 "updatePost" “deletePost” “createPost”
$postAdmin = $auth->createRole('postAdmin');
$postAdmin->description = '文章管理员';
$auth->add($postAdmin);
$auth->addChild($postAdmin, $updatePost);
$auth->addChild($postAdmin, $createPost);
$auth->addChild($postAdmin, $deletePost);
// 添加 "postOperator" 角色并赋予 “deletePost”
$postOperator = $auth->createRole('postOperator');
$postOperator->description = '文章操作员';
$auth->add($postOperator);
$auth->addChild($postOperator, $deletePost);
// 添加 "commentAuditor" 角色并赋予 “approveComment”
$commentAuditor = $auth->createRole('commentAuditor');
$commentAuditor->description = '评论审核员';
$auth->add($commentAuditor);
$auth->addChild($commentAuditor, $approveComment);
// 添加 "admin" 角色并赋予所有其他角色拥有的权限
$admin = $auth->createRole('admin');
$commentAuditor->description = '系统管理员';
$auth->add($admin);
$auth->addChild($admin, $postAdmin);
$auth->addChild($admin, $commentAuditor);
// 为用户指派角色。其中 1 和 2 是由 IdentityInterface::getId() 返回的id (译者注:user表的id)
// 通常在你的 User 模型中实现这个函数。
$auth->assign($admin, 1);
$auth->assign($postAdmin, 2);
$auth->assign($postOperator, 3);
$auth->assign($commentAuditor, 4);
}
}
然后执行php yii rbac/init