From d918af5d653498a080f8f02c4fba237900daaf13 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Fri, 26 May 2017 00:05:59 -0600 Subject: [PATCH] Add account_has_permission function --- lib/login.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/login.php b/lib/login.php index 88c5313..aeeead2 100644 --- a/lib/login.php +++ b/lib/login.php @@ -157,6 +157,37 @@ function get_account_status($username) { } } +/** + * Check if the given username has the given permission (or admin access) + * @param string $username + * @param string $permcode + * @return boolean TRUE if the user has the permission (or admin access), else FALSE + */ +function account_has_permission($username, $permcode) { + $client = new GuzzleHttp\Client(); + + $response = $client + ->request('POST', PORTAL_API, [ + 'form_params' => [ + 'key' => PORTAL_KEY, + 'action' => "permission", + 'username' => $username, + 'code' => $permcode + ] + ]); + + if ($response->getStatusCode() > 299) { + sendError("Login server error: " . $response->getBody()); + } + + $resp = json_decode($response->getBody(), TRUE); + if ($resp['status'] == "OK") { + return $resp['has_permission']; + } else { + return false; + } +} + //////////////////////////////////////////////////////////////////////////////// // Login handling // ////////////////////////////////////////////////////////////////////////////////