如何使用这个简单的ACL库到codeigniter [英] how to use this simple acl library into codeigniter

查看:184
本文介绍了如何使用这个简单的ACL库到codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前使用的CakePHP,现在使用codeigniter,但遗憾的是并没有任何认证或ACL内置库..after更多搜索我已经找到一个好的图书馆,但我不知道如何使用它..它是没有例子使用it..any人不得不创建控制器和模型为样本...感谢帮助

i used cakephp before and now use codeigniter but Unfortunately hasn't any authentication or ACL built-in library ..after more search i have found a good library, but I do not know how to use it..it is no example to use it..any one have create controller and model as sample...thanks for helping

<?php

(defined('BASEPATH')) OR exit('No direct script access allowed');

class acl {
    /* Actions::::
     * Create 1
     * Read 2
     * Update 4
     * Delete 8
     * The allowance is made by a sum of the actions allowed.
     * Ex.: user can read and update (2+4)=6 … so ill put 6 instead of 1 or 0.
     *
     * if(!$this->acl->hasPermission(‘entries_complete_access')) {
      echo "No no";
      } else
     * echo "yeah";
      }
     *
     *
     */

    var $perms = array(); //Array : Stores the permissions for the user
    var $userID; //Integer : Stores the ID of the current user
    var $userRoles = array(); //Array : Stores the roles of the current user
    var $ci;

    function __construct($config = array()) {
        $this->ci = &get_instance();
        $this->userID = floatval($this->ci->session->userdata('account_id'));
        $this->userRoles = $this->getUserRoles();
        $this->buildACL();
    }

    function buildACL() {
//first, get the rules for the user's role
        if (count($this->userRoles) > 0) {
            $this->perms = array_merge($this->perms, $this->getRolePerms($this->userRoles));
        }
//then, get the individual user permissions
        $this->perms = array_merge($this->perms, $this->getUserPerms($this->userID));
    }

    function getPermKeyFromID($permID) {
//$strSQL = "SELECT `permKey` FROM `".DB_PREFIX."permissions` WHERE `ID` = " . floatval($permID) . " LIMIT 1″;
        $this->ci->db->select('permKey');
        $this->ci->db->where('id', floatval($permID));
        $sql = $this->ci->db->get('perm_data', 1);
        $data = $sql->result();
        return $data[0]->permKey;
    }

    function getPermNameFromID($permID) {
//$strSQL = "SELECT `permName` FROM `".DB_PREFIX."permissions` WHERE `ID` = " . floatval($permID) . " LIMIT 1″;
        $this->ci->db->select('permName');
        $this->ci->db->where('id', floatval($permID));
        $sql = $this->ci->db->get('perm_data', 1);
        $data = $sql->result();
        return $data[0]->permName;
    }

    function getRoleNameFromID($roleID) {
//$strSQL = "SELECT `roleName` FROM `".DB_PREFIX."roles` WHERE `ID` = " . floatval($roleID) . " LIMIT 1″;
        $this->ci->db->select('roleName');
        $this->ci->db->where('id', floatval($roleID), 1);
        $sql = $this->ci->db->get('role_data');
        $data = $sql->result();
        return $data[0]->roleName;
    }

    function getUserRoles() {
//$strSQL = "SELECT * FROM `".DB_PREFIX."user_roles` WHERE `userID` = " . floatval($this->userID) . " ORDER BY `addDate` ASC";

        $this->ci->db->where(array('userID' => floatval($this->userID)));
        $this->ci->db->order_by('addDate', 'asc');
        $sql = $this->ci->db->get('user_roles');
        $data = $sql->result();

        $resp = array();
        foreach ($data as $row) {

            $resp[] = $row->roleID;
        }
        return $resp;
    }

    function getAllRoles($format = 'ids') {
        $format = strtolower($format);
//$strSQL = "SELECT * FROM `".DB_PREFIX."roles` ORDER BY `roleName` ASC";
        $this->ci->db->order_by('roleName', 'asc');
        $sql = $this->ci->db->get('role_data');
        $data = $sql->result();

        $resp = array();
        foreach ($data as $row) {
            if ($format == 'full') {
                $resp[] = array('id' => $row->ID, 'name' => $row->roleName);
            } else {
                $resp[] = $row->ID;
            }
        }
        return $resp;
    }

