summaryrefslogtreecommitdiff
path: root/www/admin/maintainer/maintainer.php.in
blob: 153744b3557f8f98a4879dd0eba8b551b7c42ad3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
/*
 *  Copyright (c) 2004 Klarälvdalens Datakonsult AB
 *
 *    Written by Steffen Hansen <steffen@klaralvdalens-datakonsult.se>
 *
 *  This  program is free  software; you can redistribute  it and/or
 *  modify it  under the terms of the GNU  General Public License as
 *  published by the  Free Software Foundation; either version 2, or
 *  (at your option) any later version.
 *
 *  This program is  distributed in the hope that it will be useful,
 *  but WITHOUT  ANY WARRANTY; without even the  implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You can view the  GNU General Public License, online, at the GNU
 *  Project's homepage; see <http://www.gnu.org/licenses/gpl.html>.
 */

require_once('@kolab_php_module_prefix@admin/include/mysmarty.php');
require_once('@kolab_php_module_prefix@admin/include/headers.php');
require_once('@kolab_php_module_prefix@admin/include/locale.php');
require_once('@kolab_php_module_prefix@admin/include/authenticate.php');
require_once('@kolab_php_module_prefix@admin/include/form.class.php');
require_once('@kolab_php_module_prefix@admin/include/passwd.php');

/**** Functions ***/
function comment( $s ) {
  return $s;
}

function is_unique ($a, $b) {
  global $ldap;
  if (($result = $ldap->search( $_SESSION['base_dn'],"(".$a."=".$ldap->escape($b).")")) &&
      (ldap_count_entries($ldap->connection,$result) <= 0))
    return true;
  return false;
}

function domain_dn()
{
  return $_SESSION['base_dn'];
}

// Check that a uid is unique
function checkuniquemail( $form, $key, $value ) {
  debug("checkuniquemail( $form, $key, $value )");
  if( is_unique( 'uid', $value ) ) {
	return '';
  } else {
	return _('Account with this UID already exists');
  }
}

function fill_form_for_modify( &$form, &$ldap_object ) {
  global $auth;
  if (is_array($ldap_object['sn'])) $lastname = $ldap_object['sn'][0];
  else $lastname = $ldap_object['sn'];
  if (is_array($ldap_object['cn'])) $cn = $ldap_object['cn'][0];
  else $cn = $ldap_object['cn'];
  $firstname = KolabLDAP::getGivenName($cn, $lastname);
  if (is_array($ldap_object['uid'])) $uid = $ldap_object['uid'][0];
  else $uid = $ldap_object['uid'];
  $form->entries['firstname']['value'] = $firstname;
  $form->entries['lastname']['value'] = $lastname;
  $form->entries['password_0']['value'] = '';
  $form->entries['password_1']['value'] = '';
  $form->entries['uid']['value'] = $uid;
  $form->entries['uid']['attrs'] = 'readonly';
  if( $auth->group() != 'admin' ) {
    $form->entries['firstname']['attrs'] = 'readonly';
	$form->entries['lastname']['attrs'] = 'readonly';
  }
  /*
  foreach( array( 'title', 'o', 'ou', 'street', 'postOfficeBox',
                  'postalCode', 'l', 'c', 'telephoneNumber',
                  'facsimileTelephoneNumber' ) as $attr ) {
    if (is_array($ldap_object[$attr])) $v = $ldap_object[$attr][0];
    else $v = $ldap_object[$attr];
    $form->entries[$attr.'_0']['value'] = $v;
  }
  if (is_array($ldap_object['alias'])) {
	$arr = $ldap_object['alias'];
	unset( $arr['count'] );
	$v = join("\n", $arr );
  }
  else $v = $ldap_object[$attr];
  $form->entries['alias']['value'] = $v;
  $form->entries['action']['value'] = 'save';
  if( isset( $form->entries['userquota'] ) ) {
    if (is_array($ldap_object['userquota'])) $userquota = $ldap_object['userquota'][0];
    else $userquota = $ldap_object['userquota'];
    if( $userquota > 0 ) {
      $form->entries['userquota']['value'] = $userquota;
    } else {
      $form->entries['userquota']['value'] = '';
    }
  }
  */
}

/**** Authentication etc. ***/
$sidx = 'maintainer';

require_once('@kolab_php_module_prefix@admin/include/menu.php');
$menuitems[$sidx]['selected'] = 'selected';

/**** Logic ***/
$errors = array();
$messages = array();
$valid_actions = array('save','firstsave','modify','create','delete','kill');
$contenttemplate = 'formcontainer.tpl';

// Get request data
if (!empty($_REQUEST['action']) &&
    in_array($_REQUEST['action'],$valid_actions)) $action = trim(urldecode($_REQUEST['action']));
else array_push($errors, _("Error: need valid action to proceed"));
$dn="";
if (!empty($_REQUEST['dn'])) $dn = trim(urldecode($_REQUEST['dn']));

// Check auth
if (!$errors && $auth->group() != 'admin' && $auth->group() != 'maintainer' ) {
  array_push($errors, _("Error: You don't have the required Permissions"));
}   

