forked from alekseykuleshov/rocket-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRole.php
137 lines (115 loc) · 3.23 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
<?php
namespace ATDev\RocketChat\Roles;
use ATDev\RocketChat\Common\Request;
use ATDev\RocketChat\Users\User;
/**
* Role class
*/
class Role extends Request
{
use \ATDev\RocketChat\Roles\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);
}
/**
* Assigns a role to an user
*
* @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);
}
/**
* 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;
}
}