diff --git a/src/API.php b/src/API.php index 97ed7c7..642a8fd 100644 --- a/src/API.php +++ b/src/API.php @@ -293,11 +293,16 @@ public function uploadTrack($path) /** * getUsers * - * @return array users + * @return User[] users */ public function getUsers() { - return $this->request('/bot/users'); + $users = $this->request('/bot/users'); + $out = []; + foreach ($users as $user) { + array_push($out, new User($this->token, $this->url, $this->timeout, $user)); + } + return $out; } /** @@ -306,88 +311,53 @@ public function getUsers() * @param string $username Username * @param string $password Password * @param integer $privileges Bitmask-Value - * @return array status + * @return User user object */ public function addUser($username, $password, $privileges = 0) { - return $this->request('/bot/users', 'POST', [ + $this->request('/bot/users', 'POST', [ 'username'=>$username, 'password'=>$password, 'privileges'=>$privileges, ]); + $users = $this->getUsers(); + foreach ($users as $user) { + if ($user->getName() === $username) { + return $user; + } + } } - - /** - * setUserPassword - * - * @param string $password Password - * @param string $userUUID user uuid - * @return array status - */ - public function setUserPassword($password, $userUUID) - { - return $this->request('/bot/users/'.$userUUID, 'PATCH', [ - 'password'=>$password, - ]); - } - - -/** - * setUserPrivileges - * - * @param integer $privileges Bitmask-Value - * @param string $userUUID user UUID - * @return array status - */ - public function setUserPrivileges($privileges, $userUUID) - { - return $this->request('/bot/users/'.$userUUID, 'PATCH', [ - 'privileges'=>$privileges, - ]); - } - - /** - * setUserIdentity - * - * @param string $identity teamspeak identity - * @param string $userUUID SinusBot user UUID - * @return array status - */ - public function setUserIdentity($identity, $userUUID) - { - return $this->request('/bot/users/'.$userUUID, 'PATCH', [ - 'tsuid'=>$identity, - ]); - } - - /** - * setUserServergroup + * getUserByUUID * - * @param string $groupID TeamSpeak Group ID - * @param string $userUUID SinusBot User UUID - * @return array status + * @param string $uuid User ID + * @return User user object */ - public function setUserServergroup($groupID, $userUUID) + public function getUserByUUID($uuid) { - return $this->request('/bot/users/'.$userUUID, 'PATCH', [ - 'tsgid'=>$groupID, - ]); + $users = $this->getUsers(); + foreach ($users as $user) { + if ($user->getUUID() === $uuid) { + return $user; + } + } } - - /** - * deleteUser + * getUserByName * - * @param string $userUUID SinusBot User UUID - * @return array status + * @param string $username Username + * @return User user object */ - public function deleteUser($userUUID) + public function getUserByName($username) { - return $this->request('/bot/users/'.$userUUID, 'DELETE'); + $users = $this->getUsers(); + foreach ($users as $user) { + if ($user->getName() === $username) { + return $user; + } + } } - /** * getInstances * diff --git a/src/RestClient.php b/src/RestClient.php index 44b8174..488aa01 100644 --- a/src/RestClient.php +++ b/src/RestClient.php @@ -65,20 +65,9 @@ protected function request($path, $method = "GET", $payload = null, $encoded = f } } $data = curl_exec($ch); - - if ($data === false) { - $data = [ - 'success' => false, - 'error' => curl_error($ch) - ]; - } else { - $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - if ($httpcode != 200 and $httpcode != 201) { - $data = [ - 'success' => false, - 'error' => $this->getError($httpcode) - ]; - } + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + if ($httpcode != 200 && $httpcode != 201) { + throw new \Exception('Not expected http status code: '.$httpcode." (".$this->getError($httpcode).")"); } curl_close($ch); diff --git a/src/User.php b/src/User.php new file mode 100644 index 0000000..a05bc36 --- /dev/null +++ b/src/User.php @@ -0,0 +1,136 @@ + + */ + +namespace SinusBot; + +/** + * User Instance + * + * User represents a single User of the SinusBot + */ +class User extends RestClient +{ + /** + * UUID holds the User UUID + * @var array + */ + public $uuid = null; + /** + * User stores the initial received user data + * @var array + */ + private $user = null; + /** + * __construct + * + * @param string $token SinusBot auth token + * @param string $url SinusBot Bot URL + * @param int $timeout HTTP Timeout which is used to perform HTTP API requests + * @param array $user SinusBot User array. + * @return void + */ + public function __construct($token, $url, $timeout, $user) + { + $this->token = $token; + $this->url = $url; + $this->timeout = $timeout; + $this->uuid = $user['id']; + $this->user = $user; + } + /** + * getName returns the username + * + * @return string username + * @api + */ + public function getName() + { + return array_key_exists('username', $this->user)?$this->user['username']:''; + } + /** + * getUUID returns the uuid + * + * @return string user UUID + * @api + */ + public function getUUID() + { + return $this->uuid; + } + + /** + * setPassword + * + * @param string $password Password + * @return array status + * @api + */ + public function setPassword($password) + { + return $this->request('/bot/users/'.$this->uuid, 'PATCH', [ + 'password'=>$password, + ]); + } + + /** + * setPrivileges + * + * @param integer $privileges Bitmask-Value + * @return array status + * @api + */ + public function setPrivileges($privileges) + { + return $this->request('/bot/users/'.$this->uuid, 'PATCH', [ + 'privileges'=>$privileges, + ]); + } + + /** + * setIdentity + * + * @param string $identity teamspeak identity + * @return array status + * @api + */ + public function setIdentity($identity) + { + return $this->request('/bot/users/'.$this->uuid, 'PATCH', [ + 'tsuid'=>$identity, + ]); + } + + + /** + * setServergroup + * + * @param string $groupID TeamSpeak Group ID + * @return array status + * @api + */ + public function setServergroup($groupID) + { + return $this->request('/bot/users/'.$this->uuid, 'PATCH', [ + 'tsgid'=>strval($groupID), + ]); + } + + + /** + * delete + * + * @return array status + * @api + */ + public function delete() + { + return $this->request('/bot/users/'.$this->uuid, 'DELETE'); + } +} diff --git a/src/autoload.php b/src/autoload.php index 04f7d2f..b1dbab5 100644 --- a/src/autoload.php +++ b/src/autoload.php @@ -11,4 +11,5 @@ include_once("RestClient.php"); include_once("Instance.php"); include_once("Playlist.php"); +include_once("User.php"); include_once("API.php");