// Fill in data
if ($action == "create") {
  $comment_mail_0 = _('Required, non volatile');
  $comment_password = _('Required');
} else {
  $comment_mail_0 = _('non volatile');
  $comment_password = _('Leave blank to keep password unchanged');
}

$entries = array( 'firstname' => array( 'name' => _('First Name'),
					'validation' => 'notempty',
					'comment' => _('Required') ),
		  'lastname' => array( 'name' => _('Last Name'),
				       'validation' => 'notempty',
				       'comment' => _('Required') ),
		  'password_0' => array( 'name' => _('Password'),
					 'type' => 'password',
					 'validation' => 'checkpw',
					 'comment' => $comment_password ),
		  'password_1' => array( 'name' => _('Verify Password'),
					 'type' => 'password',
					 'validation' => 'checkpw',
					 'comment' => $comment_password ),
		  'uid' => array( 'name' => _('Unique User ID'),
				     'validation' => 'notempty',
				     'comment' => $comment_mail_0 ));

$entries['action'] = array( 'name' => 'action',
			    'type' => 'hidden' );

$oc = array('top', 'inetOrgPerson','kolabInetOrgPerson');

if( $dn ) {
  $ldap_object = $ldap->read( $dn );
  if( !$ldap_object ) {
    array_push($errors, sprintf(_("LDAP Error: No such dn: %s: %s"), $dn, ldap_error($ldap->connection)));
  }
  $oc = $ldap_object['objectClass'];
  unset($oc['count']);
}

