<?php

require_once '../../core/api_bootstrap.php';
require_once '../../core/employee_auth.php';

EmployeeAuth::check('manage_roles');

$data = Request::get();

try {

Validator::required($data,['role_id']);

$db = new Database();
$conn = $db->connect();

/* get all permissions */

$permissions = $conn->query("

SELECT id,permission_key,description
FROM permissions
ORDER BY permission_key

")->fetchAll();

/* get assigned permissions */

$stmt = $conn->prepare("

SELECT permission_id
FROM role_permissions
WHERE role_id=:role

");

$stmt->execute([
':role'=>$data['role_id']
]);

$assigned = $stmt->fetchAll(PDO::FETCH_COLUMN);

$result = [];

foreach($permissions as $p){

$result[] = [

'id'=>$p['id'],
'permission_key'=>$p['permission_key'],
'description'=>$p['description'],
'assigned'=>in_array($p['id'],$assigned)

];

}

Response::success($result);

}
catch(Exception $e){

Logger::log("system.log",$e->getMessage());

Response::error("Server error",500);

}
