如何在 codeigniter 中使用这个简单的 acl 库

问题描述 投票:0回答:2

我以前用过cakephp,现在使用codeigniter,但不幸的是没有任何身份验证或ACL内置库..经过更多搜索后我找到了一个很好的库,但我不知道如何使用它..这不是例子使用它..任何人都创建了控制器和模型作为示例...感谢您的帮助

<?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;
          }
         */
    }

}

这是网址 codeigniter 的示例 ACL 类

php authentication codeigniter authorization codeigniter-2
2个回答
1
投票

这个很简单

首先将其加载到控制器中

$this->load->library('acl');

现在调用它的方法

$this->acl->buildACL();

编辑 使用它们进行菜单分配

$this->acl->perms = array('your values');
$this->acl->userID= 'user id';
$this->acl->userRoles = array('your values');

请注意,您应该有数据库表 userRoles,它将在库初始化时调用 getUserRoles 方法,并且 $userRoles 参数将具有值。


1
投票

我是来自 Tastybytes 的布莱恩。 我最好的解决方案是逐步完成代码点火器 ACL 库所基于的教程。 它是从基本 php 文件到 CI 库的 100% 直接端口。

http://net.tutsplus.com/tutorials/php/a-better-login-system/

并且可能会查看页面最底部的“安装”以及其他内容。

© www.soinside.com 2019 - 2024. All rights reserved.