$form =& new KolabForm( 'maintainer', 'createmaintainer.tpl', $entries );
/***************** Main action swicth **********************/
switch( $action ) {
 case 'firstsave':
   debug("adding checkuniquemail to validation");
   $ldap_object['objectClass'] = array( 'top', 'inetOrgPerson', 'kolabInetOrgPerson');
   $form->entries['uid']['validation'] = 'checkuniquemail';   
 case 'save':
   if( $form->isSubmitted() ) {
     if( !$form->validate() ) {
       $form->setValues();
       $content = $form->outputForm();
     } else {
       debug("Process...");
       $ldap_object = array();
       $ldap_object['objectClass'] = $oc;
       $ldap_object['sn'] = trim($_POST['lastname']);
       $ldap_object['cn'] = trim($_POST['firstname']).' '.$ldap_object['sn'];
       if( !empty( $_POST['password_0'] ) ) {
		 $ldap_object['userPassword'] = ssha( $_POST['password_0'], gensalt());
       }
       if( $action == 'firstsave' ) $ldap_object['uid'] = trim( strtolower( $_POST['uid'] ) );

	   debug_var_dump( $ldap_object );

       $domain_dn = domain_dn();
	   
       if ($action == "save") {
		 if (!$errors) {
		   if (!empty($ldap_object['cn'])) $newdn = "cn=".$ldap->dn_escape($ldap_object['cn']).",cn=internal,".$domain_dn;
		   else $newdn = $dn;
		   if (!$visible && !strstr($newdn,$dn_add)) {
			 list($cn,$rest) = split(',', $newdn, 2); 
			 $newdn = $cn.$dn_add.",".$rest;
		   } 
		   if (strcmp($dn,$newdn) != 0) {
			 if (($result=ldap_read($ldap->connection,$dn,"(objectclass=*)")) &&
				 ($entry=ldap_first_entry($ldap->connection,$result)) &&
				 ($oldattrs=ldap_get_attributes($ldap->connection,$entry))) {
			   $ldap_object['uid'] = $oldattrs['uid'][0];
			   if( empty($ldap_object['userPassword']) )
				 $ldap_object['userPassword'] = $oldattrs['userPassword'][0];

			   // Try to rename the object
			   if (!ldap_rename($ldap->connection, $dn, "cn=" . $ldap->dn_escape($ldap_object['cn']), "cn=internal,".$domain_dn, true)) {
				 array_push($errors, sprintf(_("LDAP Error: could not rename %s to %s: %s"), $dn,
											 $newdn, ldap_error($ldap->connection)));
			   }
			   if( !$errors ) {
				 // Renaming was ok, now try to modify the object accordingly
				 if (!ldap_modify($ldap->connection, $newdn, $ldap_object)) {
				   // While this should not happen, in case it does, we need to revert the
				   // renaming
				   array_push($errors, sprintf(_("LDAP Error: could not modify %s to %s: %s"), $newdn,
											   ldap_error($ldap->connection)));
				   $old_dn = substr($dn, 0, strlen($dn) - strlen("cn=internal,".$domain_dn) - 1);
				   ldap_rename($ldap->connection, $newdn, $old_dn, "cn=internal,".$domain_dn, true);
				 } else {
				   // everything is fine and we can move on
				   $messages[] = sprintf( _("%s successfully updated"), $newdn);
				   $dn = $newdn;
				 }
			   }
			 } else array_push($errors, sprintf(_("LDAP Error: could not read %s: %s"), $dn,
												ldap_error($ldap->connection)));
		   } else {
			 if (!ldap_modify($ldap->connection, $dn, $ldap_object)) {
			   array_push($errors, sprintf(_("LDAP Error: could not modify object %s: %s"), $dn,
										   ldap_error($ldap->connection)));
			 }
		   }
		 }
		 $heading = _('Modify Maintainer');
		 $messages[] = _('Maintainer ').$ldap_object['dn']._(' successfully modified');
		 $form->setValues();
		 $form->entries['password_0']['value'] = '';
		 $form->entries['password_1']['value'] = '';
		 if( $auth->group() != 'admin' ) {
		   $form->entries['firstname']['attrs'] = 'readonly';
		   $form->entries['lastname']['attrs'] = 'readonly';
		 }		 
		 $form->entries['uid']['attrs'] = 'readonly';
		 $form->entries['action']['value'] = 'save';
		 $content = $form->outputForm();
		 break;
       } else {
		 // firstsave
		 if (!$errors) {
		   $dn = "cn=".$ldap->dn_escape($ldap_object['cn']).",cn=internal,".$domain_dn;
		   debug("Calling ldap_add with dn=$dn");
		   if ($dn && !ldap_add($ldap->connection, $dn, $ldap_object)) 
			 array_push($errors, sprintf( _("LDAP Error: could not add object %s: %s"), $dn,
										  ldap_error($ldap->connection)));
		   if( $dn && !ldap_mod_add($ldap->connection, 'cn=maintainer,cn=internal,'.$domain_dn, 
									array( 'member' => $dn ) ) ) {
			 array_push($errors, sprintf(_("LDAP Error: could not add object %s to maintainer group: %s"), $dn,
										 ldap_error($ldap->connection)));
		   }
		   if( !$errors ) {
			 $messages[] = _('Maintainer ').$ldap_object['dn']._(' successfully created');
			 $heading = _('Create New Maintainer');
			 $form->entries['action']['value'] = 'firstsave';
			 $content = $form->outputForm();
			 break;
		   }
		 } else {
		   $heading = _('Create New Maintainer');
		   $blacklist = array('mail');
		   $form->entries['action']['value'] = 'firstsave';
		   $form->outputForm();
		   break;
		 }
       }
     }
     break;
   }
 case 'create':
   $heading = _('Create New Maintainer');
   if( !$dn ) {
     $form->entries['action']['value'] = 'firstsave';
   } else {
     $form->entries['action']['value'] = 'save';
   }
   $content = $form->outputForm();
   break;
 case 'modify':
   $heading = _('Modify Maintainer');
   fill_form_for_modify( $form, $ldap_object );
   $form->entries['action']['value'] = 'save';
   $content = $form->outputForm();
   break;
 case 'delete':
   $heading = _('Delete Maintainer');
   foreach( $form->entries as $k => $v ) {
     if( $v['type'] != 'hidden' ) {
       $form->entries[$k]['attrs'] = 'readonly';
     }
   }
   fill_form_for_modify( $form, $ldap_object );
   $form->entries['action']['value'] = 'kill';
   $form->submittext = 'Delete';
   $content = $form->outputForm();
   break;
 case 'kill':
   if (!$dn) array_push($errors, _("Error: need DN for delete operation"));
   elseif ($auth->group() != "maintainer" && $auth->group() != "admin") 
     array_push($errors, _("Error: you need administrative permissions to delete users"));
   
   if (!$errors) {
	 if(!ldap_mod_del($ldap->connection, 'cn=maintainer,cn=internal,'.domain_dn(), array('member' => $dn ) )) {
	   $errors[] = sprintf(_("LDAP Error: Could not remove %s from maintainer group: %s"), $dn,
						   ldap_error($ldap->connection));
	 }
	 if( !$errors ) {
	   $delete_template['kolabDeleteflag'] = 'TRUE';
	   if( !$ldap->deleteObject($dn)) {
		 array_push($errors, sprintf(_("LDAP Error: could not mark %s for deletion: %s"), $dn.
									 ldap_error($ldap->connection)));
	   }
	 }
   }
   if( !$errors ) {
	 $heading = _('Maintainer Deleted');
	 $contenttemplate = 'maintainerdeleted.tpl';
   }
   break;
}


$smarty = new MySmarty();
$smarty->assign( 'topdir', $topdir );
$smarty->assign( 'errors', array_merge((array)$errors,(array)$form->errors) );
$smarty->assign( 'uid', $auth->uid() );
$smarty->assign( 'group', $auth->group() );
$smarty->assign( 'page_title', $menuitems[$sidx]['title'] );
$smarty->assign( 'menuitems', $menuitems );
$smarty->assign( 'submenuitems', 
				 array_key_exists('submenu', 
								  $menuitems[$sidx])?$menuitems[$sidx]['submenu']:array() );
$smarty->assign( 'heading', $heading );
$smarty->assign( 'form', $content );
if( isset( $dn ) ) $smarty->assign( 'dn', $dn );
if( count($messages)>0) $smarty->assign( 'messages', $messages );
$smarty->assign( 'maincontent', $contenttemplate );
$smarty->display('page.tpl');

/*
  Local variables:
  mode: php
  indent-tabs-mode: t
  tab-width: 4
  buffer-file-coding-system: utf-8
  End:
 */
?>