-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUser.php
More file actions
155 lines (134 loc) · 3.92 KB
/
User.php
File metadata and controls
155 lines (134 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
namespace Gotify\Endpoint;
use Gotify\Json;
use stdClass;
/**
* Class for interacting with the user endpoint
*
* @see https://gotify.net/api-docs#/user API docs for the user endpoint
*/
class User extends AbstractEndpoint
{
/** @var string $endpoint API endpoint */
private string $endpoint = 'user';
/**
* Get current user
*
* @return stdClass
*
* @see https://gotify.net/api-docs#/user/currentUser API docs for getting the current user
*/
public function getCurrent(): stdClass
{
$response = $this->guzzle->get('current/user');
$current = Json::decode($response->getBody()->getContents());
return (object) $current;
}
/**
* Update password for the current user
*
* @param string $password New password
*
* @return boolean
*
* @see https://gotify.net/api-docs#/user/updateCurrentUser API docs for updating the current user's password
*/
public function updatePassword(string $password): bool
{
$data = [
'pass' => $password,
];
$response = $this->guzzle->post('current/user/password', $data);
$body = $response->getBody()->getContents();
return $response->getStatusCode() === 200 ? true : false;
}
/**
* Get user
*
* @param int $id User Id
*
* @return stdClass
*
* @see https://gotify.net/api-docs#/user/getUser API docs for getting a user
*/
public function getUser(int $id): stdClass
{
$response = $this->guzzle->get($this->endpoint . '/' . $id);
$user = Json::decode($response->getBody()->getContents());
return (object) $user;
}
/**
* Get all users
*
* @return stdClass
*
* @see https://gotify.net/api-docs#/user/getUsers API docs for getting all users
*/
public function getAll(): stdClass
{
$response = $this->guzzle->get($this->endpoint);
$users = Json::decode($response->getBody()->getContents());
return (object) ['users' => $users];
}
/**
* Create a user
*
* @param string $name Username
* @param string $password Password
* @param boolean $admin Admin status
*
* @return stdClass
*
* @see https://gotify.net/api-docs#/user/createUser API docs for creating a user
*/
public function create(string $name, string $password, bool $admin = false): stdClass
{
$data = [
'name' => $name,
'pass' => $password,
'admin' => $admin
];
$response = $this->guzzle->post($this->endpoint, $data);
$user = Json::decode($response->getBody()->getContents());
return (object) $user;
}
/**
* Update a user
*
* @param int $id User Id
* @param string $name New username
* @param string $password New password (leave if no change)
* @param boolean $admin Admin status
*
* @return stdClass
*
* @see https://gotify.net/api-docs#/user/updateUser API docs for updating a user
*/
public function update(int $id, string $name, string $password = '', bool $admin = false): stdClass
{
$data = [
'name' => $name,
'pass' => $password,
'admin' => $admin
];
$response = $this->guzzle->post($this->endpoint . '/' . $id, $data);
$user = Json::decode($response->getBody()->getContents());
return (object) $user;
}
/**
* Delete a user
*
* @param int $id User Id
*
* @return boolean
*
* @see https://gotify.net/api-docs#/user/deleteUser API docs for deleting a user
*/
public function delete(int $id): bool
{
$response = $this->guzzle->delete($this->endpoint . '/' . $id);
$body = $response->getBody()->getContents();
return $response->getStatusCode() === 200 ? true : false;
}
}