Tool to lookup computer information using a unique ID number.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.5 KiB

<?php
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
class Roles {
const ROLE_VIEWBYID = 1;
const ROLE_ADDEDIT = 2;
const ROLE_ADDHIST = 3;
const ROLE_VIEWBULK = 4;
public static function has($apikey, $roleid) {
global $database;
return $database->has('permissions', ["AND" => ['apikey' => $apikey, 'roleid' => $roleid]]) === true;
}
public static function add($apikey, $roleid) {
global $database;
if (!$database->has('apikeys', ['apikey' => $apikey])) {
throw new Exception("No such API key found.");
}
if (!$database->has('roles', ['roleid' => $roleid])) {
throw new Exception("No such role ID found.");
}
if (!$database->has('permissions', ["AND" => ['apikey' => $apikey, 'roleid' => $roleid]])) {
$database->insert('permissions', ['apikey' => $apikey, 'roleid' => $roleid]);
}
}
public static function remove($apikey, $roleid) {
global $database;
if (!$database->has('apikeys', ['apikey' => $apikey])) {
throw new Exception("No such API key found.");
}
if (!$database->has('roles', ['roleid' => $roleid])) {
throw new Exception("No such role ID found.");
}
$database->delete('permissions', ["AND" => ['apikey' => $apikey, 'roleid' => $roleid]]);
}
}