Due to the coincidence that ZendFramework has been used for a year's projects, we also have some knowledge about the relevant packages of the ZF framework. we used tp rbac to find databases the other day, it is not easy to use. maybe it is because you are biased ..
Due to the coincidence that Zend Framework has been used for one year's projects, we also have some knowledge about the packages related to the ZF Framework. we used tp rbac to find databases the other day, in addition, it is not very easy to use. maybe it is because of your own bias. so I gave up the RBAC permission provided by TP and chose ZEND ACL.
Step 1:Download the ZF framework package from the Zend official website. decompress the package and copy the Acl. php file in the same directory as the Acl package to the third-party class library directory Vendor/Zend provided by the TP framework.
Step 2:Because I used the configuration file in ini format when using the ZEND ACL, and also made it easy for the ACL to read the ini file, Zend Config is used in this place, copy the package to the Vendor/Zend directory, as well as the Config. the PHP file is also copied to the Vendor/Zend directory.
Step 3:Because both packages use the Zend_Exception class, copy the Exception. php file in the Zend directory to the Vendor directory.
Note:Why is there a Zend Directory? because the directory Zend is used when both the Acl and Config packages reference each other's classes. therefore, ensure that the program can be referenced normally, the Acl and Config packages must be placed in the Vendor/Zend directory.
Step 4:Configure resources. The so-called resource is the resource that we want to use ACL management. only these resources will be managed by ACL. resources not in the configuration column will not apply ACL control, because my program is a group used. Therefore, I created an acl. ini file under the Admin. Admin directory under the Conf Directory. This file is used to save the resources we want to control.
Format:
;; ========================================================== =
; Description of ACL settings:
; 1. resources is the resource list. only resources in the resource list are controlled.
; 2. resources format: Custom name. key name (arbitrary, but unavailable) = Module: Controller
; 3. here, move the Zend ACL to Thinkphp for use. the configuration here is no different from the configuration in Zend.
; Admin indicates the group [here Admin group] to which the application is applied. If no group is independent, a random group is set. In the ACL class.
; Here, the lower case after 'admin: 'must be entered. whether your operation is in upper or lower case, it must be in lower case.
;; ========================================================== =
- [ACL]
- ; ;=== Resource part
- Resource. index = Admin: index
- Resource. ad = Admin: ad
- Resource. article = Admin: article
- Resource. grievance = Admin: grievance
- Resource. login = Admin: login
- Resource. secret = Admin: secret
- Resource. user = Admin: user
- Resource. common = Admin: common
Step 5:When a control class is created and resources are available, we need to specify roles and allocate resources. because background programs or other management programs usually have multiple roles for operations at the same time, different roles have different permissions and different permissions correspond to different resources. Therefore, we need to use this class to refine the rules as follows:
- Class AdminAcl
- {
- // Default role
- Private $ _ defaultRole = 'guest ';
- Public function _ construct ()
- {
- Vendor ('zend. acl ');
- Vendor ('zend. Config. ini ');
- $ This-> acl = new Zend_Acl ();
- // Three roles are defined: [tourist], customer service, and Super Administrator by default.
- $ This-> acl-> addRole (new Zend_Acl_Role ($ this-> _ defaultRole ));
- $ This-> acl-> addRole (new Zend_Acl_Role ('service '));
- $ This-> acl-> addRole (new Zend_Acl_Role ('admin '));
-
- // Read the configuration file. The configuration file specifies the type to be controlled.
- $ AclConfig = new Zend_Config_Ini (APP_PATH. '/Conf/Admin/acl. ini', 'acl ');
- $ AclSettings = $ aclConfig-> toArray ();
-
- // Add resources
- Foreach ($ aclSettings as $ key => $ arr ){
- $ This-> acl-> add (new Zend_Acl_Resource ($ key ));
- Foreach ($ arr as $ value ){
- $ This-> acl-> add (new Zend_Acl_Resource ($ value), $ key );
- }
- }
-
- // Tourist permission, accessible resources: only access the index of the login module
- // The third parameter is the operation that the user can access.
- $ This-> acl-> allow ('guest ', 'admin: login', array ('index '));
-
- // Customer service permissions.
- $ This-> acl-> allow ('service', 'admin: login ');
- $ This-> acl-> allow ('service', 'admin: index ');
- $ This-> acl-> allow ('service', 'admin: ad ');
- $ This-> acl-> allow ('service', 'admin: article ');
- $ This-> acl-> allow ('service', 'admin: grievance ');
- $ This-> acl-> allow ('service', 'admin: secret ');
- // $ This-> acl-> allow ('service', 'admin: user'); // The user management must be a super administrator.
- // $ This-> acl-> allow ('service', 'admin: common ');
-
- // Super Administrator can access any resource
- $ This-> acl-> allow ('admin ');
- $ This-> preDispatch ();
- }
- Public function preDispatch ()
- {
- $ Role = $ this-> _ defaultRole; // The default role is tourist.
- // The first step is to obtain the user information, and put the relevant verification here.
- $ Is_login = true;
- If ($ is_login)
- {
- $ Userinfo = array ('usertype' => 3 );
- If ($ userinfo ['usertype'] = 1)
- {
- $ Role = 'admin ';
- }
- Elseif ($ userinfo ['usertype'] = 2)
- {
- $ Role = 'service ';
- }
- }
-
- // Determine whether the role is in the registered role.
- If (! $ This-> acl-> hasRole ($ role ))
- {
- $ Role = 'guest ';
- }
-
- $ Group_name = GROUP_NAME;
- $ Module_name = strtolower (MODULE_NAME );
- $ Action_name = strtolower (ACTION_NAME );
- $ Resource = "$ group_name: $ module_name ";
- If (! $ This-> acl-> has ($ resource ))
- {
- $ Resource = null;
- }
-
- // If you do not have the permission, the logon page is displayed.
- If (! $ This-> acl-> isAllowed ($ role, $ resource, $ action_name ))
- {
- Redirect (U ('admin-Login/index '));
- }
- }
- }
Note:APP_PATH: this is a constant. This constant is used in the index of the project entry file. the PHP file defines that its value is the Directory of the entry file. in this case, it is equivalent to the directory path of the current project, because I just added the acl. ini is stored in the Conf/Admin directory.
The above class is stored in the Commmon/Admin Group Directory under the project directory and named AdminAcl. class. php.
Step 6:Call this control class before calling any operation. This step is very simple, because we will create a CommonAction during Project creation. class. php files or classes inherit all other actions. Therefore, I call the control class in the _ initialize () method of the CommonAction class. the call method is as follows:
- Public function _ initialize ()
- {
- Import ('adminacl', 'common/admin ');
- New AdminAcl ();
- }
In this way, the initialization method will be called every time other actions are executed, and the resource control class AdminAcl will be automatically called, and judgment and control will be performed according to the preDespatch () method rules in this class.
Post-statement:In fact, in step 5, we finally called the redirect () method to redirect. this method is a TP system function. the URL address in it can be configured in the group configuration, this achieves a fully automated or semi-automatic effect.
The above usage is very common in ZF. if TP is not used, it will be used in ZF framework.