/
user.class.php
3701 lines (3220 loc) · 121 KB
/
user.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* Copyright (c) 2002-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (c) 2002-2003 Jean-Louis Bergamo <jlb@j1b.org>
* Copyright (c) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
* Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
* Copyright (C) 2005-2017 Regis Houssin <regis.houssin@inodbox.com>
* Copyright (C) 2005 Lionel Cousteix <etm_ltd@tiscali.co.uk>
* Copyright (C) 2011 Herve Prot <herve.prot@symeos.com>
* Copyright (C) 2013-2019 Philippe Grand <philippe.grand@atoo-net.com>
* Copyright (C) 2013-2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
* Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
* Copyright (C) 2018 charlene Benke <charlie@patas-monkey.com>
* Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
* Copyright (C) 2019-2020 Frédéric France <frederic.france@netlogic.fr>
* Copyright (C) 2019 Abbes Bahfir <dolipar@dolipar.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/user/class/user.class.php
* \brief File of class to manage users
* \ingroup core
*/
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
/**
* Class to manage Dolibarr users
*/
class User extends CommonObject
{
/**
* @var string ID to identify managed object
*/
public $element = 'user';
/**
* @var string Name of table without prefix where object is stored
*/
public $table_element = 'user';
/**
* @var string Field with ID of parent key if this field has a parent
*/
public $fk_element = 'fk_user';
/**
* 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
* @var int
*/
public $ismultientitymanaged = 1;
/**
* @var string picto
*/
public $picto = 'user';
public $id = 0;
public $statut;
public $ldap_sid;
public $search_sid;
public $employee;
public $civility_code;
/**
* @var string gender
*/
public $gender;
public $birth;
/**
* @var string email
*/
public $email;
/**
* @var string personal email
*/
public $personal_email;
/**
* @var array array of socialnetworks
*/
public $socialnetworks;
/**
* @var string skype account
* @deprecated
*/
public $skype;
/**
* @var string twitter account
* @deprecated
*/
public $twitter;
/**
* @var string facebook account
* @deprecated
*/
public $facebook;
/**
* @var string linkedin account
* @deprecated
*/
public $linkedin;
/**
* @var string job position
*/
public $job;
/**
* @var string user signature
*/
public $signature;
/**
* @var string Address
*/
public $address;
/**
* @var string zip code
*/
public $zip;
/**
* @var string town
*/
public $town;
public $state_id; // The state/department
public $state_code;
public $state;
/**
* @var string office phone
*/
public $office_phone;
/**
* @var string office fax
*/
public $office_fax;
/**
* @var string phone mobile
*/
public $user_mobile;
/**
* @var string personal phone mobile
*/
public $personal_mobile;
/**
* @var int 1 if admin 0 if standard user
*/
public $admin;
/**
* @var string user login
*/
public $login;
/**
* @var string user apikey
*/
public $api_key;
/**
* @var int Entity
*/
public $entity;
/**
* @var string Clear password in memory
*/
public $pass;
/**
* @var string Clear password in database (defined if DATABASE_PWD_ENCRYPTED=0)
*/
public $pass_indatabase;
/**
* @var string Encrypted password in database (always defined)
*/
public $pass_indatabase_crypted;
/**
* @var string Temporary password
*/
public $pass_temp;
/**
* Date creation record (datec)
*
* @var integer
*/
public $datec;
/**
* Date modification record (tms)
*
* @var integer
*/
public $datem;
/**
* @var int If this is defined, it is an external user
*/
public $socid;
/**
* @var int If this is defined, it is a user created from a contact
*/
public $contact_id;
/**
* @var int ID
*/
public $fk_member;
/**
* @var int User ID of supervisor
*/
public $fk_user;
/**
* @var int User ID of expense validator
*/
public $fk_user_expense_validator;
/**
* @var int User ID of holidays validator
*/
public $fk_user_holiday_validator;
/**
* @string clicktodial url
*/
public $clicktodial_url;
/**
* @var string clicktodial login
*/
public $clicktodial_login;
/**
* @var string clicktodial password
*/
public $clicktodial_password;
/**
* @var string clicktodial poste
*/
public $clicktodial_poste;
public $datelastlogin;
public $datepreviouslogin;
public $datestartvalidity;
public $dateendvalidity;
/**
* @var string photo filename
*/
public $photo;
public $lang;
/**
* @var stdClass Class of permissions user->rights->permx
*/
public $rights;
/**
* @var int All permissions are loaded
*/
public $all_permissions_are_loaded;
/**
* @var int Number of rights granted to the user
*/
public $nb_rights;
/**
* @var array To store list of groups of user (used by API /info for example)
*/
public $user_group_list;
/**
* @var array Cache array of already loaded permissions
*/
private $_tab_loaded = array();
/**
* @var stdClass To store personal config
*/
public $conf;
public $default_values; // To store default values for user
public $lastsearch_values_tmp; // To store current search criterias for user
public $lastsearch_values; // To store last saved search criterias for user
public $users = array(); // To store all tree of users hierarchy
public $parentof; // To store an array of all parents for all ids.
private $cache_childids; // Cache array of already loaded childs
public $accountancy_code; // Accountancy code in prevision of the complete accountancy module
public $thm; // Average cost of employee - Used for valuation of time spent
public $tjm; // Average cost of employee
public $salary; // Monthly salary - Denormalized value from llx_user_employment
public $salaryextra; // Monthly salary extra - Denormalized value from llx_user_employment
public $weeklyhours; // Weekly hours - Denormalized value from llx_user_employment
/**
* @var string Define background color for user in agenda
*/
public $color;
public $dateemployment; // Define date of employment by company
public $dateemploymentend; // Define date of employment end by company
public $default_c_exp_tax_cat;
public $default_range;
/**
*@var int id of warehouse
*/
public $fk_warehouse;
public $fields = array(
'rowid'=>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'index'=>1, 'position'=>1, 'comment'=>'Id'),
'lastname'=>array('type'=>'varchar(50)', 'label'=>'Name', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>1, 'index'=>1, 'position'=>20, 'searchall'=>1),
'firstname'=>array('type'=>'varchar(50)', 'label'=>'Name', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1),
);
const STATUS_DISABLED = 0;
const STATUS_ENABLED = 1;
/**
* Constructor of the class
*
* @param DoliDb $db Database handler
*/
public function __construct($db)
{
$this->db = $db;
// User preference
$this->liste_limit = 0;
$this->clicktodial_loaded = 0;
// For cache usage
$this->all_permissions_are_loaded = 0;
$this->nb_rights = 0;
// Force some default values
$this->admin = 0;
$this->employee = 1;
$this->conf = new stdClass();
$this->rights = new stdClass();
$this->rights->user = new stdClass();
$this->rights->user->user = new stdClass();
$this->rights->user->self = new stdClass();
}
/**
* Load a user from database with its id or ref (login).
* This function does not load permissions, only user properties. Use getrights() for this just after the fetch.
*
* @param int $id If defined, id to used for search
* @param string $login If defined, login to used for search
* @param string $sid If defined, sid to used for search
* @param int $loadpersonalconf 1=also load personal conf of user (in $user->conf->xxx), 0=do not load personal conf.
* @param int $entity If a value is >= 0, we force the search on a specific entity. If -1, means search depens on default setup.
* @param int $email If defined, email to used for search
* @return int <0 if KO, 0 not found, >0 if OK
*/
public function fetch($id = '', $login = '', $sid = '', $loadpersonalconf = 0, $entity = -1, $email = '')
{
global $conf, $user;
// Clean parameters
$login = trim($login);
// Get user
$sql = "SELECT u.rowid, u.lastname, u.firstname, u.employee, u.gender, u.civility as civility_code, u.birth, u.email, u.personal_email, u.job,";
$sql .= " u.socialnetworks,";
$sql .= " u.signature, u.office_phone, u.office_fax, u.user_mobile, u.personal_mobile,";
$sql .= " u.address, u.zip, u.town, u.fk_state as state_id, u.fk_country as country_id,";
$sql .= " u.admin, u.login, u.note as note_private, u.note_public,";
$sql .= " u.pass, u.pass_crypted, u.pass_temp, u.api_key,";
$sql .= " u.fk_soc, u.fk_socpeople, u.fk_member, u.fk_user, u.ldap_sid, u.fk_user_expense_validator, u.fk_user_holiday_validator,";
$sql .= " u.statut, u.lang, u.entity,";
$sql .= " u.datec as datec,";
$sql .= " u.tms as datem,";
$sql .= " u.datelastlogin as datel,";
$sql .= " u.datepreviouslogin as datep,";
$sql .= " u.datelastpassvalidation,";
$sql .= " u.datestartvalidity,";
$sql .= " u.dateendvalidity,";
$sql .= " u.photo as photo,";
$sql .= " u.openid as openid,";
$sql .= " u.accountancy_code,";
$sql .= " u.thm,";
$sql .= " u.tjm,";
$sql .= " u.salary,";
$sql .= " u.salaryextra,";
$sql .= " u.weeklyhours,";
$sql .= " u.color,";
$sql .= " u.dateemployment, u.dateemploymentend,";
$sql .= " u.fk_warehouse,";
$sql .= " u.ref_ext,";
$sql .= " u.default_range, u.default_c_exp_tax_cat,"; // Expense report default mode
$sql .= " c.code as country_code, c.label as country,";
$sql .= " d.code_departement as state_code, d.nom as state";
$sql .= " FROM ".MAIN_DB_PREFIX."user as u";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON u.fk_country = c.rowid";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON u.fk_state = d.rowid";
if ($entity < 0) {
if ((empty($conf->multicompany->enabled) || empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) && (!empty($user->entity))) {
$sql .= " WHERE u.entity IN (0, ".((int) $conf->entity).")";
} else {
$sql .= " WHERE u.entity IS NOT NULL"; // multicompany is on in transverse mode or user making fetch is on entity 0, so user is allowed to fetch anywhere into database
}
} else {
// The fetch was forced on an entity
if (!empty($conf->multicompany->enabled) && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
$sql .= " WHERE u.entity IS NOT NULL"; // multicompany is on in transverse mode or user making fetch is on entity 0, so user is allowed to fetch anywhere into database
} else {
$sql .= " WHERE u.entity IN (0, ".((int) (($entity != '' && $entity >= 0) ? $entity : $conf->entity)).")"; // search in entity provided in parameter
}
}
if ($sid) {
// permet une recherche du user par son SID ActiveDirectory ou Samba
$sql .= " AND (u.ldap_sid = '".$this->db->escape($sid)."' OR u.login = '".$this->db->escape($login)."')";
} elseif ($login) {
$sql .= " AND u.login = '".$this->db->escape($login)."'";
} elseif ($email) {
$sql .= " AND u.email = '".$this->db->escape($email)."'";
} else {
$sql .= " AND u.rowid = ".((int) $id);
}
$sql .= " ORDER BY u.entity ASC"; // Avoid random result when there is 2 login in 2 different entities
if ($sid) {
// permet une recherche du user par son SID ActiveDirectory ou Samba
$sql .= ' '.$this->db->plimit(1);
}
$result = $this->db->query($sql);
if ($result) {
$obj = $this->db->fetch_object($result);
if ($obj) {
$this->id = $obj->rowid;
$this->ref = $obj->rowid;
$this->ref_ext = $obj->ref_ext;
$this->ldap_sid = $obj->ldap_sid;
$this->civility_code = $obj->civility_code;
$this->lastname = $obj->lastname;
$this->firstname = $obj->firstname;
$this->employee = $obj->employee;
$this->login = $obj->login;
$this->gender = $obj->gender;
$this->birth = $this->db->jdate($obj->birth);
$this->pass_indatabase = $obj->pass;
$this->pass_indatabase_crypted = $obj->pass_crypted;
$this->pass = $obj->pass;
$this->pass_temp = $obj->pass_temp;
$this->api_key = $obj->api_key;
$this->address = $obj->address;
$this->zip = $obj->zip;
$this->town = $obj->town;
$this->country_id = $obj->country_id;
$this->country_code = $obj->country_id ? $obj->country_code : '';
//$this->country = $obj->country_id?($langs->trans('Country'.$obj->country_code)!='Country'.$obj->country_code?$langs->transnoentities('Country'.$obj->country_code):$obj->country):'';
$this->state_id = $obj->state_id;
$this->state_code = $obj->state_code;
$this->state = ($obj->state != '-' ? $obj->state : '');
$this->office_phone = $obj->office_phone;
$this->office_fax = $obj->office_fax;
$this->user_mobile = $obj->user_mobile;
$this->personal_mobile = $obj->personal_mobile;
$this->email = $obj->email;
$this->personal_email = $obj->personal_email;
$this->socialnetworks = (array) json_decode($obj->socialnetworks, true);
$this->job = $obj->job;
$this->signature = $obj->signature;
$this->admin = $obj->admin;
$this->note_public = $obj->note_public;
$this->note_private = $obj->note_private;
$this->note = $obj->note_private;
$this->statut = $obj->statut;
$this->photo = $obj->photo;
$this->openid = $obj->openid;
$this->lang = $obj->lang;
$this->entity = $obj->entity;
$this->accountancy_code = $obj->accountancy_code;
$this->thm = $obj->thm;
$this->tjm = $obj->tjm;
$this->salary = $obj->salary;
$this->salaryextra = $obj->salaryextra;
$this->weeklyhours = $obj->weeklyhours;
$this->color = $obj->color;
$this->dateemployment = $this->db->jdate($obj->dateemployment);
$this->dateemploymentend = $this->db->jdate($obj->dateemploymentend);
$this->datec = $this->db->jdate($obj->datec);
$this->datem = $this->db->jdate($obj->datem);
$this->datelastlogin = $this->db->jdate($obj->datel);
$this->datepreviouslogin = $this->db->jdate($obj->datep);
$this->datestartvalidity = $this->db->jdate($obj->datestartvalidity);
$this->dateendvalidity = $this->db->jdate($obj->dateendvalidity);
$this->socid = $obj->fk_soc;
$this->contact_id = $obj->fk_socpeople;
$this->fk_member = $obj->fk_member;
$this->fk_user = $obj->fk_user;
$this->fk_user_expense_validator = $obj->fk_user_expense_validator;
$this->fk_user_holiday_validator = $obj->fk_user_holiday_validator;
$this->default_range = $obj->default_range;
$this->default_c_exp_tax_cat = $obj->default_c_exp_tax_cat;
$this->fk_warehouse = $obj->fk_warehouse;
// Protection when module multicompany was set, admin was set to first entity and then, the module was disabled,
// in such case, this admin user must be admin for ALL entities.
if (empty($conf->multicompany->enabled) && $this->admin && $this->entity == 1) {
$this->entity = 0;
}
// Retrieve all extrafield
// fetch optionals attributes and labels
$this->fetch_optionals();
$this->db->free($result);
} else {
$this->error = "USERNOTFOUND";
dol_syslog(get_class($this)."::fetch user not found", LOG_DEBUG);
$this->db->free($result);
return 0;
}
} else {
$this->error = $this->db->lasterror();
return -1;
}
// To get back the global configuration unique to the user
if ($loadpersonalconf) {
// Load user->conf for user
$sql = "SELECT param, value FROM ".MAIN_DB_PREFIX."user_param";
$sql .= " WHERE fk_user = ".((int) $this->id);
$sql .= " AND entity = ".((int) $conf->entity);
//dol_syslog(get_class($this).'::fetch load personalized conf', LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
$num = $this->db->num_rows($resql);
$i = 0;
while ($i < $num) {
$obj = $this->db->fetch_object($resql);
$p = (!empty($obj->param) ? $obj->param : '');
if (!empty($p)) {
$this->conf->$p = $obj->value;
}
$i++;
}
$this->db->free($resql);
} else {
$this->error = $this->db->lasterror();
return -2;
}
$result = $this->loadDefaultValues();
if ($result < 0) {
$this->error = $this->db->lasterror();
return -3;
}
}
return 1;
}
/**
* Load default values from database table into property ->default_values
*
* @return int > 0 if OK, < 0 if KO
*/
public function loadDefaultValues()
{
global $conf;
if (!empty($conf->global->MAIN_ENABLE_DEFAULT_VALUES)) {
// Load user->default_values for user. TODO Save this in memcached ?
require_once DOL_DOCUMENT_ROOT.'/core/class/defaultvalues.class.php';
$defaultValues = new DefaultValues($this->db);
$result = $defaultValues->fetchAll('', '', 0, 0, array('t.user_id'=>array(0, $this->id), 'entity'=>array((isset($this->entity) ? $this->entity : $conf->entity), $conf->entity))); // User 0 (all) + me (if defined)
if (!is_array($result) && $result < 0) {
setEventMessages($defaultValues->error, $defaultValues->errors, 'errors');
dol_print_error($this->db);
return -1;
} elseif (count($result) > 0) {
foreach ($result as $defval) {
if (!empty($defval->page) && !empty($defval->type) && !empty($defval->param)) {
$pagewithoutquerystring = $defval->page;
$pagequeries = '';
$reg = array();
if (preg_match('/^([^\?]+)\?(.*)$/', $pagewithoutquerystring, $reg)) { // There is query param
$pagewithoutquerystring = $reg[1];
$pagequeries = $reg[2];
}
$this->default_values[$pagewithoutquerystring][$defval->type][$pagequeries ? $pagequeries : '_noquery_'][$defval->param] = $defval->value;
}
}
}
if (!empty($this->default_values)) {
foreach ($this->default_values as $a => $b) {
foreach ($b as $c => $d) {
krsort($this->default_values[$a][$c]);
}
}
}
}
return 1;
}
/**
* Return if a user has a permission.
* You can use it like this: if ($user->hasRight('module', 'level11')).
* It replaces old syntax: if ($user->rights->module->level1)
*
* @param int $module Module of permission to check
* @param string $permlevel1 Permission level1 (Example: 'read', 'write', 'delete')
* @param string $permlevel2 Permission level2
* @return int 1 if user has permission, 0 if not.
* @see clearrights(), delrights(), getrights(), hasRight()
*/
public function hasRight($module, $permlevel1, $permlevel2 = '')
{
global $conf;
// For compatibility with bad naming permissions on module
$moduletomoduletouse = array(
'contract' => 'contrat',
'member' => 'adherent', // We must check $user->rights->adherent...
'mo' => 'mrp',
'order' => 'commande',
'product' => 'produit', // We must check $user->rights->produit...
'project' => 'projet',
'shipping' => 'expedition',
'task' => 'task@projet',
'fichinter' => 'ficheinter',
'invoice' => 'facture',
'invoice_supplier' => 'fournisseur',
'knowledgerecord' => 'knowledgerecord@knowledgemanagement'
);
if (!empty($moduletomoduletouse[$module])) {
$module = $moduletomoduletouse[$module];
}
// If module is abc@module, we check permission user->rights->module->abc->permlevel1
$tmp = explode('@', $module, 2);
if (! empty($tmp[1])) {
$module = $tmp[1];
$permlevel2 = $permlevel1;
$permlevel1 = $tmp[0];
}
//var_dump($module);
//var_dump($this->rights->$module);
if (!in_array($module, $conf->modules)) {
return 0;
}
// For compatibility with bad naming permissions on permlevel1
if ($permlevel1 == 'propale') {
$permlevel1 = 'propal';
}
if ($permlevel1 == 'member') {
$permlevel1 = 'adherent';
}
if ($permlevel1 == 'recruitmentcandidature') {
$permlevel1 = 'recruitmentjobposition';
}
//var_dump($module.' '.$permlevel1.' '.$permlevel2);
if (empty($module) || empty($this->rights) || empty($this->rights->$module) || empty($permlevel1)) {
return 0;
}
if ($permlevel2) {
if (!empty($this->rights->$module->$permlevel1)) {
if (!empty($this->rights->$module->$permlevel1->$permlevel2)) {
return $this->rights->$module->$permlevel1->$permlevel2;
}
// For backward compatibility with old permissions called "lire", "creer", "create", "supprimer"
// instead of "read", "write", "delete"
if ($permlevel2 == 'read' && !empty($this->rights->$module->$permlevel1->lire)) {
return $this->rights->$module->lire;
}
if ($permlevel2 == 'write' && !empty($this->rights->$module->$permlevel1->creer)) {
return $this->rights->$module->create;
}
if ($permlevel2 == 'write' && !empty($this->rights->$module->$permlevel1->create)) {
return $this->rights->$module->create;
}
if ($permlevel2 == 'delete' && !empty($this->rights->$module->$permlevel1->supprimer)) {
return $this->rights->$module->supprimer;
}
}
} else {
if (!empty($this->rights->$module->$permlevel1)) {
return $this->rights->$module->$permlevel1;
}
// For backward compatibility with old permissions called "lire", "creer", "create", "supprimer"
// instead of "read", "write", "delete"
if ($permlevel1 == 'read' && !empty($this->rights->$module->lire)) {
return $this->rights->$module->lire;
}
if ($permlevel1 == 'write' && !empty($this->rights->$module->creer)) {
return $this->rights->$module->create;
}
if ($permlevel1 == 'write' && !empty($this->rights->$module->create)) {
return $this->rights->$module->create;
}
if ($permlevel1 == 'delete' && !empty($this->rights->$module->supprimer)) {
return $this->rights->$module->supprimer;
}
}
return 0;
}
/**
* Add a right to the user
*
* @param int $rid Id of permission to add or 0 to add several permissions
* @param string $allmodule Add all permissions of module $allmodule or 'allmodules' to include all modules.
* @param string $allperms Add all permissions of module $allmodule, subperms $allperms only or '' to include all permissions.
* @param int $entity Entity to use
* @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
* @return int > 0 if OK, < 0 if KO
* @see clearrights(), delrights(), getrights(), hasRight()
*/
public function addrights($rid, $allmodule = '', $allperms = '', $entity = 0, $notrigger = 0)
{
global $conf, $user, $langs;
$entity = (!empty($entity) ? $entity : $conf->entity);
dol_syslog(get_class($this)."::addrights $rid, $allmodule, $allperms, $entity");
$error = 0;
$whereforadd = '';
$this->db->begin();
if (!empty($rid)) {
$module = $perms = $subperms = '';
// Si on a demande ajout d'un droit en particulier, on recupere les caracteristiques (module, perms et subperms) de ce droit.
$sql = "SELECT module, perms, subperms";
$sql .= " FROM ".MAIN_DB_PREFIX."rights_def";
$sql .= " WHERE id = ".((int) $rid);
$sql .= " AND entity = ".((int) $entity);
$result = $this->db->query($sql);
if ($result) {
$obj = $this->db->fetch_object($result);
if ($obj) {
$module = $obj->module;
$perms = $obj->perms;
$subperms = $obj->subperms;
}
} else {
$error++;
dol_print_error($this->db);
}
// Where pour la liste des droits a ajouter
$whereforadd = "id=".((int) $rid);
// Ajout des droits induits
if (!empty($subperms)) {
$whereforadd .= " OR (module='".$this->db->escape($module)."' AND perms='".$this->db->escape($perms)."' AND (subperms='lire' OR subperms='read'))";
} elseif (!empty($perms)) {
$whereforadd .= " OR (module='".$this->db->escape($module)."' AND (perms='lire' OR perms='read') AND subperms IS NULL)";
}
} else {
// On a pas demande un droit en particulier mais une liste de droits
// sur la base d'un nom de module de de perms
// Where pour la liste des droits a ajouter
if (!empty($allmodule)) {
if ($allmodule == 'allmodules') {
$whereforadd = 'allmodules';
} else {
$whereforadd = "module='".$this->db->escape($allmodule)."'";
if (!empty($allperms)) {
$whereforadd .= " AND perms='".$this->db->escape($allperms)."'";
}
}
}
}
// Add automatically other permission using the criteria whereforadd
if (!empty($whereforadd)) {
//print "$module-$perms-$subperms";
$sql = "SELECT id";
$sql .= " FROM ".MAIN_DB_PREFIX."rights_def";
$sql .= " WHERE entity = ".((int) $entity);
if (!empty($whereforadd) && $whereforadd != 'allmodules') {
$sql .= " AND ".$whereforadd;
}
$result = $this->db->query($sql);
if ($result) {
$num = $this->db->num_rows($result);
$i = 0;
while ($i < $num) {
$obj = $this->db->fetch_object($result);
$nid = $obj->id;
$sql = "DELETE FROM ".MAIN_DB_PREFIX."user_rights WHERE fk_user = ".((int) $this->id)." AND fk_id = ".((int) $nid)." AND entity = ".((int) $entity);
if (!$this->db->query($sql)) {
$error++;
}
$sql = "INSERT INTO ".MAIN_DB_PREFIX."user_rights (entity, fk_user, fk_id) VALUES (".((int) $entity).", ".((int) $this->id).", ".((int) $nid).")";
if (!$this->db->query($sql)) {
$error++;
}
$i++;
}
} else {
$error++;
dol_print_error($this->db);
}
}
if (!$error && !$notrigger) {
$langs->load("other");
$this->context = array('audit'=>$langs->trans("PermissionsAdd").($rid ? ' (id='.$rid.')' : ''));
// Call trigger
$result = $this->call_trigger('USER_MODIFY', $user);
if ($result < 0) {
$error++;
}
// End call triggers
}
if ($error) {
$this->db->rollback();
return -$error;
} else {
$this->db->commit();
return 1;
}
}
/**
* Remove a right to the user
*
* @param int $rid Id du droit a retirer
* @param string $allmodule Retirer tous les droits du module allmodule
* @param string $allperms Retirer tous les droits du module allmodule, perms allperms
* @param int $entity Entity to use
* @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
* @return int > 0 if OK, < 0 if OK
* @see clearrights(), addrights(), getrights(), hasRight()
*/
public function delrights($rid, $allmodule = '', $allperms = '', $entity = 0, $notrigger = 0)
{
global $conf, $user, $langs;
$error = 0;
$wherefordel = '';
$entity = (!empty($entity) ? $entity : $conf->entity);
$this->db->begin();
if (!empty($rid)) {
$module = $perms = $subperms = '';
// Si on a demande supression d'un droit en particulier, on recupere
// les caracteristiques module, perms et subperms de ce droit.
$sql = "SELECT module, perms, subperms";
$sql .= " FROM ".MAIN_DB_PREFIX."rights_def";
$sql .= " WHERE id = '".$this->db->escape($rid)."'";
$sql .= " AND entity = ".((int) $entity);
$result = $this->db->query($sql);
if ($result) {
$obj = $this->db->fetch_object($result);
if ($obj) {
$module = $obj->module;
$perms = $obj->perms;
$subperms = $obj->subperms;
}
} else {
$error++;
dol_print_error($this->db);
}
// Where pour la liste des droits a supprimer
$wherefordel = "id=".((int) $rid);
// Suppression des droits induits
if ($subperms == 'lire' || $subperms == 'read') {
$wherefordel .= " OR (module='".$this->db->escape($module)."' AND perms='".$this->db->escape($perms)."' AND subperms IS NOT NULL)";
}
if ($perms == 'lire' || $perms == 'read') {
$wherefordel .= " OR (module='".$this->db->escape($module)."')";
}
} else {
// On a demande suppression d'un droit sur la base d'un nom de module ou perms
// Where pour la liste des droits a supprimer
if (!empty($allmodule)) {
if ($allmodule == 'allmodules') {
$wherefordel = 'allmodules';
} else {
$wherefordel = "module='".$this->db->escape($allmodule)."'";
if (!empty($allperms)) {
$wherefordel .= " AND perms='".$this->db->escape($allperms)."'";
}
}
}
}
// Suppression des droits selon critere defini dans wherefordel
if (!empty($wherefordel)) {
//print "$module-$perms-$subperms";
$sql = "SELECT id";
$sql .= " FROM ".MAIN_DB_PREFIX."rights_def";
$sql .= " WHERE entity = ".((int) $entity);
if (!empty($wherefordel) && $wherefordel != 'allmodules') {
$sql .= " AND ".$wherefordel;
}
// avoid admin can remove his own important rights
if ($this->admin == 1) {
$sql .= " AND id NOT IN (251, 252, 253, 254, 255, 256)"; // other users rights
$sql .= " AND id NOT IN (341, 342, 343, 344)"; // own rights
$sql .= " AND id NOT IN (351, 352, 353, 354)"; // groups rights
$sql .= " AND id NOT IN (358)"; // user export
}
$result = $this->db->query($sql);
if ($result) {
$num = $this->db->num_rows($result);
$i = 0;
while ($i < $num) {
$obj = $this->db->fetch_object($result);
$nid = $obj->id;
$sql = "DELETE FROM ".MAIN_DB_PREFIX."user_rights";
$sql .= " WHERE fk_user = ".((int) $this->id)." AND fk_id = ".((int) $nid);
$sql .= " AND entity = ".((int) $entity);
if (!$this->db->query($sql)) {
$error++;
}
$i++;
}
} else {
$error++;