Thinkphp custom permission management method for determining the name, thinkphp permission management
Permission management is to assign different permissions to different users. When a user logs on or operates, the user is judged to prevent the user from performing operations other than permissions. This section describes how to enable the permission only when the user logs on.
1. Create a database.
1. Permission table funcia. To store all the permissions entered. This also avoids permission failure due to permission name modification.
2. Administrator table admin. It mainly stores information such as the administrator user name.
3. the admin permission table is funadmin. It stores the enabled administrator id and permission id.
2. output the permission list.
1. access permission assignment through the Administrator list.
2. Permission assignment list.
For the permission assignment list, we need to distinguish the top-level category from its subset because of different permission sections. The permission status also needs to be displayed. A layer of cyclic judgment is also used to determine the permission status. The Code uses three layers of nested loop output.
The Code is as follows:
<Div> assign administrator {$ username} permissions </div> <table width = "100%" border = "0" cellpadding = "0" cellspacing = "0" class =" list_table mt10 "> <tr> <th> permission name </th> <th> Status </th> </tr> <volist name =" funcia "id =" v" key = "j"> <tr class = "tr"> <td> {$ v. claname} </td> <input <volist name = "funadmin" id = "d"> <if condition = "$ d ['functionid'] eq $ v ['id'] "> checked </if> </volist> type =" checkbox "id =" {$ v. id} "name =" {$ userid} "onchange =" return setfun (this, seturl) "/> </td> </tr> <volist name =" fun "id =" vo "> <if condition =" $ vo ['clapid'] eq $ v ['id'] "> <tr class =" tr "> <td style =" padding-left: 40px; "> {$ vo. claname} </td> <input <volist name = "funadmin" id = "d"> <if condition = "$ d ['functionid'] eq $ vo ['id'] "> checked </if> </volist> type =" checkbox "id =" {$ vo. id} "name =" {$ userid} "onchange =" return setfun (this, seturl) "/> </td> </tr> </if> </volist> </table>Examples of loops are shown in the following figure:
3. The specific controller is as follows:
public function setfun(){ $uid=I('get.id',0,'int'); $a=M('admin'); $user=$a->where(array(id=>$uid))->field('username,id')->find(); $this->username=$user['username']; $this->userid=$user['id']; $m=M('funcla'); $funcla=$m->where(array(clapid=>'0'))->field(true)->select(); $fun=$m->field(true)->select(); $this->fun=$fun; $this->funcla=$funcla; $fd=M('funadmin'); $funadmin=$fd->where(array(adminid=>$uid))->field(true)->select(); $this->funadmin=$funadmin; $this->display(); }
4. js. When the user permission changes, it triggers js to submit json and pass data parameters to the Controller.
function setfun(t,u){ var id=$(t).attr('id'); var uid=$(t).attr('name'); var type=$(t).is(":checked")?1:0; var url=u; $.ajax({ url:url, type:'post', data:{ id:id, type:type, uid:uid }, success:function(data){ }, error:function(data){ } })}
The url transmitted in json is obtained using the instantiated address on the page. For example:
<script type="text/javascript"> var seturl="{:U("Admin/chanefun")}";</script>
3. The controller obtains json data parameters and judges how to add and modify permissions. If the permission is added, add the id of the receiver and the id of the permission to be authorized to the funadmin table as a data record. Modify to delete a data record that meets the condition. Similarly.
public function chanefun(){ $m=M('funadmin'); $where['funclaid']=I('post.id',0,'int'); $where['adminid']=I('post.uid',0,'int'); $type=I('post.type',0,'int'); if(empty($type)){ $oid=$m->where($where)->getfield('id'); $m->delete($oid); return; } $m->data($where)->add(); }
4. True permission judgment. The previous steps are to prepare for permission judgment. Now the true judgment is made. The basic principle is to compare the name of the permission to be judged with the current logon user ID in the funadmin table. If any, this indicates that the user has the permission to display the image. Otherwise, the image is hidden. The link address is invisible to the shadow, but you can directly access it through the address if you know the address.
(1) check whether the display is displayed on the front-end. The custom method chackQ () is called ();
<If condition = "chackQ ('Task management')"> <li> <a href = "{: U ('taskinfo/dir ')} "rel =" external nofollow "> task management </a> </li> </if>
(2) specific comparison operations.
Function chackQ ($ name, $ state = false) {if (! $ State) {exit ;}$ fun = M ('funcla'); $ funclaid = $ fun-> where (array (claname => $ name )) -> getfield ('id'); $ m = M ('funadmin'); $ adminid = session ('admin _ userid '); $ reset = $ m-> where (array (funclaid => $ funclaid, adminid => $ adminid)-> find (); if (empty ($ reset )) {echo "You do not have permission"; exit;} return $ reset ;}
So far, the entire permission control is basically complete.
The Method for Determining the name of thinkphp custom permission management is all the content that I have shared with you in this article. I hope you can give me a reference and support the help house.