1. Michael Manfre
  2. xpattern

Commits

Manfre  committed 38d5831

Permissions have been moved to the database.

  • Participants
  • Parent commits 456d96f
  • Branches default

Comments (0)

Files changed (9)

File textpattern/include/txp_group.php

View file
  • Ignore whitespace
 			$rs = safe_delete('group', "`id` = $group_id");
 
 			if ($rs) {
+				// remove group permissions
+				safe_delete('group_perm', "`group_id` = $group_id");
+				// remove users from now defunct group
+				safe_update('txp_users', "`privs` = 0", "`privs` = $group_id");
+				
 				unset($groups[$group_id]);
 				group( gTxt('group_deleted', array('{name}' => $name)) );
 			}

File textpattern/include/txp_perm.php

View file
  • Ignore whitespace
+<?php
+
+if (!defined('txpinterface'))
+{
+	die('txpinterface is undefined.');
+}
+
+if (empty($perms))
+{
+	$groups = array();
+	$rs = safe_rows('`id`,`name`', 'group');
+	if ($rs)
+	{
+		foreach ($rs as $r)
+		{
+			$groups[$r['id']] = gTxt($r['name']);
+		}
+	}
+}
+
+if ($event == 'perm')
+{
+	require_privs('perm');
+
+	if (is_callable($step))
+		$step();
+	else
+		perm();
+}
+
+function perm($message='')
+{
+	pageTop($message);
+	
+	perm_list();
+}
+
+function perm_list()
+{
+	global $txp_user;
+
+	if (!has_privs('perm'))
+		return;
+
+	echo n.n.hed(gTxt('tab_permissions'), 3, ' style="text-align: center;margin-top:2em;"').
+		n.n.startTable('list').
+		n.tr(
+			n.hCell(gTxt('name')).
+			n.hCell(gTxt('lang')).
+			n.hCell()
+		);
+
+	$can_edit = has_privs('perm.edit');
+
+	$rs = safe_rows_start('*', 'perm', '1=1 ORDER BY `name`');
+
+	if ($rs)
+	{
+		while ($a = nextRow($rs))
+		{
+				echo n.tr(
+					td( htmlspecialchars($a['name']) ).
+					td( htmlspecialchars(gTxt($a['desc']))).
+					td(
+						!$can_edit ? '' :
+							dLink('perm', 'perm_delete', 'perm_id', $a['id'])
+					)
+				);
+		}
+	}
+
+	if (has_privs('perm.edit'))
+	{
+		echo n.tr(
+			form(
+				td( fInput('text', 'name', '', 'edit') ).
+				td( fInput('text', 'desc', '', 'edit') ).
+				td( fInput('submit', 'add', gTxt('add'), 'smallerbox') ).
+				n.eInput('perm').
+				n.sInput('perm_save_new')
+			)
+		);
+	}
+
+	echo n.endTable();
+}
+
+// -------------------------------------------------------------
+function perm_save_new()
+{
+	require_privs('perm.edit');
+
+	extract(doSlash(psa(array('name', 'desc'))));
+
+	if ($name && $desc)
+	{
+		$rs = safe_insert('perm', "
+			`name`	= '$name',
+			`desc`	= '$desc'
+		");
+
+		if ($rs)
+		{
+			perm(
+				gTxt('added_perm', array('{name}' => $name))
+			);
+
+			return;
+		}
+	}
+
+	perm(gTxt('error_adding_new_level'));
+}
+// -------------------------------------------------------------
+function perm_delete()
+{
+	require_privs('perm.edit');
+
+	$perm_id = assert_int(ps('perm_id'));
+
+	$rs = safe_delete('perm', "`id` = $perm_id");
+
+	if ($rs) {
+		// remove perm permissions
+		safe_delete('group_perm', "`perm_id` = $perm_id");
+		
+		perm( gTxt('perm_deleted', array('{name}' => $perm_id)) );
+	}
+}
+
+?>

File textpattern/index.php

View file
  • Ignore whitespace
 	include txpath.'/lib/txplib_db.php';
 	include txpath.'/lib/txplib_forms.php';
 	include txpath.'/lib/txplib_html.php';
-	include txpath.'/lib/admin_config.php';
 
 	$microstart = getmicrotime();
 

File textpattern/lib/admin_config.php

  • Ignore whitespace
-<?php
-/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-
-                (M O S T L Y)  D E P R E C A T E D  AS  OF  1.0RC4
-
- *	IMPORTANT:  Most settings in this file  (all that is in  $txpac)  have moved
-	into the  Database. This file remains here  mainly for  not breaking updates
- *	for people that are coming from older revisions (up to and including 1.0RC3)!
-	If you would like to change any of these settings, you can do so in Advanced
- *	Preferences	of your Textpattern admin panel.
-
- *	Only the Permission-Settings at the bottom this file are still actively used,
-	and these will be moved to the db before the next release
-
-$HeadURL: http://textpattern.googlecode.com/svn/development/4.0/textpattern/lib/admin_config.php $
-$LastChangedRevision: 2812 $
-
- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
-
-// @fixme Move to db
-$txp_permissions = array(
-	'admin'                       => '1,2,3,4,5,6',
-	'admin.edit'                  => '1',
-	'admin.list'                  => '1,2,3',
-	'article.delete.own'          => '1,2,3,4',
-	'article.delete'              => '1,2',
-	'article.edit'                => '1,2,3',
-	'article.edit.published'      => '1,2,3',
-	'article.edit.own'            => '1,2,3,4,5,6',
-	'article.edit.own.published'  => '1,2,3,4',
-	'article.publish'             => '1,2,3,4',
-	'article.php'                 => '1,2',
-	'article'                     => '1,2,3,4,5,6',
-	'list'                        => '1,2,3,4,5,6', //likely the same as for article.
-	'category'                    => '1,2,3',
-	'css'                         => '1,2,6',
-	'javascript'                  => '1,2,6',
-	'diag'                        => '1,2',
-	'discuss'                     => '1,2,3',
-	'file'                        => '1,2,3,4,6',
-	'form'                        => '1,2,3,6',
-	'group'												=> '1',
-	'group.list'									=> '1,2,3',
-	'group.edit'									=> '1',
-	'image'                       => '1,2,3,4,6',
-	'import'                      => '1,2',
-	'link'                        => '1,2,3',
-	'log'                         => '1,2,3', // more?
-	'page'                        => '1,2,3,6',
-	'plugin'                      => '1,2',
-	'prefs'                       => '1,2',
-	'section'                     => '1,2,3,6',
-	'snippet'                     => '1,2,3,6',
-	'tab.admin'                   => '1,2,3,4,5,6',
-	'tab.content'                 => '1,2,3,4,5,6',
-	'tab.extensions'              => '1,2',
-	'tab.presentation'            => '1,2,3,6',
-	'tag'                         => '1,2,3,4,5,6',
-);
-
-?>

File textpattern/lib/txplib_db.php

View file
  • Ignore whitespace
 	{
 		$ts = array();
 		foreach (explode(',', $table) as $t) {
-			$name = PFX.trim($t);
+			$t = trim($t);
+			$name = PFX.$t;
 //			if (preg_match('@[^\w._$]@', $name))
 				$ts[] = "`$name`".(PFX ? " as `$t`" : '');
 //			else

File textpattern/lib/txplib_head.php

View file
  • Ignore whitespace
 			gTxt('tab_preferences') => 'prefs',
 			gTxt('tab_site_admin')  => 'admin',
 			gTxt('tab_groups')			=> 'group',
+			gTxt('tab_permissions')	=> 'perm',
 			gTxt('tab_logs')        => 'log',
 			gTxt('tab_plugins')     => 'plugin',
 			gTxt('tab_import')      => 'import'

File textpattern/lib/txplib_misc.php

View file
  • Ignore whitespace
 		}
 	}
 
+
 // -------------------------------------------------------------
 	function add_privs($res, $perm = '1') // perm = '1,2,3'
 	{
-		global $txp_permissions;
-		// Don't let them override privs that exist
-		if (!isset($txp_permissions[$res]))
-			$txp_permissions[$res] = $perm;
+		global $txp_user, $xp_permissions, $xp_privs;
+
+		if (empty($res))
+			return;
+
+		if (!is_array($xp_permissions))
+			$xp_permissions = array();
+		
+		$new_privs = array();
+		
+		foreach(split(',', $perm) as $p)
+		{
+			$p = trim($p);
+			if (!isset($xp_permissions[$p]))
+				$xp_permissions[$p] = array($res);
+			else if (!in_array($res, $xp_permissions[$p]))
+			{
+				array_push($xp_permissions[$p], $res);
+			}
+		}
 	}
 
 // -------------------------------------------------------------
 	function has_privs($res, $user='')
 	{
-		global $txp_user, $txp_permissions;
-		static $privs;
+		global $txp_user, $xp_permissions, $xp_privs;
+		
+		if (!is_array($xp_permissions)) $xp_permissions = array();
+		if (!is_array($xp_privs)) $xp_privs = array();
+		
+		// If no user name is supplied, assume the current login name
+		if (empty($user)) $user = $txp_user;
 
-		// If no user name is supplied, assume the current login name
-		if (empty($user))
-			$user = $txp_user;
+		if (!isset($xp_privs[$user]))
+		{
+			$xp_privs[$user] = safe_field("privs", "txp_users", "name='".doSlash($user)."'");
+		}
+		
+		$priv = $xp_privs[$user];
 
-		if (!isset($privs[$user]))
+		if (!isset($xp_permissions[$priv]))
 		{
-			$privs[$user] = safe_field("privs", "txp_users", "name='".doSlash($user)."'");
+			$xp_permissions[$priv] = array();
+			
+			$rs = safe_rows('perm.name', 'perm, group_perm', "perm.id=group_perm.perm_id AND group_perm.group_id = {$xp_privs[$user]}");
+			if ($rs)
+			{
+				foreach ($rs as $r)
+				{
+					array_push($xp_permissions[$priv], $r['name']);
+				}
+			}
+		}
+		if (isset($xp_permissions[$priv]))
+		{
+			return in_array($res, $xp_permissions[$priv]);
 		}
 
-		if (isset($txp_permissions[$res]))
-		{
-			return in_array($privs[$user], explode(',', $txp_permissions[$res]));
-		}
-
-		else
-		{
-			return false;
-		}
+		return false;
 	}
 
 // -------------------------------------------------------------

File textpattern/setup/txpsql.php

View file
  • Ignore whitespace
 $create_sql[] = "INSERT INTO `".PFX."txp_prefs` VALUES (1, 'hide_login_in_email', '0', 1, 'admin', 'yesnoradio', 160)";
 $create_sql[] = "INSERT INTO `".PFX."txp_prefs` VALUES (1, 'hash_alg', 'sha256', 1, 'admin', 'hash_picker', 160)";
 $create_sql[] = "INSERT INTO `".PFX."txp_prefs` VALUES (1, 'admin_email', '', 1, 'admin', 'text_input', 160)";
-$create_sql[] = "INSERT INTO `".PFX."txp_prefs` VALUES (1, 'admin_email', '', 1, 'admin', 'text_input', 160)";
 $create_sql[] = "INSERT INTO `".PFX."txp_prefs` VALUES (1, 'plugin_dir_name', 'plugin', 2, 'admin', 'text_input', 160)";
 
 $create_sql[] = "CREATE TABLE `".PFX."txp_section` (
 ) $tabletype AUTO_INCREMENT=1 ";
 
 
