MoD
07-01-2007, 10:40 PM
Ok, users in my system can have ONE number in their permissions field.. Namely:
SUPER_ADMIN = 0;
ADMIN = 1;
MODERATOR = 2;
MENTOR = 3;
USER = 4;
GUEST = 5;
Easy ha? Well... Not exactly.. Heres the entire class
class user
{
// Rights constants
const SUPER_ADMIN = 0;
const ADMIN = 1;
const MODERATOR = 2;
const MENTOR = 3;
const USER = 4;
const GUEST = 5;
/**
* @access public
* Loads the user. Must be done in every script
* that needs user access!
*/
public function load()
{
}
/**
* Checks if the user is logged in...
* @access public
* @return bool
*/
public function logged_in()
{
if($_SESSION['auth'] === true && $_SESSION['user_id'] != '')
{
return true;
}
else
{
return false;
}
}
/**
* Checks if the user has a specific right
* @access public
* @param right - numerical
* @return true if the user has right, false otherwise
*/
public function is_right($value)
{
if($this->rights == $value)
{
return true;
}
else
{
return false;
}
}
/**
* Checks for specifics
* @access public
* @return bool
*/
public function is_sadmin()
{
if($this->rights == SUPER_ADMIN)
{
return true;
}
else
{
return false;
}
}
/**
* Checks for specifics
* @access public
* @return bool
*/
public function is_admin()
{
if($this->rights == ADMIN || $this->rights == SUPER_ADMIN)
{
return true;
}
else
{
return false;
}
}
/**
* Checks for specifics
* @access public
* @return bool
*/
public function is_mentor()
{
if($this->rights == MENTOR || $this->rights == ADMIN || $this->rights == SUPER_ADMIN)
{
return true;
}
else
{
return false;
}
}
/**
* Checks for specifics
* @access public
* @return bool
*/
public function is_user()
{
if($this->rights == USER || $this->rights == ADMIN || $this->rights == SUPER_ADMIN
|| $this->rights == MENTOR)
{
return true;
}
else
{
return false;
}
}
}
Dont bother about some unfinished functions. So, do you see the problem?
Logically, if the USER has access to X, then the admins and mods will have access to there too.. Right? Basically, the problem is, the lower you go in the functions that check the user rights ( is_user , is_mod) the more OR's there are... I dont like that.
Is there an easier method involving bitwise operations?
Thanks!
SUPER_ADMIN = 0;
ADMIN = 1;
MODERATOR = 2;
MENTOR = 3;
USER = 4;
GUEST = 5;
Easy ha? Well... Not exactly.. Heres the entire class
class user
{
// Rights constants
const SUPER_ADMIN = 0;
const ADMIN = 1;
const MODERATOR = 2;
const MENTOR = 3;
const USER = 4;
const GUEST = 5;
/**
* @access public
* Loads the user. Must be done in every script
* that needs user access!
*/
public function load()
{
}
/**
* Checks if the user is logged in...
* @access public
* @return bool
*/
public function logged_in()
{
if($_SESSION['auth'] === true && $_SESSION['user_id'] != '')
{
return true;
}
else
{
return false;
}
}
/**
* Checks if the user has a specific right
* @access public
* @param right - numerical
* @return true if the user has right, false otherwise
*/
public function is_right($value)
{
if($this->rights == $value)
{
return true;
}
else
{
return false;
}
}
/**
* Checks for specifics
* @access public
* @return bool
*/
public function is_sadmin()
{
if($this->rights == SUPER_ADMIN)
{
return true;
}
else
{
return false;
}
}
/**
* Checks for specifics
* @access public
* @return bool
*/
public function is_admin()
{
if($this->rights == ADMIN || $this->rights == SUPER_ADMIN)
{
return true;
}
else
{
return false;
}
}
/**
* Checks for specifics
* @access public
* @return bool
*/
public function is_mentor()
{
if($this->rights == MENTOR || $this->rights == ADMIN || $this->rights == SUPER_ADMIN)
{
return true;
}
else
{
return false;
}
}
/**
* Checks for specifics
* @access public
* @return bool
*/
public function is_user()
{
if($this->rights == USER || $this->rights == ADMIN || $this->rights == SUPER_ADMIN
|| $this->rights == MENTOR)
{
return true;
}
else
{
return false;
}
}
}
Dont bother about some unfinished functions. So, do you see the problem?
Logically, if the USER has access to X, then the admins and mods will have access to there too.. Right? Basically, the problem is, the lower you go in the functions that check the user rights ( is_user , is_mod) the more OR's there are... I dont like that.
Is there an easier method involving bitwise operations?
Thanks!