From bf9a6e801c0922a29513e5ee9bb24dd69ad5bf14 Mon Sep 17 00:00:00 2001 From: malle-pietje Date: Fri, 30 Mar 2018 13:57:59 +0200 Subject: [PATCH] added invite_admin() and revoke_admin() functions/methods --- README.md | 4 ++- src/Client.php | 78 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index db74825..a5a7fd6 100755 --- a/README.md +++ b/README.md @@ -31,6 +31,8 @@ The class currently supports the following functions/methods to get/post/put/del - disable_ap() - edit_usergroup() - extend_guest_validity() +- invite_admin() +- revoke_admin() - led_override() - list_admins() - list_all_admins() @@ -232,7 +234,7 @@ If you would like to contribute code (improvements), please open an issue and in This class is based on the work done by the following developers: - domwo: http://community.ubnt.com/t5/UniFi-Wireless/little-php-class-for-unifi-api/m-p/603051 - fbagnol: https://github.com/fbagnol/class.unifi.php -- and the API as published by Ubiquiti: https://dl.ubnt.com/unifi/5.6.18-8261dc5066/unifi_sh_api +- and the API as published by Ubiquiti: https://dl.ubnt.com/unifi/5.7.20/unifi_sh_api ## Important Disclaimer diff --git a/src/Client.php b/src/Client.php index ede22fc..94de51f 100755 --- a/src/Client.php +++ b/src/Client.php @@ -1041,6 +1041,76 @@ class Client return $this->process_response($response); } + /** + * Invite a new admin for access to the current site + * ------------------------------------------------- + * returns true on success + * required parameter = string, name to assign to the new admin user + * required parameter = email address to assign to the new admin user + * optional parameter = boolean, whether or not SSO will be allowed for the new admin + * default value is true which enables the SSO capability + * optional parameter = boolean, whether or not the new admin will have readonly + * permissions, default value is true which gives the new admin + * administrator permissions + * optional parameter = boolean, whether or not the new admin will have permissions to + * adopt devices, default value is false. Only applies when readonly + * is true. + * optional parameter = boolean, whether or not the new admin will have permissions to + * restart devices, default value is false. Only applies when readonly + * is true. + * + * NOTES: + * after issuing a valid request, an invite will be sent to the email address provided + */ + public function invite_admin($name, $email, $enable_sso = true, $readonly = false, $device_adopt = false, $device_restart = false) + { + if (!$this->is_loggedin) return false; + $email_valid = filter_var(trim($email), FILTER_VALIDATE_EMAIL); + if (!$email_valid) { + trigger_error('The email address provided is invalid!'); + return false; + } + + $json = ['name' => trim($name), 'email' => trim($email), 'for_sso' => $enable_sso, 'cmd' => 'invite-admin']; + if ($readonly) { + $json['role'] = 'readonly'; + $permissions = []; + if ($device_adopt) { + $permissions[] = "API_DEVICE_ADOPT"; + } + + if ($device_restart) { + $permissions[] = "API_DEVICE_RESTART"; + } + + if (count($permissions) > 0) { + $json['permissions'] = $permissions; + } + } + + $json = json_encode($json); + $response = $this->exec_curl('/api/s/'.$this->site.'/cmd/sitemgr', 'json='.$json); + return $this->process_response_boolean($response); + } + + /** + * Revoke an admin + * --------------- + * returns true on success + * required parameter = id of the admin to revoke which can be obtained using the + * list_all_admins() method/function + * + * NOTES: + * only non-superadmins account can be revoked + */ + public function revoke_admin($admin_id) + { + if (!$this->is_loggedin) return false; + $json = json_encode(['admin' => $admin_id, 'cmd' => 'revoke-admin']); + $response = $this->exec_curl('/api/s/'.$this->site.'/cmd/sitemgr', 'json='.$json); + return $this->process_response_boolean($response); + } + /** * List wlan_groups * ---------------- @@ -1245,6 +1315,10 @@ class Client * List country codes * ------------------ * returns an array of available country codes + * + * NOTES: + * these codes following the ISO standard: + * https://en.wikipedia.org/wiki/ISO_3166-1_numeric */ public function list_country_codes() { @@ -1278,8 +1352,8 @@ class Client } /** - * List port configuration - * ----------------------- + * List port configurations + * ------------------------ * returns an array of port configurations */ public function list_portconf()