forked from Tangerine-Community/robbert-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.php
executable file
·298 lines (214 loc) · 6.22 KB
/
User.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
include_once('./httpful.phar'); use \Httpful as h; // awesome http library
class User
{
/**
* @var boolean True once user has been authenticated, false if failed and null if not checked.
*/
public $is_auth;
/**
* @var string User name in plain text, Immutable.
*/
private $name;
/**
* @var string password in plain text. Immutable.
*/
private $pass;
/**
* @var Config A configuration object.
*/
private $config;
/**
* @var array CouchDB _user doc.
*/
private $doc;
/**
* @var boolean Allows this object to be manipulated without authentication.
*/
private $admin_mode;
/**
* @var array List of groups
*/
private $groups;
public function __construct( array $options = array() )
{
$this->config = new ConfigHelper();
if (! isset( $options['admin'] ) )
{
$this->admin_mode = false;
if ( ! isset( $options['name'] ) ) throw new InvalidArgumentException("Please supply a user name.");
if ( ! isset( $options['pass'] ) ) throw new InvalidArgumentException("Please supply a password.");
} else
{
$this->admin_mode = true;
$options['pass'] = "";
}
$this->set_name( $options['name'] );
$this->pass = $options['pass'];
$this->groups = array();
return $this;
} // END of construct
/**
* Sets the user's name, updates url and doc member variables.
* @param string $name user's name
* @return \User Returns User for chaining.
*/
private function set_name( $name )
{
$this->name = $name;
return $this;
} // END of set_name
/**
* Simple accessor.
* @return string The name of the user. Null if not set.
*/
public function get_name()
{
if ( ! isset( $this->name ) ) throw new LogicException("User object has no name yet.");
return $this->name;
} // END of get_name
/**
* Adds the group's name to the user's doc. If the user's doc has not been fetched, will attempt to read first.
*/
public function add_group( Group $group = null )
{
if ( $group == null ) throw new UnexpectedValueException("Please supply a group.");
$group_name = $group->get_name();
// If the doc is not there, add group.
if ( ! isset( $this->doc ) )
{
try
{
$this->read();
} catch ( Exception $e )
{
throw new Exception("Attempted to read user doc: " . $e);
}
}
// Did the doc come through?
if ( ! isset( $this->doc ) ) throw new Exception("Could not read user doc.");
// Add it if it doesn't already in there
if ( ! in_array( $group_name, $this->groups ) ) array_push( $this->groups, $group_name );
$this->save();
return $this->groups;
} // END of add_group
/**
* Adds the group's name to the user's doc. If the user's doc has not been fetched, will attempt to read first.
*/
public function remove_group( Group $group = null )
{
if ( $group == null ) throw new UnexpectedValueException("Please supply a group.");
$group_name = $group->get_name();
// If the doc is not there, add group.
if ( ! isset( $this->doc ) )
{
try
{
$this->read();
} catch ( Exception $e )
{
throw new Exception("Attempted to read user doc: " . $e);
}
}
if ( ! isset( $this->doc ) ) throw new Exception("Could not read user doc.");
$this->groups = Helpers::array_without( $group_name, $this->groups );
$this->save();
return $this->doc['groups'];
} // END of remove_group
public function is_authenticated()
{
return $this->is_auth;
}
/**
* Authenticates user's creds against Tangerine's user database.
*
* Persists success or fail in $this->is_auth
*/
public function authenticate()
{
$response_raw = h\Request::get( $this->config->user_doc_url( $this->name, "main" ) )
->authenticateWith( $this->name, $this->pass )
->sendsJson()
->send();
$response = json_decode( $response_raw, true );
$this->is_auth = isset( $response['_id'] ) ? true : false;
return $this->is_auth;
} // END of authenticate
/**
* Reads the User's doc from the Tangerine database.
*/
public function read()
{
$con = $this->config;
$response_raw = h\Request::get( $con->user_doc_url( $this->name, "main" ) )
->authenticateWith( $con->constants->ADMIN_U, $con->constants->ADMIN_P )
->sendsJson()
->send();
$response = json_decode( $response_raw, true );
// blank out member variables
$this->doc = null;
$this->is_auth = false;
$this->groups = array();
// fill member variables on success
if ( ! isset( $response['error'] ) )
{
$this->groups = isset( $response['groups'] ) ? $response['groups'] : array();
$this->doc = $response;
$this->is_auth = true;
}
return $response;
} // END of read
/**
* Creates a user in the _user database.
*/
public function save()
{
$con = $this->config;
// Prepare doc
$new_fields = array(
"_id" => $con->user_doc_name( $this->name ),
"name" => $this->name,
"groups" => $this->groups,
"type" => "user",
"roles" => array()
);
if ( $this->admin_mode == false ) $new_fields['password'] = $this->pass;
// If we've already loaded a doc, then update it.
if ( isset( $this->doc['_rev'] ) )
{
$old_doc = $this->doc;
foreach ( $new_fields as $field_key => $field_value )
$old_doc[$field_key] = $field_value;
$new_doc = $old_doc;
$response = h\Request::put( $con->user_doc_url( $this->name, "main" ) )
->authenticateWith( $con->constants->ADMIN_U, $con->constants->ADMIN_P )
->sendsJson()
->body( json_encode( $new_doc ) )
->send();
} else
{ // otherwise, make a new doc
$new_doc = $new_fields;
$response = h\Request::put( $con->user_doc_url( $this->name, "main" ) )
->authenticateWith( $con->constants->ADMIN_U, $con->constants->ADMIN_P )
->sendsJson()
->body( json_encode( $new_doc ) )
->send();
}
return json_decode( $response, true );
} // END of save
/**
* Removes user from _users database
* @return associative array of the Httpful response
*/
public function destroy ()
{
$con = $this->config;
$this->read();
$rev = "?rev=" . $this->doc["_rev"];
$delete_response = h\Request::delete( $con->user_doc_url( $this->name, "main" ) . $rev )
->authenticateWith( $con->constants->ADMIN_U, $con->constants->ADMIN_P )
->send();
return json_decode( $delete_response, true);
} // END of destroy_user
}
?>