1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140:
<?php
class RolesAuthorizations
{
private $moderators = false;
private $members = false;
private $guests = false;
private $groups = array();
private $users = array();
public function __construct(array $auth_array = array())
{
$this->build_from_auth_array($auth_array);
}
public function build_auth_array()
{
$auth_array = array();
$this->fill_levels_auths($auth_array);
$this->fill_groups_auths($auth_array);
$this->fill_users_auths($auth_array);
return $auth_array;
}
private function fill_levels_auths(array & $auth_array)
{
if ($this->moderators)
{
$auth_array['r1'] = 1;
if ($this->members)
{
$auth_array['r0'] = 1;
if ($this->guests)
{
$auth_array['r-1'] = 1;
}
}
}
}
private function fill_groups_auths(array & $auth_array)
{
foreach ($this->groups as $group_id)
{
$auth_array[$group_id] = 1;
}
}
private function fill_users_auths(array & $auth_array)
{
foreach ($this->users as $user_id)
{
$auth_array['m' . $user_id] = 1;
}
}
private function init()
{
$this->moderators = false;
$this->members = false;
$this->guests = false;
$this->groups = array();
$this->users = array();
}
public function build_from_auth_array(array $auth_array)
{
$this->init();
$this->read_levels_auths($auth_array);
$this->read_groups_auths($auth_array);
$this->read_users_auths($auth_array);
}
private function read_levels_auths(array $auth_array)
{
if (!empty($auth_array['r1']))
{
$this->moderators = true;
if (!empty($auth_array['r0']))
{
$this->members = true;
if (!empty($auth_array['r-1']))
{
$this->guests = true;
}
}
}
}
private function read_groups_auths(array $auth_array)
{
foreach ($auth_array as $role => $auth)
{
if ($auth)
{
if (is_numeric($role))
{
$this->groups[] = $role;
}
}
}
}
private function read_users_auths(array $auth_array)
{
foreach ($auth_array as $role => $auth)
{
if ($auth)
{
if ($role[0] == 'm')
{
$this->users[] = (int)TextHelper::substr($role, 1);
}
}
}
}
}
?>