Completed
Push — master ( da8032...9f3156 )
by Julito
21:48
created

WSUser::CreateUser()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

477
        /** @scrutinizer ignore-unused */ $language,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
478
        $phone,
479
        $expiration_date,
480
        $extras
481
    ) {
482
        $user_id = $this->getUserId($user_id_field_name, $user_id_value);
483
        if ($user_id instanceof WSError) {
484
            return $user_id;
485
        } else {
486
            if ($password == '') {
487
                $password = null;
488
            }
489
            $user_info = api_get_user_info($user_id);
490
            if (count($extras) == 0) {
491
                $extras = null;
492
            }
493
494
            $result = UserManager::update_user(
495
                $user_id,
496
                $firstname,
497
                $lastname,
498
                $loginname,
499
                $password,
500
                PLATFORM_AUTH_SOURCE,
501
                $email,
502
                $status,
503
                '',
504
                $phone,
505
                $user_info['picture_uri'],
506
                $expiration_date,
507
                $user_info['active'],
508
                null,
509
                $user_info['hr_dept_id'],
510
                $extras,
511
                $encrypt_method
512
            );
513
            if (!$result) {
514
                return new WSError(105, 'There was an error updating the user');
515
            } else {
516
                return $result;
517
            }
518
        }
519
    }
520
521
    /**
522
     * Edits user info
523
     *
524
     * @param string API secret key
525
     * @param string User id field name. Use "chamilo_user_id" in order to use internal system id
526
     * @param string User id value
527
     * @param string First name
528
     * @param string Last name
529
     * @param int User status
530
     * @param string Login name
531
     * @param string Password. Leave blank if you don't want to update it
532
     * @param string Encrypt method
533
     * @param string User email
534
     * @param string Language. Set by default to english
535
     * @param string Phone. Set by default to an empty string
536
     * @param string Expiration date. Set to null by default
537
     * @param array Extra fields. An array with elements of the form
538
     * ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Leave empty if you don't want to update
539
     */
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...
540
    public function EditUser(
541
        $secret_key,
542
        $user_id_field_name,
543
        $user_id_value,
544
        $firstname,
545
        $lastname,
546
        $status,
547
        $loginname,
548
        $password,
549
        $encrypt_method,
550
        $email,
551
        $language,
552
        $phone,
553
        $expiration_date,
554
        $extras
555
    ) {
556
        // First, verify the secret key
557
        $verifKey = $this->verifyKey($secret_key);
558
        if ($verifKey instanceof WSError) {
559
            $this->handleError($verifKey);
560
        } else {
561
            $extras_associative = [];
562
            if (!empty($extras)) {
563
                foreach ($extras as $extra) {
564
                    $extras_associative[$extra['field_name']] = $extra['field_value'];
565
                }
566
            }
567
568
            $result = $this->editUserHelper(
569
                $user_id_field_name,
570
                $user_id_value,
571
                $firstname,
572
                $lastname,
573
                $status,
574
                $loginname,
575
                $password,
576
                $encrypt_method,
577
                $email,
578
                $language,
579
                $phone,
580
                $expiration_date,
581
                $extras_associative
582
            );
583
            if ($result instanceof WSError) {
584
                $this->handleError($result);
585
            }
586
        }
587
    }
588
589
    /**
590
     * Edits multiple users
591
     *
592
     * @param string API secret key
593
     * @param array Users array. Each member of this array must follow the structure imposed by the EditUser method
594
     * @return array Array with elements like
595
     * array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')).
596
     * Note that if the result array contains a code different
597
     * than 0, an error occured
598
     */
599
    public function EditUsers($secret_key, $users)
600
    {
601
        $verifKey = $this->verifyKey($secret_key);
602
        if ($verifKey instanceof WSError) {
603
            $this->handleError($verifKey);
604
        } else {
605
            $results = [];
606
            foreach ($users as $user) {
607
                $result_tmp = [];
608
                // re-initialize variables just in case
609
                $user_id_field_name = $user_id_value = $firstname = $lastname = $status = $loginname = $password = $encrypt_method = $email = $language = $phone = $expiration_date = $extras = null;
610
                extract($user);
611
                $result_op = $this->editUserHelper(
612
                    $user_id_field_name,
613
                    $user_id_value,
614
                    $firstname,
615
                    $lastname,
616
                    $status,
617
                    $loginname,
618
                    $password,
619
                    $encrypt_method,
620
                    $email,
621
                    $language,
622
                    $phone,
623
                    $expiration_date,
624
                    $extras
625
                );
626
                $result_tmp['user_id_value'] = $user['user_id_value'];
627
                if ($result_op instanceof WSError) {
628
                    // Return the error in the results
629
                    $result_tmp['result'] = $result_op->toArray();
630
                } else {
631
                    $result_tmp['result'] = $this->getSuccessfulResult();
632
                }
633
                $results[] = $result_tmp;
634
            }
635
636
            return $results;
637
        }
638
    }
639
}
640