1. Go to this page and download the library: Download liliuwei/thinkphp-auth library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
liliuwei / thinkphp-auth example snippets
//简单实例
namespace app\admin\controller;
use think\Controller;
class Base extends Controller
{
public function initialize()
{
if (!session('admin_id')) {
$this->redirect('login/index');
}
$auth = new \liliuwei\think\Auth();
$controller = strtolower(request()->controller());
$action = strtolower(request()->action());
$url = $controller . "/" . $action;
if (!$auth->check($url, session('admin_id'))) {
$this->error('抱歉,您没有操作权限');
}
}
//高级实例 根据用户积分判断权限
//Auth类还可以按用户属性进行判断权限, 比如 按照用户积分进行判断,假设我们的用户表 (tp_admin) 有字段 score 记录了用户积分。我在规则表添加规则时,定义规则表的condition 字段,condition字段是规则条件,默认为空 表示没有附加条件,用户组中只有规则 就通过认证。如果定义了 condition字段,用户组中有规则不一定能通过认证,程序还会判断是否满足附加条件。 比如我们添加几条规则:
//name字段:grade1 condition字段:{score}<100
//name字段:grade2 condition字段:{score}>100 and {score}<200
//name字段:grade3 condition字段:{score}>200 and {score}<300
//这里 {score} 表示 think_members 表 中字段 score 的值。
//那么这时候
$auth = new \liliuwei\think\Auth();
$auth->check('grade1', 1); //是判断用户积分是不是0-100
$auth->check('grade2', 1); //判断用户积分是不是在100-200
$auth->check('grade3', 1); //判断用户积分是不是在200-300
~~~
~~~php
//高级实例 右侧菜单根据权限隐藏实例
namespace app\admin\controller;
use think\Controller;
use think\Db;
class Base extends Controller
{
public function initialize()
{
if (!session('admin_id')) {
$this->redirect('login/index');
}
$auth = new \liliuwei\think\Auth();
$controller = strtolower(request()->controller());
$action = strtolower(request()->action());
$url = $controller . "/" . $action;
$data = Db::name('auth_rule')->order('sort','asc')->select();
$data = list_to_tree($data);
//排除不需要验证的规则
$no_check_default = ['index/index'];
$no_check_status_list = Db::name('auth_rule')->where('status', 0)->column('name');
$no_check_rules_list = explode(',', strtolower(implode(',', array_merge($no_check_default, (array)$no_check_status_list))));
$no_check_user_list = Db::name('admin')->where('is_admin', 1)->column('id');
if (!in_array(session('admin_id'), $no_check_user_list)) {
if (!in_array($url, $no_check_rules_list)) {
if (!$auth->check($url, session('admin_id'))) {
$this->error('抱歉,您没有操作权限');
}
}
foreach ($data as $k => $v) {
if (!$auth->check($v['name'], session('admin_id'))) {
unset($data[$k]);
} else {
if (isset($v['_child'])) {
foreach ($v['_child'] as $key => $value) {
if (!$auth->check($value['name'], session('admin_id'))) {
unset($data[$k]['_child'][$key]);
}
}
}
}
}
}
//unset($data[0]['_child'][0]);
//var_dump($data);
//mysql不区分字段内容大小写
$active_id = Db::name('auth_rule')->where('name', '=', $url)->field('id,pid,top_pid')->find();
// dump($active_id);
$this->assign([
'active_id' => implode(',', (array)$active_id),
'menu_nav' => $data,
'crumb_list' => get_crumb_list($url)
]);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.