Code

Updated user management
[gosa.git] / gosa-core / plugins / admin / users / class_userManagement.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class userManagement extends management
24 {
25   var $plHeadline     = "Users";
26   var $plDescription  = "Manage users";
27   var $plIcon  = "plugins/users/images/user.png";
29   var $sn = "";
30   var $givenName = "";
31   var $uid = "";
32   var $got_uid = "";
33   var $edit_uid = "";
35   var $pwd_change_queue = array();
36   var $force_hash_type = array();
38   // Tab definition 
39   protected $tabClass = "usertabs";
40   protected $tabType = "USERTABS";
41   protected $aclCategory = "users";
42   protected $aclPlugin   = "user";
43   protected $objectName   = "user";
45   protected $proposal = "";
46   protected $proposalEnabled = FALSE;
47   protected $proposalSelected = FALSE;
49   protected $passwordChangeForceable = FALSE;
50   protected $enforcePasswordChange = FALSE;
53   function __construct($config,$ui)
54   {
55     $this->config = $config;
56     $this->ui = $ui;
57    
58     $this->storagePoints = array(get_ou("userRDN"));
59  
60     // Build filter
61     if (session::global_is_set(get_class($this)."_filter")){
62       $filter= session::global_get(get_class($this)."_filter");
63     } else {
64       $filter = new filter(get_template_path("user-filter.xml", true));
65       $filter->setObjectStorage($this->storagePoints);
66     }
67     $this->setFilter($filter);
69     // Build headpage
70     $headpage = new listing(get_template_path("user-list.xml", true));
71     $headpage->registerElementFilter("lockLabel", "userManagement::filterLockLabel");
72     $headpage->registerElementFilter("lockImage", "userManagement::filterLockImage");
73     $headpage->registerElementFilter("filterProperties", "userManagement::filterProperties");
74     $headpage->setFilter($filter);
76     // Add copy&paste and snapshot handler.
77     if ($this->config->boolValueIsTrue("main", "copyPaste")){
78       $this->cpHandler = new CopyPasteHandler($this->config);
79     }
80     if($this->config->get_cfg_value("enableSnapshots") == "true"){
81       $this->snapHandler = new SnapshotHandler($this->config);
82     }
84     parent::__construct($config, $ui, "users", $headpage);
86     // Register special user actions 
87     $this->registerAction("lock",   "lockEntry");
88     $this->registerAction("lockUsers",   "lockUsers");
89     $this->registerAction("unlockUsers", "lockUsers");
90     $this->registerAction("new_template", "newTemplate");
91     $this->registerAction("newfromtpl", "newUserFromTemplate");
92     $this->registerAction("templateContinue", "templateContinue");
93     $this->registerAction("templatize", "templatizeUsers");
94     $this->registerAction("templatizeContinue", "templatizeContinue");
96     $this->registerAction("password", "changePassword");
97     $this->registerAction("passwordQueue", "handlePasswordQueue");
98     $this->registerAction("passwordCancel", "closeDialogs");
100     $this->registerAction("sendMessage", "sendMessage");
101     $this->registerAction("saveEventDialog", "saveEventDialog");
102     $this->registerAction("abortEventDialog", "closeDialogs");
104     // Register shortcut icon actions 
105     $this->registerAction("edit_user","editEntry");
106     $this->registerAction("edit_posixAccount","editEntry");
107     $this->registerAction("edit_mailAccount","editEntry");
108     $this->registerAction("edit_sambaAccount","editEntry");
109     $this->registerAction("edit_netatalk","editEntry");
110     $this->registerAction("edit_environment","editEntry");
111     $this->registerAction("edit_gofaxAccount","editEntry");
112     $this->registerAction("edit_phoneAccount","editEntry");
113   }
116   function refreshProposal()
117   {
118     $this->proposal = passwordMethod::getPasswordProposal($this->config);
119     $this->proposalEnabled = (!empty($this->proposal));
120   }
123   // Inject user actions 
124   function detectPostActions()
125   {
126     $action = management::detectPostActions();
127     if(isset($_POST['template_continue'])) $action['action'] = "templateContinue";
128     if(isset($_POST['templatize_continue'])) $action['action'] = "templatizeContinue";
129     if(isset($_POST['save_event_dialog'])) $action['action'] = "saveEventDialog";
130     if(isset($_POST['abort_event_dialog'])) $action['action'] = "abortEventDialog";
131     if(isset($_POST['password_cancel'])){
132       $action['action'] = "passwordCancel";
133     }elseif((count($this->pwd_change_queue) || isset($_POST['password_finish']) || isset($_POST['refreshProposal']))){
134       $action['action'] = "passwordQueue";
135     }
136     return($action);
137   }
140   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
141   {
142     $str = management::editEntry($action,$target);
143     if($str) return($str);
145     if(preg_match("/^edit_/",$action)){
146       $tab = preg_replace("/^edit_/","",$action); 
147       if(isset($this->tabObject->by_object[$tab])){
148         $this->tabObject->current = $tab;
149       }else{
150         trigger_error("Unknown tab: ".$tab);
151       }
152     }
153   }
155   
156   function closeDialogs()
157   {
158     management::closeDialogs();
159     $this->pwd_change_queue = array();
160   }
161     
163   /*! \brief  Sends a message to a set of users using gosa-si events.
164    */ 
165   function sendMessage($action="",$target=array(),$all=array())
166   {
167     if(class_available("DaemonEvent")){
168       $uids = array();
169       $ldap = $this->config->get_ldap_link();
170       $ldap->cd($this->config->current['BASE']);
171       foreach($target as $dn){
172         $ldap->cat($dn,array('uid'));
173         $attrs = $ldap->fetch();
174         if(isset($attrs['uid'][0])){
175           $uids[] = $attrs['uid'][0];
176         }
177       }
178       if(count($uids)){
179         $events = DaemonEvent::get_event_types(USER_EVENT);
180         $event = "DaemonEvent_notify";
181         if(isset($events['BY_CLASS'][$event])){
182           $type = $events['BY_CLASS'][$event];
183           $this->dialogObject = new $type['CLASS_NAME']($this->config);
184           $this->dialogObject->add_users($uids);
185           $this->dialogObject->set_type(SCHEDULED_EVENT);
186         }
187       }
188     }
189   }
192   /*! \brief  Sends a message to a set of users using gosa-si events.
193    */ 
194   function saveEventDialog()
195   {
196     $this->dialogObject->save_object();
197     $msgs = $this->dialogObject->check();
198     if(count($msgs)){
199       msg_dialog::displayChecks($msgs);
200     }else{
201       $o_queue = new gosaSupportDaemon();
202       $o_queue->append($this->dialogObject);
203       if($o_queue->is_error()){
204         msg_dialog::display(_("Infrastructure error"), msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
205       }
206       $this->closeDialogs();
207     }
208   }
211   /*! \brief  Intiates template creation. 
212    */ 
213   function newTemplate($action,$entry)
214   {
215     $this->newEntry();
216     $this->tabObject->set_template_mode ();
217   }
220   /*! \brief  Queues a set of users for password changes
221    */ 
222   function changePassword($action="",$target=array(),$all=array())
223   {
224     $this->dn ="";
225     $this->pwd_change_queue = $target;
227     // Check permisions
228     $disallowed = array();
229     foreach($this->pwd_change_queue as $key => $dn){
230       if(!preg_match("/w/",$this->ui->get_permissions($dn,$this->aclCategory."/password"))){
231         unset($this->pwd_change_queue[$key]);
232         $disallowed[] = $dn; 
233       }
234     }
235     if(count($disallowed)){
236       msg_dialog::display(_("Permission"),msgPool::permModify($disallowed),INFO_DIALOG);
237     }
239     // Now display change dialog.
240     return($this->handlePasswordQueue()); 
241   }
244   function handlePasswordQueue()
245   {
246       // skip if nothing is to do
247       if(empty($this->dn) && !count($this->pwd_change_queue)) return;
249       // Refresh proposal if requested
250       if(isset($_POST['refreshProposal'])) $this->refreshProposal();
251       if(isset($_POST['proposalSelected'])) $this->proposalSelected = get_post('proposalSelected') == 1;
253       $this->enforcePasswordChange = isset($_POST['new_password']) && isset($_POST['enforcePasswordChange']);
255       $smarty = get_smarty();
256       $smarty->assign("proposal" , $this->proposal);
257       $smarty->assign("proposalEnabled" , $this->proposalEnabled);
258       $smarty->assign("proposalSelected" , $this->proposalSelected);
260       $smarty->assign("passwordChangeForceable" , $this->passwordChangeForceable);
261       $smarty->assign("enforcePasswordChange" , $this->enforcePasswordChange);
263       // Get next entry from queue.
264       if(empty($this->dn) && count($this->pwd_change_queue)){
266           // Generate new proposal
267           $this->refreshProposal();
268           $this->proposalSelected = ($this->proposal != "");
269           $this->dn = array_pop($this->pwd_change_queue);
271           // Check if we are able to enforce a password change
272           $ldap = $this->config->get_ldap_link();
273           $ldap->cd($this->config->current['BASE']);
274           $ldap->cat($this->dn);
275           $attrs = $ldap->fetch();
276           $this->passwordChangeForceable =
277               in_array('sambaAccount', $attrs['objectClass']) ||
278               (in_array('posixAccount', $attrs['objectClass']) && isset($attrs['shadowMax']));
279           $smarty->assign("passwordChangeForceable" , $this->passwordChangeForceable);
280           $smarty->assign("enforcePasswordChange" , $this->enforcePasswordChange);
282           // Assign proposal variables
283           $smarty->assign("proposal" , $this->proposal);
284           $smarty->assign("proposalEnabled" , $this->proposalEnabled);
285           $smarty->assign("proposalSelected" , $this->proposalSelected);
287           set_object_info($this->dn);
288           return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
289       }
291       // If we've just refreshed the proposal then do not check the password for validity.
292       if(isset($_POST['refreshProposal'])){
293           return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
294       }
296       // Check permissions
297       if(isset($_POST['password_finish'])){
299           $dn  = $this->dn;
300           $acl = $this->ui->get_permissions($dn, "users/password");
301           $cacl= $this->ui->get_permissions($dn, "users/user");
302           if (preg_match('/w/', $acl) || preg_match('/c/', $cacl)){
304               // Get posted passwords
305               if($this->proposalSelected){
306                   $new_password = $this->proposal;
307                   $repeated_password = $this->proposal;
308               }else{
309                   $new_password = get_post('new_password');
310                   $repeated_password = get_post('repeated_password');
311               }
313               // Check posted passwords now.
314               $message= array();
315               if ($new_password != $repeated_password){
316                   $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
317               } else {
318                   if ($new_password == ""){
319                       $message[] = msgPool::required(_("New password"));
320                   }
321               }
323               // Display errors
324               if (count($message) != 0){
325                   msg_dialog::displayChecks($message);
326                   return($smarty->fetch(get_template_path('password.tpl', TRUE)));
327               }
329               // Change cassword
330               if(isset($this->force_hash_type[$this->dn])){
331                   if(!change_password ($this->dn, $new_password,0,$this->force_hash_type[$this->dn])){
332                       return($smarty->fetch(get_template_path('password.tpl', TRUE)));
333                   }
334               }else{
335                   if(!change_password ($this->dn, $new_password)){
336                       return($smarty->fetch(get_template_path('password.tpl', TRUE)));
337                   }
338               }
339               if ($this->config->get_cfg_value("passwordHook") != ""){
340                   $ldap = $this->config->get_ldap_link();
341                   $ldap->cd($this->config->current['BASE']);
342                   $ldap->cat($this->dn,array('uid'));
343                   $attrs = $ldap->fetch();
344                   exec($this->config->get_cfg_value("passwordHook")." ".$attrs['uid'][0]." ".$_POST['new_password'], $resarr);
345               }
347               // The user has to change his password on next login
348               // - We are going to update samba and posix attributes here, to enforce
349               //   such a password change.
350               if($this->passwordChangeForceable && $this->enforcePasswordChange){
352                   // Check if we are able to enforce a password change
353                   $ldap = $this->config->get_ldap_link();
354                   $ldap->cd($this->config->current['BASE']);
355                   $ldap->cat($this->dn);
356                   $attrs = $ldap->fetch();
357                   $samba = in_array('sambaSamAccount', $attrs['objectClass']);
358                   $posix = in_array('posixAccount', $attrs['objectClass']);
360                   // Update the posix shadow flag...
361                   if($posix){
362                       $current= floor(date("U") /60 /60 /24);
363                       $enforceDate = $current -  $attrs['shadowMax'][0];
364                       $new_attrs = array();
365                       $new_attrs['shadowLastChange'] = $enforceDate;
366                       $ldap->cd($this->dn);
367                       $ldap->modify($new_attrs);
369 #                     $posixAccount = new posixAccount($this->config, $this->dn);
370 #                     $posixAccount->is_modified=TRUE;
371 #                     $posixAccount->activate_shadowExpire=1;
372 #                     $posixAccount->shadowExpire = date('d.m.Y', time() - (1 * 24 * 60 *60));
373 #                     $posixAccount->save();
374                   }
376                   // Update the samba kickoff flag...
377                   if($samba){
378                       $sambaAccount = new sambaAccount($this->config, $this->dn);
379                       $sambaAccount->is_modified=TRUE;
380                       $sambaAccount->enforcePasswordChange=TRUE;
381                       $sambaAccount->cannotChangePassword=FALSE;
382                       $sambaAccount->sambaPwdCanChange=0;
383                       $sambaAccount->sambaAcctFlags = preg_replace("/X/i","",$sambaAccount->sambaAcctFlags);
384                       $sambaAccount->save();
385                   }
386               }
388               new log("modify","users/".get_class($this),$this->dn,array(),"Password has been changed");
389               $this->dn ="";
391           } else {
392               msg_dialog::display(_("Password change"),
393                       _("You have no permission to change this users password!"),
394                       WARNING_DIALOG);
395           }
396       }
397       // Cleanup
398       if(!count($this->pwd_change_queue) && $this->dn=""){
399           $this->remove_lock();
400           $this->closeDialogs();
401       }else{
402           return($this->handlePasswordQueue());
403       }
404   }
409   /*! \brief  Save user modifications. 
410    *          Whenever we save a 'new' user, request a password change for him.
411    */ 
412   function saveChanges()
413   {
414     $str = management::saveChanges();
415   
416     if(!empty($str)) return($str);
418     if($this->last_tabObject instanceOf multi_plug){
419       foreach($this->last_tabObject->a_handles as $user){
420         if($user->password_change_needed()){
421           $this->force_hash_type[$user->dn] = $user->by_object['user']->pw_storage;
422           $this->pwd_change_queue[] = $user->dn;
423         }
424       }
425       return($this->handlePasswordQueue());
426     }
428     if(isset($this->last_tabObject->by_object['user']) && $this->last_tabObject->by_object['user']->password_change_needed()){
429       $this->force_hash_type[$this->last_tabObject->dn] = $this->last_tabObject->by_object['user']->pw_storage;
430       $this->pwd_change_queue[] = $this->last_tabObject->dn;
431       return($this->handlePasswordQueue());
432     }
433   }
435   function cancelEdit()
436   {
437     $str = management::cancelEdit();
438     if(!empty($str)) return($str);
440     if(isset($this->last_tabObject->by_object['user']) && 
441         $this->last_tabObject->by_object['user']->dn != "new" &&  
442         $this->last_tabObject->by_object['user']->password_change_needed()){
443       $this->force_hash_type[$this->last_tabObject->dn] = $this->last_tabObject->by_object['user']->pw_storage;
444       $this->pwd_change_queue[] = $this->last_tabObject->dn;
445       return($this->handlePasswordQueue());
446     }
447   }
450   /*! \brief  Intiates user creation. 
451    *          If we've user templates, then the user will be asked to use to use one. 
452    *          -> See 'templateContinue' for further handling.
453    */ 
454   function newUserFromTemplate($action="",$target=array(),$all=array())
455   {
456     // Call parent method, it knows whats to do, locking and so on ...
457     $str = management::newEntry($action,$target,$all);
458     if(!empty($str)) return($str);
460     // Reset uid selection.
461     $this->got_uid= "";
463     // Use template if there are any of them 
464     $templates = array();
465     $templates['none']= _("none");
466     $templates = array_merge($templates,$this->get_templates());
468     // We've templates, so preset the current template and display the input dialog.
469     if (count($templates)){
470       $smarty = get_smarty();
471       foreach(array("sn", "givenName", "uid", "got_uid") as $attr){
472         $smarty->assign("$attr", "");
473       }
474       $smarty->assign("template",  array_pop($target));
475       $smarty->assign("templates", $templates);
476       $smarty->assign("edit_uid", "");
477       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
479       // -> See 'templateContinue' for further handling!
480     }
481   }
485   /*! \brief  Intiates user creation. 
486    *          If we've user templates, then the user will be asked 
487    *           if he wants to use one. 
488    *          -> See 'templateContinue' for further handling.
489    */ 
490   function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
491   {
492   
493     // Call parent method, it manages everything, locking, object creation...
494     $str = management::newEntry($action,$target,$all);
495     if(!empty($str)) return($str);
496     
497     // If we've at least one template, then ask the user if he wants to use one?
498     $templates = array();
499     $templates['none']= _("none");
500     $templates = array_merge($templates,$this->get_templates());
502     // Display template selection
503     if (count($templates) > 1){
504       $smarty = get_smarty();
505   
506       // Set default variables, normally empty.
507       foreach(array("sn", "givenName", "uid", "got_uid") as $attr){
508         $smarty->assign($attr, "");
509       }
510       $smarty->assign("template", "none");
511       $smarty->assign("templates", $templates);
512       $smarty->assign("edit_uid", "");
513       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
515       // -> See 'templateContinue' for further handling!
516     }
517   }
520   /* !\brief  This method is called whenever a template selection was displayed.
521    *          Here we act on the use selection. 
522    *          - Does the user want to create a user from template?
523    *          - Create user without template?
524    *          - Input correct, every value given and valid? 
525    */ 
526   function templateContinue()
527   {
528     // Get the list of available templates.
529     $templates = array();
530     $templates['none']= _("none");
531     $templates = array_merge($templates,$this->get_templates());
533     // Input validation, if someone wants to create a user from a template
534     //  then validate the given values.
535     $message = array();
536     if(!isset($_POST['template']) || (empty($_POST['template']))){
537       $message[]= msgPool::invalid(_("Template"));
538     }
539     if(!isset($_POST['sn']) || (empty($_POST['sn']))){
540       $message[]= msgPool::required(_("Name"));
541     }
542     if(!isset($_POST['givenName']) || (empty($_POST['givenName']))){
543       $message[]= msgPool::required(_("Given name"));
544     }
546     /********************
547      * 1   We've had input errors - Display errors and show input dialog again. 
548      ********************/
550     if (count($message) > 0){
551       msg_dialog::displayChecks($message);
553       // Preset input fields with user input. 
554       $smarty = get_smarty();
555       foreach(array("sn", "givenName", "uid", "template") as $attr){
556         if(isset($_POST[$attr])){
557           $smarty->assign("$attr", get_post($attr));
558         }else{
559           $smarty->assign("$attr", "");
560         }
561       }
563       $smarty->assign("templates",$templates);
564       $smarty->assign("got_uid", $this->got_uid);
565       $smarty->assign("edit_uid",false);
566       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
567     }
570     /********************
571      * 2   There was a template selected, now ask for the uid.
572      ********************/
574     if ($_POST['template'] != 'none' && !isset($_POST['uid'])){
576       // Remember user input.
577       $smarty = get_smarty();
578       $this->sn             = $_POST['sn'];
579       $this->givenName      = $_POST['givenName'];
581       // Avoid duplicate entries, check if such a user already exists.
582       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
583       $ldap= $this->config->get_ldap_link();
584       $ldap->cd ($dn);
585       $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
586       if ($ldap->count () != 0){
587         msg_dialog::displayChecks(array(msgPool::duplicated(_("Name"))));
588       }else{
590         // Preset uid field by using the idGenerator 
591         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
592         if ($this->config->get_cfg_value("idGenerator") != ""){
593           $uids= gen_uids ($this->config->get_cfg_value("idGenerator"), $attributes);
594           if (count($uids)){
595             $smarty->assign("edit_uid", "false");
596             $smarty->assign("uids", $uids);
597             $this->uid= current($uids);
598           }
599         } else {
600           $smarty->assign("edit_uid", "");
601           $this->uid= "";
602         }
603         $this->got_uid= true;
604       }
606       // Assign user input 
607       foreach(array("sn", "givenName", "uid", "got_uid") as $attr){
608         $smarty->assign("$attr", $this->$attr);
609       }
610       if (isset($_POST['template'])){
611         $smarty->assign("template", $_POST['template']);
612       }
613       $smarty->assign("templates",$templates); 
614       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
615     }
618     /********************
619      * 3   No template - Ok. Lets fill the data into the user object and skip templating here. 
620      ********************/
621     if ($_POST['template'] == 'none'){
622       foreach(array("sn", "givenName", "uid") as $attr){
623         if (isset($_POST[$attr])){
624           $this->tabObject->by_object['user']->$attr= $_POST[$attr];
625         }
626       }
627       
628       // The user Tab object is already instantiated, so just go back and let the 
629       //  management class do the rest.
630       return("");
631     }
634     /********************
635      * 4   Template selected and uid given - Ok, then lets adapt tempalte values. 
636      ********************/
637     if(isset($_POST['uid'])){
639       // Move user supplied data to sub plugins 
640       foreach(array("uid","sn","givenName") as $attr){
641         $this->$attr = $_POST[$attr];
642         $this->tabObject->$attr       = $this->$attr;
643         $this->tabObject->by_object['user']->$attr = $this->$attr;
644       }
646       // Adapt template values.
647       $template_dn              = $_POST['template'];
648       $this->tabObject->adapt_from_template($template_dn, array("uid","cn","givenName","sn"));
649       $template_base            = preg_replace("/^[^,]+,".preg_quote(get_people_ou(), '/i')."/", '', $template_dn);
650       $this->tabObject->by_object['user']->base= $template_base;
652       // The user Tab object is already instantiated, so just go back and let the 
653       //  management class do the rest.
654       return("");
655     }
656   }
659   /* !\brief  This method applies a template to a set of users.
660    */ 
661   function templatizeUsers($action="",$target=array(),$all=array())
662   {
663     $this->dns = array();
664     if(count($target)){
666       // Get the list of available templates.
667       $templates = $this->get_templates();
669       // Check entry locking
670       foreach($target as $dn){
671         if (($user= get_lock($dn)) != ""){
672           $this->dn = $dn;
673           return(gen_locked_message ($user, $dn));
674         }
675         $this->dns[] = $dn;
676       }
677           
678       // Display template
679       $smarty = get_smarty();
680       $smarty->assign("templates", $templates);
681       return($smarty->fetch(get_template_path('templatize.tpl', TRUE)));
682     }
683   }
686   /* !\brief  This method is called whenever the templatize dialog was used.
687    */ 
688   function templatizeContinue()
689   {
690     // Template readable? 
691     $template= get_post('template');
692     $acl = $this->ui->get_permissions($template, $this->aclCategory."/".$this->aclPlugin);
693     if (preg_match('/r/', $acl)){
694       $tab = $this->tabClass;
695       foreach ($this->dns as $dn){
697         // User writeable
698         $acl = $this->ui->get_permissions($dn,  $this->aclCategory."/".$this->aclPlugin);
699         if (preg_match('/w/', $acl)){
700           $this->tabObject= new $tab($this->config, $this->config->data['TABS'][$this->tabType], $dn, $this->aclCategory);
701           $this->tabObject->adapt_from_template($template, array("sn", "givenName", "uid"));
702           $this->tabObject->save();
703         } else {
704           msg_dialog::display(_("Permission error"), msgPool::permModify($dn), ERROR_DIALOG);
705         }
706       }
707     } else {
708       msg_dialog::display(_("Permission error"), msgPool::permView($template), ERROR_DIALOG);
709     }
711     // Cleanup!
712     $this->remove_lock(); 
713     $this->closeDialogs();
714   }
717   /* !\brief  Lock/unlock multiple users.
718    */ 
719   function lockUsers($action,$target,$all)
720   {
721     if(!count($target)) return;
722     if($action == "lockUsers"){
723       $this->lockEntry($action,$target, $all, "lock");
724     }else{
725       $this->lockEntry($action,$target, $all, "unlock");
726     }
727   }
729   
730   /* !\brief  Locks/unlocks the given user(s).
731    */ 
732   function lockEntry($action,$entry, $all, $type = "toggle")
733   {
734     
735     // Filter out entries we are not allowed to modify
736     $disallowed = array();
737     $dns = array();
738     foreach($entry as $dn){
739       if (!preg_match("/w/",$this->ui->get_permissions($dn,"users/password"))){
740         $disallowed[] = $dn;
741       }else{
742         $allowed[] = $dn;
743       }
744     }
745     if(count($disallowed)){
746       msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
747     }
749     // Try to lock/unlock the rest of the entries.
750     $ldap = $this->config->get_ldap_link();
751     foreach($allowed as $dn){
752       $ldap->cat($dn, array('userPassword'));
753       if($ldap->count() == 1){
755         // We can't lock empty passwords.
756         $val = $ldap->fetch();
757         if(!isset($val['userPassword'])){
758           continue;
759         }
761         // Detect the password method and try to lock/unlock.
762         $pwd = $val['userPassword'][0];
763         $method = passwordMethod::get_method($pwd,$val['dn']);
764         $success= true;
765         if($method instanceOf passwordMethod){
766           if($type == "toggle"){
767             if($method->is_locked($this->config,$val['dn'])){
768               $success= $method->unlock_account($this->config,$val['dn']);
769             }else{
770               $success= $method->lock_account($this->config,$val['dn']);
771             }
772           }elseif($type == "lock" && !$method->is_locked($this->config,$val['dn'])){
773             $success= $method->lock_account($this->config,$val['dn']);
774           }elseif($type == "unlock" && $method->is_locked($this->config,$val['dn'])){
775             $success= $method->unlock_account($this->config,$val['dn']);
776           }
778           // Check if everything went fine.
779           if (!$success){
780             $hn= $method->get_hash_name();
781             if (is_array($hn)){
782               $hn= $hn[0];
783             }
784             msg_dialog::display(_("Account locking"),
785                 sprintf(_("Password method '%s' does not support locking. Account (%s) has not been locked!"), 
786                   $hn,$dn),WARNING_DIALOG);
787           }
788         }else{
789           // Can't lock unknown methods.
790         }
791       }
792     }
793   }
796   /* !\brief  This method returns a list of all available templates.
797    */ 
798   function get_templates()
799   {
800     $templates= array();
801     $ldap= $this->config->get_ldap_link();
802     foreach ($this->config->departments as $key => $value){
803       $acl = $this->ui->get_permissions($value,$this->aclCategory."/".$this->aclPlugin);
804       if (preg_match("/c/",$acl)){
806         // Search all templates from the current dn.
807         $ldap->cd (get_people_ou().$value);
808         $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
809         if ($ldap->count() != 0){
810           while ($attrs= $ldap->fetch()){
811             $templates[$ldap->getDN()]= $attrs['uid'][0]." - ".LDAP::fix($key);
812           }
813         }
814       }
815     }
816     natcasesort ($templates);
817     reset ($templates);
818     return($templates);
819   }
822   function copyPasteHandler($action="",$target=array(),$all=array(),
823       $altTabClass ="", $altTabType = "", $altAclCategory="",$altAclPlugin="")
824   {
825     if ($this->config->boolValueIsTrue("main", "copyPaste")){
826       $this->cpHandler->lastdn = "";
827       $str = management::copyPasteHandler($action,$target,$all);
828       if($this->cpHandler->lastdn != "" && isset($_POST['passwordTodo']) && $_POST['passwordTodo'] == "new"){
829         $this->pwd_change_queue[] = $this->cpHandler->lastdn;
830         return($this->handlePasswordQueue());
831       }
832       return($str);
833     }
835     return "";
836   }
839   static function filterLockImage($userPassword)
840   {
841     $image= "images/empty.png";
842     if(isset($userPassword[0]) && preg_match("/^\{[^\}]/",$userPassword[0])){
843       if(preg_match("/^[^\}]*+\}!/",$userPassword[0])){
844         $image= "images/lists/locked.png";
845       }else{
846         $image= "images/lists/unlocked.png";
847       }
848     }
849     return $image;
850   }
853   static function filterLockLabel($userPassword)
854   {
855     $label= "";
856     if(isset($userPassword[0]) && preg_match("/^\{[^\}]/",$userPassword[0])){
857       if(preg_match("/^[^\}]*+\}!/",$userPassword[0])){
858         $label= _("Unlock account");
859       }else{
860         $label= _("Lock account");
861       }
862     }
863     return $label;
864   }
867   static function filterProperties($row, $classes)
868   {
869     $result= "";
870     $map= array( "gosaAccount" => array( "image" => "plugins/users/images/select_user.png",
871                                          "plugin" => "user",
872                                          "alt" => _("Generic"),
873                                          "title" => _("Edit generic properties")),
874                  "posixAccount" => array("image" => "images/penguin.png",
875                                          "plugin" => "posixAccount",
876                                          "alt" => _("POSIX"),
877                                          "title" => _("Edit POSIX properties")),
878                  "gosaMailAccount" => array("image" => "images/mailto.png",
879                                          "alt" => _("Mail"),
880                                          "plugin" => "mailAccount",
881                                          "title" => _("Edit mail properties")),
882                  "sambaSamAccount" => array("image" => "plugins/systems/images/select_winstation.png",
883                                          "plugin" => "sambaAccount",
884                                          "alt" => _("Samba"),
885                                          "title" => _("Edit samba properties")),
886                  "apple-user" => array("image" => "plugins/netatalk/images/select_netatalk.png",
887                                          "plugin" => "netatalk",
888                                          "alt" => _("Netatalk"),
889                                          "title" => _("Edit netatalk properties")),
890                  "gotoEnvironment" => array("image" => "plugins/users/images/small_environment.png",
891                                          "plugin" => "environment",
892                                          "alt" => _("Environment"),
893                                          "title" => _("Edit environment properties")),
894                  "goFaxAccount" => array("image" => "plugins/users/images/fax_small.png",
895                                          "plugin" => "gofaxAccount",
896                                          "alt" => _("FAX"),
897                                          "title" => _("Edit FAX properties")),
898                  "goFonAccount" => array("image" => "plugins/gofon/images/select_phone.png",
899                                          "plugin" => "phoneAccount",
900                                          "alt" => _("Phone"),
901                                          "title" => _("Edit phone properties")));
903     // Walk thru map
904     foreach ($map as $oc => $properties) {
905       if (in_array_ics($oc, $classes)) {
906         $result.="<input class='center' type='image' src='".$properties['image']."' ".
907                  "alt='".$properties['alt']."' title='".$properties['title'].
908                  "' name='listing_edit_".$properties['plugin']."_$row' style='padding:1px'>";
909       } else {
910         $result.="<img src='images/empty.png' alt=' ' class='center' style='padding:1px'>";
911       }
912     }
913     return $result;
914   }
916
917 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
918 ?>