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