/
user.class.php
2041 lines (1770 loc) · 56.2 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-2012 Regis Houssin <regis@dolibarr.fr>
* Copyright (C) 2005 Lionel Cousteix <etm_ltd@tiscali.co.uk>
* Copyright (C) 2011 Herve Prot <herve.prot@symeos.com>
*
* 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 2 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 <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/user/class/user.class.php
* \brief File of class to manage users
*/
require_once(DOL_DOCUMENT_ROOT ."/core/class/commonobject.class.php");
/**
* \class User
* \brief Class to manage users
*/
class User extends CommonObject
{
public $element='user';
public $table_element='user';
protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
var $id=0;
var $ldap_sid;
var $search_sid;
var $nom; // TODO deprecated
var $prenom; // TODO deprecated
var $lastname;
var $firstname;
var $note;
var $email;
var $signature;
var $office_phone;
var $office_fax;
var $user_mobile;
var $admin;
var $login;
var $entity;
//! Clear password in memory
var $pass;
//! Clear password in database (defined if DATABASE_PWD_ENCRYPTED=0)
var $pass_indatabase;
//! Encrypted password in database (always defined)
var $pass_indatabase_crypted;
var $datec;
var $datem;
//! If this is defined, it is an external user
var $societe_id;
var $fk_member;
var $webcal_login;
var $phenix_login;
var $phenix_pass;
var $phenix_pass_crypted;
var $datelastlogin;
var $datepreviouslogin;
var $statut;
var $photo;
var $lang;
//! Liste des entrepots auquel a acces l'utilisateur
var $entrepots;
var $rights; // Array of permissions user->rights->permx
var $all_permissions_are_loaded; /**< \private all_permissions_are_loaded */
private $_tab_loaded=array(); // Array of cache of already loaded permissions
var $oldcopy; // To contains a clone of this when we need to save old properties of object
/**
* Constructor de la classe
*
* @param DoliDb $db Database handler
*/
function __construct($db)
{
$this->db = $db;
// Preference utilisateur
$this->liste_limit = 0;
$this->clicktodial_loaded = 0;
$this->all_permissions_are_loaded = 0;
$this->admin=0;
}
/**
* Load a user from database with its id or ref (login)
*
* @param int $id Si defini, id a utiliser pour recherche
* @param string $login Si defini, login a utiliser pour recherche
* @param strinf $sid Si defini, sid a utiliser pour recherche
* @param int $loadpersonalconf Also load personal conf of user (in $user->conf->xxx)
* @return int <0 if KO, 0 not found, >0 if OK
*/
function fetch($id='', $login='',$sid='',$loadpersonalconf=1)
{
global $conf, $user;
// Clean parameters
$login=trim($login);
// Get user
$sql = "SELECT u.rowid, u.name, u.firstname, u.email, u.signature, u.office_phone, u.office_fax, u.user_mobile,";
$sql.= " u.admin, u.login, u.webcal_login, u.phenix_login, u.phenix_pass, u.note,";
$sql.= " u.pass, u.pass_crypted, u.pass_temp,";
$sql.= " u.fk_societe, u.fk_socpeople, u.fk_member, u.ldap_sid,";
$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.photo as photo,";
$sql.= " u.openid as openid,";
$sql.= " u.ref_int, u.ref_ext";
$sql.= " FROM ".MAIN_DB_PREFIX."user as u";
if(! empty($conf->multicompany->enabled) && $conf->entity == 1)
{
$sql.= " WHERE u.entity IS NOT NULL";
}
else
{
$sql.= " WHERE u.entity IN (0,".$conf->entity.")";
}
if ($sid) // permet une recherche du user par son SID ActiveDirectory ou Samba
{
$sql.= " AND (u.ldap_sid = '".$sid."' OR u.login = '".$this->db->escape($login)."') LIMIT 1";
}
else if ($login)
{
$sql.= " AND u.login = '".$this->db->escape($login)."'";
}
else
{
$sql.= " AND u.rowid = ".$id;
}
dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
$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_int = $obj->ref_int;
$this->ref_ext = $obj->ref_ext;
$this->ldap_sid = $obj->ldap_sid;
$this->nom = $obj->name; // TODO deprecated
$this->lastname = $obj->name;
$this->prenom = $obj->firstname; // TODO deprecated
$this->firstname = $obj->firstname;
$this->login = $obj->login;
$this->pass_indatabase = $obj->pass;
$this->pass_indatabase_crypted = $obj->pass_crypted;
$this->pass = $obj->pass;
$this->pass_temp = $obj->pass_temp;
$this->office_phone = $obj->office_phone;
$this->office_fax = $obj->office_fax;
$this->user_mobile = $obj->user_mobile;
$this->email = $obj->email;
$this->signature = $obj->signature;
$this->admin = $obj->admin;
$this->note = $obj->note;
$this->statut = $obj->statut;
$this->photo = $obj->photo;
$this->openid = $obj->openid;
$this->lang = $obj->lang;
$this->entity = $obj->entity;
$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->webcal_login = $obj->webcal_login;
$this->phenix_login = $obj->phenix_login;
$this->phenix_pass_crypted = $obj->phenix_pass;
$this->societe_id = $obj->fk_societe;
$this->contact_id = $obj->fk_socpeople;
$this->fk_member = $obj->fk_member;
if (! $this->lang) $this->lang='fr_FR';
$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->error();
dol_syslog(get_class($this)."::fetch Error -1, fails to get user - ".$this->error." - sql=".$sql, LOG_ERR);
return -1;
}
// Recupere parametrage global propre a l'utilisateur
if ($loadpersonalconf)
{
$sql = "SELECT param, value FROM ".MAIN_DB_PREFIX."user_param";
$sql.= " WHERE fk_user = ".$this->id;
$sql.= " AND entity = ".$conf->entity;
$result=$this->db->query($sql);
if ($result)
{
$num = $this->db->num_rows($result);
$i = 0;
while ($i < $num)
{
$obj = $this->db->fetch_object($result);
$p=$obj->param;
if ($p) $this->conf->$p = $obj->value;
$i++;
}
$this->db->free($result);
}
else
{
$this->error=$this->db->error();
dol_syslog(get_class($this)."::fetch Error -2, fails to get setup user - ".$this->error." - sql=".$sql, LOG_ERR);
return -2;
}
}
return 1;
}
/**
* Ajoute un droit a l'utilisateur
*
* @param int $rid id du droit a ajouter
* @param string $allmodule Ajouter tous les droits du module allmodule
* @param string $allperms Ajouter tous les droits du module allmodule, perms allperms
* @return int > 0 if OK, < 0 if KO
*/
function addrights($rid,$allmodule='',$allperms='')
{
global $conf;
dol_syslog(get_class($this)."::addrights $rid, $allmodule, $allperms");
$err=0;
$whereforadd='';
$this->db->begin();
if ($rid)
{
// 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 = '".$rid."'";
$sql.= " AND entity = ".$conf->entity;
$result=$this->db->query($sql);
if ($result) {
$obj = $this->db->fetch_object($result);
$module=$obj->module;
$perms=$obj->perms;
$subperms=$obj->subperms;
}
else {
$err++;
dol_print_error($this->db);
}
// Where pour la liste des droits a ajouter
$whereforadd="id=".$rid;
// Ajout des droits induits
if ($subperms) $whereforadd.=" OR (module='$module' AND perms='$perms' AND (subperms='lire' OR subperms='read'))";
else if ($perms) $whereforadd.=" OR (module='$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 ($allmodule) $whereforadd="module='$allmodule'";
if ($allperms) $whereforadd=" AND perms='$allperms'";
}
// Ajout des droits trouves grace au critere whereforadd
if ($whereforadd)
{
//print "$module-$perms-$subperms";
$sql = "SELECT id";
$sql.= " FROM ".MAIN_DB_PREFIX."rights_def";
$sql.= " WHERE ".$whereforadd;
$sql.= " AND entity = ".$conf->entity;
$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 = ".$this->id." AND fk_id=".$nid;
if (! $this->db->query($sql)) $err++;
$sql = "INSERT INTO ".MAIN_DB_PREFIX."user_rights (fk_user, fk_id) VALUES (".$this->id.", ".$nid.")";
if (! $this->db->query($sql)) $err++;
$i++;
}
}
else
{
$err++;
dol_print_error($this->db);
}
}
if ($err) {
$this->db->rollback();
return -$err;
}
else {
$this->db->commit();
return 1;
}
}
/**
* Retire un droit a l'utilisateur
*
* @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
* @return int > 0 if OK, < 0 if OK
*/
function delrights($rid,$allmodule='',$allperms='')
{
global $conf;
$err=0;
$wherefordel='';
$this->db->begin();
if ($rid)
{
// 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 = '".$rid."'";
$sql.= " AND entity = ".$conf->entity;
$result=$this->db->query($sql);
if ($result) {
$obj = $this->db->fetch_object($result);
$module=$obj->module;
$perms=$obj->perms;
$subperms=$obj->subperms;
}
else {
$err++;
dol_print_error($this->db);
}
// Where pour la liste des droits a supprimer
$wherefordel="id=".$rid;
// Suppression des droits induits
if ($subperms=='lire' || $subperms=='read') $wherefordel.=" OR (module='$module' AND perms='$perms' AND subperms IS NOT NULL)";
if ($perms=='lire' || $perms=='read') $wherefordel.=" OR (module='$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 ($allmodule) $wherefordel="module='$allmodule'";
if ($allperms) $wherefordel=" AND perms='$allperms'";
}
// Suppression des droits selon critere defini dans wherefordel
if ($wherefordel)
{
//print "$module-$perms-$subperms";
$sql = "SELECT id";
$sql.= " FROM ".MAIN_DB_PREFIX."rights_def";
$sql.= " WHERE $wherefordel";
$sql.= " AND entity = ".$conf->entity;
$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 = ".$this->id." AND fk_id=".$nid;
if (! $this->db->query($sql)) $err++;
$i++;
}
}
else
{
$err++;
dol_print_error($this->db);
}
}
if ($err) {
$this->db->rollback();
return -$err;
}
else {
$this->db->commit();
return 1;
}
}
/**
* Clear all permissions array of user
*
* @return void
*/
function clearrights()
{
dol_syslog(get_class($this)."::clearrights reset user->rights");
$this->rights='';
$this->all_permissions_are_loaded=false;
$this->_tab_loaded=array();
}
/**
* Load permissions granted to user into object user
*
* @param string $moduletag Limit permission for a particular module ('' by default means load all permissions)
* @return void
*/
function getrights($moduletag='')
{
global $conf;
if ($moduletag && isset($this->_tab_loaded[$moduletag]) && $this->_tab_loaded[$moduletag])
{
// Le fichier de ce module est deja charge
return;
}
if ($this->all_permissions_are_loaded)
{
// Si les permissions ont deja ete charge pour ce user, on quitte
return;
}
// Recuperation des droits utilisateurs + recuperation des droits groupes
// D'abord les droits utilisateurs
$sql = "SELECT r.module, r.perms, r.subperms";
$sql.= " FROM ".MAIN_DB_PREFIX."user_rights as ur";
$sql.= ", ".MAIN_DB_PREFIX."rights_def as r";
$sql.= " WHERE r.id = ur.fk_id";
$sql.= " AND r.entity in (0,".(!empty($conf->multicompany->transverse_mode)?"1,":"").$conf->entity.")";
$sql.= " AND ur.fk_user= ".$this->id;
$sql.= " AND r.perms IS NOT NULL";
if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'";
dol_syslog(get_class($this).'::getrights sql='.$sql, 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);
$module=$obj->module;
$perms=$obj->perms;
$subperms=$obj->subperms;
if ($perms)
{
if ($subperms)
{
if (! isset($this->rights->$module) ||
(is_object($this->rights->$module) && ! isset($this->rights->$module->$perms)) ||
(is_object($this->rights->$module->$perms)) )
{
$this->rights->$module->$perms->$subperms = 1;
}
}
else
{
$this->rights->$module->$perms = 1;
}
}
$i++;
}
$this->db->free($resql);
}
// Maintenant les droits groupes
$sql = "SELECT r.module, r.perms, r.subperms";
$sql.= " FROM ".MAIN_DB_PREFIX."usergroup_rights as gr,";
$sql.= " ".MAIN_DB_PREFIX."usergroup_user as gu,";
$sql.= " ".MAIN_DB_PREFIX."rights_def as r";
$sql.= " WHERE r.id = gr.fk_id";
$sql.= " AND gr.fk_usergroup = gu.fk_usergroup";
$sql.= " AND gu.fk_user = ".$this->id;
$sql.= " AND r.perms IS NOT NULL";
$sql.= " AND r.entity = ".$conf->entity;
$sql.= " AND gu.entity IN (0,".$conf->entity.")";
if ($moduletag) $sql.= " AND r.module = '".$this->db->escape($moduletag)."'";
dol_syslog(get_class($this).'::getrights sql='.$sql, 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);
$module=$obj->module;
$perms=$obj->perms;
$subperms=$obj->subperms;
if ($perms)
{
if ($subperms)
{
$this->rights->$module->$perms->$subperms = 1;
}
else
{
$this->rights->$module->$perms = 1;
}
}
$i++;
}
$this->db->free($resql);
}
if (! $moduletag)
{
// Si module etait non defini, alors on a tout charge, on peut donc considerer
// que les droits sont en cache (car tous charges) pour cet instance de user
$this->all_permissions_are_loaded=1;
}
else
{
// Si module defini, on le marque comme charge en cache
$this->_tab_loaded[$moduletag]=1;
}
}
/**
* Change status of a user
*
* @param int $statut Status to set
* @return int <0 if KO, 0 if nothing is done, >0 if OK
*/
function setstatus($statut)
{
global $conf,$langs,$user;
$error=0;
// Check parameters
if ($this->statut == $statut) return 0;
else $this->statut = $statut;
$this->db->begin();
// Desactive utilisateur
$sql = "UPDATE ".MAIN_DB_PREFIX."user";
$sql.= " SET statut = ".$this->statut;
$sql.= " WHERE rowid = ".$this->id;
$result = $this->db->query($sql);
dol_syslog(get_class($this)."::setstatus sql=".$sql);
if ($result)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('USER_ENABLEDISABLE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
}
if ($error)
{
$this->db->rollback();
return -$error;
}
else
{
$this->db->commit();
return 1;
}
}
/**
* Delete the user
*
* @return int <0 if KO, >0 if OK
*/
function delete()
{
global $user,$conf,$langs;
$error=0;
$this->db->begin();
$this->fetch($this->id);
// Supprime droits
$sql = "DELETE FROM ".MAIN_DB_PREFIX."user_rights WHERE fk_user = ".$this->id;
if ($this->db->query($sql))
{
}
// Remove group
$sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_user WHERE fk_user = ".$this->id;
if ($this->db->query($sql))
{
}
// Si contact, supprime lien
if ($this->contact_id)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET fk_user_creat = null WHERE rowid = ".$this->contact_id;
if ($this->db->query($sql))
{
}
}
// Supprime utilisateur
$sql = "DELETE FROM ".MAIN_DB_PREFIX."user WHERE rowid = $this->id";
$result = $this->db->query($sql);
if ($result)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('USER_DELETE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
return -1;
}
}
/**
* Create a user into database
*
* @param User $user Objet user qui demande la creation
* @param int $notrigger 1 ne declenche pas les triggers, 0 sinon
* @return int <0 si KO, id compte cree si OK
*/
function create($user,$notrigger=0)
{
global $conf,$langs;
// Clean parameters
$this->login = trim($this->login);
if (! isset($this->entity)) $this->entity=$conf->entity; // If not defined, we use default value
dol_syslog(get_class($this)."::create login=".$this->login.", user=".(is_object($user)?$user->id:''), LOG_DEBUG);
// Check parameters
if (! empty($conf->global->USER_MAIL_REQUIRED) && ! isValidEMail($this->email))
{
$langs->load("errors");
$this->error = $langs->trans("ErrorBadEMail",$this->email);
return -1;
}
$this->datec = dol_now();
$error=0;
$this->db->begin();
$sql = "SELECT login FROM ".MAIN_DB_PREFIX."user";
$sql.= " WHERE login ='".$this->db->escape($this->login)."'";
$sql.= " AND entity IN (0,".$conf->entity.")";
dol_syslog("User::Create sql=".$sql, LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
$this->db->free($resql);
if ($num)
{
$this->error = 'ErrorLoginAlreadyExists';
dol_syslog(get_class($this)."::create ".$this->error, LOG_WARNING);
$this->db->rollback();
return -6;
}
else
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."user (datec,login,ldap_sid,entity)";
$sql.= " VALUES('".$this->db->idate($this->datec)."','".$this->db->escape($this->login)."','".$this->ldap_sid."',".$this->entity.")";
$result=$this->db->query($sql);
dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
if ($result)
{
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."user");
// Set default rights
if ($this->set_default_rights() < 0)
{
$this->error=$this->db->error();
$this->db->rollback();
return -5;
}
// Update minor fields
$result = $this->update($user,1,1);
if ($result < 0)
{
$this->db->rollback();
return -4;
}
if (! empty($conf->global->STOCK_USERSTOCK_AUTOCREATE))
{
require_once(DOL_DOCUMENT_ROOT."/product/stock/class/entrepot.class.php");
$langs->load("stocks");
$entrepot = new Entrepot($this->db);
$entrepot->libelle = $langs->trans("PersonalStock",$this->nom);
$entrepot->description = $langs->trans("ThisWarehouseIsPersonalStock",$this->prenom,$this->nom);
$entrepot->statut = 1;
$entrepot->create($user);
}
if (! $notrigger)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
$interface = new Interfaces($this->db);
$result = $interface->run_triggers('USER_CREATE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
}
if (! $error)
{
$this->db->commit();
return $this->id;
}
else
{
$this->error=$interface->error;
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
$this->db->rollback();
return -3;
}
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
$this->db->rollback();
return -2;
}
}
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
$this->db->rollback();
return -1;
}
}
/**
* Create a user from a contact object. User will be internal but if contact is linked to a third party, user will be external
*
* @param Contact $contact Object for source contact
* @param string $login Login to force
* @param string $password Password to force
* @return int <0 if error, if OK returns id of created user
*/
function create_from_contact($contact,$login='',$password='')
{
global $conf,$user,$langs;
$error=0;
// Positionne parametres
$this->admin = 0;
$this->nom = $contact->nom; // TODO deprecated
$this->prenom = $contact->prenom; // TODO deprecated
$this->lastname = $contact->nom;
$this->firstname = $contact->prenom;
$this->email = $contact->email;
$this->office_phone = $contact->phone_pro;
$this->office_fax = $contact->fax;
$this->user_mobile = $contact->phone_mobile;
if (empty($login)) $login=strtolower(substr($contact->prenom, 0, 4)) . strtolower(substr($contact->nom, 0, 4));
$this->login = $login;
$this->db->begin();
// Cree et positionne $this->id
$result=$this->create($user);
if ($result > 0)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."user";
$sql.= " SET fk_socpeople=".$contact->id;
$sql.= ", office_phone = '".$this->db->escape($this->office_phone)."'";
$sql.= ", office_fax = '".$this->db->escape($this->office_fax)."'";
$sql.= ", user_mobile = '".$this->db->escape($this->user_mobile)."'";
if ($contact->socid) $sql.=", fk_societe=".$contact->socid;
$sql.= " WHERE rowid=".$this->id;
$resql=$this->db->query($sql);
dol_syslog(get_class($this)."::create_from_contact sql=".$sql, LOG_DEBUG);
if ($resql)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
$interface = new Interfaces($this->db);
$result = $interface->run_triggers('USER_CREATE_FROM_CONTACT',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
$this->db->commit();
return $this->id;
}
else
{
$this->error=$this->db->error();
dol_syslog(get_class($this)."::create_from_contact ".$this->error, LOG_ERR);
$this->db->rollback();
return -1;
}
}
else
{
// $this->error deja positionne
dol_syslog(get_class($this)."::create_from_contact - 0");
$this->db->rollback();
return $result;
}
}
/**
* Create a user into database from a member object
*
* @param Adherent $member Object member source
* @param string $login Login to force
* @return int <0 if KO, if OK, return id of created account
*/
function create_from_member($member,$login='')
{
global $conf,$user,$langs;
// Positionne parametres
$this->admin = 0;
$this->nom = $member->nom;
$this->prenom = $member->prenom;
$this->email = $member->email;
$this->pass = $member->pass;
if (empty($login)) $login=strtolower(substr($member->prenom, 0, 4)) . strtolower(substr($member->nom, 0, 4));
$this->login = $login;
$this->db->begin();
// Cree et positionne $this->id
$result=$this->create($user);
if ($result > 0)
{
$result=$this->setPassword($user,$this->pass);
$sql = "UPDATE ".MAIN_DB_PREFIX."user";
$sql.= " SET fk_member=".$member->id;
if ($member->fk_soc) $sql.= ", fk_societe=".$member->fk_soc;
$sql.= " WHERE rowid=".$this->id;
dol_syslog(get_class($this)."::create_from_member sql=".$sql, LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
$this->db->commit();
return $this->id;
}
else
{
$this->error=$this->db->error();
dol_syslog(get_class($this)."::create_from_member - 1 - ".$this->error, LOG_ERR);
$this->db->rollback();
return -1;
}
}
else
{
// $this->error deja positionne
dol_syslog(get_class($this)."::create_from_member - 2 - ".$this->error, LOG_ERR);
$this->db->rollback();
return $result;
}
}
/**
* Affectation des permissions par defaut
*
* @return Si erreur <0, si ok renvoi le nbre de droits par defaut positionnes
*/
function set_default_rights()
{
global $conf;
$sql = "SELECT id FROM ".MAIN_DB_PREFIX."rights_def";
$sql.= " WHERE bydefault = 1";
$sql.= " AND entity = ".$conf->entity;
$resql=$this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
$i = 0;
$rd = array();
while ($i < $num)
{
$row = $this->db->fetch_row($resql);
$rd[$i] = $row[0];
$i++;
}
$this->db->free($resql);