Skip to content

Commit

Permalink
added User class (fix: #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Sep 8, 2018
1 parent fdb04a9 commit 05edaf9
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 79 deletions.
100 changes: 35 additions & 65 deletions src/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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
*
Expand Down
17 changes: 3 additions & 14 deletions src/RestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
136 changes: 136 additions & 0 deletions src/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* Class User | src/User.php
*
* A single User with his available actions
*
* @package SinusBot
* @author Max Schmitt <max@schmitt.mx>
*/

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');
}
}
1 change: 1 addition & 0 deletions src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
include_once("RestClient.php");
include_once("Instance.php");
include_once("Playlist.php");
include_once("User.php");
include_once("API.php");

0 comments on commit 05edaf9

Please sign in to comment.