* CalDAV: implemented calendar proxy support and cleaned up principals tree

This commit is contained in:
Ralf Becker 2011-09-15 22:45:00 +00:00
parent 0d47709ed1
commit 6ce0906838

View File

@ -31,7 +31,7 @@ class groupdav_principals extends groupdav_handler
* @param string $app 'calendar', 'addressbook' or 'infolog' * @param string $app 'calendar', 'addressbook' or 'infolog'
* @param int $debug=null debug-level to set * @param int $debug=null debug-level to set
* @param string $base_uri=null base url of handler * @param string $base_uri=null base url of handler
* @param string $principalURL=null pricipal url of handler * @param string $principalURL=null principal url of handler
*/ */
function __construct($app,$debug=null,$base_uri=null,$principalURL=null) function __construct($app,$debug=null,$base_uri=null,$principalURL=null)
{ {
@ -73,12 +73,12 @@ class groupdav_principals extends groupdav_handler
case 'groups': case 'groups':
$files['files'] = $this->propfind_groups($name,$rest,$options); $files['files'] = $this->propfind_groups($name,$rest,$options);
break; break;
case 'resources': /*case 'resources':
$files['files'] = $this->propfind_resources($name,$rest,$options); $files['files'] = $this->propfind_resources($name,$rest,$options);
break; break;
case '__uids__': case '__uids__':
$files['files'] = $this->propfind_uids($name,$rest,$options); $files['files'] = $this->propfind_uids($name,$rest,$options);
break; break;*/
case '': case '':
$files['files'] = $this->propfind_principals($options); $files['files'] = $this->propfind_principals($options);
break; break;
@ -90,41 +90,6 @@ class groupdav_principals extends groupdav_handler
return $files['files']; return $files['files'];
} }
return true; return true;
list(,,$id) = explode('/',$path);
if ($id && !($id = $this->accounts->id2name($id)))
{
return false;
}
foreach($id ? array($this->accounts->read($id)) : $this->accounts->search(array('type' => 'accounts')) as $account)
{
$displayname = translation::convert($account['account_fullname'],
translation::charset(),'utf-8');
$props = array(
HTTP_WebDAV_Server::mkprop('displayname',$displayname),
HTTP_WebDAV_Server::mkprop('getetag',$this->get_etag($account)),
HTTP_WebDAV_Server::mkprop('resourcetype',array(
HTTP_WebDAV_Server::mkprop('principal', ''))),
HTTP_WebDAV_Server::mkprop('alternate-URI-set',''),
HTTP_WebDAV_Server::mkprop('principal-URL',$this->base_uri.'/principals/'.$account['account_lid']),
HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'calendar-home-set',array(
HTTP_WebDAV_Server::mkprop('href',$this->base_uri.'/'.$account['account_lid'].'/'))),
HTTP_WebDAV_Server::mkprop(groupdav::CARDDAV,'addressbook-home-set',array(
HTTP_WebDAV_Server::mkprop('href',$this->base_uri.'/'.$account['account_lid'].'/'))),
);
foreach($this->accounts->memberships($account['account_id']) as $gid => $group)
{
$props[] = HTTP_WebDAV_Server::mkprop('group-membership',$this->base_uri.'/groups/'.$group);
}
$files['files'][] = array(
'path' => '/principals/'.$account['account_lid'],
'props' => $props,
);
if ($this->debug > 1) error_log(__METHOD__."($path) path=/principals/".$account['account_lid'].', props='.array2string($props));
}
return files;
} }
/** /**
@ -137,15 +102,13 @@ class groupdav_principals extends groupdav_handler
*/ */
protected function propfind_users($name,$rest,array $options) protected function propfind_users($name,$rest,array $options)
{ {
//echo "<p>".__METHOD__."($name,$rest,".array2string($options).")</p>\n"; error_log(__METHOD__."($name,$rest,".array2string($options).')');
if (empty($name)) if (empty($name))
{ {
$files = array(); $files = array();
// add /pricipals/users/ entry // add /pricipals/users/ entry
$files[] = $this->add_collection('/principals/users/',array( $files[] = $this->add_collection('/principals/users/');
HTTP_WebDAV_Server::mkprop('current-user-principal',array(
HTTP_WebDAV_Server::mkprop('href',$this->base_uri.'/principals/users/'.$GLOBALS['egw_info']['user']['account_lid'].'/'))),
));
if ($options['depth']) if ($options['depth'])
{ {
// add all users // add all users
@ -162,21 +125,21 @@ class groupdav_principals extends groupdav_handler
{ {
return '404 Not Found'; return '404 Not Found';
} }
while (substr($rest,-1) == '/') $rest = substr($rest,0,-1);
switch((string)$rest) switch((string)$rest)
{ {
case '': case '':
$files[] = $this->add_account($account); $files[] = $this->add_account($account);
if ($options['depth']) if ($options['depth'])
{ {
$files[] = $this->add_collection('/principals/users/'.$account['account_lid'].'/calendar-proxy-read'); $files[] = $this->add_proxys('users/'.$account['account_lid'], 'calendar-proxy-read');
$files[] = $this->add_collection('/principals/users/'.$account['account_lid'].'/calendar-proxy-write'); $files[] = $this->add_proxys('users/'.$account['account_lid'], 'calendar-proxy-write');
} }
break; break;
case 'calendar-proxy-read': case 'calendar-proxy-read':
case 'calendar-proxy-write': case 'calendar-proxy-write':
$files = array(); $files = array();
$files[] = $this->add_collection('/principals/users/'.$account['account_lid'].'/'.$rest); $files[] = $this->add_proxys('users/'.$account['account_lid'], $rest);
// add proxys
break; break;
default: default:
return '404 Not Found'; return '404 Not Found';
@ -200,10 +163,8 @@ class groupdav_principals extends groupdav_handler
{ {
$files = array(); $files = array();
// add /pricipals/users/ entry // add /pricipals/users/ entry
$files[] = $this->add_collection('/principals/groups/',array( $files[] = $this->add_collection('/principals/groups/');
HTTP_WebDAV_Server::mkprop('current-user-principal',array(
HTTP_WebDAV_Server::mkprop('href',$this->base_uri.'/principals/users/'.$GLOBALS['egw_info']['user']['account_lid'].'/'))),
));
if ($options['depth']) if ($options['depth'])
{ {
// add all users // add all users
@ -220,18 +181,21 @@ class groupdav_principals extends groupdav_handler
{ {
return '404 Not Found'; return '404 Not Found';
} }
while (substr($rest,-1) == '/') $rest = substr($rest,0,-1);
switch((string)$rest) switch((string)$rest)
{ {
case '': case '':
$files[] = $this->add_group($account); $files[] = $this->add_group($account);
$files[] = $this->add_collection('/principals/groups/'.$account['account_lid'].'/calendar-proxy-read'); if ($options['depth'])
$files[] = $this->add_collection('/principals/groups/'.$account['account_lid'].'/calendar-proxy-write'); {
$files[] = $this->add_proxys('groups/'.$account['account_lid'], 'calendar-proxy-read');
$files[] = $this->add_proxys('groups/'.$account['account_lid'], 'calendar-proxy-write');
}
break; break;
case 'calendar-proxy-read': case 'calendar-proxy-read':
case 'calendar-proxy-write': case 'calendar-proxy-write':
$files = array(); $files = array();
$files[] = $this->add_collection('/principals/groups/'.$account['account_lid'].'/'.$rest); $files[] = $this->add_proxys('groups/'.$account['account_lid'], $rest);
// add proxys
break; break;
default: default:
return '404 Not Found'; return '404 Not Found';
@ -248,19 +212,6 @@ class groupdav_principals extends groupdav_handler
*/ */
protected function add_account(array $account) protected function add_account(array $account)
{ {
//echo "<p>".__METHOD__."(".array2string($account).")</p>\n";
$displayname = translation::convert($account['account_fullname'],
translation::charset(),'utf-8');
$memberships = array();
foreach($this->accounts->memberships($account['account_id']) as $gid => $group)
{
if ($group)
{
$memberships[] = HTTP_WebDAV_Server::mkprop('href',
$this->base_uri.'/principals/groups/'.$group);
}
}
$addressbooks = $calendars = array(); $addressbooks = $calendars = array();
if ($account['account_id'] == $GLOBALS['egw_info']['user']['account_id']) if ($account['account_id'] == $GLOBALS['egw_info']['user']['account_id'])
{ {
@ -313,15 +264,12 @@ class groupdav_principals extends groupdav_handler
$calendars[] = HTTP_WebDAV_Server::mkprop('href', $calendars[] = HTTP_WebDAV_Server::mkprop('href',
$this->base_uri.'/'.$account['account_lid'].'/'); $this->base_uri.'/'.$account['account_lid'].'/');
} }
$props = array( $displayname = translation::convert($account['account_fullname'], translation::charset(),'utf-8');
return $this->add_principal('users/'.$account['account_lid'], array(
HTTP_WebDAV_Server::mkprop('displayname',$displayname), HTTP_WebDAV_Server::mkprop('displayname',$displayname),
HTTP_WebDAV_Server::mkprop('getetag',$this->get_etag($account)),
HTTP_WebDAV_Server::mkprop('resourcetype',array(
HTTP_WebDAV_Server::mkprop('principal', ''))),
HTTP_WebDAV_Server::mkprop('alternate-URI-set',array( HTTP_WebDAV_Server::mkprop('alternate-URI-set',array(
HTTP_WebDAV_Server::mkprop('href','MAILTO:'.$account['account_email']))), HTTP_WebDAV_Server::mkprop('href','MAILTO:'.$account['account_email']))),
HTTP_WebDAV_Server::mkprop('principal-URL',array(
HTTP_WebDAV_Server::mkprop('href',$this->base_uri.'/principals/users/'.$account['account_lid'].'/'))),
HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'calendar-home-set',$calendars), HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'calendar-home-set',$calendars),
HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'calendar-user-address-set',array( HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'calendar-user-address-set',array(
HTTP_WebDAV_Server::mkprop('href','MAILTO:'.$account['account_email']), HTTP_WebDAV_Server::mkprop('href','MAILTO:'.$account['account_email']),
@ -336,17 +284,13 @@ class groupdav_principals extends groupdav_handler
HTTP_WebDAV_Server::mkprop(groupdav::CALENDARSERVER,'record-type','user'), HTTP_WebDAV_Server::mkprop(groupdav::CALENDARSERVER,'record-type','user'),
HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'calendar-user-type','INDIVIDUAL'), HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'calendar-user-type','INDIVIDUAL'),
HTTP_WebDAV_Server::mkprop(groupdav::CARDDAV,'addressbook-home-set',$addressbooks), HTTP_WebDAV_Server::mkprop(groupdav::CARDDAV,'addressbook-home-set',$addressbooks),
HTTP_WebDAV_Server::mkprop('group-member-ship', $memberships), $this->principal_set('group-membership', $this->accounts->memberships($account['account_id']),
'calendar', $account['account_id']), // add proxy-rights
HTTP_WebDAV_Server::mkprop('supported-report-set',array( HTTP_WebDAV_Server::mkprop('supported-report-set',array(
HTTP_WebDAV_Server::mkprop('supported-report',array( HTTP_WebDAV_Server::mkprop('supported-report',array(
HTTP_WebDAV_Server::mkprop('report',array( HTTP_WebDAV_Server::mkprop('report',array(
HTTP_WebDAV_Server::mkprop('acl-principal-prop-set'))))))), HTTP_WebDAV_Server::mkprop('acl-principal-prop-set'))))))),
); ), array(), $this->get_etag($account));
if ($this->debug > 1) error_log(__METHOD__."($path) path=/principals/users/".$account['account_lid'].', props='.array2string($props));
return array(
'path' => '/principals/users/'.$account['account_lid'].'/',
'props' => $props,
);
} }
/** /**
@ -357,22 +301,10 @@ class groupdav_principals extends groupdav_handler
*/ */
protected function add_group(array $account) protected function add_group(array $account)
{ {
$displayname = translation::convert(lang('Group').' '.$account['account_lid'], $displayname = translation::convert(lang('Group').' '.$account['account_lid'], translation::charset(), 'utf-8');
translation::charset(),'utf-8');
$members = array(); return $this->add_principal('groups/'.$account['account_lid'], array(
foreach($this->accounts->members($account['account_id']) as $gid => $user)
{
if ($user)
{
$members[] = HTTP_WebDAV_Server::mkprop('href',
$this->base_uri.'/principals/users/'.$user);
}
}
$props = array(
HTTP_WebDAV_Server::mkprop('displayname',$displayname), HTTP_WebDAV_Server::mkprop('displayname',$displayname),
HTTP_WebDAV_Server::mkprop('getetag',$this->get_etag($account)),
HTTP_WebDAV_Server::mkprop('resourcetype',array(
HTTP_WebDAV_Server::mkprop('principal', ''))),
HTTP_WebDAV_Server::mkprop('alternate-URI-set',''), HTTP_WebDAV_Server::mkprop('alternate-URI-set',''),
HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'calendar-home-set',array( HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'calendar-home-set',array(
HTTP_WebDAV_Server::mkprop('href',$this->base_uri.'/'.$account['account_lid'].'/'))), HTTP_WebDAV_Server::mkprop('href',$this->base_uri.'/'.$account['account_lid'].'/'))),
@ -380,18 +312,8 @@ class groupdav_principals extends groupdav_handler
HTTP_WebDAV_Server::mkprop('href',$this->base_uri.'/'.$account['account_lid'].'/'))), HTTP_WebDAV_Server::mkprop('href',$this->base_uri.'/'.$account['account_lid'].'/'))),
HTTP_WebDAV_Server::mkprop(groupdav::CALENDARSERVER,'record-type','group'), HTTP_WebDAV_Server::mkprop(groupdav::CALENDARSERVER,'record-type','group'),
HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'calendar-user-type','GROUP'), HTTP_WebDAV_Server::mkprop(groupdav::CALDAV,'calendar-user-type','GROUP'),
HTTP_WebDAV_Server::mkprop('group-member-set', $members), $this->principal_set('group-member-set', $this->accounts->members($account['account_id'])),
//HTTP_WebDAV_Server::mkprop('principal-URL',array(self::mkprop('href',$this->principalURL))), ), array(), $this->get_etag($account));
);
$files['files'][] = array(
'path' => '/principals/groups/'.$account['account_lid'].'/',
'props' => $props,
);
if ($this->debug > 1) error_log(__METHOD__."($path) path=/principals/groups/".$account['account_lid'].', props='.array2string($props));
return array(
'path' => '/principals/groups/'.$account['account_lid'].'/',
'props' => $props,
);
} }
/** /**
@ -399,16 +321,31 @@ class groupdav_principals extends groupdav_handler
* *
* @param string $path * @param string $path
* @param array $props=array() extra properties 'resourcetype' is added anyway * @param array $props=array() extra properties 'resourcetype' is added anyway
* @return array * @param array $additional_resource_types=array() additional resource-types, collection and principal are always added
* @param string $etag=''
* @return array with values for keys 'path' and 'props'
*/ */
protected function add_collection($path,$props=array()) protected function add_collection($path, array $props = array(), array $additional_resource_types=array(), $etag='')
{ {
//echo "<p>".__METHOD__."($path,".array($props).")</p>\n"; // resourcetype: collection + $additional_resource_types
$props[] = HTTP_WebDAV_Server::mkprop('resourcetype',array( $props[] = HTTP_WebDAV_Server::mkprop('resourcetype',array_merge(array(
HTTP_WebDAV_Server::mkprop('collection',''), HTTP_WebDAV_Server::mkprop('collection',''),
HTTP_WebDAV_Server::mkprop('resourcetype',array( ),$additional_resource_types));
HTTP_WebDAV_Server::mkprop('principal', ''))),
)); // props for all collections: current-user-principal and principal-collection-set
$props[] = HTTP_WebDAV_Server::mkprop('current-user-principal',array(
HTTP_WebDAV_Server::mkprop('href',$this->base_uri.'/principals/users/'.$GLOBALS['egw_info']['user']['account_lid'].'/')));
$props[] = HTTP_WebDAV_Server::mkprop('principal-collection-set',array(
HTTP_WebDAV_Server::mkprop('href',$this->base_uri.'/principals/')));
// required per WebDAV standard
$props[] = HTTP_WebDAV_Server::mkprop('getcontentlength', '');
$props[] = HTTP_WebDAV_Server::mkprop('getlastmodified', '');
$props[] = HTTP_WebDAV_Server::mkprop('getcontenttype', '');
$props[] = HTTP_WebDAV_Server::mkprop('getetag', $etag);
if ($this->debug > 1) error_log(__METHOD__."(path='$path', props=".array2string($props).')');
return array( return array(
'path' => $path, 'path' => $path,
'props' => $props, 'props' => $props,
@ -416,7 +353,108 @@ class groupdav_principals extends groupdav_handler
} }
/** /**
* Do propfind of /pricipals * Add a principal collection
*
* @param string $principal relative to principal-collection-set, eg. "users/username"
* @param array $props=array() extra properties 'resourcetype' is added anyway
* @param array $additional_resource_types=array() additional resource-types, collection and principal are always added
* @param string $principal_url=null include given principal url, relative to principal-collection-set
* @param string $etag=''
* @return array with values for keys 'path' and 'props'
*/
protected function add_principal($principal, array $props = array(), array $additional_resource_types=array(), $etag='')
{
$additional_resource_types[] = HTTP_WebDAV_Server::mkprop('principal', '');
if (!$principal_url) $principal_url = $principal;
$props[] = HTTP_WebDAV_Server::mkprop('principal-URL',array(
HTTP_WebDAV_Server::mkprop('href',$this->base_uri.'/principals/'.$principal.'/')));
return $this->add_collection('/principals/'.$principal.'/', $props, $additional_resource_types, $etag);
}
/**
* Add a proxy collection for given principal and type
*
* A proxy is a user or group who has the right to act on behalf of the user
*
* @param string $principal relative to principal-collection-set, eg. "users/username"
* @param string $type eg. 'calendar-proxy-read' or 'calendar-proxy-write'
* @param array $proxys=array()
* @return array with values for 'path' and 'props'
*/
protected function add_proxys($principal, $type, array $proxys=array())
{
list($app,,$what) = explode('-', $type);
$right = $what == 'write' ? EGW_ACL_EDIT : EGW_ACL_READ;
$mask = $what == 'write' ? EGW_ACL_EDIT : EGW_ACL_EDIT|EGW_ACL_READ; // do NOT report write+read in read
//echo "<p>type=$type --> app=$app, what=$what --> right=$right, mask=$mask</p>\n";
list($account_type,$account) = explode('/', $principal);
$account = $GLOBALS['egw']->accounts->name2id($account, 'account_lid', $account_type[0]);
$proxys = array();
foreach($GLOBALS['egw']->acl->get_all_location_rights($account, $app, $app != 'addressbook') as $account_id => $rights)
{
if ($account_id !== 'run' && $account_id != $account && ($rights & $mask) == $right &&
($account_lid = $GLOBALS['egw']->accounts->id2name($account_id)))
{
$proxys[$account_id] = $account_lid;
// for groups add members too, if app is not addressbook
if ($account_id < 0 && $app != 'addressbook')
{
foreach($GLOBALS['egw']->accounts->members($account_id) as $account_id => $account_lid)
{
$proxys[$account_id] = $account_lid;
}
}
}
//echo "<p>$account_id ($account_lid): (rights=$rights & mask=$mask) == right=$right --> ".array2string(($rights & $mask) == $right)."</p>\n";
}
return $this->add_principal($principal.'/'.$type, array(
$this->principal_set('group-member-set', $proxys),
), array(
HTTP_WebDAV_Server::mkprop(groupdav::CALENDARSERVER, $type, ''),
),'EGw-'.md5(serialize($proxys)).'-wGE');
}
/**
* Create a named property with set or principal-urls
*
* @param string $prop egw. 'group-member-set' or 'membership'
* @param array $accounts=array() account_id => account_lid pairs
* @param string|array $app_proxys=null applications for which proxys should be added
* @param int $account who is the proxy
*/
protected function principal_set($prop, array $accounts=array(), $add_proxys=null, $account=null)
{
$set = array();
foreach($accounts as $account_id => $account_lid)
{
$set[] = HTTP_WebDAV_Server::mkprop('href', $this->base_uri.'/principals/'.($account_id < 0 ? 'groups/' : 'users/').$account_lid);
}
if ($add_proxys)
{
foreach((array)$add_proxys as $app)
{
foreach($GLOBALS['egw']->acl->get_grants($app, $app != 'addressbook', $account) as $account_id => $rights)
{
if ($account_id != $account && ($rights & EGW_ACL_READ) &&
($account_lid = $GLOBALS['egw']->accounts->id2name($account_id)))
{
$set[] = HTTP_WebDAV_Server::mkprop('href', $this->base_uri.'/principals/'.
($account_id < 0 ? 'groups/' : 'users/').
$account_lid.'/'.$app.'-proxy-'.($rights & EGW_ACL_EDIT ? 'write' : 'read'));
}
}
}
}
return HTTP_WebDAV_Server::mkprop($prop, $set);
}
/**
* Do propfind of /principals/
* *
* @param string $name name of group or empty * @param string $name name of group or empty
* @param string $rest name of rest of path behind group-name * @param string $rest name of rest of path behind group-name
@ -427,10 +465,7 @@ class groupdav_principals extends groupdav_handler
{ {
//echo "<p>".__METHOD__."(".array($options).")</p>\n"; //echo "<p>".__METHOD__."(".array($options).")</p>\n";
$files = array(); $files = array();
$files[] = $this->add_collection('/principals/',array( $files[] = $this->add_collection('/principals/');
HTTP_WebDAV_Server::mkprop('current-user-principal',array(
HTTP_WebDAV_Server::mkprop('href',$this->base_uri.'/principals/users/'.$GLOBALS['egw_info']['user']['account_lid'].'/'))),
));
if ($options['depth']) if ($options['depth'])
{ {
@ -541,7 +576,9 @@ class groupdav_principals extends groupdav_handler
$account = $this->read($account); $account = $this->read($account);
} }
return 'EGw-'.$account['account_id'].':'.md5(serialize($account)). return 'EGw-'.$account['account_id'].':'.md5(serialize($account)).
// as the pricipal of current user is influenced by GroupDAV prefs, we have to include them in the etag // add md5 from calendar grants, as they are listed as memberships
':'.md5(serialize($GLOBALS['egw']->acl->get_grants('calendar', true, $account['account_id']))).
// as the principal of current user is influenced by GroupDAV prefs, we have to include them in the etag
($account['account_id'] == $GLOBALS['egw_info']['user']['account_id'] ? ($account['account_id'] == $GLOBALS['egw_info']['user']['account_id'] ?
':'.md5(serialize($GLOBALS['egw_info']['user']['preferences']['groupdav'])) : '').'-wGE'; ':'.md5(serialize($GLOBALS['egw_info']['user']['preferences']['groupdav'])) : '').'-wGE';
} }