Guest User

Untitled

a guest
Apr 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. <?php
  2. $config = include '../config.file.php';
  3.  
  4. try {
  5. $db = new PDO('mysql:host='.$config['dbhost'].';dbname='.$config['dbname'].';charset=utf8mb4', $config['dbusername'], $config['dbpass']);
  6. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  7. $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  8. } catch(PDOException $ex) {
  9. echo $ex->getMessage();
  10. }
  11.  
  12. <?php
  13. session_start();
  14.  
  15. // Starts the Session Expire Checker
  16. require 'SessionExpireChecker.php';
  17.  
  18. // Checks whether user is a valid user
  19. if (isset($_SESSION['valid_user'])) {
  20.  
  21. require_once './classes/Role.php';
  22. require_once './classes/EntitledUser.php';
  23. require_once '../DBConnect.php';
  24.  
  25. $u = EntitledUser::getByUsername($db, $_SESSION['valid_user']);
  26.  
  27. if ($u->isEntitled('can_view_testme')) {
  28. echo "You have permission to read.<br />";
  29. }
  30.  
  31. if ($u->isEntitled('can_edit_testme')) {
  32. echo "You have permission to edit/update.<br />";
  33. }
  34.  
  35. if ($u->isEntitled('can_delete_testme')) {
  36. echo "You have permission to delete.<br />";
  37. }
  38.  
  39. if ($u->isEntitled('can_create_testme')) {
  40. echo "You have permission to create/add.<br />";
  41. }
  42.  
  43. }
  44.  
  45. <?php
  46.  
  47. class EntitledUser
  48. {
  49. private $db;
  50. private $roles;
  51.  
  52. public function __construct(PDO $db) {
  53. $this->db = $db;
  54. }
  55.  
  56. public static function getByUsername(PDO $db, $username) {
  57. try {
  58. $stmt = $db->prepare("SELECT authorized_users.USERname FROM authorized_users WHERE authorized_users.USERname = :usernamevar");
  59. $stmt->bindValue(':usernamevar', $username, PDO::PARAM_STR);
  60. $stmt->execute();
  61. } catch(PDOException $ex) {
  62. echo $ex->getMessage();
  63. }
  64. $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
  65.  
  66. if (!empty($result)) {
  67. $entUser = new EntitledUser($db);
  68. $entUser->user_id = $result[0]['USERname'];
  69. $entUser->initializeRoles();
  70. return $entUser;
  71. } else {
  72. return false;
  73. }
  74. }
  75.  
  76. protected function initializeRoles() {
  77. $this->roles = array();
  78.  
  79. try {
  80. $stmt2 = $this->db->prepare("SELECT rbac_user_roles.USERname, rbac_user_roles.role_id, rbac_roles.role_id, rbac_roles.role_name
  81. FROM rbac_user_roles, rbac_roles
  82. WHERE rbac_user_roles.role_id = rbac_roles.role_id
  83. AND rbac_user_roles.USERname = :user_idvar");
  84.  
  85. $stmt2->bindValue(':user_idvar', $this->user_id, PDO::PARAM_STR);
  86. $stmt2->execute();
  87. } catch(PDOException $ex) {
  88. echo $ex->getMessage();
  89. }
  90.  
  91. while($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
  92. $this->roles[$row['role_name']] = Role::getRolePermissions($this->db, $row['role_id']);
  93. }
  94. }
  95.  
  96. // check if user has a specific entitlement
  97. public function isEntitled($permission) {
  98. foreach ($this->roles as $role) {
  99. if ($role->hasPermission($permission)) {
  100. return true;
  101. }
  102. }
  103. return false;
  104. }
  105.  
  106. <?php
  107.  
  108. class Role
  109. {
  110. protected $permissions;
  111.  
  112. protected function __construct() {
  113. $this->permissions = array();
  114. }
  115.  
  116. // return a role object with permissions
  117. public static function getRolePermissions(PDO $db, $role_id) {
  118. $role = new Role();
  119.  
  120. try {
  121. $stmt3 = $db->prepare("SELECT rbac_role_permissions.role_id, rbac_role_permissions.permission_id, rbac_permissions.permission_id, rbac_permissions.permission_description
  122. FROM rbac_role_permissions, rbac_permissions
  123. WHERE rbac_role_permissions.permission_id = rbac_permissions.permission_id
  124. AND rbac_role_permissions.role_id = :role_idvar");
  125.  
  126. $stmt3->bindValue(':role_idvar', $role_id, PDO::PARAM_STR);
  127. $stmt3->execute();
  128. } catch(PDOException $ex) {
  129. echo $ex->getMessage();
  130. }
  131.  
  132.  
  133. while($row = $stmt3->fetch(PDO::FETCH_ASSOC)) {
  134. $role->permissions[$row['permission_description']] = true;
  135. }
  136. return $role;
  137. }
  138.  
  139. // check if a permission is set
  140. public function hasPermission($permission) {
  141. return isset($this->permissions[$permission]);
  142. }
  143. }
Add Comment
Please, Sign In to add comment