/**
* Check whether the current administrator has permission to access this page
*/
Public Function Chkpri ()
{
Gets the model name, controller name, method name that the current administrator is trying to access
TP Zhongzheng with three constants
Module_name, Controller_name, Action_name
$adminId = session (' ID ');
If the Super administrator returns TRUE directly
if ($adminId = = 1)
return TRUE;
$arModel = D (' Admin_role ');
$has = $arModel->alias (' a ')
->join (' LEFT join __role_pri__ B on a.role_id=b.role_id
Left JOIN __privilege__ C on B.pri_id=c.id ')
->where (Array (
' a.admin_id ' = Array (' eq ', $adminId),
' C.module_name ' = Array (' eq ', module_name),
' C.controller_name ' = Array (' eq ', controller_name),
' C.action_name ' = Array (' eq ', action_name),
))->count ();
Return ($has > 0);
}
Call this method before accessing any method to determine if there is permission to access this page
In public method display
<?php
namespace Admin\controller;
Use Think\controller;
Class Basecontroller extends Controller
{
Public Function __construct ()
{
The constructor of the parent class must be called first
Parent::__construct ();
Determine login
if (!session (' id '))
$this->error (' must be logged in first! ', U (' Login/login '));
All administrators have access to the back page
if (Controller_name = = ' Index ')
return true;
$priModel =d (' privilege ');
if (! $priModel->chkpri ())
$this->error (' Unauthorized access! ');
}
}
/**
* Get permissions for the first two levels currently owned by the administrator
*
*/
Public Function Getbtns ()
{
/*************** Remove all permissions that are currently owned by the current administrator ****************/
$adminId = session (' ID ');
if ($adminId = = 1)
{
$priModel = D (' Privilege ');
$priData = $priModel->select ();
}
Else
{
Remove the permissions that are owned by the current administrator's role
$arModel = D (' Admin_role ');
$priData = $arModel->alias (' a ')
->field (' DISTINCT c.id,c.pri_name,c.module_name,c.controller_name,c.action_name,c.parent_id ')
->join (' LEFT join __role_pri__ B on a.role_id=b.role_id
Left JOIN __privilege__ C on B.pri_id=c.id ')
->where (Array (
' a.admin_id ' = Array (' eq ', $adminId),
))->select ();
}
/*************** Select the first two levels of **********************/from all permissions
$btns = Array (); Top Level Two permissions
foreach ($priData as $k = $v)
{
if ($v [' parent_id '] = = 0)
{
and find the child of this top.
foreach ($priData as $k 1 = $v 1)
{
if ($v 1[' parent_id ') = = $v [' id '])
{
$v [' Children '] [] = $v 1;
}
}
$btns [] = $v;
}
}
return $btns;
}
<div id= "Menu-list" >
<ul id= "Menu-ul" >
<?php
$priModel = D (' privilege ');
$btns = $priModel->getbtns ();
foreach ($btns as $k = $v):?>
<li class= "Explode" key= "02_cat_and_goods" name= "Menu" >
<?php echo $v [' pri_name '];?>
<ul>
<?php foreach ($v [' Children '] as $k 1 = $v 1):?>
<li class= "Menu-item" ><a href= "<?php Echo U ($v 1[' module_name ']. ' /'. $v 1[' Controller_name '). ' /'. $v 1[' action_name ');?> "target=" Main-frame "><?php echo $v 1[' Pri_name '];?></a></li>
<?php Endforeach;?>
</ul>
</li>
<?php Endforeach;?>
</ul>
</div>
Check whether the current administrator has permission to access this page, the background only shows the current administrator has access to the button