Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include "database.php";
- class User
- {
- private $userRoles = array();
- private static $db;
- //Alternatively use your own way of setting your Database connection.
- public static function setDatabase($db)
- {
- global $db;
- self::$db = $db;
- }
- //Populate the user object when it's created
- public function __construct($user_id)
- {
- $getUser = $this->db->prepare("SELECT * FROM admin WHERE id = :userid ");
- $getUser->execute(array(":userid" => $userid));
- if($getUser->rowCount() == 1)
- {
- $userData = $getUser->fetch(PDO::FETCH_ASSOC);
- $this->user_id = $user_id;
- $this->fnamename = ucfirst($userData['fname']);
- $this->email = $userData['email'];
- //etc.. More data if needed
- self::loadRoles();//Initiate the userroles
- }
- }
- //Fill the array with this user's roles, it's
- protected static function loadRoles()
- {
- $fetchRoles = $this->db->prepare("SELECT user_role.role_id, role.role_name FROM user_role JOIN role ON user_role.role_id = role.role_id WHERE user_role.user_id = :user_id");
- $fetchRoles->execute(array(":user_id" => $this->user_id));
- //Populate the array
- while($row = $fetchRoles->fetch(PDO::FETCH_ASSOC))
- {
- $this->userRoles[$row["role_name"]] = Role::getRolePermissions($row["role_id"]);
- }
- }
- //Check if the user has a certain permission
- public function hasPermission($permission)
- {
- //If the user has more roles, check them too
- foreach ($this->userRoles as $role)
- {
- //Do the actual checking
- if ($role->hasPermission($permission))
- {
- return true;
- }
- }
- return false;
- }
- }
- $userid = 1;
- $db = new Database($dbhost,$dbuser,$dbpass,$dbname);
- $test = new User(1);
- class Database
- {
- private $database = 'dbname';
- private $dbhost = 'localhost';
- private $dbuser = 'usr';
- private $dbpass = 'pw';
- public function __construct($dbhost,$dbuser,$dbpass,$dbname) {
- $this->database = $dbname;
- $this->dbhost = $dbhost;
- $this->dbuser = $dbuser;
- $this->dbpass = $dbpass;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement