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>
- */
- /**
- * Update single permission record - specific record will get role and level updated
- *
- * @param int Reference (Required), Permission record ID - id
- * @param string String (Required), Permission role - role
- * @param int Integer (Required), Permission level - level
- *
- * @return string JSON, result (HTTP result code), error (in case of failure)
- */
- header("Access-Control-Allow-Origin: *");
- header("Content-Type: application/json; charset=UTF-8");
- header("Access-Control-Allow-Methods: POST");
- header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
- require_once(__DIR__."/../db.php");
- require_once(__DIR__."/../util.php");
- require_once(__DIR__."/permission.php");
- require_once(__DIR__."/../session/session.php");
- require_once(__DIR__."/../auth/auth.php");
- // Session set up
- $auth = new Auth();
- $session = new Session();
- $data = json_decode(file_get_contents("php://input"));
- // Payload requirements
- $payloadError = Util::PayloadCheck($data, "id", "role", "level");
- if ($payloadError != false)
- {
- http_response_code(200);
- echo json_encode(array("result" => 500, "error" => $payloadError));
- exit();
- }
- // Connect to database, attach to session
- $db = new Database();
- $session->SetDB($db);
- // Require user authentication
- $auth_id = $session->GetUserID($auth);
- if ($auth_id != null)
- {
- // Check permission
- $permission = new Permission($db);
- $permissionCheck = $permission->Check($auth_id, "permission", Permission::WRITE);
- if ($permissionCheck == true)
- {
- // Permission check success - update record
- $permission->id = $data->id;
- $permission->role = $data->role;
- $permission->level = $data->level;
- $result = $permission->Update();
- if ($result != null)
- {
- // Successfully updated record
- http_response_code(200);
- echo json_encode(array("result" => 200));
- }
- else
- {
- // DB Error during record creation
- http_response_code(200);
- echo json_encode(array("result" => 500, "error" => $db->GetLastError()));
- }
- }
- else if ($permissionCheck === false)
- {
- // Permission check failure
- http_response_code(200);
- echo json_encode(array("result" => 401, "error" => "Unauthorized: Permission level too low."));
- }
- else
- {
- // DB Error during permission check
- http_response_code(200);
- echo json_encode(array("result" => 500, "error" => $db->GetLastError()));
- }
- }
- else
- {
- // User unauthorized
- http_response_code(200);
- echo json_encode(array("result" => 401, "error" => "Unauthorized."));
- }
- // Close database connection
- $db->Disconnect();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement