Cakephp的权限管理可以基于Controller - Action,当用户访问某个方法时,判断是否已经针对该方法授权。
数据库:
手动维护Action清单比较麻烦,可以写代码自动生成:
//create actions
public function createActions()
{
$connection = ConnectionManager::get('default');
//get all tables
$results = $connection->execute('truncate actions');
$results = $connection->execute('show tables')->fetchAll();
foreach($results as $result){
//get each controller
$controllerName = Inflector::camelize($result[0]);
//insert into action: index/add/delete/view/edit
$this->loadModel('Actions');
$methods = ['index', 'add', 'edit', 'view', 'delete'];
foreach($methods as $method){
$action = $this->Actions->newEntity();
$actionData = [
'name' => $controllerName.'-'.$method,
'controller' => $controllerName,
'action' => $method
];
$action = $this->Actions->patchEntity($action, $actionData);
$this->Actions->save($action);
}
}
echo 'create actions success';
exit;
}