WSUser::editUserHelper()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 50
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 31
nc 9
nop 13
dl 0
loc 50
rs 9.1128
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/* For licensing terms, see /license.txt */
3
/**
4
 * @package chamilo.webservices
5
 */
6
require_once __DIR__.'/../inc/global.inc.php';
7
require_once __DIR__.'/webservice.php';
8
9
/**
10
 * Web services available for the User module. This class extends the WS class.
11
 */
12
class WSUser extends WS
13
{
14
    /**
15
     * Disables a user.
16
     *
17
     * @param string API secret key
18
     * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
19
     * @param string User id value
20
     */
21
    public function DisableUser(
22
        $secret_key,
23
        $user_id_field_name,
24
        $user_id_value
25
    ) {
26
        $verifKey = $this->verifyKey($secret_key);
27
        if ($verifKey instanceof WSError) {
28
            // Let the implementation handle it
29
            $this->handleError($verifKey);
30
        } else {
31
            $result = $this->changeUserActiveState(
32
                $user_id_field_name,
33
                $user_id_value,
34
                0
35
            );
36
            if ($result instanceof WSError) {
0 ignored issues
show
introduced by
$result is never a sub-type of WSError.
Loading history...
37
                $this->handleError($result);
38
            }
39
        }
40
    }
41
42
    /**
43
     * Disables multiple users.
44
     *
45
     * @param string API secret key
46
     * @param array Array of users with elements of the form
47
     * array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
48
     *
49
     * @return array Array with elements like
50
     *               array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')).
51
     *               Note that if the result array contains a code different
52
     *               than 0, an error occured
53
     */
54
    public function DisableUsers($secret_key, $users)
55
    {
56
        $verifKey = $this->verifyKey($secret_key);
57
        if ($verifKey instanceof WSError) {
58
            // Let the implementation handle it
59
            $this->handleError($verifKey);
60
        } else {
61
            return $this->changeUsersActiveState($users, 0);
62
        }
63
    }
64
65
    /**
66
     * Enables a user.
67
     *
68
     * @param string API secret key
69
     * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
70
     * @param string User id value
71
     */
72
    public function EnableUser($secret_key, $user_id_field_name, $user_id_value)
73
    {
74
        $verifKey = $this->verifyKey($secret_key);
75
        if ($verifKey instanceof WSError) {
76
            $this->handleError($verifKey);
77
        } else {
78
            $result = $this->changeUserActiveState(
79
                $user_id_field_name,
80
                $user_id_value,
81
                1
82
            );
83
            if ($result instanceof WSError) {
0 ignored issues
show
introduced by
$result is never a sub-type of WSError.
Loading history...
84
                $this->handleError($result);
85
            }
86
        }
87
    }
88
89
    /**
90
     * Enables multiple users.
91
     *
92
     * @param string API secret key
93
     * @param array Array of users with elements of the form
94
     * array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
95
     *
96
     * @return array Array with elements like
97
     *               array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')).
98
     *               Note that if the result array contains a code different
99
     *               than 0, an error occured
100
     */
101
    public function EnableUsers($secret_key, $users)
102
    {
103
        $verifKey = $this->verifyKey($secret_key);
104
        if ($verifKey instanceof WSError) {
105
            // Let the implementation handle it
106
            $this->handleError($verifKey);
107
        } else {
108
            return $this->changeUsersActiveState($users, 1);
109
        }
110
    }
111
112
    /**
113
     * Deletes a user.
114
     *
115
     * @param string API secret key
116
     * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
117
     * @param string User id value
118
     */
119
    public function DeleteUser($secret_key, $user_id_field_name, $user_id_value)
120
    {
121
        $verifKey = $this->verifyKey($secret_key);
122
        if ($verifKey instanceof WSError) {
123
            $this->handleError($verifKey);
124
        } else {
125
            $result = $this->deleteUserHelper(
126
                $user_id_field_name,
127
                $user_id_value
128
            );
129
            if ($result instanceof WSError) {
130
                $this->handleError($result);
131
            }
132
        }
133
    }
134
135
    /**
136
     * Deletes multiple users.
137
     *
138
     * @param string API secret key
139
     * @param array Array of users with elements of the form
140
     * array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
141
     *
142
     * @return array Array with elements like
143
     *               array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')).
144
     *               Note that if the result array contains a code different
145
     *               than 0, an error occured
146
     */
147
    public function DeleteUsers($secret_key, $users)
148
    {
149
        $verifKey = $this->verifyKey($secret_key);
150
        if ($verifKey instanceof WSError) {
151
            $this->handleError($verifKey);
152
        } else {
153
            $results = [];
154
            foreach ($users as $user) {
155
                $result_tmp = [];
156
                $result_op = $this->deleteUserHelper(
157
                    $user['user_id_field_name'],
158
                    $user['user_id_value']
159
                );
160
                $result_tmp['user_id_value'] = $user['user_id_value'];
161
                if ($result_op instanceof WSError) {
162
                    // Return the error in the results
163
                    $result_tmp['result'] = $result_op->toArray();
164
                } else {
165
                    $result_tmp['result'] = $this->getSuccessfulResult();
166
                }
167
                $results[] = $result_tmp;
168
            }
169
170
            return $results;
171
        }
172
    }
173
174
    /**
175
     * Creates a user.
176
     *
177
     * @param string API secret key
178
     * @param string User first name
179
     * @param string User last name
180
     * @param int User status
181
     * @param string Login name
182
     * @param string Password (encrypted or not)
183
     * @param string Encrypt method. Leave blank if you are passing the password in clear text,
184
     * set to the encrypt method used to encrypt the password otherwise. Remember
185
     * to include the salt in the extra fields if you are encrypting the password
186
     * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
187
     * @param string User id value. Leave blank if you are using the internal user_id
188
     * @param int Visibility. Set by default to 1
0 ignored issues
show
Documentation Bug introduced by
The doc comment Visibility. at position 0 could not be parsed: Unknown type name 'Visibility.' at position 0 in Visibility..
Loading history...
189
     * @param string User email. Set by default to an empty string
190
     * @param string Language. Set by default to english
191
     * @param string Phone. Set by default to an empty string
192
     * @param string Expiration date. Set to null by default
193
     * @param array Extra fields. An array with elements of the form
194
     * array('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Set to an empty array by default
195
     *
196
     * @return int New user id generated by the system
197
     */
198
    public function CreateUser(
199
        $secret_key,
200
        $firstname,
201
        $lastname,
202
        $status,
203
        $login,
204
        $password,
205
        $encrypt_method,
206
        $user_id_field_name,
207
        $user_id_value,
208
        $visibility = 1,
209
        $email = '',
210
        $language = 'english',
211
        $phone = '',
212
        $expiration_date = '0000-00-00 00:00:00',
213
        $extras = []
214
    ) {
215
        // First, verify the secret key
216
        $verifKey = $this->verifyKey($secret_key);
217
        if ($verifKey instanceof WSError) {
218
            $this->handleError($verifKey);
219
        } else {
220
            $result = $this->createUserHelper(
221
                $firstname,
222
                $lastname,
223
                $status,
224
                $login,
225
                $password,
226
                $encrypt_method,
227
                $user_id_field_name,
228
                $user_id_value,
229
                $visibility,
230
                $email,
231
                $language,
232
                $phone,
233
                $expiration_date,
234
                $extras
235
            );
236
            if ($result instanceof WSError) {
237
                $this->handleError($result);
238
            } else {
239
                return $result;
240
            }
241
        }
242
    }
243
244
    /**
245
     * Creates multiple users.
246
     *
247
     * @param string API secret key
248
     * @param array Users array. Each member of this array must follow the structure imposed by the CreateUser method
249
     *
250
     * @return array Array with elements of the form
251
     *               array('user_id_value' => 'original value sent', 'user_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful'))
252
     */
253
    public function CreateUsers($secret_key, $users)
254
    {
255
        $verifKey = $this->verifyKey($secret_key);
256
        if ($verifKey instanceof WSError) {
257
            $this->handleError($verifKey);
258
        } else {
259
            $results = [];
260
            foreach ($users as $user) {
261
                $result_tmp = [];
262
                // re-initialize variables just in case
263
                $firstname = $lastname = $status = $login = $password = $encrypt_method = $user_id_field_name = $user_id_value = $visibility = $email = $language = $phone = $expiration_date = $extras = null;
264
                extract($user);
265
                $result = $this->createUserHelper(
266
                    $firstname,
267
                    $lastname,
268
                    $status,
269
                    $login,
270
                    $password,
271
                    $encrypt_method,
272
                    $user_id_field_name,
273
                    $user_id_value,
274
                    $visibility,
275
                    $email,
276
                    $language,
277
                    $phone,
278
                    $expiration_date,
279
                    $extras
280
                );
281
                if ($result instanceof WSError) {
282
                    $result_tmp['result'] = $result->toArray();
283
                    $result_tmp['user_id_value'] = $user_id_value;
284
                    $result_tmp['user_id_generated'] = 0;
285
                } else {
286
                    $result_tmp['result'] = $this->getSuccessfulResult();
287
                    $result_tmp['user_id_value'] = $user_id_value;
288
                    $result_tmp['user_id_generated'] = $result;
289
                }
290
                $results[] = $result_tmp;
291
            }
292
293
            return $results;
294
        }
295
    }
296
297
    /**
298
     * Edits user info.
299
     *
300
     * @param string API secret key
301
     * @param string User id field name. Use "chamilo_user_id" in order to use internal system id
302
     * @param string User id value
303
     * @param string First name
304
     * @param string Last name
305
     * @param int User status
306
     * @param string Login name
307
     * @param string Password. Leave blank if you don't want to update it
0 ignored issues
show
Documentation Bug introduced by
The doc comment Password. at position 0 could not be parsed: Unknown type name 'Password.' at position 0 in Password..
Loading history...
308
     * @param string Encrypt method
309
     * @param string User email
310
     * @param string Language. Set by default to english
311
     * @param string Phone. Set by default to an empty string
312
     * @param string Expiration date. Set to null by default
313
     * @param array Extra fields. An array with elements of the form
314
     * ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Leave empty if you don't want to update
315
     */
316
    public function EditUser(
317
        $secret_key,
318
        $user_id_field_name,
319
        $user_id_value,
320
        $firstname,
321
        $lastname,
322
        $status,
323
        $loginname,
324
        $password,
325
        $encrypt_method,
326
        $email,
327
        $language,
328
        $phone,
329
        $expiration_date,
330
        $extras
331
    ) {
332
        // First, verify the secret key
333
        $verifKey = $this->verifyKey($secret_key);
334
        if ($verifKey instanceof WSError) {
335
            $this->handleError($verifKey);
336
        } else {
337
            $extras_associative = [];
338
            if (!empty($extras)) {
339
                foreach ($extras as $extra) {
340
                    $extras_associative[$extra['field_name']] = $extra['field_value'];
341
                }
342
            }
343
344
            $result = $this->editUserHelper(
345
                $user_id_field_name,
346
                $user_id_value,
347
                $firstname,
348
                $lastname,
349
                $status,
350
                $loginname,
351
                $password,
352
                $encrypt_method,
353
                $email,
354
                $language,
355
                $phone,
356
                $expiration_date,
357
                $extras_associative
358
            );
359
            if ($result instanceof WSError) {
360
                $this->handleError($result);
361
            }
362
        }
363
    }
364
365
    /**
366
     * Edits multiple users.
367
     *
368
     * @param string API secret key
369
     * @param array Users array. Each member of this array must follow the structure imposed by the EditUser method
370
     *
371
     * @return array Array with elements like
372
     *               array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')).
373
     *               Note that if the result array contains a code different
374
     *               than 0, an error occured
375
     */
376
    public function EditUsers($secret_key, $users)
377
    {
378
        $verifKey = $this->verifyKey($secret_key);
379
        if ($verifKey instanceof WSError) {
380
            $this->handleError($verifKey);
381
        } else {
382
            $results = [];
383
            foreach ($users as $user) {
384
                $result_tmp = [];
385
                // re-initialize variables just in case
386
                $user_id_field_name = $user_id_value = $firstname = $lastname = $status = $loginname = $password = $encrypt_method = $email = $language = $phone = $expiration_date = $extras = null;
387
                extract($user);
388
                $result_op = $this->editUserHelper(
389
                    $user_id_field_name,
390
                    $user_id_value,
391
                    $firstname,
392
                    $lastname,
393
                    $status,
394
                    $loginname,
395
                    $password,
396
                    $encrypt_method,
397
                    $email,
398
                    $language,
399
                    $phone,
400
                    $expiration_date,
401
                    $extras
402
                );
403
                $result_tmp['user_id_value'] = $user['user_id_value'];
404
                if ($result_op instanceof WSError) {
405
                    // Return the error in the results
406
                    $result_tmp['result'] = $result_op->toArray();
407
                } else {
408
                    $result_tmp['result'] = $this->getSuccessfulResult();
409
                }
410
                $results[] = $result_tmp;
411
            }
412
413
            return $results;
414
        }
415
    }
416
417
    /**
418
     * Enables or disables a user.
419
     *
420
     * @param string User id field name
421
     * @param string User id value
422
     * @param int Set to 1 to enable and to 0 to disable
423
     *
424
     * @return int
425
     */
426
    protected function changeUserActiveState(
427
        $user_id_field_name,
428
        $user_id_value,
429
        $state
430
    ) {
431
        $user_id = $this->getUserId($user_id_field_name, $user_id_value);
432
        if ($user_id instanceof WSError) {
433
            return $user_id;
434
        } else {
435
            if ($state == 0) {
436
                UserManager::disable($user_id);
437
            } else {
438
                if ($state == 1) {
439
                    UserManager::enable($user_id);
440
                }
441
            }
442
        }
443
    }
444
445
    /**
446
     * Enables or disables multiple users.
447
     *
448
     * @param array Users
449
     * @param int Set to 1 to enable and to 0 to disable
450
     *
451
     * @return array Array of results
452
     */
453
    protected function changeUsersActiveState($users, $state)
454
    {
455
        $results = [];
456
        foreach ($users as $user) {
457
            $result_tmp = [];
458
            $result_op = $this->changeUserActiveState(
459
                $user['user_id_field_name'],
460
                $user['user_id_value'],
461
                $state
462
            );
463
            $result_tmp['user_id_value'] = $user['user_id_value'];
464
            if ($result_op instanceof WSError) {
465
                // Return the error in the results
466
                $result_tmp['result'] = $result_op->toArray();
467
            } else {
468
                $result_tmp['result'] = $this->getSuccessfulResult();
469
            }
470
            $results[] = $result_tmp;
471
        }
472
473
        return $results;
474
    }
475
476
    /**
477
     * Deletes a user (helper method).
478
     *
479
     * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
480
     * @param string User id value
481
     *
482
     * @return mixed True if user was successfully deleted, WSError otherwise
483
     */
484
    protected function deleteUserHelper($user_id_field_name, $user_id_value)
485
    {
486
        $user_id = $this->getUserId($user_id_field_name, $user_id_value);
487
        if ($user_id instanceof WSError) {
488
            return $user_id;
489
        } else {
490
            if (!UserManager::delete_user($user_id)) {
491
                return new WSError(
492
                    101,
493
                    "There was a problem while deleting this user"
494
                );
495
            } else {
496
                return true;
497
            }
498
        }
499
    }
500
501
    /**
502
     * Creates a user (helper method).
503
     *
504
     * @param string User first name
505
     * @param string User last name
506
     * @param int User status
507
     * @param string Login name
508
     * @param string Password (encrypted or not)
509
     * @param string Encrypt method. Leave blank if you are passing the password in clear text,
510
     * set to the encrypt method used to encrypt the password otherwise. Remember
511
     * to include the salt in the extra fields if you are encrypting the password
512
     * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
513
     * @param string User id value. Leave blank if you are using the internal user_id
514
     * @param int visibility
515
     * @param string user email
516
     * @param string language
517
     * @param string phone
518
     * @param string Expiration date
519
     * @param array Extra fields. An array with elements of the form
520
     * array('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field').
521
     *
522
     * @return mixed New user id generated by the system, WSError otherwise
523
     */
524
    protected function createUserHelper(
525
        $firstname,
526
        $lastname,
527
        $status,
528
        $login,
529
        $password,
530
        $encrypt_method,
531
        $user_id_field_name,
532
        $user_id_value,
533
        $visibility,
534
        $email,
535
        $language,
536
        $phone,
537
        $expiration_date,
538
        $extras = []
539
    ) {
540
        // Add the original user id field name and value to the extra fields if needed
541
        $extras_associative = [];
542
        if ($user_id_field_name != "chamilo_user_id") {
543
            $extras_associative[$user_id_field_name] = $user_id_value;
544
        }
545
        if (!empty($extras)) {
546
            foreach ($extras as $extra) {
547
                $extras_associative[$extra['field_name']] = $extra['field_value'];
548
            }
549
        }
550
        $result = UserManager::create_user(
551
            $firstname,
552
            $lastname,
553
            $status,
554
            $email,
555
            $login,
556
            $password,
557
            '',
558
            $language,
559
            $phone,
560
            '',
561
            PLATFORM_AUTH_SOURCE,
562
            $expiration_date,
563
            $visibility,
564
            0,
565
            $extras_associative,
566
            $encrypt_method
567
        );
568
        if (!$result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type false|integer is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
569
            return new WSError(104, 'There was an error creating the user');
570
        } else {
571
            return $result;
572
        }
573
    }
574
575
    /**
576
     * Edits user info (helper method).
577
     *
578
     * @param string User id field name. Use "chamilo_user_id" in order to use internal system id
579
     * @param string User id value
580
     * @param string First name
581
     * @param string Last name
582
     * @param int User status
583
     * @param string Login name
584
     * @param string Password. Leave blank if you don't want to update it
0 ignored issues
show
Documentation Bug introduced by
The doc comment Password. at position 0 could not be parsed: Unknown type name 'Password.' at position 0 in Password..
Loading history...
585
     * @param string Encrypt method
586
     * @param string User email
587
     * @param string Language. Set by default to english
588
     * @param string Phone. Set by default to an empty string
589
     * @param string Expiration date. Set to null by default
590
     * @param array Extra fields. An array with elements of the form
591
     * ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field').
592
     * Leave empty if you don't want to update
593
     *
594
     * @return mixed True if user was successfully updated, WSError otherwise
595
     */
596
    protected function editUserHelper(
597
        $user_id_field_name,
598
        $user_id_value,
599
        $firstname,
600
        $lastname,
601
        $status,
602
        $loginname,
603
        $password,
604
        $encrypt_method,
605
        $email,
606
        $language,
607
        $phone,
608
        $expiration_date,
609
        $extras
610
    ) {
611
        $user_id = $this->getUserId($user_id_field_name, $user_id_value);
612
        if ($user_id instanceof WSError) {
613
            return $user_id;
614
        } else {
615
            if ($password == '') {
616
                $password = null;
617
            }
618
            $user_info = api_get_user_info($user_id);
619
            if (count($extras) == 0) {
620
                $extras = null;
621
            }
622
623
            $result = UserManager::update_user(
624
                $user_id,
625
                $firstname,
626
                $lastname,
627
                $loginname,
628
                $password,
629
                PLATFORM_AUTH_SOURCE,
630
                $email,
631
                $status,
632
                '',
633
                $phone,
634
                $user_info['picture_uri'],
635
                $expiration_date,
636
                $user_info['active'],
637
                null,
638
                $user_info['hr_dept_id'],
639
                $extras,
640
                $encrypt_method
641
            );
642
            if (!$result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type false|integer is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
643
                return new WSError(105, 'There was an error updating the user');
644
            } else {
645
                return $result;
646
            }
647
        }
648
    }
649
}
650