    function getAllPerms($format = 'ids') {
        $format = strtolower($format);
//$strSQL = "SELECT * FROM `".DB_PREFIX."permissions` ORDER BY `permKey` ASC";

        $this->ci->db->order_by('permKey', 'asc');
        $sql = $this->ci->db->get('perm_data');
        $data = $sql->result();

        $resp = array();
        foreach ($data as $row) {
            if ($format == 'full') {
                $resp[$row->permKey] = array('id' => $row->ID, 'name' => $row->permName, 'key' => $row->permKey);
            } else {
                $resp[] = $row->ID;
            }
        }
        return $resp;
    }

    function getRolePerms($role) {
        if (is_array($role)) {
//$roleSQL = "SELECT * FROM `".DB_PREFIX."role_perms` WHERE `roleID` IN (" . implode(",",$role) . ") ORDER BY `ID` ASC";
            $this->ci->db->where_in('roleID', $role);
        } else {
//$roleSQL = "SELECT * FROM `".DB_PREFIX."role_perms` WHERE `roleID` = " . floatval($role) . " ORDER BY `ID` ASC";
            $this->ci->db->where(array('roleID' => floatval($role)));
        }
        $this->ci->db->order_by('id', 'asc');
        $sql = $this->ci->db->get('role_perms'); //$this->db->select($roleSQL);
        $data = $sql->result();
        $perms = array();
        foreach ($data as $row) {
            $pK = strtolower($this->getPermKeyFromID($row->permID));

            if ($pK == '') {
                continue;
            }
            /* if ($row->value == '1′) {
              $hP = true;
              } else {
              $hP = false;
              } */
            if ($row->value == '0') {
                $hP = false;
            } else {
                $hP = $row->value;
            }

            $perms[$pK] = array('perm' => $pK, '1inheritted' => true, 'value' => $hP, 'name' => $this->getPermNameFromID($row->permID), 'id' => $row->permID);
        }
        return $perms;
    }

    function getUserPerms($userID) {
//$strSQL = "SELECT * FROM `".DB_PREFIX."user_perms` WHERE `userID` = " . floatval($userID) . " ORDER BY `addDate` ASC";

        $this->ci->db->where('userID', floatval($userID));
        $this->ci->db->order_by('addDate', 'asc');
        $sql = $this->ci->db->get('user_perms');
        $data = $sql->result();

        $perms = array();
        foreach ($data as $row) {
            $pK = strtolower($this->getPermKeyFromID($row->permID));
            if ($pK == '') {
                continue;
            }
            /* if ($row->value == '1′) {
              $hP = true;
              } else {
              $hP = false;
              } */
            if ($row->value == '0') {
                $hP = false;
            } else {
                $hP = $row->value;
            }

            $perms[$pK] = array('perm' => $pK, '2inheritted' => false, 'value' => $hP, 'name' => $this->getPermNameFromID($row->permID), 'id' => $row->permID);
        }
        return $perms;
    }

    function hasRole($roleID) {
        foreach ($this->userRoles as $k => $v) {
            if (floatval($v) === floatval($roleID)) {
                return true;
            }
        }
        return false;
    }

    function actionPerm($value, $wanted) {
        /* Actions::::
         * Create 1
         * Read, 2
         * Update, 4
         * Delete 8
         */
        $action['create'] = array('1', '3', '5', '9', '11', '13', '15'); //1
        $action['read'] = array('2', '3', '6', '10', '14', '15'); //2
        $action['update'] = array('4', '5', '6', '7', '12', '13', '14', '15'); //4
        $action['delete'] = array('8', '9', '10', '11', '12', '13', '14', '15'); //8
        $action['all'] = array('15');

        if (in_array($value, $action[$wanted], true)) {
            return true;
        } else {
            return false;
        }
    }

    function hasPermission($permKey, $action = 'all') {

        $permKey = strtolower($permKey);

        if (array_key_exists($permKey, $this->perms)) {
            if ($this->actionPerm($this->perms[$permKey]['value'], $action)) {

                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
        /* OLD METHOD
          if ($this->perms[$permKey]['value'] === '1′ || $this->perms[$permKey]['value'] === true)
          {
          return true;
          } else {
          return false;
          }
          } else {
          return false;
          }
         */
    }

}

和这是url
样品ACL班codeigniter

推荐答案

我是来自Tastybytes布赖恩。我最好的解决办法是步了code点火ACL库是基于教程。它是从基础的PHP文件CI图书馆100%的直口。

I'm Brian from Tastybytes. My best solution would be to step through the tutorial that the code igniter ACL library is based on. It is a 100% straight port from the base php files to CI Library.

http://net.tutsplus.com/tutorials/php / A-更好登录系统/

和可能看网页的底部在哪里办理安装,什么不是。

And possibly look at the very bottom of the page where I go through "Installation" and what not.

这篇关于如何使用这个简单的ACL库到codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