Heim > Artikel > Backend-Entwicklung > Einführung der Auth- und Acl-Kontrolle in das CakePHP-Projekt
Notieren Sie die Schritte hier einfach zum späteren Nachschlagen.
1. Auth /app/Controller/AppController.php einführen
PHP-Code
class AppController extends Controller { public $components = array( 'Acl', 'Auth' => array( 'authorize' => array( 'Actions' => array('actionPath' => 'controllers') ) ), 'Session' ); public $helpers = array('Html', 'Form', 'Session'); public function beforeFilter() { //Configure AuthComponent $this->Auth->loginAction = array( 'controller' => 'users', 'action' => 'login' ); $this->Auth->logoutRedirect = array( 'controller' => 'users', 'action' => 'login' ); $this->Auth->loginRedirect = array( 'controller' => 'posts', 'action' => 'add' ); } }
2. ACL-Tabelle generieren
Bash-Code
./Console/cake schema create DbAcl
3. Gruppen und Benutzer hinzufügen
Modelldatei/app/Model/User.php festlegen
PHP-Code
class User extends AppModel { public $belongsTo = array('Group'); public $actsAs = array('Acl' => array('type' => 'requester')); public function parentNode() { if (!$this->id && emptyempty($this->data)) { return null; } if (isset($this->data['User']['group_id'])) { $groupId = $this->data['User']['group_id']; } else { $groupId = $this->field('group_id'); } if (!$groupId) { return null; } return array('Group' => array('id' => $groupId)); } public function bindNode($user) { return array('model' => 'Group', 'foreign_key' => $user['User']['group_id']); } }
Datei/App/Modell/Gruppe.php
PHP-Code
class Group extends AppModel { public $actsAs = array('Acl' => array('type' => 'requester')); public function parentNode() { return null; } }
Verwenden Sie Bake, um MVC-Dateien für Benutzer und Gruppen zu generieren, Gruppen und Benutzer hinzuzufügen, und Aros-Daten generieren.
4. Verwenden Sie AclExtras, um ACO-Tabellendaten zu generieren.
Laden Sie AclExtras herunter und installieren Sie es im Verzeichnis /app/Plugin/.
PHP-Code
//app/Config/boostrap.php // ... CakePlugin::load('AclExtras'); 利用bash命令生成可用的acos数据 Bash代码 ./Console/cake AclExtras.AclExtras aco_sync
5. An- und Abmeldung ergänzen
PHP-Code
<!-- login.ctp --> <h2>Login</h2> <?php echo $this->Form->create('User', array( 'url' => array( 'controller' => 'users', 'action' => 'login' ) )); echo $this->Form->input('User.username'); echo $this->Form->input('User.password'); echo $this->Form->end('Login'); ?> ############分割线######## // action public function login() { if ($this->Session->read('Auth.User')) { $this->Session->setFlash('You are logged in!'); return $this->redirect('/'); } }
PHP-Code
public function logout() { $this->redirect($this->Auth->logout()); }
6. ACO verwandt
Die Anzeige von ACOS verwendet TreeBehavior
PHP-Code
// /app/Model/Aco.php 文件 public $actsAs = array('Tree'); public $displayField = 'alias'; // 输出 $this->Aco->generateTreeList(null, null, null, ' ');
7. Berechtigungszuweisung
PHP-Code
public function initDB() { $group = $this->User->Group; // Allow admins to everything $group->id = 1; $this->Acl->allow($group, 'controllers'); // allow managers to posts and widgets $group->id = 2; $this->Acl->deny($group, 'controllers'); $this->Acl->allow($group, 'controllers/Posts'); $this->Acl->allow($group, 'controllers/Widgets'); // allow users to only add and edit on posts and widgets $group->id = 3; $this->Acl->deny($group, 'controllers'); $this->Acl->allow($group, 'controllers/Posts/add'); $this->Acl->allow($group, 'controllers/Posts/edit'); $this->Acl->allow($group, 'controllers/Widgets/add'); $this->Acl->allow($group, 'controllers/Widgets/edit'); // allow basic users to log out $this->Acl->allow($group, 'controllers/users/logout'); // we add an exit to avoid an ugly "missing views" error message echo "all done"; exit; }
8. Organisieren
Php代码 /** * custom beforeFilter */ public function beforeFilter() { parent::beforeFilter(); $this->Auth->allow('XXX'); // $this->Auth->allow(); }