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($model);
54
            $org = new Organization($model);
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 array $data
96
     * @return bool
97
     */
98
    public function update(int $id = null, array $data, callable $after = null): bool
99
    {
100
        return $this->remote(function() use ($id, &$data)
101
        {
102
            $data = $this->json('update', $data, [$id]);
103
104
            return true;
105
106
        }, AccountException::class, $after);
107
    }
108
109
    /**
110
     * @param int|null $id
111
     * @param array $data
112
     * @return bool
113
     */
114
    public function updateCustomAttributes(int $id = null, array $data): bool
115
    {
116
        return $this->remote(function() use ($id, &$data)
117
        {
118
            $data = $this->json('update_custom_attr', ['attributes' => $data], [$id]);
119
120
            return true;
121
122
        }, AccountException::class);
123
    }
124
125
    /**
126
     * @param int|null $id
127
     * @return bool
128
     */
129
    public function delete(int $id = null): bool
130
    {
131
        return $this->remote(function() use ($id)
132
        {
133
            $data = $this->json('delete', [], [$id]);
134
135
            return true;
136
        }, AccountException::class);
137
    }
138
139
    /**
140
     * @param int $id
141
     * @param int $group_id
142
     * @return bool
143
     */
144
    public function addToGroup(int $id, int $group_id): bool
145
    {
146
        return $this->remote(function() use ($id, $group_id)
147
        {
148
            $data = $this->json('add_to_group', [], [$id, $group_id]);
149
150
            return true;
151
        }, AccountException::class);
152
    }
153
154
    /**
155
     * @param int $id
156
     * @param array $groups
157
     * @return bool
158
     */
159
    public function addToMultipleGroups(int $id, array $groups): bool
160
    {
161
        return $this->remote(function() use ($id, $groups)
162
        {
163
            $data = $this->json('add_to_multiple_groups', ['groups' => $groups], [$id]);
164
165
            return true;
166
        }, AccountException::class);
167
    }
168
169
    /**
170
     * @param int $id
171
     * @param int $group_id
172
     * @return bool
173
     */
174
    public function removeFromGroup(int $id, int $group_id): bool
175
    {
176
        return $this->remote(function() use ($id, $group_id)
177
        {
178
            $data = $this->json('remove_from_group', [], [$id, $group_id]);
179
180
            return true;
181
        }, AccountException::class);
182
    }
183
184
    /**
185
     * @param int $id
186
     * @return bool
187
     */
188
    public function activate(int $id): bool
189
    {
190
        return $this->remote(function() use ($id)
191
        {
192
            $result = $this->json('activate', [], [$id]);
193
194
            return true;
195
        }, AccountException::class);
196
    }
197
198
    /**
199
     * @param int $id
200
     * @return bool
201
     */
202
    public function deactivate(int $id): bool
203
    {
204
        return $this->remote(function() use ($id)
205
        {
206
            $result = $this->json('deactivate', [], [$id]);
207
208
            return true;
209
        }, AccountException::class);
210
    }
211
212
    /**
213
     * @param int $id
214
     * @return bool
215
     */
216
    public function suspend(int $id): bool
217
    {
218
        return $this->remote(function() use ($id)
219
        {
220
            $result = $this->json('suspend', [], [$id]);
221
222
            return true;
223
        }, AccountException::class);
224
    }
225
226
    /**
227
     * @param array $options
228
     * @return array
229
     */
230
    public function listing(array $options = []): array
231
    {
232
        return $this->remote(function() use (&$options)
233
        {
234
            $result = $this->json('listing', null, null, $options);
235
236
            return $result;
237
238
        }, AccountException::class);
239
    }
240
241
    /**
242
     * @param int $id
243
     * @return bool
244
     */
245
    public function setManager(int $id): bool
246
    {
247
        return $this->remote(function() use ($id)
248
        {
249
            $result = $this->json('setmanager', [], [$id]);
250
251
            return true;
252
        }, AccountException::class);
253
    }
254
255
    /**
256
     * @param int $id
257
     * @return bool
258
     */
259
    public function unsetManager(int $id): bool
260
    {
261
        return $this->remote(function() use ($id)
262
        {
263
            $result = $this->json('unsetmanager', [], [$id]);
264
265
            return true;
266
        }, AccountException::class);
267
    }
268
269
    /**
270
     * @param int $id
271
     * @param string|null $when
272
     * @return bool
273
     */
274
    public function setExpiresAt(int $id, string $when = null): bool
275
    {
276
        return $this->remote(function() use ($id, $when)
277
        {
278
            // If $when is null, then account never expires, else it expires at $when
279
            $params = is_null($when) ? ['never_expires' => true] : ['expires_at' => $when];
280
281
            $result = $this->json('expiry', $params, [$id]);
282
283
            return true;
284
        }, AccountException::class);
285
    }
286
287
    /**
288
     * @param int $id
289
     * @param int $organization_id
290
     * @return bool
291
     */
292
    public function addToOrganization(int $id, int $organization_id): bool
293
    {
294
        return $this->remote(function() use ($id, $organization_id)
295
        {
296
            $response = $this->json('add_to_org', [], [$id, $organization_id]);
297
298
            return true;
299
300
        }, AccountException::class);
301
    }
302
303
    /**
304
     * @param int $id
305
     * @return bool
306
     */
307
    public function removeFromOrganization(int $id): bool
308
    {
309
        return $this->remote(function() use ($id)
310
        {
311
            $response = $this->json('remove_from_org', [], [$id]);
312
313
            return true;
314
315
        }, AccountException::class);
316
    }
317
318
    /**
319
     * @param int $id
320
     * @param string $username
321
     * @return bool
322
     */
323
    public function changeUsername(int $id, string $username): bool
324
    {
325
        return $this->remote(function() use ($id, $username)
326
        {
327
            $response = $this->json('change_username', ['username' => $username], [$id]);
328
329
            return $response['result'];
330
        }, AccountException::class);
331
    }
332
333
    /**
334
     * @param int $id
335
     * @param string $password
336
     * @return bool
337
     */
338
    public function changePassword(int $id, string $password): bool
339
    {
340
        return $this->remote(function() use ($id, $password)
341
        {
342
            $response = $this->json('change_password', ['password' => $password], [$id]);
343
344
            return $response['result'];
345
346
        }, AccountException::class);
347
    }
348
349
    /**
350
     * @param int $id
351
     * @param int $policy_id
352
     * @return bool
353
     */
354
    public function changePasswordPolicy(int $id, int $policy_id): bool
355
    {
356
        return $this->remote(function() use ($id, $policy_id)
357
        {
358
            $response = $this->json('change_ppolicy', [], [$id, $policy_id]);
359
360
            return $response['result'];
361
362
        }, AccountException::class);
363
    }
364
365
    /**
366
     * @param int $id
367
     * @param array $options
368
     * @return mixed
369
     */
370
    public function getLogs(int $id, array $options = [])
371
    {
372
        return $this->remote(function() use ($id, $options)
373
        {
374
            $response = $this->json('logs', [], [$id], $options);
375
376
            return $response;
377
378
        }, AccountException::class);
379
    }
380
381
    /**
382
     * @param int|null $id
383
     * @return int
384
     * @throws AccountException
385
     */
386
    protected function __id(int $id = null) : int
387
    {
388
        if(!is_int($this->account_id) && !is_int($id))
389
        {
390
            throw new AccountException("Incorrect method call - no Account ID specified");
391
        }
392
393
        return is_int($id) ? $id : $this->account_id;
394
    }
395
}