Code

Fixed priority
[gosa.git] / plugins / admin / groups / class_groupGeneric.inc
1 <?php
2 class group extends plugin
3 {
4   /* CLI vars */
5   var $cli_summary= "Handling of GOsa's base group object";
6   var $cli_description= "Some longer text\nfor help";
7   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9   /* Group attributes */
10   var $cn= "";
11   var $description= "";
12   var $gidNumber= "";
13   var $memberUid= array();
15   /* Helpers */
16   var $base= "";
17   var $force_gid= FALSE;
18   var $fon_group= FALSE;
19   var $smbgroup= FALSE;
20   var $groupType= FALSE;
21   var $samba3= FALSE;
22   var $sambaSID= "";
23   var $sambaDomainName= "DEFAULT";
24   var $SID= "";
25   var $ridBase= 0;
26   var $members= array();
27   var $users= array();
28   var $allusers= array();
29   var $department= "";
30   var $saved_gidNumber= "";
31   var $oldgroupType= "";
32   var $orig_dn= "";
33   var $has_mailAccount= FALSE;
34   var $group_dialog= FALSE;
35   var $nagios_group =FALSE;
37   /* attribute list for save action */
38   var $attributes= array("cn", "description", "gidNumber");
39   var $objectclasses= array("top", "posixGroup");
41   function group ($config, $dn= NULL)
42   {
43     plugin::plugin ($config, $dn);
45     /* Load attributes depending on the samba version */
46     $this->samba3= ($config->current['SAMBAVERSION'] == 3);
47     $this->orig_dn= $dn;
49     /* Get member list */
50     if (isset($this->attrs['memberUid'][0])){
51       $tmp= array();
52       for ($i= 0; $i<$this->attrs['memberUid']['count']; $i++){
53         $tmp[]= $this->attrs['memberUid'][$i];
54       }
55       $this->memberUid= $tmp;
56       sort ($this->memberUid);
57     }
59     /* Save gidNumber for later use */
60     if (isset($this->attrs['gidNumber'])){
61       $this->saved_gidNumber= $this->attrs['gidNumber'][0];
62     }
64     /* Is a samba group? */
65     if (isset($this->attrs['objectClass'])){
66       if (array_search ('sambaGroupMapping', $this->attrs['objectClass']) == NULL ){
67         $this->smbgroup= FALSE;
68       } else {
69         $this->smbgroup= TRUE;
70         if (isset($this->attrs['sambaSID'])){
71           $this->sambaSID= $this->attrs['sambaSID'][0];
72         }
73       }
74       if (array_search ('goFonPickupGroup', $this->attrs['objectClass']) == NULL ){
75         $this->fon_group= FALSE;
76       } else {
77         $this->fon_group= TRUE;
78       }
79       if (array_search ('nagiosContactGroup', $this->attrs['objectClass']) == NULL ){
80         $this->nagios_group= FALSE;
81       } else {
82         $this->nagios_group= TRUE;
83       }
84     }
86     /* Set mail flag */
87     if (isset($this->attrs['objectClass']) && in_array('gosaMailAccount', $this->attrs['objectClass'])){
88       $this->has_mailAccount= TRUE;
89     }
91     /* Get samba Domain in case of samba 3 */
92     if ($this->samba3 && $this->sambaSID != ""){
93       $this->SID= preg_replace ("/-[^-]+$/", "", $this->sambaSID);
94       $ldap= $this->config->get_ldap_link();
95       $ldap->cd($this->config->current['BASE']);
96       $ldap->search ("(&(objectClass=sambaDomain)(sambaSID=$this->SID))");
97       if ($ldap->count() != 0){
98         $attrs= $ldap->fetch();
99         $this->ridBase= $attrs['sambaAlgorithmicRidBase'][0];
101         /* Get domain name for SID */
102         $this->sambaDomainName= "DEFAULT";
103         foreach ($this->config->data['SERVERS']['SAMBA'] as $key => $val){
104           if ($val['SID'] == $this->SID){
105             $this->sambaDomainName= $key;
106             break;
107           }
108         }
109       } else {
110         if (isset($this->config->current['RIDBASE'])){
111           $this->sambaDomainName= "DEFAULT";
112           $this->ridBase= $this->config->current['RIDBASE'];
113           $this->SID= $this->config->current['SID'];
114         } else {
115           print_red(_("Can't find this groups SID in LDAP or in your configuration file!"));
116         }
117       }
119       /* Get group type */
120       $this->groupType= (int)substr(strrchr($this->sambaSID, "-"), 1);
121       if ($this->groupType < 500 || $this->groupType > 553){
122         $this->groupType= 0;
123       }
124       $this->oldgroupType= $this->groupType;
125     }
127     /* Get global filter config */
128     if (!is_global("gufilter")){
129       $ui= get_userinfo();
130       $base= get_base_from_people($ui->dn);
131       $gufilter= array( "dselect"       => $base,
132           "regex"           => "*");
133       register_global("gufilter", $gufilter);
134     }
135     $gufilter= get_global('gufilter');
137       /* Bases / Departments */
138       
139     if(isset($_SESSION['groupfilter']['depselect'])){
140       $this->base = $_SESSION['groupfilter']['depselect'];
141     }else{
142       if ($this->dn == "new"){
143         $ui= get_userinfo();
144         $this->base= dn2base($ui->dn);
145       } else {
146         $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
147       }
148     }
151     /* This is always an account */
152     $this->is_account= TRUE;
153     $this->reload();
154   }
156   function execute()
157   {
158         /* Call parent execute */
159         plugin::execute();
161     /* Do we represent a valid group? */
162     if (!$this->is_account && $this->parent == NULL){
163       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
164         _("This 'dn' is no group.")."</b>";
165       return ($display);
166     }
168     /* Delete user from group */
169     if (isset($_POST['del_users']) && isset($_POST['members'])){
170       foreach ($_POST['members'] as $value){
171         unset ($this->members["$value"]);
172         $this->removeUser($value);
173       }
174       $this->reload();
175     }
177     /* Add objects? */
178     if (isset($_POST["edit_membership"])){
179       $this->group_dialog= TRUE;
180       $this->dialog= TRUE;
181     }
183     /* Add objects finished? */
184     if (isset($_POST["add_users_finish"]) || isset($_POST["add_users_cancel"])){
185       $this->group_dialog= FALSE;
186       $this->dialog= FALSE;
187     }
189     /* Add user to group */
190     if (isset($_POST['add_users_finish']) && isset($_POST['users'])){
191       foreach ($_POST['users'] as $value){
192         $this->members["$value"]= $this->allusers[$value];
193         asort($this->members);
194         $this->addUser($value);
195       }
196       $this->reload();
197     }
199     /* Assign templating stuff */
200     $smarty= get_smarty();
201     if ($this->samba3){
202       $smarty->assign("samba3", "true");
203     } else {
204       $smarty->assign("samba3", "");
205     }
207     if(search_config($this->config->data['MENU'], "nagiosaccount", "CLASS")){
208       $smarty->assign("nagios",true);
209     }else{
210       $smarty->assign("nagios",false);
211     }
212     
213     if(search_config($this->config->data['MENU'], "phoneAccount", "CLASS")){
214       $smarty->assign("pickupGroup",true);
215     }else{
216       $smarty->assign("pickupGroup",false);
217     }
219     /* Manage object add dialog */
220     if ($this->group_dialog){
222       /* Save data */
223       $gufilter= get_global("gufilter");
224       foreach( array("dselect", "regex") as $type){
225         if (isset($_POST[$type])){
226           $gufilter[$type]= $_POST[$type];
227         }
228       }
229       if (isset($_GET['search'])){
230         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
231         if ($s == "**"){
232           $s= "*";
233         }
234         $gufilter['regex']= $s;
235       }
236       $regex= preg_replace('/[*]/', ".*", $gufilter['regex']);
237       register_global("gufilter", $gufilter);
238       $this->reload();
240       /* Show dialog */
241       $smarty->assign("search_image", get_template_path('images/search.png'));
242       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
243       $smarty->assign("tree_image", get_template_path('images/tree.png'));
244       $smarty->assign("deplist", $this->config->idepartments);
245       $smarty->assign("alphabet", generate_alphabet());
246       foreach( array("dselect", "regex") as $type){
247         $smarty->assign("$type", $gufilter[$type]);
248       }
249       $smarty->assign("hint", print_sizelimit_warning());
251       $users= array();
252       foreach ($this->allusers as $key => $value){
253         if (!array_key_exists($key, $this->members)){
254           if (preg_match("/^$regex/i", $key)){
255             $users[$key]= $value;
256           }
257         }
258       }
259       $smarty->assign("users", $users);
260       $smarty->assign("apply", apply_filter());
261       $display= $smarty->fetch (get_template_path('group_objects.tpl', TRUE, dirname(__FILE__)));
262       return ($display);
263     }
265     /* Bases / Departments */
266     if (isset($_POST['base'])){
267       $this->base= $_POST['base'];
268     }
270     $smarty->assign("bases", $this->config->idepartments);
271     $smarty->assign("base_select", $this->base);
272     $smarty->assign("department", $this->department);
274     if ($this->samba3){
275       $domains= array();
276       foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
277         $domains[$name]= $name;
278       }
279       $smarty->assign("sambaDomains", $domains);
280       $smarty->assign("sambaDomainName", $this->sambaDomainName);
281       $groupTypes= array(0 => _("Samba group"), 512 => _("Domain admins"), 513 => _("Domain users"),
282           514 => _("Domain guests"));
284       /* Don't loose special groups! If not key'ed above, add it to
285          the combo box... */    
286       if ($this->groupType >= 500 && $this->groupType <= 553 && !isset($groupTypes[$this->groupType])){
287         $groupTypes[$this->groupType]= sprintf(_("Special group (%d)"), $this->groupType);
288       }
290       $smarty->assign("groupTypes", $groupTypes);
291       $smarty->assign("groupType", $this->groupType);
292     }
294     /* Members and users */
295     $smarty->assign("members", $this->members);
297     /* Checkboxes */
298     foreach (array("force_gid", "smbgroup") as $val){
299       if ($this->$val == "1"){
300         $smarty->assign("$val", "checked");
301       } else {
302         $smarty->assign("$val", "");
303       }
304     }
305     if ($this->force_gid != "1"){
306       $smarty->assign("forceMode", "disabled");
307     }
308     $smarty->assign("force_gidACL", chkacl($this->acl, "gidNumber"));
309     $smarty->assign("sambaDomainNameACL", chkacl($this->acl, "sambaDomainName"));
310     if ($this->fon_group){
311       $smarty->assign("fon_group", "checked");
312     } else {
313       $smarty->assign("fon_group", "");
314     }
315     $smarty->assign("fon_groupACL", chkacl($this->acl, "fon_group"));
317     if ($this->nagios_group){
318       $smarty->assign("nagios_group", "checked");
319     } else {
320       $smarty->assign("nagios_group", "");
321     }
322     $smarty->assign("nagios_groupACL", chkacl($this->acl, "nagios_group"));
324     /* Fields */
325     foreach (array("cn", "description", "gidNumber") as $val){
326       $smarty->assign("$val", $this->$val);
327       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
328     }
330     /* Missing ACL's */
331     foreach (array("base", "smbgroup", "members") as $val){
332       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
333     }
335     /* Show main page */
336     $smarty->assign("alphabet", generate_alphabet(10));
337     $smarty->assign("search_image", get_template_path('images/search.png'));
338     $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
339     $smarty->assign("tree_image", get_template_path('images/tree.png'));
340     $smarty->assign("deplist", $this->config->idepartments);
341     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
342   }
344   function addUser($uid)
345   {
346     $this->memberUid[]= $uid;
347     $this->memberUid= array_unique($this->memberUid);
348   }
350   function removeUser($uid)
351   {
352     $temp= array();
353     foreach ($this->memberUid as $value){
354       if ($value != $uid){
355         $temp[]= $value;
356       }
357     }
358     $this->memberUid= $temp;
359   }
362   /* Reload data */
363   function reload()
364   {
365     /* Generate userlists */
366     $this->last_sorting= "invalid";
367     $this->users= array();
368     $ldap= $this->config->get_ldap_link();
369     $gufilter= get_global("gufilter");
370     $ldap->cd ($gufilter['dselect']);
371     $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$)))");
372     natcasesort ($this->users);
373     reset ($this->users);
375     $ldap->cd ($gufilter['dselect']);
376     $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$)))");
377     $this->allusers= array();
378     while ($attrs= $ldap->fetch()){
379       if (isset($attrs["givenName"][0]) && isset($attrs["sn"][0])){
380         $this->allusers[$attrs["uid"][0]]= $attrs["sn"][0].", ".
381           $attrs["givenName"][0].
382           " [".$attrs["uid"][0]."]";
383       } else {
384         $this->allusers[$attrs["uid"][0]]= $attrs['uid'][0];
385       }
386     }
387     natcasesort ($this->allusers);
388     reset ($this->allusers);
390     /* Fill memberlist */
391     $this->members= array();
392     foreach ($this->memberUid as $value){
393       if (isset($this->allusers[$value])){
394         $this->members[$value]= $this->allusers[$value];
395       } else {
396         $this->members[$value]= "[$value]";
397       }
398     }
399     asort($this->members);
400     reset($this->members);
401   }
405   function remove_from_parent()
406   {
407     plugin::remove_from_parent();
409     $ldap= $this->config->get_ldap_link();
410     $ldap->rmdir($this->dn);
411     show_ldap_error($ldap->get_error());
413     /* Delete references to object groups */
414     $ldap->cd ($this->config->current['BASE']);
415     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
416     while ($ldap->fetch()){
417       $og= new ogroup($this->config, $ldap->getDN());
418       unset($og->member[$this->dn]);
419       $og->save ();
420     }
422     /* Send signal to the world that we've done */
423     $this->handle_post_events("remove");
424   }
427   /* Save data to object */
428   function save_object()
429   {
430     /* Save additional values for possible next step */
431     if (isset($_POST['groupedit'])){
432       plugin::save_object();
434       $this->force_gid= 0;
435       $this->smbgroup= 0;
436       foreach (array("force_gid", "department", "base", "smbgroup") as $val) {
437         if (chkacl ($this->acl, "$val") == "" && isset($_POST["$val"])){
438           $this->$val= $_POST["$val"];
439         }
440       }
442       /* Save sambaDomain attribute */
443       if (chkacl ($this->acl, "sambaDomainName") == "" && $this->samba3 &&
444           isset ($_POST['sambaDomainName'])){
446         $this->sambaDomainName= $_POST['sambaDomainName'];
447         $this->groupType= $_POST['groupType'];
448       }
450       /* Save fon attribute */
451       if (chkacl ($this->acl, "fon_group") == ""){
452         if (isset ($_POST['fon_group'])){
453           $this->fon_group= TRUE;
454         } else {
455           $this->fon_group= FALSE;
456         }
457       }
458          if (chkacl ($this->acl, "nagios_group") == ""){
459         if (isset ($_POST['nagios_group'])){
460           $this->nagios_group= TRUE;
461         } else {
462           $this->nagios_group= FALSE;
463         }
464       }
465     }
466   }
469   /* Save to LDAP */
470   function save()
471   {
472     /* ID handling */
473     if ($this->force_gid == 0){
474       if ($this->saved_gidNumber != ""){
475         $this->gidNumber= $this->saved_gidNumber;
476       } else {
477         /* Calculate new, lock uids */
478         $wait= 10;
479         while (get_lock("uidnumber") != ""){
480           sleep (1);
482           /* timed out? */
483           if ($wait-- == 0){
484             break;
485           }
486         }
487         add_lock ("uidnumber", "gosa");
488         $this->gidNumber= $this->get_next_id("gidNumber");
489       }
490     }
492     plugin::save(); 
494     /* Remove objectClass for samba/phone support */
495     $tmp= array();
496     for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
497       if ($this->attrs['objectClass'][$i] != 'sambaGroupMapping' &&
498           $this->attrs['objectClass'][$i] != 'sambaIdmapEntry' &&
499           $this->attrs['objectClass'][$i] != 'goFonPickupGroup' &&
500          $this->attrs['objectClass'][$i] != 'nagiosContactGroup'){
501         $tmp[]= $this->attrs['objectClass'][$i];
502       }
503     }
504     $this->attrs['objectClass']= $tmp;
505     $ldap= $this->config->get_ldap_link();
507     /* Add samba group functionality */
508     if ($this->samba3 && $this->smbgroup){
509       $this->SID= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
510       $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'];
512       $this->attrs['objectClass'][]= 'sambaGroupMapping';
513       $this->attrs['sambaGroupType']= "2";
515       /* Check if we need to create a special entry */
516       if ($this->groupType == 0){
518         if ($this->sambaSID == "" || $this->oldgroupType != $this->groupType){
519           $gidNumber= $this->gidNumber;
520           while(TRUE){
521             $sid= $this->SID."-".($gidNumber*2 + $this->ridBase+1);
522             $ldap->cd($this->config->current['BASE']);
523             $ldap->search("(sambaSID=$sid)");
524             if ($ldap->count() == 0){
525               break;
526             }
527             $gidNumber++;
528           }
529           $this->attrs['sambaSID']= $sid;
530           $this->sambaSID= $sid;
531         }
533       } else {
534         $this->attrs['sambaSID']=$this->SID."-".$this->groupType;
535       }
537       /* User wants me to fake the idMappings? This is useful for
538          making winbind resolve the group names in a reasonable amount
539          of time in combination with larger databases. */
540       if (isset($this->config->current['SAMBAIDMAPPING']) &&
541           preg_match('/true/i', $this->config->current['SAMBAIDMAPPING'])){
542         $this->attrs['objectClass'][]= "sambaIdmapEntry";
543       }
545     }
547     /* Add phone functionality */
548     if ($this->fon_group){
549       $this->attrs['objectClass'][]= "goFonPickupGroup";
550     }
552     /* Add nagios functionality */
553     if ($this->nagios_group){
554         $this->attrs['objectClass'][]= "nagiosContactGroup";
555     }
557     /* Take members array */
558     if (count ($this->memberUid)){
559       $this->attrs['memberUid']= array_unique($this->memberUid);
560     }
562     /* New accounts need proper 'dn', propagate it to remaining objects */
563     if ($this->dn == 'new'){
564       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
565     }
567     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
568        new entries. So do a check first... */
569     $ldap->cat ($this->dn);
570     if ($ldap->fetch()){
571       /* Modify needs array() to remove values :-( */
572       if (!count ($this->memberUid)){
573         $this->attrs['memberUid']= array();
574       }
575       if ($this->samba3){
576         if (!$this->smbgroup){
577           $this->attrs['sambaGroupType']= array();
578           $this->attrs['sambaSID']= array();
579         }
580       }
581       $mode= "modify";
582     } else {
583       $mode= "add";
584       $ldap->cd($this->config->current['BASE']);
585       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
586     }
588     /* Write back to ldap */
589     $ldap->cd($this->dn);
590     $ldap->$mode($this->attrs);
592     $ret= 0;
593     if (show_ldap_error($ldap->get_error())){
594       $ret= 1;
595     }
597     /* Remove uid lock */
598     del_lock ("uidnumber");
600     /* Post that we've done*/
601     $this->handle_post_events($mode);
603     return ($ret);
604   }
606   function check()
607   {
608     $message= array();
610     /* Permissions for that base? */
611     if ($this->base != ""){
612       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
613     } else {
614       $new_dn= $this->dn;
615     }
617     $ui= get_userinfo();
618     $acl= get_permissions ($new_dn, $ui->subtreeACL);
619     $acl= get_module_permission($acl, "group", $new_dn);
620     if (chkacl($acl, "create") != ""){
621       $message[]= _("You have no permissions to create a group on this 'Base'.");
622     }
624     /* must: cn */
625     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
626       $message[]= "The required field 'Name' is not set.";
627     }
629     /* Check for valid input */
630     if (!is_uid($this->cn)){
631       $message[]= _("The field 'Name' contains invalid characters. Lowercase, numbers and dashes are allowed.");
632     }
634     /* Check for used 'cn' */
635     $ldap= $this->config->get_ldap_link();
636     $ldap->cd($this->config->current['BASE']);
637     $ldap->search("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))");
638     if ($ldap->count() != 0){
640       /* New entry? */
641       if ($this->dn == 'new'){
642         $message[]= _("Value specified as 'Name' is already used.");
643       }
644       
645       /* Moved? */
646       elseif ($new_dn != $this->orig_dn){
647         $ldap->fetch();
648         if ($ldap->getDN() != $this->orig_dn){
649           $message[]= _("Value specified as 'Name' is already used.");
650         }
651       }
652     }
654     /* Check ID */
655     if ($this->force_gid == "1"){
656       if (!is_id($this->gidNumber)){
657         $message[]= _("Value specified as 'GID' is not valid.");
658       } else {
659         if ($this->gidNumber < $this->config->current['MINID']){
660           $message[]= _("Value specified as 'GID' is too small.");
661         }
663       }
664     }
666     return ($message);
667   }
669   function get_next_id($attrib)
670   {
671     $ids= array();
672     $ldap= $this->config->get_ldap_link();
674     $ldap->cd ($this->config->current['BASE']);
675     $ldap->search ("($attrib=*)");
677     /* Get list of ids */
678     while ($attrs= $ldap->fetch()){
679       $ids[]= (int)$attrs["$attrib"][0];
680     }
682     /* Find out next free id near to UID_BASE */
683     for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
684       if (!in_array($id, $ids)){
685         return ($id);
686       }
687     }
689     /* Should not happen */
690     if ($id == 65000){
691       print_red(_("Too many users, can't allocate a free ID!"));
692       exit;
693     }
694   }
698 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
699 ?>