Home >PHP Framework >ThinkPHP >How to set the permissions of the ThinkPHP project team
1. Understanding ThinkPHP permission settings
Permission settings refer to the allocation of operation permissions according to the user's permission range so that they can perform corresponding operate. In the ThinkPHP framework, permission settings can be implemented through RBAC (Role-Based Access Control). Role-based access control (RBAC) is an access control model that assigns permissions based on roles rather than users, and grants users access permissions by assigning roles. RBAC has the following characteristics:
Authorize permissions to roles, and then assign roles to users to facilitate management;
Users only need to have roles. You can have all the permissions owned by the role;
The system is easy to expand and maintain, and has good scalability;
Permissions are implemented Separation from business logic significantly improves code reuse and access security.
2. RBAC-based permission setting steps
Step 1 Create permission table
In Create a permission table in the database, including the fields id, name, title and status;
The id and name fields are the primary key and permission identifier;
In the ThinkPHP project, the method to implement permission control is as follows:
public function _initialize(){ if(!authcheck()){ } } public function authcheck(){ $auth=new Auth; if($auth->check(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME,session('uid'))){ return true; }else{ return false; } }
class Auth { //检查权限 public function check($name, $uid){ if(in_array($uid, C('AUTH_SUPER_ADMIN'))){ return true; } $infos=M('user')->field('role_id')->where('id='.$uid)->find(); $role_id=$infos['role_id']; $rules=M('access')->where('role_id='.$role_id)->select(); foreach($rules as $v){ $rule_ids[]=$v['rule_id']; } $rules=M('rule')->where('id in ('.implode(',',$rule_ids).')')->select(); foreach($rules as $r){ $urls[]=$r['name']; } if(in_array($name,$urls)){ return true; }else{ return false; } } }
The above is the detailed content of How to set the permissions of the ThinkPHP project team. For more information, please follow other related articles on the PHP Chinese website!