yii2学习权限啦(我想助你学好权限基础操作)注释打开一个操作一个,不然会报错哦

2022-03-25 20:41:18 浏览数 (1)

代码语言:javascript复制
'authManager' => [
            'class' => 'yiirbacDbManager',
            'itemTable' => 'auth_item',
            'assignmentTable' => 'auth_assignment',
            'itemChildTable' => 'auth_item_child',
        ],

user表:

代码语言:javascript复制
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: 2022-02-10 08:08:14
-- 服务器版本: 10.1.13-MariaDB
-- PHP Version: 5.6.21

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = " 00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `fff`
--

-- --------------------------------------------------------

--
-- 表的结构 `user`
--

CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `auth_key` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
  `password_hash` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `password_reset_token` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `status` smallint(6) NOT NULL DEFAULT '10',
  `created_at` int(11) NOT NULL,
  `updated_at` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- 转存表中的数据 `user`
--

INSERT INTO `user` (`id`, `username`, `auth_key`, `password_hash`, `password_reset_token`, `email`, `status`, `created_at`, `updated_at`) VALUES
(1, 'cyg', '123456', '123456', NULL, '', 10, 0, 0);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `user`
--
ALTER TABLE `user`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `username` (`username`),
  ADD UNIQUE KEY `email` (`email`),
  ADD UNIQUE KEY `password_reset_token` (`password_reset_token`);

--
-- 在导出的表使用AUTO_INCREMENT
--

--
-- 使用表AUTO_INCREMENT `user`
--
ALTER TABLE `user`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
代码语言:javascript复制
<?php
namespace frontendcontrollers;
use Yii;
use yiiwebController;
class RbacController extends Controller
{
	public function actionIndex()
	{
		//第一步设置权限
	/*$auth=Yii::$app->authManager;
	$createPost=$auth->createPermission("创建");
	//注册进权限表中
	//描述
	$createPost->description="这是创建的权限";
	$auth->add($createPost);*/
	/*$auth=Yii::$app->authManager;
	$role=$auth->createRole("陈业贵");
	$role->description="陈业贵角色";
	$auth->add($role);*/
	//获取权限
	/*$auth=Yii::$app->authManager;
	$parent=$auth->getRole("陈业贵");
	$child=$auth->getPermission("创建");
	//把创建的权限给陈业贵角色
		$auth->addChild($parent,$child);*/
		//给角色分配用户(user表中的cyg用户)
		/*$auth=Yii::$app->authManager;
		$par=$auth->getRole("陈业贵");
		$auth->assign($par,1);*/
		//验证用户是否用权限
		/*$user=commonmodelsUser::findOne(['id'=>1]);
			Yii::$app->user->login($user);
			var_dump(Yii::$app->user->can('创建'));*/
			/*$id = Yii::$app->user->id;
			var_dump($id);*/
			
	}


}

0 人点赞