-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
usergroup.class.php
887 lines (762 loc) · 23.7 KB
/
usergroup.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
<?php
/* Copyright (c) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (c) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (c) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2012 Florian Henry <florian.henry@open-concept.pro>
* Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2014 Alexis Algoud <alexis@atm-consulting.fr>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/user/class/usergroup.class.php
* \brief File of class to manage user groups
*/
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
if (! empty($conf->ldap->enabled)) require_once (DOL_DOCUMENT_ROOT."/core/class/ldap.class.php");
/**
* Class to manage user groups
*/
class UserGroup extends CommonObject
{
public $element='usergroup';
public $table_element='usergroup';
protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
public $picto='group';
public $entity; // Entity of group
/**
* @deprecated
* @see name
*/
public $nom; // Name of group
public $globalgroup; // Global group
public $datec; // Creation date of group
public $datem; // Modification date of group
public $members=array(); // Array of users
private $_tab_loaded=array(); // Array of cache of already loaded permissions
public $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;
return 0;
}
/**
* Charge un objet group avec toutes ces caracteristiques (except ->members array)
*
* @param int $id id du groupe a charger
* @param string $groupname name du groupe a charger
* @return int <0 if KO, >0 if OK
*/
function fetch($id='', $groupname='')
{
global $conf;
$sql = "SELECT g.rowid, g.entity, g.nom as name, g.note, g.datec, g.tms as datem";
$sql.= " FROM ".MAIN_DB_PREFIX."usergroup as g";
if ($groupname)
{
$sql.= " WHERE g.nom = '".$this->db->escape($groupname)."'";
}
else
{
$sql.= " WHERE g.rowid = ".$id;
}
dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
$result = $this->db->query($sql);
if ($result)
{
if ($this->db->num_rows($result))
{
$obj = $this->db->fetch_object($result);
$this->id = $obj->rowid;
$this->ref = $obj->rowid;
$this->entity = $obj->entity;
$this->name = $obj->name;
$this->nom = $obj->name; // Deprecated
$this->note = $obj->note;
$this->datec = $obj->datec;
$this->datem = $obj->datem;
$this->members=$this->listUsersForGroup();
// Retreive all extrafield for group
// fetch optionals attributes and labels
dol_include_once('/core/class/extrafields.class.php');
$extrafields=new ExtraFields($this->db);
$extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
$this->fetch_optionals($this->id,$extralabels);
// Sav current LDAP Current DN
//$this->ldap_dn = $this->_load_ldap_dn($this->_load_ldap_info(),0);
}
$this->db->free($result);
return 1;
}
else
{
$this->error=$this->db->lasterror();
return -1;
}
}
/**
* Return array of groups objects for a particular user
*
* @param int $userid User id to search
* @return array Array of groups objects
*/
function listGroupsForUser($userid)
{
global $conf, $user;
$ret=array();
$sql = "SELECT g.rowid, ug.entity as usergroup_entity";
$sql.= " FROM ".MAIN_DB_PREFIX."usergroup as g,";
$sql.= " ".MAIN_DB_PREFIX."usergroup_user as ug";
$sql.= " WHERE ug.fk_usergroup = g.rowid";
$sql.= " AND ug.fk_user = ".$userid;
if(! empty($conf->multicompany->enabled) && $conf->entity == 1 && $user->admin && ! $user->entity)
{
$sql.= " AND g.entity IS NOT NULL";
}
else
{
$sql.= " AND g.entity IN (0,".$conf->entity.")";
}
$sql.= " ORDER BY g.nom";
dol_syslog(get_class($this)."::listGroupsForUser", LOG_DEBUG);
$result = $this->db->query($sql);
if ($result)
{
while ($obj = $this->db->fetch_object($result))
{
if (! array_key_exists($obj->rowid, $ret))
{
$newgroup=new UserGroup($this->db);
$newgroup->fetch($obj->rowid);
$ret[$obj->rowid]=$newgroup;
}
$ret[$obj->rowid]->usergroup_entity[]=$obj->usergroup_entity;
}
$this->db->free($result);
return $ret;
}
else
{
$this->error=$this->db->lasterror();
return -1;
}
}
/**
* Return array of User objects for group this->id (or all if this->id not defined)
*
* @param string $excludefilter Filter to exclude
* @param int $mode 0=Return array of user instance, 1=Return array of users id only
* @return mixed Array of users or -1 on error
*/
function listUsersForGroup($excludefilter='', $mode=0)
{
global $conf, $user;
$ret=array();
$sql = "SELECT u.rowid";
if (! empty($this->id)) $sql.= ", ug.entity as usergroup_entity";
$sql.= " FROM ".MAIN_DB_PREFIX."user as u";
if (! empty($this->id)) $sql.= ", ".MAIN_DB_PREFIX."usergroup_user as ug";
$sql.= " WHERE 1 = 1";
if (! empty($this->id)) $sql.= " AND ug.fk_user = u.rowid";
if (! empty($this->id)) $sql.= " AND ug.fk_usergroup = ".$this->id;
if (! empty($conf->multicompany->enabled) && $conf->entity == 1 && $user->admin && ! $user->entity)
{
$sql.= " AND u.entity IS NOT NULL";
}
else
{
$sql.= " AND u.entity IN (0,".$conf->entity.")";
}
if (! empty($excludefilter)) $sql.=' AND ('.$excludefilter.')';
dol_syslog(get_class($this)."::listUsersForGroup", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
while ($obj = $this->db->fetch_object($resql))
{
if (! array_key_exists($obj->rowid, $ret))
{
if ($mode != 1)
{
$newuser=new User($this->db);
$newuser->fetch($obj->rowid);
$ret[$obj->rowid]=$newuser;
}
else $ret[$obj->rowid]=$obj->rowid;
}
if ($mode != 1 && ! empty($obj->usergroup_entity))
{
$ret[$obj->rowid]->usergroup_entity[]=$obj->usergroup_entity;
}
}
$this->db->free($resql);
return $ret;
}
else
{
$this->error=$this->db->lasterror();
return -1;
}
}
/**
* Add a permission to a group
*
* @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, $user, $langs;
dol_syslog(get_class($this)."::addrights $rid, $allmodule, $allperms");
$error=0;
$whereforadd='';
$this->db->begin();
if (! empty($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 = '".$this->db->escape($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 {
$error++;
dol_print_error($this->db);
}
// Where pour la liste des droits a ajouter
$whereforadd="id=".$this->db->escape($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)";
// Pour compatibilite, si lowid = 0, on est en mode ajout de tout
// TODO A virer quand sera gere par l'appelant
//if (substr($rid,-1,1) == 0) $whereforadd="module='$module'";
}
else {
// Where pour la liste des droits a ajouter
if (! empty($allmodule)) $whereforadd="module='".$this->db->escape($allmodule)."'";
if (! empty($allperms)) $whereforadd=" AND perms='".$this->db->escape($allperms)."'";
}
// Ajout des droits de la liste whereforadd
if (! empty($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."usergroup_rights WHERE fk_usergroup = $this->id AND fk_id=".$nid;
if (! $this->db->query($sql)) $error++;
$sql = "INSERT INTO ".MAIN_DB_PREFIX."usergroup_rights (fk_usergroup, fk_id) VALUES ($this->id, $nid)";
if (! $this->db->query($sql)) $error++;
$i++;
}
}
else
{
$error++;
dol_print_error($this->db);
}
if (! $error)
{
$this->context = array('audit'=>$langs->trans("PermissionsAdd"));
// Call trigger
$result=$this->call_trigger('GROUP_MODIFY',$user);
if ($result < 0) { $error++; }
// End call triggers
}
}
if ($error) {
$this->db->rollback();
return -$error;
}
else {
$this->db->commit();
return 1;
}
}
/**
* Remove a permission from group
*
* @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, $user, $langs;
$error=0;
$wherefordel='';
$this->db->begin();
if (! empty($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 = '".$this->db->escape($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 {
$error++;
dol_print_error($this->db);
}
// Where pour la liste des droits a supprimer
$wherefordel="id=".$this->db->escape($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')";
// Pour compatibilite, si lowid = 0, on est en mode suppression de tout
// TODO A virer quand sera gere par l'appelant
//if (substr($rid,-1,1) == 0) $wherefordel="module='$module'";
}
else {
// Where pour la liste des droits a supprimer
if (! empty($allmodule)) $wherefordel="module='".$this->db->escape($allmodule)."'";
if (! empty($allperms)) $wherefordel=" AND perms='".$this->db->escape($allperms)."'";
}
// Suppression des droits de la liste wherefordel
if (! empty($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."usergroup_rights";
$sql.= " WHERE fk_usergroup = $this->id AND fk_id=".$nid;
if (! $this->db->query($sql)) $error++;
$i++;
}
}
else
{
$error++;
dol_print_error($this->db);
}
if (! $error)
{
$this->context = array('audit'=>$langs->trans("PermissionsDelete"));
// Call trigger
$result=$this->call_trigger('GROUP_MODIFY',$user);
if ($result < 0) { $error++; }
// End call triggers
}
}
if ($error) {
$this->db->rollback();
return -$error;
}
else {
$this->db->commit();
return 1;
}
}
/**
* Charge dans l'objet group, la liste des permissions auquels le groupe a droit
*
* @param string $moduletag Name of module we want permissions ('' means all)
* @return int <0 if KO, >0 if OK
*/
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 (! empty($this->all_permissions_are_loaded))
{
// Si les permissions ont deja ete chargees, on quitte
return;
}
/*
* Recuperation des droits
*/
$sql = "SELECT r.module, r.perms, r.subperms ";
$sql.= " FROM ".MAIN_DB_PREFIX."usergroup_rights as u, ".MAIN_DB_PREFIX."rights_def as r";
$sql.= " WHERE r.id = u.fk_id";
$sql.= " AND r.entity = ".$conf->entity;
$sql.= " AND u.fk_usergroup = ".$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', 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 (! isset($this->rights)) $this->rights = new stdClass(); // For avoid error
if (! isset($this->rights->$module) || ! is_object($this->rights->$module)) $this->rights->$module = new stdClass();
if ($subperms)
{
if (! isset($this->rights->$module->$perms) || ! is_object($this->rights->$module->$perms)) $this->rights->$module->$perms = new stdClass();
$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 group
$this->all_permissions_are_loaded=1;
}
else
{
// Si module defini, on le marque comme charge en cache
$this->_tab_loaded[$moduletag]=1;
}
return 1;
}
/**
* Efface un groupe de la base
*
* @return <0 if KO, > 0 if OK
*/
function delete()
{
global $user,$conf,$langs;
$error=0;
$this->db->begin();
$sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_rights";
$sql .= " WHERE fk_usergroup = ".$this->id;
$this->db->query($sql);
$sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup_user";
$sql .= " WHERE fk_usergroup = ".$this->id;
$this->db->query($sql);
// Remove extrafields
if ((! $error) && (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED))) // For avoid conflicts if trigger used
{
$result=$this->deleteExtraFields();
if ($result < 0)
{
$error++;
dol_syslog(get_class($this)."::delete error -4 ".$this->error, LOG_ERR);
}
}
$sql = "DELETE FROM ".MAIN_DB_PREFIX."usergroup";
$sql .= " WHERE rowid = ".$this->id;
$result=$this->db->query($sql);
if ($result)
{
// Call trigger
$result=$this->call_trigger('GROUP_DELETE',$user);
if ($result < 0) { $error++; $this->db->rollback(); return -1; }
// End call triggers
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
dol_print_error($this->db);
return -1;
}
}
/**
* Create group into database
*
* @param int $notrigger 0=triggers enabled, 1=triggers disabled
* @return int <0 if KO, >=0 if OK
*/
function create($notrigger=0)
{
global $user, $conf, $langs, $hookmanager;
$error=0;
$now=dol_now();
if (! isset($this->entity)) $this->entity=$conf->entity; // If not defined, we use default value
$entity=$this->entity;
if (! empty($conf->multicompany->enabled) && $conf->entity == 1) $entity=$this->entity;
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."usergroup (";
$sql.= "datec";
$sql.= ", nom";
$sql.= ", entity";
$sql.= ") VALUES (";
$sql.= "'".$this->db->idate($now)."'";
$sql.= ",'".$this->db->escape($this->nom)."'";
$sql.= ",".$this->db->escape($entity);
$sql.= ")";
dol_syslog(get_class($this)."::create", LOG_DEBUG);
$result=$this->db->query($sql);
if ($result)
{
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."usergroup");
if ($this->update(1) < 0) return -2;
$action='create';
// Actions on extra fields (by external module or standard code)
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
if (! $error && ! $notrigger)
{
// Call trigger
$result=$this->call_trigger('GROUP_CREATE',$user);
if ($result < 0) { $error++; $this->db->rollback(); return -1; }
// End call triggers
}
if ($error > 0) { $error++; $this->db->rollback(); return -1; }
else $this->db->commit();
return $this->id;
}
else
{
$this->db->rollback();
$this->error=$this->db->lasterror();
return -1;
}
}
/**
* Update group into database
*
* @param int $notrigger 0=triggers enabled, 1=triggers disabled
* @return int <0 if KO, >=0 if OK
*/
function update($notrigger=0)
{
global $user, $conf, $langs, $hookmanager;
$error=0;
$entity=$conf->entity;
if(! empty($conf->multicompany->enabled) && $conf->entity == 1)
{
$entity=$this->entity;
}
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."usergroup SET ";
$sql.= " nom = '" . $this->db->escape($this->name) . "'";
$sql.= ", entity = " . $this->db->escape($entity);
$sql.= ", note = '" . $this->db->escape($this->note) . "'";
$sql.= " WHERE rowid = " . $this->id;
dol_syslog(get_class($this)."::update", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$action='update';
// Actions on extra fields (by external module or standard code)
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
if (! $error && ! $notrigger)
{
// Call trigger
$result=$this->call_trigger('GROUP_MODIFY',$user);
if ($result < 0) { $error++; }
// End call triggers
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
return -$error;
}
}
else
{
$this->db->rollback();
dol_print_error($this->db);
return -1;
}
}
/**
* Return label of status of user (active, inactive)
*
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
* @return string Label of status
*/
function getLibStatut($mode=0)
{
return $this->LibStatut(0,$mode);
}
/**
* Renvoi le libelle d'un statut donne
*
* @param int $statut Id statut
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
* @return string Label of status
*/
function LibStatut($statut,$mode=0)
{
global $langs;
$langs->load('users');
return '';
}
/**
* Retourne chaine DN complete dans l'annuaire LDAP pour l'objet
*
* @param array $info Info array loaded by _load_ldap_info
* @param int $mode 0=Return full DN (uid=qqq,ou=xxx,dc=aaa,dc=bbb)
* 1=Return DN without key inside (ou=xxx,dc=aaa,dc=bbb)
* 2=Return key only (uid=qqq)
* @return string DN
*/
function _load_ldap_dn($info,$mode=0)
{
global $conf;
$dn='';
if ($mode==0) $dn=$conf->global->LDAP_KEY_GROUPS."=".$info[$conf->global->LDAP_KEY_GROUPS].",".$conf->global->LDAP_GROUP_DN;
if ($mode==1) $dn=$conf->global->LDAP_GROUP_DN;
if ($mode==2) $dn=$conf->global->LDAP_KEY_GROUPS."=".$info[$conf->global->LDAP_KEY_GROUPS];
return $dn;
}
/**
* Initialize the info array (array of LDAP values) that will be used to call LDAP functions
*
* @return array Tableau info des attributs
*/
function _load_ldap_info()
{
global $conf,$langs;
$info=array();
// Object classes
$info["objectclass"]=explode(',',$conf->global->LDAP_GROUP_OBJECT_CLASS);
// Champs
if ($this->name && ! empty($conf->global->LDAP_GROUP_FIELD_FULLNAME)) $info[$conf->global->LDAP_GROUP_FIELD_FULLNAME] = $this->name;
//if ($this->name && ! empty($conf->global->LDAP_GROUP_FIELD_NAME)) $info[$conf->global->LDAP_GROUP_FIELD_NAME] = $this->name;
if ($this->note && ! empty($conf->global->LDAP_GROUP_FIELD_DESCRIPTION)) $info[$conf->global->LDAP_GROUP_FIELD_DESCRIPTION] = $this->note;
if (! empty($conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS))
{
$valueofldapfield=array();
foreach($this->members as $key=>$val) // This is array of users for group into dolibarr database.
{
$muser=new User($this->db);
$muser->fetch($val->id);
$info2 = $muser->_load_ldap_info();
$valueofldapfield[] = $muser->_load_ldap_dn($info2);
}
$info[$conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS] = (!empty($valueofldapfield)?$valueofldapfield:'');
}
return $info;
}
/**
* Initialise an instance with random values.
* Used to build previews or test instances.
* id must be 0 if object instance is a specimen.
*
* @return void
*/
function initAsSpecimen()
{
global $conf, $user, $langs;
// Initialise parametres
$this->id=0;
$this->ref = 'SPECIMEN';
$this->specimen=1;
$this->name='DOLIBARR GROUP SPECIMEN';
$this->note='This is a note';
$this->datec=time();
$this->datem=time();
// Members of this group is just me
$this->members=array(
$user->id => $user
);
}
/**
* Create a document onto disk according to template module.
*
* @param string $modele Force model to use ('' to not force)
* @param Translate $outputlangs Object langs to use for output
* @param int $hidedetails Hide details of lines
* @param int $hidedesc Hide description
* @param int $hideref Hide ref
* @return int 0 if KO, 1 if OK
*/
public function generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
{
global $conf,$user,$langs;
$langs->load("user");
// Positionne le modele sur le nom du modele a utiliser
if (! dol_strlen($modele))
{
if (! empty($conf->global->USERGROUP_ADDON_PDF))
{
$modele = $conf->global->USERGROUP_ADDON_PDF;
}
else
{
$modele = 'grass';
}
}
$modelpath = "core/modules/usergroup/doc/";
return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
}
}