Code

Updated userManagement - > password change
[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 aspects of user accounts like generic, POSIX, samba and mail settings";
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;
52     function __construct($config,$ui)
53     {
54         $this->config = $config;
55         $this->ui = $ui;
57         $this->storagePoints = array(get_ou("core", "userRDN"));
59         // Build filter
60         if (session::global_is_set(get_class($this)."_filter")){
61             $filter= session::global_get(get_class($this)."_filter");
62         } else {
63             $filter = new filter(get_template_path("user-filter.xml", true));
64             $filter->setObjectStorage($this->storagePoints);
65         }
66         $this->setFilter($filter);
68         // Build headpage
69         $headpage = new listing(get_template_path("user-list.xml", true));
70         $headpage->registerElementFilter("lockLabel", "userManagement::filterLockLabel");
71         $headpage->registerElementFilter("lockImage", "userManagement::filterLockImage");
72         $headpage->registerElementFilter("filterProperties", "userManagement::filterProperties");
73         $headpage->setFilter($filter);
75         // Add copy&paste and snapshot handler.
76         if ($this->config->boolValueIsTrue("core", "copyPaste")){
77             $this->cpHandler = new CopyPasteHandler($this->config);
78         }
79         if($this->config->get_cfg_value("core","enableSnapshots") == "true"){
80             $this->snapHandler = new SnapshotHandler($this->config);
81         }
83         parent::__construct($config, $ui, "users", $headpage);
85         // Register special user actions 
86         $this->registerAction("lock",   "lockEntry");
87         $this->registerAction("lockUsers",   "lockUsers");
88         $this->registerAction("unlockUsers", "lockUsers");
89         $this->registerAction("new_template", "newTemplate");
90         $this->registerAction("newfromtpl", "newUserFromTemplate");
91         $this->registerAction("templateContinue", "templateContinue");
92         $this->registerAction("templatize", "templatizeUsers");
93         $this->registerAction("templatizeContinue", "templatizeContinue");
95         $this->registerAction("password", "changePassword");
96         $this->registerAction("passwordQueue", "handlePasswordQueue");
97         $this->registerAction("passwordCancel", "closeDialogs");
99         $this->registerAction("sendMessage", "sendMessage");
100         $this->registerAction("saveEventDialog", "saveEventDialog");
101         $this->registerAction("abortEventDialog", "closeDialogs");
103         // Register shortcut icon actions 
104         $this->registerAction("edit_user","editEntry");
105         $this->registerAction("edit_posixAccount","editEntry");
106         $this->registerAction("edit_mailAccount","editEntry");
107         $this->registerAction("edit_sambaAccount","editEntry");
108         $this->registerAction("edit_netatalk","editEntry");
109         $this->registerAction("edit_environment","editEntry");
110         $this->registerAction("edit_gofaxAccount","editEntry");
111         $this->registerAction("edit_phoneAccount","editEntry");
112     }
115     // Inject user actions 
116     function detectPostActions()
117     {
118         $action = management::detectPostActions();
119         if(isset($_POST['template_continue'])) $action['action'] = "templateContinue";
120         if(isset($_POST['templatize_continue'])) $action['action'] = "templatizeContinue";
121         if(isset($_POST['save_event_dialog'])) $action['action'] = "saveEventDialog";
122         if(isset($_POST['abort_event_dialog'])) $action['action'] = "abortEventDialog";
123         if(isset($_POST['password_cancel'])){
124             $action['action'] = "passwordCancel";
125         }elseif((count($this->pwd_change_queue) || isset($_POST['password_finish']) || isset($_POST['refreshProposal']))){
126             $action['action'] = "passwordQueue";
127         }
128         return($action);
129     }
132     function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
133     {
134         $str = management::editEntry($action,$target);
135         if($str) return($str);
137         if(preg_match("/^edit_/",$action)){
138             $tab = preg_replace("/^edit_/","",$action); 
139             if(isset($this->tabObject->by_object[$tab])){
140                 $this->tabObject->current = $tab;
141             }else{
142                 trigger_error("Unknown tab: ".$tab);
143             }
144         }
145     }
148     function closeDialogs()
149     {
150         management::closeDialogs();
151         $this->pwd_change_queue = array();
152     }
155     /*! \brief  Sends a message to a set of users using gosa-si events.
156      */ 
157     function sendMessage($action="",$target=array(),$all=array())
158     {
159         if(class_available("DaemonEvent")){
160             $uids = array();
161             $ldap = $this->config->get_ldap_link();
162             $ldap->cd($this->config->current['BASE']);
163             foreach($target as $dn){
164                 $ldap->cat($dn,array('uid'));
165                 $attrs = $ldap->fetch();
166                 if(isset($attrs['uid'][0])){
167                     $uids[] = $attrs['uid'][0];
168                 }
169             }
170             if(count($uids)){
171                 $events = DaemonEvent::get_event_types(USER_EVENT);
172                 $event = "DaemonEvent_notify";
173                 if(isset($events['BY_CLASS'][$event])){
174                     $type = $events['BY_CLASS'][$event];
175                     $this->dialogObject = new $type['CLASS_NAME']($this->config);
176                     $this->dialogObject->add_users($uids);
177                     $this->dialogObject->set_type(SCHEDULED_EVENT);
178                 }
179             }
180         }
181     }
184     /*! \brief  Sends a message to a set of users using gosa-si events.
185      */ 
186     function saveEventDialog()
187     {
188         $this->dialogObject->save_object();
189         $msgs = $this->dialogObject->check();
190         if(count($msgs)){
191             msg_dialog::displayChecks($msgs);
192         }else{
193             $o_queue = new gosaSupportDaemon();
194             $o_queue->append($this->dialogObject);
195             if($o_queue->is_error()){
196                 msg_dialog::display(_("Infrastructure error"), msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
197             }
198             $this->closeDialogs();
199         }
200     }
203     /*! \brief  Intiates template creation. 
204      */ 
205     function newTemplate($action,$entry)
206     {
207         $this->newEntry();
208         $this->tabObject->set_template_mode ();
209     }
212     /*! \brief  Queues a set of users for password changes
213      */ 
214     function changePassword($action="",$target=array(),$all=array())
215     {
216         $this->dn ="";
217         $this->pwd_change_queue = $target;
219         // Check permisions
220         $disallowed = array();
221         foreach($this->pwd_change_queue as $key => $dn){
222             if(!preg_match("/w/",$this->ui->get_permissions($dn,$this->aclCategory."/password"))){
223                 unset($this->pwd_change_queue[$key]);
224                 $disallowed[] = $dn; 
225             }
226         }
227         if(count($disallowed)){
228             msg_dialog::display(_("Permission"),msgPool::permModify($disallowed),INFO_DIALOG);
229         }
231         // Now display change dialog.
232         return($this->handlePasswordQueue()); 
233     }
237     function refreshProposal()
238     {
239         $this->proposal = passwordMethod::getPasswordProposal($this->config);
240         $this->proposalEnabled = (!empty($this->proposal));
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']);
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("core","passwordHook") != ""){
340                     exec($this->config->get_cfg_value("core","passwordHook")." ".$username." ".$new_password, $resarr);
341                 }
342         
343                 // The user has to change his password on next login
344                 // - We are going to update samba and posix attributes here, to enforce
345                 //   such a password change.
346                 if($this->passwordChangeForceable && $this->enforcePasswordChange){
348                     // Check if we are able to enforce a password change
349                     $ldap = $this->config->get_ldap_link();
350                     $ldap->cd($this->config->current['BASE']);
351                     $ldap->cat($this->dn);
352                     $attrs = $ldap->fetch();
353                     $samba = in_array('sambaSamAccount', $attrs['objectClass']);
354                     $posix = in_array('posixAccount', $attrs['objectClass']);
356                     // Update the posix shadow flag...
357                     if($posix){
358                         $posixAccount = new posixAccount($this->config, $this->dn);
359                         $posixAccount->is_modified=TRUE;
360                         $posixAccount->activate_shadowExpire=1;
361                         $posixAccount->shadowExpire = date('d.m.Y', time() - (1 * 24 * 60 *60));
362                         $posixAccount->save();
363                     }
364                    
365                     // Update the samba kickoff flag...
366                     if($samba){
367                         $sambaAccount = new sambaAccount($this->config, $this->dn);
368                         $sambaAccount->is_modified=TRUE;
369                         $sambaAccount->kickoff_time_set  = "1";
370                         $sambaAccount->sambaKickoffTime= date('d.m.Y', time() - (1 * 24 * 60 *60));
371                         $sambaAccount->save();
372                     }
373                 }
375                 new log("modify","users/".get_class($this),$this->dn,array(),"Password has been changed");
376                 $this->dn ="";
378             } else {
379                 msg_dialog::display(_("Password change"),
380                         _("You have no permission to change this users password!"),
381                         WARNING_DIALOG);
382             }
383         }
385         // Cleanup
386         if(!count($this->pwd_change_queue) && $this->dn=""){
387             $this->remove_lock();
388             $this->closeDialogs();
389         }else{
390             return($this->handlePasswordQueue());
391         }
392     }
395     /*! \brief  Save user modifications. 
396      *          Whenever we save a 'new' user, request a password change for him.
397      */ 
398     function saveChanges()
399     {
400         $str = management::saveChanges();
402         if(!empty($str)) return($str);
404         if($this->last_tabObject instanceOf multi_plug){
405             foreach($this->last_tabObject->a_handles as $user){
406                 if($user->password_change_needed()){
407                     $this->force_hash_type[$user->dn] = $user->by_object['user']->pw_storage;
408                     $this->pwd_change_queue[] = $user->dn;
409                 }
410             }
411             return($this->handlePasswordQueue());
412         }
414         if(isset($this->last_tabObject->by_object['user']) && $this->last_tabObject->by_object['user']->password_change_needed()){
415             $this->force_hash_type[$this->last_tabObject->dn] = $this->last_tabObject->by_object['user']->pw_storage;
416             $this->pwd_change_queue[] = $this->last_tabObject->dn;
417             return($this->handlePasswordQueue());
418         }
419     }
421     function cancelEdit()
422     {
423         $str = management::cancelEdit();
424         if(!empty($str)) return($str);
426         if(isset($this->last_tabObject->by_object['user']) && 
427                 $this->last_tabObject->by_object['user']->dn != "new" &&  
428                 $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     }
436     /*! \brief  Intiates user creation. 
437      *          If we've user templates, then the user will be asked to use to use one. 
438      *          -> See 'templateContinue' for further handling.
439      */ 
440     function newUserFromTemplate($action="",$target=array(),$all=array())
441     {
442         // Call parent method, it knows whats to do, locking and so on ...
443         $str = management::newEntry($action,$target,$all);
444         if(!empty($str)) return($str);
446         // Reset uid selection.
447         $this->got_uid= "";
449         // Use template if there are any of them 
450         $templates = array();
451         $templates['none']= _("none");
452         $templates = array_merge($templates,$this->get_templates());
454         // We've templates, so preset the current template and display the input dialog.
455         if (count($templates)){
456             $smarty = get_smarty();
457             foreach(array("sn", "givenName", "uid", "got_uid") as $attr){
458                 $smarty->assign("$attr", "");
459             }
460             $smarty->assign("template",  array_pop($target));
461             $smarty->assign("templates", $templates);
462             $smarty->assign("edit_uid", "");
463             return($smarty->fetch(get_template_path('template.tpl', TRUE)));
465             // -> See 'templateContinue' for further handling!
466         }
467     }
471     /*! \brief  Intiates user creation. 
472      *          If we've user templates, then the user will be asked 
473      *           if he wants to use one. 
474      *          -> See 'templateContinue' for further handling.
475      */ 
476     function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
477     {
479         // Call parent method, it manages everything, locking, object creation...
480         $str = management::newEntry($action,$target,$all);
481         if(!empty($str)) return($str);
483         // If we've at least one template, then ask the user if he wants to use one?
484         $templates = array();
485         $templates['none']= _("none");
486         $templates = array_merge($templates,$this->get_templates());
488         // Display template selection
489         if (count($templates) > 1){
490             $smarty = get_smarty();
492             // Set default variables, normally empty.
493             foreach(array("sn", "givenName", "uid", "got_uid") as $attr){
494                 $smarty->assign($attr, "");
495             }
496             $smarty->assign("template", "none");
497             $smarty->assign("templates", $templates);
498             $smarty->assign("edit_uid", "");
499             return($smarty->fetch(get_template_path('template.tpl', TRUE)));
501             // -> See 'templateContinue' for further handling!
502         }
503     }
506     /* !\brief  This method is called whenever a template selection was displayed.
507      *          Here we act on the use selection. 
508      *          - Does the user want to create a user from template?
509      *          - Create user without template?
510      *          - Input correct, every value given and valid? 
511      */ 
512     function templateContinue()
513     {
514         // Get the list of available templates.
515         $templates = array();
516         $templates['none']= _("none");
517         $templates = array_merge($templates,$this->get_templates());
519         // Input validation, if someone wants to create a user from a template
520         //  then validate the given values.
521         $message = array();
522         if(!isset($_POST['template']) || (empty($_POST['template']))){
523             $message[]= msgPool::invalid(_("Template"));
524         }
525         if(!isset($_POST['sn']) || (empty($_POST['sn']))){
526             $message[]= msgPool::required(_("Name"));
527         }
528         if(!isset($_POST['givenName']) || (empty($_POST['givenName']))){
529             $message[]= msgPool::required(_("Given name"));
530         }
532         /********************
533          * 1   We've had input errors - Display errors and show input dialog again. 
534          ********************/
536         if (count($message) > 0){
537             msg_dialog::displayChecks($message);
539             // Preset input fields with user input. 
540             $smarty = get_smarty();
541             foreach(array("sn", "givenName", "uid", "template") as $attr){
542                 if(isset($_POST[$attr])){
543                     $smarty->assign("$attr", get_post($attr));
544                 }else{
545                     $smarty->assign("$attr", "");
546                 }
547             }
549             $smarty->assign("templates",$templates);
550             $smarty->assign("got_uid", $this->got_uid);
551             $smarty->assign("edit_uid",false);
552             return($smarty->fetch(get_template_path('template.tpl', TRUE)));
553         }
556         /********************
557          * 2   There was a template selected, now ask for the uid.
558          ********************/
560         if ($_POST['template'] != 'none' && !isset($_POST['uid'])){
562             // Remember user input.
563             $smarty = get_smarty();
564             $this->sn             = $_POST['sn'];
565             $this->givenName      = $_POST['givenName'];
567             // Avoid duplicate entries, check if such a user already exists.
568             $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
569             $ldap= $this->config->get_ldap_link();
570             $ldap->cd ($dn);
571             $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
572             if ($ldap->count () != 0){
573                 msg_dialog::displayChecks(array(msgPool::duplicated(_("Name"))));
574             }else{
576                 // Preset uid field by using the idGenerator 
577                 $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
578                 if ($this->config->get_cfg_value("core","idGenerator") != ""){
579                     $uids= gen_uids ($this->config->get_cfg_value("core","idGenerator"), $attributes);
580                     if (count($uids)){
581                         $smarty->assign("edit_uid", "false");
582                         $smarty->assign("uids", $uids);
583                         $this->uid= current($uids);
584                     }
585                 } else {
586                     $smarty->assign("edit_uid", "");
587                     $this->uid= "";
588                 }
589                 $this->got_uid= true;
590             }
592             // Assign user input 
593             foreach(array("sn", "givenName", "uid", "got_uid") as $attr){
594                 $smarty->assign("$attr", $this->$attr);
595             }
596             if (isset($_POST['template'])){
597                 $smarty->assign("template", $_POST['template']);
598             }
599             $smarty->assign("templates",$templates); 
600             return($smarty->fetch(get_template_path('template.tpl', TRUE)));
601         }
604         /********************
605          * 3   No template - Ok. Lets fill the data into the user object and skip templating here. 
606          ********************/
607         if ($_POST['template'] == 'none'){
608             foreach(array("sn", "givenName", "uid") as $attr){
609                 if (isset($_POST[$attr])){
610                     $this->tabObject->by_object['user']->$attr= $_POST[$attr];
611                 }
612             }
614             // The user Tab object is already instantiated, so just go back and let the 
615             //  management class do the rest.
616             return("");
617         }
620         /********************
621          * 4   Template selected and uid given - Ok, then lets adapt tempalte values. 
622          ********************/
623         if(isset($_POST['uid'])){
625             // Move user supplied data to sub plugins 
626             foreach(array("uid","sn","givenName") as $attr){
627                 $this->$attr = $_POST[$attr];
628                 $this->tabObject->$attr       = $this->$attr;
629                 $this->tabObject->by_object['user']->$attr = $this->$attr;
630             }
632             // Adapt template values.
633             $template_dn              = $_POST['template'];
634             $this->tabObject->adapt_from_template($template_dn, array("uid","cn","givenName","sn"));
635             $template_base            = preg_replace("/^[^,]+,".preg_quote(get_people_ou(), '/i')."/", '', $template_dn);
636             $this->tabObject->by_object['user']->base= $template_base;
638             // The user Tab object is already instantiated, so just go back and let the 
639             //  management class do the rest.
640             return("");
641         }
642     }
645     /* !\brief  This method applies a template to a set of users.
646      */ 
647     function templatizeUsers($action="",$target=array(),$all=array())
648     {
649         $this->dns = array();
650         if(count($target)){
652             // Get the list of available templates.
653             $templates = $this->get_templates();
655             // Check entry locking
656             foreach($target as $dn){
657                 if (($user= get_lock($dn)) != ""){
658                     $this->dn = $dn;
659                     return(gen_locked_message ($user, $dn));
660                 }
661                 $this->dns[] = $dn;
662             }
664             // Display template
665             $smarty = get_smarty();
666             $smarty->assign("templates", $templates);
667             return($smarty->fetch(get_template_path('templatize.tpl', TRUE)));
668         }
669     }
672     /* !\brief  This method is called whenever the templatize dialog was used.
673      */ 
674     function templatizeContinue()
675     {
676         // Template readable? 
677         $template= get_post('template');
678         $acl = $this->ui->get_permissions($template, $this->aclCategory."/".$this->aclPlugin);
679         if (preg_match('/r/', $acl)){
680             $tab = $this->tabClass;
681             foreach ($this->dns as $dn){
683                 // User writeable
684                 $acl = $this->ui->get_permissions($dn,  $this->aclCategory."/".$this->aclPlugin);
685                 if (preg_match('/w/', $acl)){
686                     $this->tabObject= new $tab($this->config, $this->config->data['TABS'][$this->tabType], $dn, $this->aclCategory);
687                     $this->tabObject->adapt_from_template($template, array("sn", "givenName", "uid"));
688                     $this->tabObject->save();
689                 } else {
690                     msg_dialog::display(_("Permission error"), msgPool::permModify($dn), ERROR_DIALOG);
691                 }
692             }
693         } else {
694             msg_dialog::display(_("Permission error"), msgPool::permView($template), ERROR_DIALOG);
695         }
697         // Cleanup!
698         $this->remove_lock(); 
699         $this->closeDialogs();
700     }
703     /* !\brief  Lock/unlock multiple users.
704      */ 
705     function lockUsers($action,$target,$all)
706     {
707         if(!count($target)) return;
708         if($action == "lockUsers"){
709             $this->lockEntry($action,$target, $all, "lock");
710         }else{
711             $this->lockEntry($action,$target, $all, "unlock");
712         }
713     }
716     /* !\brief  Locks/unlocks the given user(s).
717      */ 
718     function lockEntry($action,$entry, $all, $type = "toggle")
719     {
721         // Filter out entries we are not allowed to modify
722         $disallowed = array();
723         $dns = array();
724         foreach($entry as $dn){
725             if (!preg_match("/w/",$this->ui->get_permissions($dn,"users/password"))){
726                 $disallowed[] = $dn;
727             }else{
728                 $allowed[] = $dn;
729             }
730         }
731         if(count($disallowed)){
732             msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
733         }
735         // Try to lock/unlock the rest of the entries.
736         $ldap = $this->config->get_ldap_link();
737         foreach($allowed as $dn){
738             $ldap->cat($dn, array('userPassword'));
739             if($ldap->count() == 1){
741                 // We can't lock empty passwords.
742                 $val = $ldap->fetch();
743                 if(!isset($val['userPassword'])){
744                     continue;
745                 }
747                 // Detect the password method and try to lock/unlock.
748                 $pwd = $val['userPassword'][0];
749                 $method = passwordMethod::get_method($pwd,$val['dn']);
750                 $success= true;
751                 if($method instanceOf passwordMethod){
752                     if($type == "toggle"){
753                         if($method->is_locked($this->config,$val['dn'])){
754                             $success= $method->unlock_account($this->config,$val['dn']);
755                         }else{
756                             $success= $method->lock_account($this->config,$val['dn']);
757                         }
758                     }elseif($type == "lock" && !$method->is_locked($this->config,$val['dn'])){
759                         $success= $method->lock_account($this->config,$val['dn']);
760                     }elseif($type == "unlock" && $method->is_locked($this->config,$val['dn'])){
761                         $success= $method->unlock_account($this->config,$val['dn']);
762                     }
764                     // Check if everything went fine.
765                     if (!$success){
766                         $hn= $method->get_hash_name();
767                         if (is_array($hn)){
768                             $hn= $hn[0];
769                         }
770                         msg_dialog::display(_("Account locking"),
771                                 sprintf(_("Password method '%s' does not support locking. Account (%s) has not been locked!"), 
772                                     $hn,$dn),WARNING_DIALOG);
773                     }
774                 }else{
775                     // Can't lock unknown methods.
776                 }
777             }
778         }
779     }
782     /* !\brief  This method returns a list of all available templates.
783      */ 
784     function get_templates()
785     {
786         $templates= array();
787         $ldap= $this->config->get_ldap_link();
788         foreach ($this->config->departments as $key => $value){
789             $acl = $this->ui->get_permissions($value,$this->aclCategory."/".$this->aclPlugin);
790             if (preg_match("/c/",$acl)){
792                 // Search all templates from the current dn.
793                 $ldap->cd (get_people_ou().$value);
794                 $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
795                 if ($ldap->count() != 0){
796                     while ($attrs= $ldap->fetch()){
797                         $templates[$ldap->getDN()]= $attrs['uid'][0]." - ".LDAP::fix($key);
798                     }
799                 }
800             }
801         }
802         natcasesort ($templates);
803         reset ($templates);
804         return($templates);
805     }
808     function copyPasteHandler($action="",$target=array(),$all=array(),
809             $altTabClass ="", $altTabType = "", $altAclCategory="",$altAclPlugin="")
810     {
811         if ($this->config->boolValueIsTrue("core", "copyPaste")){
812             $this->cpHandler->lastdn = "";
813             $str = management::copyPasteHandler($action,$target,$all);
814             if($this->cpHandler->lastdn != "" && isset($_POST['passwordTodo']) && $_POST['passwordTodo'] == "new"){
815                 $this->pwd_change_queue[] = $this->cpHandler->lastdn;
816                 return($this->handlePasswordQueue());
817             }
818             return($str);
819         }
821         return "";
822     }
825     static function filterLockImage($userPassword)
826     {
827         $image= "images/empty.png";
828         if(isset($userPassword[0]) && preg_match("/^\{[^\}]/",$userPassword[0])){
829             if(preg_match("/^[^\}]*+\}!/",$userPassword[0])){
830                 $image= "images/lists/locked.png";
831             }else{
832                 $image= "images/lists/unlocked.png";
833             }
834         }
835         return $image;
836     }
839     static function filterLockLabel($userPassword)
840     {
841         $label= "";
842         if(isset($userPassword[0]) && preg_match("/^\{[^\}]/",$userPassword[0])){
843             if(preg_match("/^[^\}]*+\}!/",$userPassword[0])){
844                 $label= _("Unlock account")."<rowClass:entry-locked/><rowLabel:locked/>";
845             }else{
846                 $label= _("Lock account");
847             }
848         }
849         return $label;
850     }
853     static function filterProperties($row, $classes)
854     {
855         $result= "";
856         $map= array( "gosaAccount" => array( "image" => "plugins/users/images/select_user.png",
857                     "plugin" => "user",
858                     "alt" => _("Generic"),
859                     "title" => _("Edit generic properties")),
860                 "posixAccount" => array("image" => "images/penguin.png",
861                     "plugin" => "posixAccount",
862                     "alt" => _("POSIX"),
863                     "title" => _("Edit POSIX properties")),
864                 "gosaMailAccount" => array("image" => "images/mailto.png",
865                     "alt" => _("Mail"),
866                     "plugin" => "mailAccount",
867                     "title" => _("Edit mail properties")),
868                 "sambaSamAccount" => array("image" => "plugins/systems/images/select_winstation.png",
869                     "plugin" => "sambaAccount",
870                     "alt" => _("Samba"),
871                     "title" => _("Edit samba properties")),
872                 "apple-user" => array("image" => "plugins/netatalk/images/select_netatalk.png",
873                     "plugin" => "netatalk",
874                     "alt" => _("Netatalk"),
875                     "title" => _("Edit netatalk properties")),
876                 "gotoEnvironment" => array("image" => "plugins/users/images/small_environment.png",
877                     "plugin" => "environment",
878                     "alt" => _("Environment"),
879                     "title" => _("Edit environment properties")),
880                 "goFaxAccount" => array("image" => "plugins/users/images/fax_small.png",
881                         "plugin" => "gofaxAccount",
882                         "alt" => _("FAX"),
883                         "title" => _("Edit FAX properties")),
884                 "goFonAccount" => array("image" => "plugins/gofon/images/select_phone.png",
885                         "plugin" => "phoneAccount",
886                         "alt" => _("Phone"),
887                         "title" => _("Edit phone properties")));
889         // Walk thru map
890         foreach ($map as $oc => $properties) {
891             if (in_array_ics($oc, $classes)) {
892                 $result.= image($properties['image'], "listing_edit_".$properties['plugin']."_$row", $properties['title']);
893             } else {
894                 $result.= image('images/empty.png');
895             }
896         }
897         return $result;
898     }
900
901 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
902 ?>