egroupware_official/admin/inc/class.boaccounts.inc.php

562 lines
16 KiB
PHP
Raw Normal View History

<?php
/**************************************************************************\
2004-01-27 00:26:19 +01:00
* eGroupWare - account administration *
* http://www.egroupware.org *
* -------------------------------------------- *
* 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 of the License, or (at your *
* option) any later version. *
\**************************************************************************/
/* $Id$ */
class boaccounts
{
var $so;
var $public_functions = array(
'add_group' => True,
'add_user' => True,
'delete_group' => True,
'delete_user' => True,
'edit_group' => True,
'edit_user' => True
);
2001-10-10 17:10:15 +02:00
var $xml_functions = array();
var $soap_functions = array(
'add_user' => array(
'in' => array('int', 'struct'),
'out' => array()
2001-10-10 17:10:15 +02:00
)
);
function boaccounts()
{
2005-10-14 19:03:16 +02:00
$this->so =& CreateObject('admin.soaccounts');
}
function delete_group($account_id='')
{
if(!$account_id || $GLOBALS['egw']->acl->check('group_access',32,'admin'))
{
return False;
2003-04-23 23:52:22 +02:00
}
$account_id = (int)$account_id;
// delete all acl (and memberships) of group
$GLOBALS['egw']->acl->delete_account($account_id);
// make this information also available in the hook
$lid = $GLOBALS['egw']->accounts->id2name($account_id);
$GLOBALS['egw']->hooks->process($GLOBALS['hook_values'] = array(
'account_id' => $account_id,
'account_name' => $lid,
'location' => 'deletegroup'
),False,True); // called for every app now, not only enabled ones)
2005-10-14 19:03:16 +02:00
$GLOBALS['egw']->accounts->delete($account_id);
return True;
2003-04-23 23:52:22 +02:00
}
function delete_user($account_id='',$new_owner='')
{
if(!$account_id || $GLOBALS['egw']->acl->check('account_access',32,'admin'))
{
return False;
}
$accountid = (int)$account_id;
$account_id = get_account_id($accountid);
// make this information also available in the hook
$lid = $GLOBALS['egw']->accounts->id2name($account_id);
$GLOBALS['hook_values'] = array(
'account_id' => $account_id,
'account_lid' => $lid,
'new_owner' => (int)$new_owner,
'location' => 'deleteaccount'
);
// first all other apps, then preferences and admin
foreach(array_merge(array_diff(array_keys($GLOBALS['egw_info']['apps']),array('preferences','admin')),array('preferences','admin')) as $app)
{
$GLOBALS['egw']->hooks->single($GLOBALS['hook_values'],$app);
}
return True;
}
function add_group($group_info)
2001-09-03 04:58:25 +02:00
{
if($GLOBALS['egw']->acl->check('group_access',4,'admin'))
{
return False;
}
$errors = $this->validate_group($group_info);
if(count($errors))
2001-09-03 04:58:25 +02:00
{
return $errors;
2001-09-03 04:58:25 +02:00
}
2005-10-14 19:03:16 +02:00
$group =& CreateObject('phpgwapi.accounts',$group_info['account_id'],'g');
$group->acct_type = 'g';
$account_info = array(
2001-09-03 04:58:25 +02:00
'account_type' => 'g',
'account_lid' => $group_info['account_name'],
'account_passwd' => '',
'account_firstname' => $group_info['account_name'],
'account_lastname' => 'Group',
'account_status' => 'A',
'account_expires' => -1,
2001-09-03 04:58:25 +02:00
// 'account_file_space' => $account_file_space_number . "-" . $account_file_space_type,
'account_email' => $group_info['account_email'],
'account_members' => $group_info['account_user']
2001-09-03 04:58:25 +02:00
);
$group_info['account_id'] = $group->create($account_info);
// do the following only if we got an id - the create succeeded
if($group_info['account_id'])
2001-09-03 04:58:25 +02:00
{
$group->set_members($group_info['account_user'],$group_info['account_id']);
2005-10-14 19:03:16 +02:00
$apps =& CreateObject('phpgwapi.applications',$group_info['account_id']);
$apps->update_data(Array());
reset($group_info['account_apps']);
while(list($app,$value) = each($group_info['account_apps']))
{
$apps->add($app);
$new_apps[] = $app;
}
$apps->save_repository();
$GLOBALS['hook_values'] = $group_info;
$GLOBALS['egw']->hooks->process($GLOBALS['hook_values']+array(
'location' => 'addgroup'
),False,True); // called for every app now, not only enabled ones)
return True;
2001-09-03 04:58:25 +02:00
}
return False;
2001-09-03 04:58:25 +02:00
}
function add_user($userData)
{
if($GLOBALS['egw']->acl->check('account_access',4,'admin'))
{
return False;
}
$accountPrefix = '';
2005-10-14 19:03:16 +02:00
if(isset($GLOBALS['egw_info']['server']['account_prefix']))
{
2005-10-14 19:03:16 +02:00
$accountPrefix = $GLOBALS['egw_info']['server']['account_prefix'];
}
if($accountPrefix)
{
$userData['account_lid'] = $accountPrefix . $userData['account_lid'];
}
// add the primary group, to the users other groups, if not already added
if(is_array($userData['account_groups']))
{
if(!in_array($userData['account_primary_group'],$userData['account_groups']))
{
$userData['account_groups'][] = (int)$userData['account_primary_group'];
}
}
else
{
$userData['account_groups'] = array((int)$userData['account_primary_group']);
}
// do we have all needed data??
if(!($errors = $this->validate_user($userData)) &&
($userData['account_id'] = $account_id = $this->so->add_user($userData))) // no error in the creation
{
if($userData['anonymous'])
{
$GLOBALS['egw']->acl->add_repository('phpgwapi','anonymous',$account_id,1);
}
else
{
$GLOBALS['egw']->acl->delete_repository('phpgwapi','anonymous',$account_id);
}
// make this information for the hooks available
$GLOBALS['hook_values'] = $userData + array('new_passwd' => $userData['account_passwd']);
$GLOBALS['egw']->hooks->process($GLOBALS['hook_values']+array(
'location' => 'addaccount'
),False,True); // called for every app now, not only enabled ones
return True;
}
else
{
return $errors;
}
return False;
}
function edit_group($group_info)
{
if($GLOBALS['egw']->acl->check('group_access',16,'admin'))
{
return False;
}
$errors = $this->validate_group($group_info);
if(count($errors))
{
return $errors;
}
2005-10-14 19:03:16 +02:00
$group =& CreateObject('phpgwapi.accounts',$group_info['account_id'],'g');
2001-09-03 04:58:25 +02:00
$old_group_info = $group->read_repository();
2001-12-18 07:10:32 +01:00
// Set group apps
2005-10-14 19:03:16 +02:00
$apps =& CreateObject('phpgwapi.applications',$group_info['account_id']);
$apps_before = $apps->read_account_specific();
$apps->update_data(Array());
$new_apps = Array();
2001-09-03 04:58:25 +02:00
if(count($group_info['account_apps']))
{
2001-09-03 04:58:25 +02:00
reset($group_info['account_apps']);
while(list($app,$value) = each($group_info['account_apps']))
{
2001-09-03 04:58:25 +02:00
$apps->add($app);
if(!@$apps_before[$app] || @$apps_before == False)
{
2001-09-03 04:58:25 +02:00
$new_apps[] = $app;
}
}
}
$apps->save_repository();
$group->set_members($group_info['account_user'],$group_info['account_id']);
$GLOBALS['hook_values'] = $group_info;
$GLOBALS['hook_values']['old_name'] = $group->id2name($group_info['account_id']);
// This is down here so we are sure to catch the acl changes
// for LDAP to update the memberuid attribute
$group->data['account_email'] = $group_info['account_email'];
$group->data['account_lid'] = $group_info['account_name'];
$group->save_repository();
$GLOBALS['egw']->hooks->process($GLOBALS['hook_values']+array(
'location' => 'editgroup'
),False,True); // called for every app now, not only enabled ones)
return True;
}
function edit_user($userData)
{
if($GLOBALS['egw']->acl->check('account_access',16,'admin'))
{
return False;
}
//error_log(array2string($userData));
$accountPrefix = '';
2005-10-14 19:03:16 +02:00
if(isset($GLOBALS['egw_info']['server']['account_prefix']))
{
2005-10-14 19:03:16 +02:00
$accountPrefix = $GLOBALS['egw_info']['server']['account_prefix'];
}
if($accountPrefix)
{
$userData['account_lid'] = $accountPrefix . $userData['account_lid'];
}
$errors = $this->validate_user($userData);
if(@is_array($errors))
{
return $errors;
}
else
{
$this->save_user($userData);
$GLOBALS['hook_values'] = $userData;
$GLOBALS['egw']->hooks->process($GLOBALS['hook_values']+array(
'location' => 'editaccount'
),False,True); // called for every app now, not only enabled ones)
return True;
}
return True;
}
function validate_group($group_info)
2001-09-03 04:58:25 +02:00
{
$errors = Array();
2005-10-14 19:03:16 +02:00
$group =& CreateObject('phpgwapi.accounts',$group_info['account_id'],'g');
2001-09-03 04:58:25 +02:00
$group->read_repository();
if(!$group_info['account_name'])
2001-09-03 04:58:25 +02:00
{
$errors[] = lang('You must enter a group name.');
}
/* For LDAP */
if(!$group_info['account_user'])
{
$errors[] = lang('You must select at least one group member.');
}
if($group_info['account_name'] != $group->id2name($group_info['account_id']))
{
if($group->exists($group_info['account_name']))
2001-09-03 04:58:25 +02:00
{
$errors[] = lang('Sorry, that group name has already been taken.');
2001-09-03 04:58:25 +02:00
}
}
/*
if(preg_match("/\D/", $account_file_space_number))
2001-09-03 04:58:25 +02:00
{
$errors[] = lang('File space must be an integer');
2001-09-03 04:58:25 +02:00
}
*/
if(count($errors))
2001-09-03 04:58:25 +02:00
{
return $errors;
2001-09-03 04:58:25 +02:00
}
}
/* checks if the userdata are valid
returns FALSE if the data are correct
otherwise the error array
*/
function validate_user(&$_userData)
{
$totalerrors = 0;
if($GLOBALS['egw_info']['server']['account_repository'] == 'ldap' &&
(!$_userData['account_lastname'] && !$_userData['lastname']))
{
$error[$totalerrors] = lang('You must enter a lastname');
$totalerrors++;
}
if(!$_userData['account_lid'])
{
$error[$totalerrors] = lang('You must enter a loginid');
$totalerrors++;
}
2003-09-13 19:14:30 +02:00
if(!in_array($_userData['account_primary_group'],$_userData['account_groups']))
{
$error[$totalerrors] = lang('The groups must include the primary group');
$totalerrors++;
}
// Check if an account already exists as system user, and if it does deny creation
// (increase the totalerrors counter and the message thereof)
if ($GLOBALS['egw_info']['server']['account_repository'] == 'ldap' &&
!$GLOBALS['egw_info']['server']['ldap_allow_systemusernames'] &&
function_exists('posix_getpwnam') && posix_getpwnam($_userData['account_lid']))
{
$error[$totalerrors] = lang('There already is a system-user with this name. User\'s should not have the same name as a systemuser');
$totalerrors++;
}
if($_userData['old_loginid'] != $_userData['account_lid'])
{
if($GLOBALS['egw']->accounts->exists($_userData['account_lid']))
{
if($GLOBALS['egw']->accounts->exists($_userData['account_lid']) && $GLOBALS['egw']->accounts->get_type($_userData['account_lid'])=='g')
{
2005-10-14 19:03:16 +02:00
$error[$totalerrors] = lang('There already is a group with this name. Userid\'s can not have the same name as a groupid');
}
else
{
2005-10-14 19:03:16 +02:00
$error[$totalerrors] = lang('That loginid has already been taken');
}
$totalerrors++;
}
}
if($_userData['account_passwd'] || $_userData['account_passwd_2'])
{
if($_userData['account_passwd'] != $_userData['account_passwd_2'])
{
$error[$totalerrors] = lang('The two passwords are not the same');
$totalerrors++;
}
}
if(!count($_userData['account_permissions']) && !count($_userData['account_groups']))
{
$error[$totalerrors] = lang('You must add at least 1 permission or group to this account');
$totalerrors++;
}
if($_userData['account_expires_month'] || $_userData['account_expires_day'] || $_userData['account_expires_year'] || $_userData['account_expires_never'])
{
if($_userData['account_expires_never'])
{
$_userData['expires'] = -1;
$_userData['account_expires'] = $_userData['expires'];
}
else
{
if(! checkdate($_userData['account_expires_month'],$_userData['account_expires_day'],$_userData['account_expires_year']))
{
$error[$totalerrors] = lang('You have entered an invalid expiration date');
$totalerrors++;
}
else
{
$_userData['expires'] = mktime(2,0,0,$_userData['account_expires_month'],$_userData['account_expires_day'],$_userData['account_expires_year']);
$_userData['account_expires'] = $_userData['expires'];
}
}
}
else
{
$_userData['expires'] = -1;
$_userData['account_expires'] = $_userData['expires'];
}
/*
$check_account_file_space = explode('-', $_userData['file_space']);
if(preg_match("/\D/", $check_account_file_space[0]))
{
$error[$totalerrors] = lang('File space must be an integer');
$totalerrors++;
}
*/
if($totalerrors == 0)
{
return False;
}
else
{
return $error;
}
}
2001-12-18 07:10:32 +01:00
/* stores the userdata */
function save_user($_userData)
{
//error_log(__METHOD__.array2string($_userData));
//error_log(__METHOD__.array2string($old_passwd));
2005-10-14 19:03:16 +02:00
$account =& CreateObject('phpgwapi.accounts',$_userData['account_id'],'u');
$account->update_data($_userData);
$account->save_repository();
$account->set_memberships($_userData['account_groups'],$_userData['account_id']);
if($_userData['account_passwd'])
2001-12-18 07:10:32 +01:00
{
$auth =& CreateObject('phpgwapi.auth');
$auth->change_password($old_passwd, $_userData['account_passwd'], $_userData['account_id']);
$GLOBALS['hook_values']['account_id'] = $_userData['account_id'];
$GLOBALS['hook_values']['old_passwd'] = $old_passwd;
$GLOBALS['hook_values']['new_passwd'] = $_userData['account_passwd'];
2005-10-14 19:03:16 +02:00
$GLOBALS['egw']->hooks->process($GLOBALS['hook_values']+array(
'location' => 'changepassword'
),False,True); // called for every app now, not only enabled ones)
if ($_userData['account_lastpwd_change']==0)
{
// change password sets the shadow_timestamp/account_lastpwd_change timestamp
// so we need to reset that to 0 as Admin required the change of password upon next login
unset($_userData['account_passwd']);
$this->save_user($_userData);
}
}
if ($_userData['account_lastpwd_change']==0)
{
if (!isset($auth)) $auth =& CreateObject('phpgwapi.auth');
// we call that with NULL for 2nd Parameter as we are doing an admin action.
$auth->setLastPwdChange($_userData['account_id'],NULL, $_userData['account_lastpwd_change']);
}
2005-10-14 19:03:16 +02:00
$apps =& CreateObject('phpgwapi.applications',(int)$_userData['account_id']);
if($_userData['account_permissions'])
2003-04-28 01:16:15 +02:00
{
foreach($_userData['account_permissions'] as $app => $enabled)
{
if($enabled)
{
$apps->add($app);
}
}
}
$apps->save_repository();
2005-10-14 19:03:16 +02:00
$acl =& CreateObject('phpgwapi.acl',$_userData['account_id']);
if($_userData['anonymous'])
{
$acl->add_repository('phpgwapi','anonymous',$_userData['account_id'],1);
}
else
{
$acl->delete_repository('phpgwapi','anonymous',$_userData['account_id']);
}
if(!$_userData['changepassword'])
{
$GLOBALS['egw']->acl->add_repository('preferences','nopasswordchange',$_userData['account_id'],1);
}
else
{
$GLOBALS['egw']->acl->delete_repository('preferences','nopasswordchange',$_userData['account_id']);
}
2005-10-14 19:03:16 +02:00
$GLOBALS['egw']->session->delete_cache((int)$_userData['account_id']);
}
2001-09-03 04:58:25 +02:00
function load_group_managers($account_id)
{
2005-10-14 19:03:16 +02:00
$temp_user = $GLOBALS['egw']->acl->get_ids_for_location($account_id,EGW_ACL_GROUP_MANAGERS,'phpgw_group');
if(!$temp_user)
{
return Array();
}
else
{
$group_user = $temp_user;
}
$account_user = Array();
while(list($key,$user) = each($group_user))
{
$account_user[$user] = ' selected';
}
@reset($account_user);
return $account_user;
}
2001-09-03 04:58:25 +02:00
function load_group_apps($account_id)
{
2005-10-14 19:03:16 +02:00
$apps =& CreateObject('phpgwapi.applications',(int)$account_id);
2001-09-03 04:58:25 +02:00
$app_list = $apps->read_account_specific();
$account_apps = Array();
while(list($key,$app) = each($app_list))
{
$account_apps[$app['name']] = True;
}
@reset($account_apps);
return $account_apps;
}
2001-10-10 17:10:15 +02:00
// xmlrpc functions
function rpc_add_user($data)
{
2001-12-18 07:10:32 +01:00
exit;
if(!$errors = $this->validate_user($data))
2001-10-10 17:10:15 +02:00
{
$result = $this->so->add_user($data);
}
else
{
$result = $errors;
}
return $result;
}
}
?>