+
+$create_sql[] = "CREATE TABLE `".PFX."perm` (
+  `id` int(10) NOT NULL auto_increment,
+  `name` varchar(64) NOT NULL default '',
+  `desc` varchar(64) NOT NULL default '',
+  PRIMARY KEY  (`id`),
+  KEY `name` (`name`)
+) $tabletype AUTO_INCREMENT=1 ";
+
+$create_sql[] = "CREATE TABLE `".PFX."group_perm` (
+  `group_id` int(10) NOT NULL,
+  `perm_id` int(10) NOT NULL,
+  PRIMARY KEY  (`group_id`, `perm_id`),
+  KEY `perm` (`perm_id`)
+) $tabletype ";
+
+$perm = array(
+	'admin'                       => array('perm_admin','1,2,3,4,5,6'),
+	'admin.edit'                  => array('perm_admin_edit', '1'),
+	'admin.list'                  => array('perm_admin_list', '1,2,3'),
+	'article.delete.own'          => array('perm_article_delete_own', '1,2,3,4'),
+	'article.delete'              => array('perm_article_delete', '1,2'),
+	'article.edit'                => array('perm_article_edit', '1,2,3'),
+	'article.edit.published'      => array('perm_article_edit_published', '1,2,3'),
+	'article.edit.own'            => array('perm_article_edit_own', '1,2,3,4,5,6'),
+	'article.edit.own.published'  => array('perm_article_edit_own_published', '1,2,3,4'),
+	'article.publish'             => array('perm_article_publish', '1,2,3,4'),
+	'article.php'                 => array('perm_article_php', '1,2'),
+	'article'                     => array('perm_article', '1,2,3,4,5,6'),
+	'list'                        => array('perm_list', '1,2,3,4,5,6'), //likely the same as for article.
+	'category'                    => array('perm_category', '1,2,3'),
+	'css'                         => array('perm_css', '1,2,6'),
+	'javascript'                  => array('perm_javascript', '1,2,6'),
+	'diag'                        => array('perm_diag', '1,2'),
+	'discuss'                     => array('perm_discuss', '1,2,3'),
+	'file'                        => array('perm_file', '1,2,3,4,6'),
+	'form'                        => array('perm_form', '1,2,3,6'),
+	'group'												=> array('perm_group', '1'),
+	'group.list'									=> array('perm_group_list', '1,2,3'),
+	'group.edit'									=> array('perm_group_edit', '1'),
+	'image'                       => array('perm_image', '1,2,3,4,6'),
+	'import'                      => array('perm_import', '1,2'),
+	'link'                        => array('perm_link', '1,2,3'),
+	'log'                         => array('perm_log', '1,2,3'), // more?
+	'page'                        => array('perm_page', '1,2,3,6'),
+	'perm'                       	=> array('perm_perm', '1'),
+	'plugin'                      => array('perm_plugin', '1,2'),
+	'prefs'                       => array('perm_prefs', '1,2'),
+	'section'                     => array('perm_section', '1,2,3,6'),
+	'snippet'                     => array('perm_snippet', '1,2,3,6'),
+	'tab.admin'                   => array('perm_tab_admin', '1,2,3,4,5,6'),
+	'tab.content'                 => array('perm_tab.content', '1,2,3,4,5,6'),
+	'tab.extensions'              => array('perm_tab_extensions', '1,2'),
+	'tab.presentation'            => array('perm_tab_presentation', '1,2,3,6'),
+	'tag'                         => array('perm_tag', '1,2,3,4,5,6'),
+);
+
+foreach($perm as $k => $v)
+{
+	// add permission entry
+	$create_sql[] = "INSERT INTO `".PFX."perm` (`name`, `desc`) VALUES ('$k', '".doSlash($v[0])."')";
+	
+	$groups = array();
+	foreach(split(',', $v[1]) as $p)
+	{
+		$groups[] = "($p, LAST_INSERT_ID())";
+	}
+	// bind permission to group
+	$create_sql[] = $x = "INSERT INTO `".PFX."group_perm` (`group_id`, `perm_id`) VALUES " . join(', ', $groups);
+}
+
+
+
 $GLOBALS['txp_install_successful'] = true;
 $GLOBALS['txp_err_count'] = 0;
 foreach ($create_sql as $query)

File textpattern/update/_to_xp_1.0.0.php

View file
  • Ignore whitespace
 		safe_query('INSERT INTO `'.safe_pfx('group')."` ( `id`, `name` ) 
 				VALUES (0, 'none'), (1, 'publisher'), (2, 'managing_editor'), (3, 'copy_editor'), (4, 'staff_writer'), 	(5, 'freelancer'), (6, 'designer')"); 
 		}	
-	
+	}
 ?>