• T7_825442
  • 165.9KB
  • zip
  • 0
  • VIP专享
  • 0
  • 2022-06-13 17:22
cakephp acl的实现,一个简单但功能强大的Acl / Auth插件
TO install just unzip 'pippoacl' folder in your plugins directory. execute sql statement in pippoacl/docs/sql/pippoacl.sql to your db. Add these to app_controller in appropriate places or copy the app_controller.php file in docs directory: var $helpers = array('Html','Ajax','Javascript'); var $components = array('Acl', 'Auth', 'RequestHandler','Cookie'); function beforeFilter() { $this->Auth->authorize = 'actions'; $this->Auth->loginAction = array('plugin'=>'pippoacl','controller' => 'users', 'action' => 'login'); $this->Auth->logoutRedirect = array('plugin'=>'pippoacl','controller' => 'users', 'action' => 'login'); //action that doesn't need have an acl $this->Auth->allowedActions = array('display','logout','login','register', 'confirm', 'forget', 'activate', 'reset','profile','switch_language'); //user needs to be active. $this->Auth->userScope = array('User.active' => 1); //add this if you want to localize your application # Localization App::import('Core', 'l10n'); $this->L10n = new L10n(); # if language is already set in session, get that $language = $this->Session->read('Config.language'); if(!$language) { $this->Session->write('Config.language', $language); } # now set the language $this->L10n->get($language); setlocale(LC_ALL, substr($this->L10n->locale, 0, 3) . strtoupper(substr($this->L10n->locale, 3, 2)) . '.' . $this->L10n->charset ); } Add these to routes.php: include(APP.'plugins'.DS.'pippoacl'.DS.'config'.DS.'routes.php'); add this if you wanna use the js and css bundled in the plugin: <?php echo $javascript->link('/pippoacl/js/prototype');?> <?php echo $javascript->link('/pippoacl/js/scriptaculous.js?load=effects');?> <?php echo $javascript->link('/pippoacl/js/controls');?> <?php echo $javascript->link('/pippoacl/js/table');?> <?php echo $html->css('/pippoacl/css/pippo');?> or copy the folder inside pippoacl/vendor directory into your app/webroot folder and then add them in ypur default.ctp layout like: <?php echo $javascript->link('prototype');?> <?php echo $javascript->link('scriptaculous.js?load=effects');?> <?php echo $javascript->link('controls');?> <?php echo $javascript->link('table');?> <?php echo $html->css('pippo');?> to add simple language u need to put in app/config/bootstrap.php look in example folder there is 1: <?php Configure::write('Languages', array( 'en' => 'English', 'it' => 'Italiano' )); ?> add your desidered language. after i make a simple elements for the view, to call it just put in your view or layout: <?php echo $this->element('languages'); ?> and you can edit the element to suite your need in app/plugins/pippoacl/views/elements/languages.ctp create in your app/locale the folder referred to your language and edit pippoacl/docs/locale/localedefault.ctp to your language and rename it in default.po so for example for italian in the end i have appname/app/locale/ita/LC_MESSAGES/default.po remember to change in pippoacl/controllers/users_controller.php this line: var $activationEmail = 'Activation <some@mail.it>'; var $activationSubject = 'Activate Your Account'; var $forgetEmail = 'Forget Email <some@mail.it>'; var $forgetSubject = 'Forgot Password'; var $resetEmail = 'Reset Password <some@mail.it>'; var $resetSubject = 'Reset Password'; they are used to email notification. also remember to chagne line 211: //set default role $this->User->saveField('role_id',1); in my example new reister user will be administrator, change the id based on your need. call the url http://yourserver/roles/cleanupAcl to initialize/cleanup the acl for your controller. login with username= admin passwd = admin Pippoacl basic action are: /users/index - list of user /users/login - login function /users/logout - logout function /users/register - simple form to register to your site /users/forget - password forget form, to reset it, an email will be sent to the user or email address with the reset code /users/reset/code - to reset the password only works with code /users/activate - action for activating user /users/profile - simple users's profile, if logged ofc /roles/index - list of roles /roles/acl - ajax based acl management /roles/cleanupAcl - used for automatic clean/delete of the acl all the mail layout can be modified, they are stored in pippoacl/views/elements/email folder. Pippoacl come with a acl based menu 1 component and 1 helper to render in the view. The component take care to build the menu and to store it in the session based on the current user logged in or to generate a "pulbic menu" in no oen is logged. For set up the component u only need to change the first variable to suite your need, go into appname/app/plugins/pippoacl/controllers/components/menu.php and change the line below: //avoid some of the controller, controller that don't have a menu public $avoidControllers = array('Pages', 'Utils','App'); //menu displayed in every page public $everMenu = array('Home' => '/'); //public menu items, menu stuff that don't need authentication public $publicMenu = array('Register'=>'/users/register','Forget'=> '/users/forget'); //action that don't need to be included in menu public $avoidActions = array('switch_language','forget','reset','register','activate','adjustperm','login','logout'); //user login menu public $userLogin = array('Login' => '/users/login'); //user logout menu public $userLogout = array('Logout' => '/users/logout'); This component take care of generate the menu, and store in a session variable called pippomenu, you can read and display the array in the view with: pr($session->read('pippomenu')); if you're lazy or you want an example how to reuse this menu i create a menu helper located in appname/app/plugins/pippoacl/views/helper/menu.php. The main function is render and the default syntax to use in your view is: <?php //Controller's action that need to be displayed, other in the controller will be not displayed $display = array('index','profile','acl','cleanupAcl'); //u can give a title to a action, else the title is taken with the controller name $title=array('Profile' => '/users/profile','Acl' => '/roles/acl' ,'Forget' => '/users/forget','Clean ACL'=>'/roles/cleanupAcl'); // u can only display some of your controller, this are displayed $filtercontroller=array('User','other','controller'); //else u can exclude (not displayed) other controller $exclude=array('Roles','some','controller'); //u can pass also a parameter, for example for edit, or delete function $id=$myid; //u can pass also a message for example for the delete message $message="Are you sure you want to delete # %s?"; echo $menu->render($session->read('pippomenu'),$display,$title,$filtercontroller,$id,$exclude,$message); ?> for the basic menu i use div and ul,li , u can copy and paste edit the code to suite your needs. Real word example usage: so if u want to customize the basic index view created with "cake bake" exactly the action row (edit,add,etc etc) u can use this piece of code: <?php //action we wanna display $display = array('view','edit','delete'); //give them some title $title=array('Edit' => '/roles/edit','View' => '/roles/view' ,'Delete' => '/roles/delete'); //we filter only the controller we want $filter= array('Roles'); //we give every row a different id $id=$role['Role']['id']; echo $menu->rendertable($session->read('pippomenu'),$display,$title,$filter,$id); ?> so we now u have a simple menu based on