Added option for ACL for public pages

And fixed a few things in ACL to make sure we use clean IDs and use
right string if
This commit is contained in:
Simon DELAGE 2014-09-07 09:20:39 +02:00
parent ac71e08bdf
commit 942e6c20e9

View file

@ -15,11 +15,15 @@ require_once (DOKU_PLUGIN . '/acl/admin.php');
class action_plugin_userhomepage extends DokuWiki_Action_Plugin{
function register(&$controller) {
$controller->register_hook('AUTH_LOGIN_CHECK', 'AFTER', $this, 'init',array());
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'redirect',array());
$controller->register_hook('AUTH_LOGIN_CHECK', 'AFTER', $this, 'templates',array());
}
function templates(&$event, $param) {
function init(&$event, $param) {
global $conf;
global $INFO;
// COPY TEMPLATES IF NEEDED
if (!file_exists(DOKU_INC.$this->getConf('templates_path').'/userhomepage_private.txt')) {
// If version 3.0.4 was installed, 'templatepath' option isn't empty and points to former template
if (file_exists(DOKU_INC.$this->getConf('templatepath'))) {
if (!copy(DOKU_INC.$this->getConf('templatepath'), DOKU_INC.$this->getConf('templates_path').'/userhomepage_private.txt')) {
// echo ' An error occured while attempting to copy old template to '.DOKU_INC.$this->getConf('templates_path').'/userhomepage_private.txt';
@ -39,6 +43,44 @@ class action_plugin_userhomepage extends DokuWiki_Action_Plugin{
// echo ' Successfully copied public template.';
}
}
// TARGETS
if ($this->getConf('group_by_name')) {
// private:s:simon or private:s:simon_delage
$this->private_ns = cleanID($this->getConf('users_namespace').':'.substr($this->privateNamespace(), 0, 1).':'. $this->privateNamespace());
} else {
// private:simon or private:simon_delage
$this->private_ns = cleanID($this->getConf('users_namespace').':'. $this->privateNamespace());
}
// private:simon:start.txt
$this->private_page = $this->private_ns . ':' . $this->privatePage();
// user:simon.txt
$this->public_page = cleanID($this->getConf('public_pages_ns').':'. $_SERVER['REMOTE_USER']);
// ACL
// For private namespace
if ($this->getConf('set_permissions')) {
$acl = new admin_plugin_acl();
$acl->_acl_add(cleanID($this->getConf('users_namespace')).':*', '@ALL', (int)$this->getConf('set_permissions_others'));
$acl->_acl_add(cleanID($this->getConf('users_namespace')).':*', '@user', (int)$this->getConf('set_permissions_others'));
// If use_name_string is enabled, we can't use ACL wildcard
if ($this->getConf('use_name_string')) {
$ns = $this->private_ns.':*';
$acl->_acl_add($ns, $INFO['userinfo']['name'], AUTH_DELETE);
} else {
$acl->_acl_add(cleanID($this->getConf('users_namespace')).':%USER%:*', '%USER%', AUTH_DELETE);
}
}
// For public user pages
if ($this->getConf('set_permissions_public')) {
$acl->_acl_add(cleanID($this->getConf('public_pages_ns')).':*', '@ALL', AUTH_READ);
$acl->_acl_add(cleanID($this->getConf('public_pages_ns')).':*', '@user', AUTH_READ);
$acl->_acl_add(cleanID($this->getConf('public_pages_ns')).':%USER%', '%USER%', AUTH_EDIT);
}
// Some lines in conf/acl.auth.php file have probably been duplicated so let's read the file
$lines = file(DOKU_INC.'conf/acl.auth.php');
// And only keep unique lines (OK, we loose an empty comment line...)
$lines = array_unique($lines);
// Write things back to conf/acl.auth.php
file_put_contents(DOKU_INC.'conf/acl.auth.php', implode($lines));
}
function redirect(&$event, $param) {
global $conf;
@ -46,25 +88,13 @@ class action_plugin_userhomepage extends DokuWiki_Action_Plugin{
$created = array();
// If user just logged in
if (($_SERVER['REMOTE_USER']!=null)&&($_REQUEST['do']=='login')) {
// Determine targets
if ($this->getConf('group_by_name')) {
// private:s:simon
$this->private_ns = cleanID($this->getConf('users_namespace').':'.strtolower(substr($this->privateNamespace(), 0, 1)).':'. $this->privateNamespace());
} else {
// private:simon
$this->private_ns = cleanID($this->getConf('users_namespace').':'. $this->privateNamespace());
}
// private:simon:start.txt
$this->private_page = cleanID($this->private_ns . ':' . $this->privatePage());
// user:simon.txt
$this->public_page = cleanID($this->getConf('public_pages_ns').':'. $_SERVER['REMOTE_USER']);
// if private page doesn't exists, create it (from template)
if ($this->getConf('create_private_ns') && !page_exists($this->private_page) && !checklock($this->private_page) && !checkwordblock()) {
// Read private start page template
$this->private_page_template = DOKU_INC.$this->getConf('templates_path').'/userhomepage_private.txt';
// Create private page
lock($this->private_page);
saveWikiText($this->private_page,$this->_template_private(),$lang['created']);
saveWikiText($this->private_page,$this->apply_template('private'),$lang['created']);
unlock($this->private_page);
// Note that we created private page
$created['private'] = true;
@ -76,33 +106,11 @@ class action_plugin_userhomepage extends DokuWiki_Action_Plugin{
$this->public_page_template = DOKU_INC.$this->getConf('templates_path').'/userhomepage_public.txt';
// Create public page
lock($this->public_page);
saveWikiText($this->public_page,$this->_template_public(),$lang['created']);
saveWikiText($this->public_page,$this->apply_template('public'),$lang['created']);
unlock($this->public_page);
// Note that we created public page
$created['public'] = true;
}
// Set ACL?
if ($this->getConf('set_permissions')) {
$acl = new admin_plugin_acl();
$acl->_acl_add($this->getConf('users_namespace').':*', '@ALL', (int)$this->getConf('set_permissions_others'));
$acl->_acl_add($this->getConf('users_namespace').':*', '@user', (int)$this->getConf('set_permissions_others'));
if ($this->getConf('use_name_string')) {
$ns = cleanID($this->private_ns).':*';
$acl->_acl_add($ns, strtolower($_SERVER['REMOTE_USER']), AUTH_DELETE);
} else {
$acl->_acl_add($this->getConf('users_namespace').':%USER%:*', '%USER%', AUTH_DELETE);
}
// ACL for public user pages
$acl->_acl_add($this->getConf('public_pages_ns').':*', '@ALL', AUTH_READ);
$acl->_acl_add($this->getConf('public_pages_ns').':*', '@user', AUTH_READ);
$acl->_acl_add($this->getConf('public_pages_ns').':%USER%', '%USER%', AUTH_EDIT);
}
// If the 2 lines concerning set_permissions_others above allready existed in conf/acl.auth.php file they've been duplicated so let's read the file
$lines = file(DOKU_INC.'conf/acl.auth.php');
// Only keep unique lines (OK, we loose an empty comment line...)
$lines = array_unique($lines);
// Write things back to conf/acl.auth.php
file_put_contents(DOKU_INC.'conf/acl.auth.php', implode($lines));
// If Translation plugin is active, determine if we're at wikistart
if (!plugin_isdisabled('translation')) {
foreach (explode(' ',$conf['plugin']['translation']['translations']) as $lang){
@ -127,7 +135,7 @@ class action_plugin_userhomepage extends DokuWiki_Action_Plugin{
function privateNamespace() {
if ( $this->getConf('use_name_string')) {
global $INFO;
$raw_string = $INFO['userinfo']['name'];
$raw_string = cleanID($INFO['userinfo']['name']);
// simon_delage
return $raw_string;
} else {
@ -136,35 +144,52 @@ class action_plugin_userhomepage extends DokuWiki_Action_Plugin{
}
}
function privatePage() {
if ( $this->getConf('use_start_page')) {
if ($this->getConf('use_start_page')) {
global $conf;
return $conf['start'];
return cleanID($conf['start']);
} else {
return $this->homeNamespace();
return $this->privateNamespace();
}
}
function _template_private() {
// function _template_private() {
// global $INFO;
// global $lang;
// $content = io_readFile($this->private_page_template, false);
// $content = str_replace('@PRIVATENAMESPACE@',$this->getLang('privatenamespace'),$content);
// // Improved template process to use any replacement patterns from https://www.dokuwiki.org/namespace_templates
// // Code by Christian Nancy
// // Build a fake data structure for the parser
// $data = array('tpl' => $content, 'id' => $this->private_page);
// // Use the built-in parser
// $content = parsePageTemplate($data);
// return $content;
// }
// function _template_public() {
// global $INFO;
// global $lang;
// $content = io_readFile($this->public_page_template, false);
// $content = str_replace('@PUBLICPAGE@',$this->getLang('publicpage'),$content);
// // Improved template process to use any replacement patterns from https://www.dokuwiki.org/namespace_templates
// // Code by Christian Nancy
// // Build a fake data structure for the parser
// $data = array('tpl' => $content, 'id' => $this->private_page);
// // Use the built-in parser
// $content = parsePageTemplate($data);
// return $content;
// }
function apply_template($type) {
global $INFO;
global $lang;
$content = io_readFile($this->private_page_template, false);
$content = str_replace('@PRIVATENAMESPACE@',$this->getLang('privatenamespace'),$content);
// Improved template process to use any replacement patterns from https://www.dokuwiki.org/namespace_templates
// Code by Christian Nancy
// Build a fake data structure for the parser
$data = array('tpl' => $content, 'id' => $this->private_page);
// Use the built-in parser
$content = parsePageTemplate($data);
return $content;
}
function _template_public() {
global $INFO;
global $lang;
$content = io_readFile($this->public_page_template, false);
$content = str_replace('@PUBLICPAGE@',$this->getLang('publicpage'),$content);
// Improved template process to use any replacement patterns from https://www.dokuwiki.org/namespace_templates
// Code by Christian Nancy
// Build a fake data structure for the parser
$data = array('tpl' => $content, 'id' => $this->private_page);
// Improved template process to use any replacement patterns from https://www.dokuwiki.org/namespace_templates based on code proposed by Christian Nancy
if ($type == 'private') {
$content = io_readFile($this->private_page_template, false);
$content = str_replace('@PRIVATENAMESPACE@',$this->getLang('privatenamespace'),$content);
$data = array('tpl' => $content, 'id' => $this->private_page);
} elseif ($type == 'public') {
$content = io_readFile($this->public_page_template, false);
$content = str_replace('@PUBLICPAGE@',$this->getLang('publicpage'),$content);
$data = array('tpl' => $content, 'id' => $this->public_page);
}
// Use the built-in parser
$content = parsePageTemplate($data);
return $content;