2008-02-27 11:20:48 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2016-04-02 16:16:20 +02:00
|
|
|
* EGroup2are API: VFS - Hooks to add/rename/delete user and group home-directories
|
2008-02-27 11:20:48 +01:00
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package api
|
|
|
|
* @subpackage vfs
|
|
|
|
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
2016-04-02 16:16:20 +02:00
|
|
|
* @copyright (c) 2008-16 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
2008-02-27 11:20:48 +01:00
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
2016-04-02 16:16:20 +02:00
|
|
|
namespace EGroupware\Api\Vfs;
|
|
|
|
|
|
|
|
use EGroupware\Api;
|
|
|
|
|
2008-02-27 11:20:48 +01:00
|
|
|
/**
|
2016-04-02 16:16:20 +02:00
|
|
|
* VFS - Hooks to add/rename/delete user and group home-directories
|
2008-02-27 11:20:48 +01:00
|
|
|
*
|
|
|
|
* This class implements the creation, renaming or deletion of home-dirs via some hooks from admin:
|
|
|
|
* - create the homedir if a new user gets created
|
|
|
|
* - rename the homedir if the user-name changes
|
|
|
|
* - delete the homedir or copy its content to an other users homedir, if a user gets deleted
|
|
|
|
* --> these hooks are registered via phpgwapi/setup/setup.inc.php and called by the admin app
|
|
|
|
*/
|
2016-04-02 16:16:20 +02:00
|
|
|
class Hooks
|
2008-02-27 11:20:48 +01:00
|
|
|
{
|
2009-03-27 19:05:42 +01:00
|
|
|
/**
|
|
|
|
* Should we log our calls to the error_log
|
|
|
|
* 0 - no logging
|
|
|
|
* 1 - log method calls
|
|
|
|
*/
|
|
|
|
const LOG_LEVEL = 0;
|
|
|
|
|
2008-02-27 11:20:48 +01:00
|
|
|
/**
|
|
|
|
* Hook called after new accounts have been added
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param int $data['account_id'] numerical id
|
|
|
|
* @param string $data['account_lid'] account-name
|
|
|
|
*/
|
|
|
|
static function addAccount($data)
|
|
|
|
{
|
2009-03-27 19:05:42 +01:00
|
|
|
if (self::LOG_LEVEL > 0) error_log(__METHOD__.'('.array2string($data).')');
|
2008-02-27 11:20:48 +01:00
|
|
|
// create a user-dir
|
2016-04-02 16:16:20 +02:00
|
|
|
Api\Vfs::$is_root = true;
|
2016-06-06 13:59:45 +02:00
|
|
|
if (Api\Vfs::file_exists($dir='/home/'.$data['account_lid']) || Api\Vfs::mkdir($dir, 0700, 0))
|
2009-03-27 19:05:42 +01:00
|
|
|
{
|
2016-08-06 12:41:20 +02:00
|
|
|
Api\Vfs::chown($dir,(int)$data['account_id']);
|
2016-04-02 16:16:20 +02:00
|
|
|
Api\Vfs::chgrp($dir,0);
|
|
|
|
Api\Vfs::chmod($dir,0700); // only user has access
|
2009-03-27 19:05:42 +01:00
|
|
|
}
|
2016-04-02 16:16:20 +02:00
|
|
|
Api\Vfs::$is_root = false;
|
2008-02-27 11:20:48 +01:00
|
|
|
}
|
2008-04-19 11:14:59 +02:00
|
|
|
|
2008-02-27 11:20:48 +01:00
|
|
|
/**
|
|
|
|
* Hook called after accounts has been modified
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param int $data['account_id'] numerical id
|
|
|
|
* @param string $data['account_lid'] new account-name
|
|
|
|
* @param string $data['old_loginid'] old account-name
|
|
|
|
*/
|
2008-02-27 11:33:48 +01:00
|
|
|
static function editAccount($data)
|
2008-02-27 11:20:48 +01:00
|
|
|
{
|
2009-03-27 19:05:42 +01:00
|
|
|
if (self::LOG_LEVEL > 0) error_log(__METHOD__.'('.array2string($data).')');
|
2010-04-21 14:28:27 +02:00
|
|
|
if (empty($data['account_lid']) || empty($data['old_loginid']) || $data['account_lid'] == $data['old_loginid'])
|
|
|
|
{
|
|
|
|
return; // nothing to do here
|
|
|
|
}
|
2008-02-27 11:20:48 +01:00
|
|
|
// rename the user-dir
|
2016-04-02 16:16:20 +02:00
|
|
|
Api\Vfs::$is_root = true;
|
|
|
|
Api\Vfs::rename('/home/'.$data['old_loginid'],'/home/'.$data['account_lid']);
|
|
|
|
Api\Vfs::$is_root = false;
|
2008-02-27 11:20:48 +01:00
|
|
|
}
|
2008-04-19 11:14:59 +02:00
|
|
|
|
2008-02-27 11:20:48 +01:00
|
|
|
/**
|
|
|
|
* Hook called before an account get deleted
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param int $data['account_id'] numerical id
|
|
|
|
* @param string $data['account_lid'] account-name
|
|
|
|
* @param int $data['new_owner'] account-id of new owner, or false if data should get deleted
|
|
|
|
*/
|
2008-02-27 11:33:48 +01:00
|
|
|
static function deleteAccount($data)
|
2008-02-27 11:20:48 +01:00
|
|
|
{
|
2009-03-27 19:05:42 +01:00
|
|
|
if (self::LOG_LEVEL > 0) error_log(__METHOD__.'('.array2string($data).')');
|
2016-04-02 16:16:20 +02:00
|
|
|
Api\Vfs::$is_root = true;
|
2008-04-19 11:14:59 +02:00
|
|
|
if ($data['new_owner'] && ($new_lid = $GLOBALS['egw']->accounts->id2name($data['new_owner'])))
|
2008-02-27 11:20:48 +01:00
|
|
|
{
|
2008-04-19 11:14:59 +02:00
|
|
|
// copy content of user-dir to new owner's user-dir as old-home-$name
|
2016-04-02 16:16:20 +02:00
|
|
|
for ($i=''; file_exists(Api\Vfs::PREFIX.($new_dir = '/home/'.$new_lid.'/old-home-'.$data['account_lid'].$i)); $i++)
|
|
|
|
{
|
2016-04-05 20:39:55 +02:00
|
|
|
|
2016-04-02 16:16:20 +02:00
|
|
|
}
|
|
|
|
Api\Vfs::rename('/home/'.$data['account_lid'],$new_dir);
|
2008-04-19 11:14:59 +02:00
|
|
|
// make the new owner the owner of the dir and it's content
|
2016-04-02 16:16:20 +02:00
|
|
|
Api\Vfs::find($new_dir,array(),array('egw_vfs','chown'),$data['new_owner']);
|
2008-04-19 11:14:59 +02:00
|
|
|
}
|
2009-04-29 21:15:54 +02:00
|
|
|
elseif(!empty($data['account_lid']) && $data['account_lid'] != '/')
|
2008-04-19 11:14:59 +02:00
|
|
|
{
|
|
|
|
// delete the user-directory
|
2016-04-02 16:16:20 +02:00
|
|
|
Api\Vfs::remove('/home/'.$data['account_lid']);
|
2008-02-27 11:20:48 +01:00
|
|
|
}
|
2009-04-29 21:15:54 +02:00
|
|
|
else
|
|
|
|
{
|
2016-04-02 16:16:20 +02:00
|
|
|
throw new Api\Exception\AssertionFailed(__METHOD__.'('.array2string($data).') account_lid NOT set!');
|
2009-04-29 21:15:54 +02:00
|
|
|
}
|
2016-04-02 16:16:20 +02:00
|
|
|
Api\Vfs::$is_root = false;
|
2008-02-27 11:20:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook called after new groups have been added
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param int $data['account_id'] numerical id
|
2014-06-04 11:26:29 +02:00
|
|
|
* @param string $data['account_lid'] group-name
|
2008-02-27 11:20:48 +01:00
|
|
|
*/
|
2008-02-27 11:33:48 +01:00
|
|
|
static function addGroup($data)
|
2008-02-27 11:20:48 +01:00
|
|
|
{
|
2009-03-27 19:05:42 +01:00
|
|
|
if (self::LOG_LEVEL > 0) error_log(__METHOD__.'('.array2string($data).')');
|
2016-04-02 16:16:20 +02:00
|
|
|
if (empty($data['account_lid'])) throw new Api\Exception\WrongParameter('account_lid must not be empty!');
|
2014-06-04 11:26:29 +02:00
|
|
|
|
2008-02-27 11:20:48 +01:00
|
|
|
// create a group-dir
|
2016-04-02 16:16:20 +02:00
|
|
|
Api\Vfs::$is_root = true;
|
2016-06-06 13:59:45 +02:00
|
|
|
if (Api\Vfs::file_exists($dir='/home/'.$data['account_lid']) || Api\Vfs::mkdir($dir, 0070, 0))
|
2009-03-27 19:05:42 +01:00
|
|
|
{
|
2016-04-02 16:16:20 +02:00
|
|
|
Api\Vfs::chown($dir,0);
|
|
|
|
Api\Vfs::chgrp($dir,abs($data['account_id'])); // gid in Vfs is positiv!
|
|
|
|
Api\Vfs::chmod($dir,0070); // only group has access
|
2009-03-27 19:05:42 +01:00
|
|
|
}
|
2016-04-02 16:16:20 +02:00
|
|
|
Api\Vfs::$is_root = false;
|
2008-02-27 11:20:48 +01:00
|
|
|
}
|
2008-04-19 11:14:59 +02:00
|
|
|
|
2008-02-27 11:20:48 +01:00
|
|
|
/**
|
|
|
|
* Hook called after group has been modified
|
|
|
|
*
|
2011-06-10 13:00:40 +02:00
|
|
|
* Checks if group has been renamed and renames the group directory too,
|
|
|
|
* or if the group directory exists and creates it if not.
|
|
|
|
*
|
2008-02-27 11:20:48 +01:00
|
|
|
* @param array $data
|
|
|
|
* @param int $data['account_id'] numerical id
|
2014-06-04 11:26:29 +02:00
|
|
|
* @param string $data['account_lid'] new group-name
|
2008-02-27 11:20:48 +01:00
|
|
|
* @param string $data['old_name'] old account-name
|
|
|
|
*/
|
2008-02-27 11:33:48 +01:00
|
|
|
static function editGroup($data)
|
2008-02-27 11:20:48 +01:00
|
|
|
{
|
2009-03-27 19:05:42 +01:00
|
|
|
if (self::LOG_LEVEL > 0) error_log(__METHOD__.'('.array2string($data).')');
|
2016-04-05 20:39:55 +02:00
|
|
|
if (empty($data['account_lid']) || empty($data['old_name'])) throw new Api\Exception\WrongParameter('account_lid and old_name must not be empty!');
|
2008-04-19 11:14:59 +02:00
|
|
|
|
2014-06-04 11:26:29 +02:00
|
|
|
if ($data['account_lid'] == $data['old_name'])
|
2011-06-10 13:00:40 +02:00
|
|
|
{
|
|
|
|
// check if group directory exists and create it if not (by calling addGroup hook)
|
2016-04-02 16:16:20 +02:00
|
|
|
if (!Api\Vfs::stat('/home/'.$data['account_lid']))
|
2011-06-10 13:00:40 +02:00
|
|
|
{
|
|
|
|
self::addGroup($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// rename the group-dir
|
2016-04-02 16:16:20 +02:00
|
|
|
Api\Vfs::$is_root = true;
|
|
|
|
Api\Vfs::rename('/home/'.$data['old_name'],'/home/'.$data['account_lid']);
|
|
|
|
Api\Vfs::$is_root = false;
|
2011-06-10 13:00:40 +02:00
|
|
|
}
|
2008-02-27 11:20:48 +01:00
|
|
|
}
|
2008-04-19 11:14:59 +02:00
|
|
|
|
2008-02-27 11:20:48 +01:00
|
|
|
/**
|
|
|
|
* Hook called before a group get deleted
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param int $data['account_id'] numerical id
|
2014-06-04 11:26:29 +02:00
|
|
|
* @param string $data['account_lid'] account-name
|
2008-02-27 11:20:48 +01:00
|
|
|
*/
|
2008-02-27 11:33:48 +01:00
|
|
|
static function deleteGroup($data)
|
2008-02-27 11:20:48 +01:00
|
|
|
{
|
2009-03-27 19:05:42 +01:00
|
|
|
if (self::LOG_LEVEL > 0) error_log(__METHOD__.'('.array2string($data).')');
|
2009-04-29 21:15:54 +02:00
|
|
|
|
2014-06-04 11:26:29 +02:00
|
|
|
if(empty($data['account_lid']) || $data['account_lid'] == '/')
|
2009-04-29 21:15:54 +02:00
|
|
|
{
|
2016-04-02 16:16:20 +02:00
|
|
|
throw new Api\Exception\AssertionFailed(__METHOD__.'('.array2string($data).') account_lid NOT set!');
|
2009-04-29 21:15:54 +02:00
|
|
|
}
|
2008-02-27 11:20:48 +01:00
|
|
|
// delete the group-directory
|
2016-04-02 16:16:20 +02:00
|
|
|
Api\Vfs::$is_root = true;
|
|
|
|
Api\Vfs::remove('/home/'.$data['account_lid']);
|
|
|
|
Api\Vfs::$is_root = false;
|
2008-02-27 11:20:48 +01:00
|
|
|
}
|
|
|
|
}
|