Methods

Source

LineCode
1
<?php
2
3
namespace AuthStack\UserDirectory\Service;
4
5
use AuthStack\UserDirectory\Exceptions\AccountException;
6
7
use AuthStack\UserDirectory\Exceptions\UserDirectoryException;
8
use AuthStack\UserDirectory\Model\Account as AccountModel;
9
use AuthStack\UserDirectory\Model\Organization;
10
11
class Account extends RESTService
12
{
13
    protected $account_id;
14
15
    public function setID($id)
16
    {
17
        $this->account_id = $id;
18
19
        return $this;
20
    }
21
22
    /**
23
     * @param string $username
24
     * @param string $password
25
     * @param callable $after
26
     * @return AccountModel
27
     * @throws AccountException
28
     */
29
    public function authenticate(string $username, string $password, callable $after = null): AccountModel
30
    {
31
        return $this->remote(function() use ($username, $password)
32
        {
33
            $data = $this->json('authenticate', ['username' => $username, 'password' => $password]);
34
35
            return new AccountModel($data);
36
37
        }, AccountException::class, $after);
38
    }
39
40
    /**
41
     * @param array $model
42
     * @param callable|null $after
43
     * @return array
44
     */
45
    public function createOrganizational(array $model, callable $after = null): array
46
    {
47
        return $this->remote(function() use (&$model)
48
        {
49
            $result = $this->json('create_organizational', $model);
50
51
            $model = array_merge($model, $result);
52
53
            $account = new AccountModel(array_merge($model, ['id' => $result['account_id']]));
54
            $org = new Organization(array_merge($model, ['id' => $result['organization_id']]));
55
56
            return ['account' => $account, 'organization' => $org];
57
58
        }, AccountException::class, $after);
59
    }
60
61
    /**
62
     * @param array $model
63
     * @param callable|null $after
64
     * @return AccountModel
65
     */
66
    public function create(array $model, callable $after = null): AccountModel
67
    {
68
        return $this->remote(function() use (&$model)
69
        {
70
            $data = $this->json('create', $model);
71
72
            return new AccountModel($data);
73
74
        }, AccountException::class, $after);
75
    }
76
77
    /**
78
     * @param int|null $id
79
     * @param callable|null $after
80
     * @return AccountModel
81
     */
82
    public function read(int $id = null, callable $after = null): AccountModel
83
    {
84
        return $this->remote(function() use ($id)
85
        {
86
            $data = $this->json('read', [], [$id]);
87
88
            return new AccountModel($data);
89
90
        }, AccountException::class, $after);
91
    }
92
93
    /**
94
     * @param int|null $id
95
     * @param callable|null $after
96
     * @return AccountModel
97
     */
98
    public function findByUsername(string $username, callable $after = null): AccountModel
99
    {
100
        return $this->remote(function() use ($username)
101
        {
102
            $data = $this->json('read_by_username', [], [], ['username' => $username]);
103
104
            return new AccountModel($data);
105
106
        }, AccountException::class, $after);
107
    }
108
109
    /**
110
     * @param int|null $id
111
     * @param array $data
112
     * @return bool
113
     */
114
    public function update(int $id = null, array $data, callable $after = null): bool
115
    {
116
        return $this->remote(function() use ($id, &$data)
117
        {
118
            $data = $this->json('update', $data, [$id]);
119
120
            return true;
121
122
        }, AccountException::class, $after);
123
    }
124
125
    /**
126
     * @param int|null $id
127
     * @param array $data
128
     * @return bool
129
     */
130
    public function updateCustomAttributes(int $id = null, array $data): bool
131
    {
132
        return $this->remote(function() use ($id, &$data)
133
        {
134
            $data = $this->json('update_custom_attr', ['attributes' => $data], [$id]);
135
136
            return true;
137
138
        }, AccountException::class);
139
    }
140
141
    /**
142
     * @param int|null $id
143
     * @return bool
144
     */
145
    public function delete(int $id = null): bool
146
    {
147
        return $this->remote(function() use ($id)
148
        {
149
            $data = $this->json('delete', [], [$id]);
150
151
            return true;
152
        }, AccountException::class);
153
    }
154
155
    /**
156
     * @param int $id
157
     * @param int $group_id
158
     * @return bool
159
     */
160
    public function addToGroup(int $id, int $group_id): bool
161
    {
162
        return $this->remote(function() use ($id, $group_id)
163
        {
164
            $data = $this->json('add_to_group', [], [$id, $group_id]);
165
166
            return true;
167
        }, AccountException::class);
168
    }
169
170
    /**
171
     * @param int $id
172
     * @param array $groups
173
     * @return bool
174
     */
175
    public function addToMultipleGroups(int $id, array $groups): bool
176
    {
177
        return $this->remote(function() use ($id, $groups)
178
        {
179
            $data = $this->json('add_to_multiple_groups', ['groups' => $groups], [$id]);
180
181
            return true;
182
        }, AccountException::class);
183
    }
184
185
    /**
186
     * @param int $id
187
     * @param int $group_id
188
     * @return bool
189
     */
190
    public function removeFromGroup(int $id, int $group_id): bool
191
    {
192
        return $this->remote(function() use ($id, $group_id)
193
        {
194
            $data = $this->json('remove_from_group', [], [$id, $group_id]);
195
196
            return true;
197
        }, AccountException::class);
198
    }
199
200
    /**
201
     * @param int $id
202
     * @return bool
203
     */
204
    public function activate(int $id): bool
205
    {
206
        return $this->remote(function() use ($id)
207
        {
208
            $result = $this->json('activate', [], [$id]);
209
210
            return true;
211
        }, AccountException::class);
212
    }
213
214
    /**
215
     * @param int $id
216
     * @return bool
217
     */
218
    public function deactivate(int $id): bool
219
    {
220
        return $this->remote(function() use ($id)
221
        {
222
            $result = $this->json('deactivate', [], [$id]);
223
224
            return true;
225
        }, AccountException::class);
226
    }
227
228
    /**
229
     * @param int $id
230
     * @return bool
231
     */
232
    public function suspend(int $id): bool
233
    {
234
        return $this->remote(function() use ($id)
235
        {
236
            $result = $this->json('suspend', [], [$id]);
237
238
            return true;
239
        }, AccountException::class);
240
    }
241
242
    /**
243
     * @param array $options
244
     * @return array
245
     */
246
    public function listing(array $options = []): array
247
    {
248
        return $this->remote(function() use (&$options)
249
        {
250
            $result = $this->json('listing', null, null, $options);
251
252
            return $result;
253
254
        }, AccountException::class);
255
    }
256
257
    /**
258
     * @param int $id
259
     * @return bool
260
     */
261
    public function setManager(int $id): bool
262
    {
263
        return $this->remote(function() use ($id)
264
        {
265
            $result = $this->json('setmanager', [], [$id]);
266
267
            return true;
268
        }, AccountException::class);
269
    }
270
271
    /**
272
     * @param int $id
273
     * @return bool
274
     */
275
    public function unsetManager(int $id): bool
276
    {
277
        return $this->remote(function() use ($id)
278
        {
279
            $result = $this->json('unsetmanager', [], [$id]);
280
281
            return true;
282
        }, AccountException::class);
283
    }
284
285
    /**
286
     * @param int $id
287
     * @param string|null $when
288
     * @return bool
289
     */
290
    public function setExpiresAt(int $id, string $when = null): bool
291
    {
292
        return $this->remote(function() use ($id, $when)
293
        {
294
            // If $when is null, then account never expires, else it expires at $when
295
            $params = is_null($when) ? ['never_expires' => true] : ['expires_at' => $when];
296
297
            $result = $this->json('expiry', $params, [$id]);
298
299
            return true;
300
        }, AccountException::class);
301
    }
302
303
    /**
304
     * @param int $id
305
     * @param int $organization_id
306
     * @return bool
307
     */
308
    public function addToOrganization(int $id, int $organization_id): bool
309
    {
310
        return $this->remote(function() use ($id, $organization_id)
311
        {
312
            $response = $this->json('add_to_org', [], [$id, $organization_id]);
313
314
            return true;
315
316
        }, AccountException::class);
317
    }
318
319
    /**
320
     * @param int $id
321
     * @return bool
322
     */
323
    public function removeFromOrganization(int $id): bool
324
    {
325
        return $this->remote(function() use ($id)
326
        {
327
            $response = $this->json('remove_from_org', [], [$id]);
328
329
            return true;
330
331
        }, AccountException::class);
332
    }
333
334
    /**
335
     * @param int $id
336
     * @param string $username
337
     * @return bool
338
     */
339
    public function changeUsername(int $id, string $username): bool
340
    {
341
        return $this->remote(function() use ($id, $username)
342
        {
343
            $response = $this->json('change_username', ['username' => $username], [$id]);
344
345
            return $response['result'];
346
        }, AccountException::class);
347
    }
348
349
    /**
350
     * @param int $id
351
     * @param string $password
352
     * @return bool
353
     */
354
    public function changePassword(int $id, string $password): bool
355
    {
356
        return $this->remote(function() use ($id, $password)
357
        {
358
            $response = $this->json('change_password', ['password' => $password], [$id]);
359
360
            return $response['result'];
361
362
        }, AccountException::class);
363
    }
364
365
    /**
366
     * @param int $id
367
     * @param int $policy_id
368
     * @return bool
369
     */
370
    public function changePasswordPolicy(int $id, int $policy_id): bool
371
    {
372
        return $this->remote(function() use ($id, $policy_id)
373
        {
374
            $response = $this->json('change_ppolicy', [], [$id, $policy_id]);
375
376
            return $response['result'];
377
378
        }, AccountException::class);
379
    }
380
381
    /**
382
     * @param int $id
383
     * @param array $options
384
     * @return mixed
385
     */
386
    public function getLogs(int $id, array $options = [])
387
    {
388
        return $this->remote(function() use ($id, $options)
389
        {
390
            $response = $this->json('logs', [], [$id], $options);
391
392
            return $response;
393
394
        }, AccountException::class);
395
    }
396
397
    /**
398
     * @param int|null $id
399
     * @return int
400
     * @throws AccountException
401
     */
402
    protected function __id(int $id = null) : int
403
    {
404
        if(!is_int($this->account_id) && !is_int($id))
405
        {
406
            throw new AccountException("Incorrect method call - no Account ID specified");
407
        }
408
409
        return is_int($id) ? $id : $this->account_id;
410
    }
411
}