Created
April 11, 2012 15:08
-
-
Save dazld/2359922 to your computer and use it in GitHub Desktop.
Codeigniter (very basic) ACL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
*/ | |
class Acl extends CI_Model | |
{ | |
protected $actions = array(); | |
function __construct() { | |
parent::__construct(); | |
// define public-facing actions | |
$this->add_action('build_archive',array(1,2)); | |
$this->add_action('edit_users',array(1)); | |
$this->add_action('edit_devices',array(1,2)); | |
$this->add_action('edit_title',array(1,2)); | |
$this->add_action('edit_messages', array(1,2)); | |
$this->add_action('update_order',array(1,2)); | |
$this->add_action('add_issue',array(1,2,3)); | |
$this->add_action('delete_issue',array(1,2)); | |
$this->add_action('system_menu',array(1,2)); | |
} | |
public function can_do($action, $user_id = false){ | |
if ($user_id == false) { | |
$user = $this->session->userdata('auth'); | |
} else { | |
$user = $this->user->get_by_id($user_id); | |
} | |
if (!is_object($user)) { | |
return false; | |
} | |
$user_id = $user->id; | |
$type = $user->type; | |
if (in_array($action, $this->get_type_roles($type))) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function get_type_roles($type){ | |
$roles = array(); | |
switch ($type) { | |
case '1': | |
$roles = $this->get_roles(1); | |
break; | |
case '2': | |
$roles = $this->get_roles(2); | |
break; | |
case '3': | |
$roles = $this->get_roles(3); | |
break; | |
} | |
return $roles; | |
} | |
public function get_roles($id){ | |
$roles = array(); | |
foreach ($this->actions as $k => $v) { | |
if (in_array($id, $v)) { | |
$roles[] = $k; | |
} | |
} | |
return $roles; | |
} | |
// privates | |
private function add_action($name = false,$roles = false){ | |
$this->actions[$name] = $roles; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment