Home > Article > PHP Framework > Module permission assignment in thinkphp
uses the Authority permission class that comes with ThinkPHP! ThinkPHP version is 3.1.3
What I want to achieve is to assign permissions based on the module name. Of course, this can be extended to the operation name.
If I have these module files:
Then the contents of the think_auth_rule table should be almost like this:
For example, my login user’s uid=7.
The think_auth_group_access table has uid=9, group=6;
The think_auth_group table has id=6, title=" Universe administrator", rules="4,5,8";
Then, I just need to add:
class CommAction extends Action{ public function __construct(){ parent::__construct(); $this->assign('waitSecond',2); $this->checkRight(); } private function checkRight(){ import('ORG.Util.Authority'); $auth=new Authority(); $r = $auth->getAuth(MODULE_NAME,session('S_USER_ID')); if(!$r){ $this->error('没有权限!'); } } }
to the module file CommAction.class.php, and then let other The module file inherits this file, for example, ActivityAction.class.php:
<?php class activityAction extends CommAction{ //操作方法 } ?>
In this way, when I access the modules with IDs 4, 5, and 8 in think_auth_rules, I can access it normally;
If you access the module with ID 10, 11, 12, 13, it will jump to the page that fails and prompts that there is no permission~
Now the problem I encounter when using this permission class is: think_auth_rule table I need to add the content manually. This part is developed by attributes. It would be great if it could be automatically generated.
Recommended tutorial: "TP5"
The above is the detailed content of Module permission assignment in thinkphp. For more information, please follow other related articles on the PHP Chinese website!