Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- This file is subject to the terms and conditions defined in
- file 'LICENSE', which is part of this source code package.
- © 2022 OtteIT s.r.o.
- All Rights Reserved.
- Author: Vilem Otte <dev@otte.cz>
- */
- require_once(__DIR__."/../db.php");
- /**
- * Permission class
- *
- * Functionality for permission creating, deleting, reading and updating
- */
- class Permission
- {
- /**
- * Database class instance
- */
- private $conn;
- /**
- * Permission ID
- */
- public $id;
- /**
- * User's ID (for whom the permission record is)
- */
- public $user_id;
- /**
- * User's role
- */
- public $role;
- /**
- * User's permission level
- */
- public $level;
- /**
- * READ permission (allows reading records)
- */
- public const READ = 1;
- /**
- * WRITE_SELF permission (allows reading and updating own records)
- */
- public const WRITE_SELF = 2;
- /**
- * WRITE permission (allows reading and writing records)
- */
- public const WRITE = 4;
- /**
- * Constructor
- *
- * @param Database Database connection instance - dbconn
- */
- public function __construct($dbconn)
- {
- $this->conn = $dbconn;
- $this->id = null;
- $this->user_id = null;
- $this->role = null;
- $this->level = null;
- }
- /**
- * Creates permission record
- *
- * Creates single permission record for specific user, with specific role and level. If such record exists, error is raised
- *
- * @return mixed Permission record ID on success, null on failure
- */
- public function Create()
- {
- $queryCheck = "SELECT * FROM permission WHERE user_id = '" . $this->conn->EscapeString($this->user_id) . "' AND role = '" . $this->conn->EscapeString($this->role) . "'";
- $resultCheck = $this->conn->Query($queryCheck);
- if ($resultCheck != false)
- {
- if ($this->conn->GetRowsCount($resultCheck) > 0)
- {
- $this->conn->SetError("Permission record for user ID '" . $this->conn->EscapeString($this->user_id) . "' and role '" . $this->conn->EscapeString($this->role) . "' already exists!");
- return null;
- }
- }
- $query = "INSERT INTO permission (user_id, role, level) VALUES('" . $this->conn->EscapeString($this->user_id) . "', '" . $this->conn->EscapeString($this->role) . "', '" . $this->conn->EscapeString($this->level) . "')";
- $result = $this->conn->Query($query);
- if ($result == false)
- {
- return null;
- }
- else
- {
- $this->id = $this->conn->GetLastInsertID();
- return $this->id;
- }
- }
- /**
- * Reads permission records
- *
- * Reads permission records, optionally for specific user and/or role
- *
- * @return mixed Null on failure, array of objects on success
- */
- public function Read()
- {
- $query = "SELECT * FROM permission WHERE 1 = 1 ";
- if ($this->user_id != null)
- {
- $query .= " AND user_id = '" . $this->conn->EscapeString($this->user_id) . "'";
- }
- if ($this->role != null)
- {
- $query .= " AND role = '" . $this->conn->EscapeString($this->role) . "'";
- }
- $result = $this->conn->Query($query);
- if ($result == false)
- {
- return null;
- }
- else
- {
- $numRows = $this->conn->GetRowsCount($result);
- if ($numRows > 0)
- {
- $output = [];
- for ($i = 0; $i < $numRows; $i++)
- {
- $row = $this->conn->GetRow($result, $i);
- $output[$i] = (object)array();
- $output[$i]->id = $row['id'];
- $output[$i]->user_id = $row['user_id'];
- $output[$i]->role = $row['role'];
- $output[$i]->level = $row['level'];
- }
- return $output;
- }
- else
- {
- $output = [];
- $output[0] = (object)array();
- $output[0]->id = $row['id'];
- $output[0]->user_id = $row['user_id'];
- $output[0]->role = $row['role'];
- $output[0]->level = Permission::READ;
- return $output;
- }
- }
- }
- /**
- * Check permission
- *
- * Checks permission for specific user with specific role at required level
- *
- * @param int $user_id User ID for which we check permission
- * @param string $role Permission role to check for user
- * @param int $level Permission level that is required
- *
- * @return mixed Null on query failure, false when permission is not granted, true when granted
- */
- public function Check($user_id, $role, $level)
- {
- $query = "SELECT * FROM permission WHERE user_id = '" . $this->conn->EscapeString($user_id) . "' AND role = '" . $this->conn->EscapeString($role) . "'";
- $result = $this->conn->Query($query);
- if ($result == false)
- {
- return null;
- }
- else
- {
- $numRows = $this->conn->GetRowsCount($result);
- for ($i = 0; $i < $numRows; $i++)
- {
- $row = $this->conn->GetRow($result, $i);
- if ((intval($level) & intval($row['level'])) > 0)
- {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * Update permission record
- *
- * Updates single permission record, sets permission role and permission level for given record
- *
- * @return mixed Null on failure, true on success
- */
- public function Update()
- {
- $query = "UPDATE permission SET
- role = '" . $this->conn->EscapeString($this->role) . "',
- level = '" . $this->conn->EscapeString($this->level) . "' ";
- $query .= "WHERE id = '" . $this->conn->EscapeString($this->id) . "'";
- $result = $this->conn->Query($query);
- if ($result == false)
- {
- return null;
- }
- else
- {
- return true;
- }
- }
- /**
- * Delete permission record
- *
- * Removes permission record from database
- *
- * @return mixed Null on failure, true on success
- */
- public function Delete()
- {
- $query = "DELETE FROM permission WHERE id = '" . $this->conn->EscapeString($this->id) . "'";
- $result = $this->conn->Query($query);
- if ($result == false)
- {
- return null;
- }
- else
- {
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement