-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserManagementAPI.php
162 lines (141 loc) · 4.8 KB
/
UserManagementAPI.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
<?php
namespace UWMadison\UserManagementAPI;
use ExternalModules\AbstractExternalModule;
use Logging;
class UserManagementAPI extends AbstractExternalModule
{
private $requesting_user;
private $default_page_size = 100;
private $action_list = ["add", "remove", "suspend", "reset", "modify", "read", "dump"];
private $term_map = [
"id" => "ui_id", // no changes
"user" => "username", // no changes
"first_name" => "user_firstname",
"last_name" => "user_lastname",
"email" => "user_email", // email
// Optional
"email2" => "user_email2", // email
"email3" => "user_email3", // email
"phone" => "user_phone", // phone
"mobile" => "user_phone_sms", // phone
"institution_id" => "user_inst_id",
"sponsor" => "user_sponsor", // username
"expiration" => "user_expiration", // datetime
"comments" => "user_comments",
"display_email" => "display_on_email_users", // boolean
"allow_creation" => "allow_create_db", // boolean
// Times
"creation" => "user_creation", // no changes
"activity" => "user_lastactivity", // no changes
"login" => "user_lastlogin", // no changes
"suspended" => "user_suspended_time", // no changes
// Admin
"super_user" => "super_user", // no changes
// "admin" flag if any admin setting is on
];
public function process()
{
$result = [
"status" => "failure",
"message" => "Invalid token",
];
$token = $this->sanitizeAPIToken($_POST["token"]);
$action = lower($_POST["action"]);
$user = $_POST["user"];
$payload = $_POST;
unset($payload["token"]);
unset($payload["action"]);
unset($payload["user"]);
if (strlen($token) !== 64) {
return $result;
}
$q = $this->query("
SELECT username, super_user
FROM redcap_user_information
WHERE api_token = ?
AND user_suspended_time IS NULL
LIMIT 1", $token);
if (!($q && $q !== false && db_num_rows($q) == 1)) {
return $result;
}
$this->requesting_user = db_fetch_assoc($q)["username"];
if (empty($action) || (empty($user) && $action !== "dump")) {
$result["message"] = "Missing user or action";
return $result;
}
if (!in_array($action, $this->action_list)) {
$result["message"] = "Invalid action";
return $result;
}
return $this->$action($user, $payload);
}
private function systemLog($sql, $username, $msg)
{
// TODO not sure if we need this
}
private function package($status, $msg, $data)
{
$result = [
"status" => $status,
"message" => $msg,
"data" => []
];
if (empty($data))
$result;
foreach ($this->term_map as $nice => $orig)
$result["data"][$nice] = $data[$orig];
$result["data"]["admin"] = false;
$admin_flags = ["super_user", "account_manager", "access_system_config", "access_system_upgrade", "access_external_module_install", "admin_rights", "access_admin_dashboards"];
foreach ($admin_flags as $flag) {
if ($data[$flag]) {
$result["data"]["admin"] = true;
break;
}
}
return $result;
}
private function add($user, $payload)
{
// TODO (Table based user only)
// What data needs to be defaulted?
}
private function remove($user, $payload)
{
// TODO
}
private function suspend($user, $payload)
{
// TODO
}
private function reset($user, $payload)
{
// TODO (Table based user only)
}
private function modify($user, $payload)
{
// TODO
}
private function read($user, $payload)
{
$q = $this->query("SELECT * FROM redcap_user_information WHERE username = ?", $user);
if (!($q && $q !== false && db_num_rows($q) == 1))
return $this->package("failure", "User not found", []);
return $this->package("success", "User found, data returned.", db_fetch_assoc($q));
}
private function dump($user, $payload)
{
$page = $payload["page"] ?? 0;
$size = $payload["page_size"] ?? $this->default_page_size;
$q = $this->query("SELECT * FROM redcap_user_information LIMIT ? OFFSET ?", $size, $page * $size);
$result = [
"status" => "success",
"message" => "Dumping users, page $page",
"data" => []
];
$i = $page * $size;
while ($row = db_fetch_assoc($q)) {
$result["data"][$i] = $this->package("", "", $row)["data"];
$i++;
}
}
}