-
Notifications
You must be signed in to change notification settings - Fork 16
/
Role.php
194 lines (163 loc) · 4.42 KB
/
Role.php
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace ATDev\RocketChat\Roles;
use ATDev\RocketChat\Common\Request;
use ATDev\RocketChat\Users\User;
/**
* Role class
*/
class Role extends Request
{
use Data;
/**
* Gets role listing
*
* @return Collection|bool
*/
public static function listing()
{
static::send("roles.list", "GET");
if (!static::getSuccess()) {
return false;
}
$roles = new Collection();
foreach (static::getResponse()->roles as $role) {
$roles->add(static::createOutOfResponse($role));
}
return $roles;
}
/**
* Gets all the roles in the system which are updated after a given date
*
* @param string $updatedSince
* @return array|false
*/
public static function sync($updatedSince)
{
static::send("roles.sync", "GET", ["updatedSince" => $updatedSince]);
if (!static::getSuccess()) {
return false;
}
$roles = static::getResponse()->roles;
$result = ["update" => new Collection(), "remove" => new Collection()];
foreach ($roles->update as $role) {
$result['update']->add(static::createOutOfResponse($role));
}
foreach ($roles->remove as $role) {
$result['remove']->add(static::createOutOfResponse($role));
}
return $result;
}
/**
* Creates a new role in the system
*
* @return Role|false
*/
public function create()
{
static::send("roles.create", "POST", $this);
if (!static::getSuccess()) {
return false;
}
return $this->updateOutOfResponse(static::getResponse()->role);
}
/**
* Delete a role in the system
*
* @return Role|false
*/
public function delete()
{
static::send("roles.delete", "POST", ['roleId' => $this->getRoleId()]);
if (!static::getSuccess()) {
return false;
}
return $this->setRoleId(null);
}
/**
* Updates a role
*
* @return Role|false
*/
public function update()
{
static::send("roles.update", "POST", $this);
if (!static::getSuccess()) {
return false;
}
return $this->updateOutOfResponse(static::getResponse()->role);
}
/**
* Add a user to a role
*
* @param string $username
* @param string $roomId
* @return Role|false
*/
public function addUserToRole($username, $roomId = '')
{
$data = [
'roleName' => $this->name,
'username' => $username
];
if (!empty($roomId)) {
$data["roomId"] = $roomId;
}
static::send("roles.addUserToRole", "POST", $data);
if (!static::getSuccess()) {
return false;
}
return $this->updateOutOfResponse(static::getResponse()->role);
}
/**
* Remove a user from a role
*
* @param string $username
* @param string $roomId
* @return Role|false
*/
public function removeUserFromRole($username, $roomId = '')
{
$data = [
'roleName' => $this->name,
'username' => $username
];
if (!empty($roomId)) {
$data["roomId"] = $roomId;
}
static::send("roles.removeUserFromRole", "POST", $data);
if (!static::getSuccess()) {
return false;
}
return $this->updateOutOfResponse(static::getResponse()->role);
}
/**
* Gets the users that belongs to a role
*
* @param int $offset
* @param int $count
* @param string $roomId
* @return \ATDev\RocketChat\Users\Collection|false
*/
public function getUsersInRole($offset = 0, $count = 0, $roomId = '')
{
static::send(
"roles.getUsersInRole",
"GET",
["offset" => $offset, "count" => $count, "role" => $this->getName(), "roomId" => $roomId]
);
if (!static::getSuccess()) {
return false;
}
$users = new \ATDev\RocketChat\Users\Collection();
$response = static::getResponse();
if (isset($response->users)) {
foreach ($response->users as $user) {
$users->add(User::createOutOfResponse($user));
}
}
if (isset($response->total)) {
$users->setTotal($response->total);
}
return $users;
}
}