-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount_model.php
218 lines (182 loc) · 8.03 KB
/
account_model.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
class Accounts
{
private $mysqli;
private $redis;
private $user;
private $table = "accounts";
public function __construct($mysqli, $redis, $user)
{
$this->mysqli = $mysqli;
$this->redis = $redis;
$this->user = $user;
}
public function list($adminuser)
{
$adminuser = (int) $adminuser;
$accounts = array();
$result = $this->mysqli->query("SELECT linkeduser FROM ".$this->table." WHERE adminuser = '$adminuser' ORDER BY linkeduser ASC");
while ($row = $result->fetch_object()) {
// Get user details
$u = $this->user->get($row->linkeduser);
// Count feeds
$result2 = $this->mysqli->query("SELECT COUNT(*) AS feeds FROM feeds WHERE userid = '$row->linkeduser'");
$f = $result2->fetch_object();
$account = new stdClass();
$account->id = $row->linkeduser*1;
$account->username = $u->username;
$account->location = $u->location;
$account->email = $u->email;
$account->feeds = $f->feeds*1;
$account->access = $this->user->get_access($row->linkeduser);
if (isset($u->activefeeds)) {
$account->activefeeds = $u->activefeeds*1;
}
$accounts[] = $account;
}
return $accounts;
}
public function add($adminuser,$username,$password,$email,$timezone)
{
// Check if adminuser is a linkeduser
$result = $this->mysqli->query("SELECT * FROM ".$this->table." WHERE linkeduser='$adminuser'");
if ($result->fetch_object()) {
return array("success"=>false,"message"=>"Cannot add user as admin user is a linked user");
}
$userid = $this->user->get_id($username);
// If user does not exist then register
if (!$userid) {
// Get adminuser details, if email is the same turn off email verification
$adminuser_details = $this->user->get($adminuser);
if ($adminuser_details->email == $email) {
$this->user->set_email_verification(false);
}
// User does not exist
// Register user
$result = $this->user->register($username, $password, $email, $timezone);
if (!$result['success']) return $result;
// if success then get userid
$linkeduser = $result['userid'];
// verify email
$this->mysqli->query("UPDATE users SET email_verified='1' WHERE `id`='$linkeduser'");
// Disable login access by default
$this->user->set_access($linkeduser,0);
} else {
// else check password and fetch userid
$result = $this->user->get_apikeys_from_login($username,$password);
if (!$result['success']) {
return array("success"=>false,"message"=>"invalid username or password");
}
$linkeduser = $result['userid'];
}
// Check linked user is not admin user
if ($adminuser==$linkeduser) {
return array("success"=>false,"message"=>"cannot link to self");
}
// Check if user is already linked
$result = $this->mysqli->query("SELECT * FROM ".$this->table." WHERE linkeduser='$linkeduser'");
if ($row = $result->fetch_object()) {
return array("success"=>false,"message"=>"user already linked");
}
// Add user to linked table
$this->mysqli->query("INSERT INTO ".$this->table." (adminuser,linkeduser) VALUES ('$adminuser','$linkeduser')");
return array("success"=>true,"message"=>"user linked");
}
public function unlink($adminuser,$linkeduser)
{
$adminuser = (int) $adminuser;
$linkeduser = (int) $linkeduser;
// Check if linkeduser belongs to adminuser
if (!$this->is_linked($adminuser,$linkeduser)) {
return array("success"=>false,"message"=>"invalid linked userid");
}
// Unlink user
$this->mysqli->query("DELETE FROM ".$this->table." WHERE adminuser='$adminuser' AND linkeduser='$linkeduser'");
return array("success"=>true,"message"=>"user unlinked");
}
public function switch($adminuser, $linkeduser) {
$adminuser = (int) $adminuser;
$linkeduser = (int) $linkeduser;
// switch to adminuser
if (!$linkeduser && isset($_SESSION["adminuser"])) {
$userid = $_SESSION["adminuser"];
unset($_SESSION["adminuser"]);
$this->load_user_session($userid);
header("Location: ../account/list");
// stop any other code from running once http header sent
exit();
}
// check that linkeduser is linked to adminuser
if (!$this->is_linked($adminuser,$linkeduser)) {
return array("success"=>false,"message"=>"invalid linked userid");
}
// switch user
$_SESSION["adminuser"] = $adminuser;
$this->load_user_session($linkeduser);
header("Location: ../".$_SESSION["startingpage"]);
// stop any other code from running once http header sent
exit();
}
public function load_user_session($userid) {
$userid = (int) $userid;
$result = $this->mysqli->query("SELECT `username`,`admin`,`timezone`,`startingpage`,`gravatar` FROM users WHERE `id`='$userid'");
if ($row = $result->fetch_object()) {
$_SESSION["userid"] = $userid;
$_SESSION['admin'] = $row->admin;
$_SESSION["username"] = $row->username;
$_SESSION["timezone"] = $row->timezone;
$_SESSION["startingpage"] = $row->startingpage;
$_SESSION["gravatar"] = $row->gravatar;
}
}
// Set access
public function set_access($adminuser,$linkeduser,$access) {
$adminuser = (int) $adminuser;
$linkeduser = (int) $linkeduser;
$access = (int) $access;
// Check if linkeduser belongs to adminuser
if (!$this->is_linked($adminuser,$linkeduser)) {
return array("success"=>false,"message"=>"invalid linked userid");
}
// Set access
$this->user->set_access($linkeduser,$access);
return array("success"=>true,"message"=>"access updated");
}
// Check if linkeduser belongs to adminuser
public function is_linked($adminuser,$linkeduser) {
$adminuser = (int) $adminuser;
$linkeduser = (int) $linkeduser;
$result = $this->mysqli->query("SELECT * FROM ".$this->table." WHERE adminuser='$adminuser' AND linkeduser='$linkeduser'");
if ($result->fetch_object()) {
return true;
}
return false;
}
// Delete user method
public function delete_user($userid, $dryrun = true) {
$userid = (int) $userid;
// Check if user is a linked user
$result = $this->mysqli->query("SELECT * FROM ".$this->table." WHERE linkeduser='$userid'");
if ($result->fetch_object()) {
if (!$dryrun) {
// Unlink user
$this->mysqli->query("DELETE FROM ".$this->table." WHERE linkeduser='$userid'");
}
return array("success"=>true,"message"=>"removed user from accounts table");
}
// Check if user is an admin user
$result = $this->mysqli->query("SELECT * FROM ".$this->table." WHERE adminuser='$userid'");
// If user is an admin user then delete all linked users
// get number of linked users
$num_linked_users = $result->num_rows;
if ($num_linked_users) {
if (!$dryrun) {
// Delete all linked users
$this->mysqli->query("DELETE FROM ".$this->table." WHERE adminuser='$userid'");
}
return array("success"=>true,"message"=>"removed $num_linked_users linked users from accounts table");
}
// no linked or admin users
return array("success"=>false,"message"=>"user not found in accounts table");
}
}