From 5c91e2569cb3a400acd20bf06cc0e609dd63a778 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Tue, 8 Jan 2013 22:09:14 -0600 Subject: [feature/migrations] Migrations now somewhat works PHPBB3-9737 --- phpBB/includes/db/migration/tool/permission.php | 562 ++++++++++++++++++++++++ 1 file changed, 562 insertions(+) create mode 100644 phpBB/includes/db/migration/tool/permission.php (limited to 'phpBB/includes/db/migration/tool/permission.php') diff --git a/phpBB/includes/db/migration/tool/permission.php b/phpBB/includes/db/migration/tool/permission.php new file mode 100644 index 0000000000..ebe404bc62 --- /dev/null +++ b/phpBB/includes/db/migration/tool/permission.php @@ -0,0 +1,562 @@ +db = $db; + $this->cache = $cache; + $this->auth = $auth; + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $php_ext; + } + + /** + * {@inheritdoc} + */ + public function get_name() + { + return 'permission'; + } + + /** + * Permission Exists + * + * Check if a permission (auth) setting exists + * + * @param string $auth_option The name of the permission (auth) option + * @param bool $global True for checking a global permission setting, False for a local permission setting + * + * @return bool true if it exists, false if not + */ + public function exists($auth_option, $global = true) + { + if ($global) + { + $type_sql = ' AND is_global = 1'; + } + else + { + $type_sql = ' AND is_local = 1'; + } + + $sql = 'SELECT auth_option_id + FROM ' . ACL_OPTIONS_TABLE . " + WHERE auth_option = '" . $this->db->sql_escape($auth_option) . "'" + . $type_sql; + $result = $this->db->sql_query($sql); + + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + if ($row) + { + return true; + } + + return false; + } + + /** + * Permission Add + * + * Add a permission (auth) option + * + * @param string $auth_option The name of the permission (auth) option + * @param bool $global True for checking a global permission setting, False for a local permission setting + * + * @return result + */ + public function add($auth_option, $global = true, $copy_from = false) + { + if ($this->exists($auth_option, $global)) + { + throw new phpbb_db_migration_exception('PERMISSION_ALREADY_EXISTS', $auth_option); + } + + // We've added permissions, so set to true to notify the user. + $this->permissions_added = true; + + if (!class_exists('auth_admin')) + { + include($this->phpbb_root_path . 'includes/acp/auth.' . $this->php_ext); + } + $auth_admin = new auth_admin(); + + // We have to add a check to see if the !$global (if global, local, and if local, global) permission already exists. If it does, acl_add_option currently has a bug which would break the ACL system, so we are having a work-around here. + if ($this->exists($auth_option, !$global)) + { + $sql_ary = array( + 'is_global' => 1, + 'is_local' => 1, + ); + $sql = 'UPDATE ' . ACL_OPTIONS_TABLE . ' + SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . " + WHERE auth_option = '" . $this->db->sql_escape($auth_option) . "'"; + $this->db->sql_query($sql); + } + else + { + if ($global) + { + $auth_admin->acl_add_option(array('global' => array($auth_option))); + } + else + { + $auth_admin->acl_add_option(array('local' => array($auth_option))); + } + } + + // The permission has been added, now we can copy it if needed + if ($copy_from && isset($auth_admin->acl_options['id'][$copy_from])) + { + $old_id = $auth_admin->acl_options['id'][$copy_from]; + $new_id = $auth_admin->acl_options['id'][$auth_option]; + + $tables = array(ACL_GROUPS_TABLE, ACL_ROLES_DATA_TABLE, ACL_USERS_TABLE); + + foreach ($tables as $table) + { + $sql = 'SELECT * + FROM ' . $table . ' + WHERE auth_option_id = ' . $old_id; + $result = $this->db->sql_query($sql); + + $sql_ary = array(); + while ($row = $this->db->sql_fetchrow($result)) + { + $row['auth_option_id'] = $new_id; + $sql_ary[] = $row; + } + $this->db->sql_freeresult($result); + + if (sizeof($sql_ary)) + { + $this->db->sql_multi_insert($table, $sql_ary); + } + } + + $auth_admin->acl_clear_prefetch(); + } + + return false; + } + + /** + * Permission Remove + * + * Remove a permission (auth) option + * + * @param string $auth_option The name of the permission (auth) option + * @param bool $global True for checking a global permission setting, False for a local permission setting + * + * @return result + */ + public function remove($auth_option, $global = true) + { + if (!$this->exists($auth_option, $global)) + { + throw new phpbb_db_migration_exception('PERMISSION_NOT_EXIST', $auth_option); + } + + if ($global) + { + $type_sql = ' AND is_global = 1'; + } + else + { + $type_sql = ' AND is_local = 1'; + } + $sql = 'SELECT auth_option_id, is_global, is_local + FROM ' . ACL_OPTIONS_TABLE . " + WHERE auth_option = '" . $this->db->sql_escape($auth_option) . "'" . + $type_sql; + $result = $this->db->sql_query($sql); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + $id = $row['auth_option_id']; + + // If it is a local and global permission, do not remove the row! :P + if ($row['is_global'] && $row['is_local']) + { + $sql = 'UPDATE ' . ACL_OPTIONS_TABLE . ' + SET ' . (($global) ? 'is_global = 0' : 'is_local = 0') . ' + WHERE auth_option_id = ' . $id; + $this->db->sql_query($sql); + } + else + { + // Delete time + $this->db->sql_query('DELETE FROM ' . ACL_GROUPS_TABLE . ' WHERE auth_option_id = ' . $id); + $this->db->sql_query('DELETE FROM ' . ACL_ROLES_DATA_TABLE . ' WHERE auth_option_id = ' . $id); + $this->db->sql_query('DELETE FROM ' . ACL_USERS_TABLE . ' WHERE auth_option_id = ' . $id); + $this->db->sql_query('DELETE FROM ' . ACL_OPTIONS_TABLE . ' WHERE auth_option_id = ' . $id); + } + + // Purge the auth cache + $this->cache->destroy('_acl_options'); + $this->auth->acl_clear_prefetch(); + + return false; + } + + /** + * Add a new permission role + * + * @param string $role_name The new role name + * @param sting $role_type The type (u_, m_, a_) + */ + public function role_add($role_name, $role_type = '', $role_description = '') + { + $sql = 'SELECT role_id + FROM ' . ACL_ROLES_TABLE . " + WHERE role_name = '" . $this->db->sql_escape($role_name) . "'"; + $this->db->sql_query($sql); + $role_id = $this->db->sql_fetchfield('role_id'); + + if ($role_id) + { + throw new phpbb_db_migration_exception('ROLE_ALREADY_EXISTS', $old_role_name); + } + + $sql = 'SELECT MAX(role_order) AS max + FROM ' . ACL_ROLES_TABLE . " + WHERE role_type = '" . $this->db->sql_escape($role_type) . "'"; + $this->db->sql_query($sql); + $role_order = $this->db->sql_fetchfield('max'); + $role_order = (!$role_order) ? 1 : $role_order + 1; + + $sql_ary = array( + 'role_name' => $role_name, + 'role_description' => $role_description, + 'role_type' => $role_type, + 'role_order' => $role_order, + ); + + $sql = 'INSERT INTO ' . ACL_ROLES_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary); + $this->db->sql_query($sql); + + return false; + } + + /** + * Update the name on a permission role + * + * @param string $old_role_name The old role name + * @param string $new_role_name The new role name + */ + public function role_update($old_role_name, $new_role_name = '') + { + $sql = 'SELECT role_id + FROM ' . ACL_ROLES_TABLE . " + WHERE role_name = '" . $this->db->sql_escape($old_role_name) . "'"; + $this->db->sql_query($sql); + $role_id = $this->db->sql_fetchfield('role_id'); + + if (!$role_id) + { + throw new phpbb_db_migration_exception('ROLE_NOT_EXISTS', $old_role_name); + } + + $sql = 'UPDATE ' . ACL_ROLES_TABLE . " + SET role_name = '" . $this->db->sql_escape($new_role_name) . "' + WHERE role_name = '" . $this->db->sql_escape($old_role_name) . "'"; + $this->db->sql_query($sql); + + return false; + } + + /** + * Remove a permission role + * + * @param string $role_name The role name to remove + */ + public function role_remove($role_name) + { + $sql = 'SELECT role_id + FROM ' . ACL_ROLES_TABLE . " + WHERE role_name = '" . $this->db->sql_escape($role_name) . "'"; + $this->db->sql_query($sql); + $role_id = $this->db->sql_fetchfield('role_id'); + + if (!$role_id) + { + throw new phpbb_db_migration_exception('ROLE_NOT_EXIST', $role_name); + } + + $sql = 'DELETE FROM ' . ACL_ROLES_DATA_TABLE . ' + WHERE role_id = ' . $role_id; + $this->db->sql_query($sql); + + $sql = 'DELETE FROM ' . ACL_ROLES_TABLE . ' + WHERE role_id = ' . $role_id; + $this->db->sql_query($sql); + + $this->auth->acl_clear_prefetch(); + + return false; + } + + /** + * Permission Set + * + * Allows you to set permissions for a certain group/role + * + * @param string $name The name of the role/group + * @param string|array $auth_option The auth_option or array of auth_options you would like to set + * @param string $type The type (role|group) + * @param bool $has_permission True if you want to give them permission, false if you want to deny them permission + */ + public function permission_set($name, $auth_option = array(), $type = 'role', $has_permission = true) + { + if (!is_array($auth_option)) + { + $auth_option = array($auth_option); + } + + $new_auth = array(); + $sql = 'SELECT auth_option_id + FROM ' . ACL_OPTIONS_TABLE . ' + WHERE ' . $this->db->sql_in_set('auth_option', $auth_option); + $result = $this->db->sql_query($sql); + while ($row = $this->db->sql_fetchrow($result)) + { + $new_auth[] = $row['auth_option_id']; + } + $this->db->sql_freeresult($result); + + if (!sizeof($new_auth)) + { + return false; + } + + $current_auth = array(); + + $type = (string) $type; // Prevent PHP bug. + + switch ($type) + { + case 'role' : + $sql = 'SELECT role_id + FROM ' . ACL_ROLES_TABLE . " + WHERE role_name = '" . $this->db->sql_escape($name) . "'"; + $this->db->sql_query($sql); + $role_id = $this->db->sql_fetchfield('role_id'); + + if (!$role_id) + { + throw new phpbb_db_migration_exception('ROLE_NOT_EXIST', $name); + } + + $sql = 'SELECT auth_option_id, auth_setting + FROM ' . ACL_ROLES_DATA_TABLE . ' + WHERE role_id = ' . $role_id; + $result = $this->db->sql_query($sql); + while ($row = $this->db->sql_fetchrow($result)) + { + $current_auth[$row['auth_option_id']] = $row['auth_setting']; + } + $this->db->sql_freeresult($result); + break; + + case 'group' : + $sql = 'SELECT group_id + FROM ' . GROUPS_TABLE . " + WHERE group_name = '" . $this->db->sql_escape($name) . "'"; + $this->db->sql_query($sql); + $group_id = $this->db->sql_fetchfield('group_id'); + + if (!$group_id) + { + throw new phpbb_db_migration_exception('GROUP_NOT_EXIST', $name); + } + + // If the group has a role set for them we will add the requested permissions to that role. + $sql = 'SELECT auth_role_id + FROM ' . ACL_GROUPS_TABLE . ' + WHERE group_id = ' . $group_id . ' + AND auth_role_id <> 0 + AND forum_id = 0'; + $this->db->sql_query($sql); + $role_id = $this->db->sql_fetchfield('auth_role_id'); + if ($role_id) + { + $sql = 'SELECT role_name + FROM ' . ACL_ROLES_TABLE . ' + WHERE role_id = ' . $role_id; + $this->db->sql_query($sql); + $role_name = $this->db->sql_fetchfield('role_name'); + + return $this->set($role_name, $auth_option, 'role', $has_permission); + } + + $sql = 'SELECT auth_option_id, auth_setting + FROM ' . ACL_GROUPS_TABLE . ' + WHERE group_id = ' . $group_id; + $result = $this->db->sql_query($sql); + while ($row = $this->db->sql_fetchrow($result)) + { + $current_auth[$row['auth_option_id']] = $row['auth_setting']; + } + $this->db->sql_freeresult($result); + break; + } + + $sql_ary = array(); + switch ($type) + { + case 'role' : + foreach ($new_auth as $auth_option_id) + { + if (!isset($current_auth[$auth_option_id])) + { + $sql_ary[] = array( + 'role_id' => $role_id, + 'auth_option_id' => $auth_option_id, + 'auth_setting' => $has_permission, + ); + } + } + + $this->db->sql_multi_insert(ACL_ROLES_DATA_TABLE, $sql_ary); + break; + + case 'group' : + foreach ($new_auth as $auth_option_id) + { + if (!isset($current_auth[$auth_option_id])) + { + $sql_ary[] = array( + 'group_id' => $group_id, + 'auth_option_id' => $auth_option_id, + 'auth_setting' => $has_permission, + ); + } + } + + $this->db->sql_multi_insert(ACL_GROUPS_TABLE, $sql_ary); + break; + } + + $this->auth->acl_clear_prefetch(); + + return false; + } + + /** + * Permission Unset + * + * Allows you to unset (remove) permissions for a certain group/role + * + * @param string $name The name of the role/group + * @param string|array $auth_option The auth_option or array of auth_options you would like to set + * @param string $type The type (role|group) + */ + public function permission_unset($name, $auth_option = array(), $type = 'role') + { + if (!is_array($auth_option)) + { + $auth_option = array($auth_option); + } + + $to_remove = array(); + $sql = 'SELECT auth_option_id + FROM ' . ACL_OPTIONS_TABLE . ' + WHERE ' . $this->db->sql_in_set('auth_option', $auth_option); + $result = $this->db->sql_query($sql); + while ($row = $this->db->sql_fetchrow($result)) + { + $to_remove[] = $row['auth_option_id']; + } + $this->db->sql_freeresult($result); + + if (!sizeof($to_remove)) + { + return false; + } + + $type = (string) $type; // Prevent PHP bug. + + switch ($type) + { + case 'role' : + $sql = 'SELECT role_id + FROM ' . ACL_ROLES_TABLE . " + WHERE role_name = '" . $this->db->sql_escape($name) . "'"; + $this->db->sql_query($sql); + $role_id = $this->db->sql_fetchfield('role_id'); + + if (!$role_id) + { + throw new phpbb_db_migration_exception('ROLE_NOT_EXIST', $name); + } + + $sql = 'DELETE FROM ' . ACL_ROLES_DATA_TABLE . ' + WHERE ' . $this->db->sql_in_set('auth_option_id', $to_remove); + $this->db->sql_query($sql); + break; + + case 'group' : + $sql = 'SELECT group_id + FROM ' . GROUPS_TABLE . " + WHERE group_name = '" . $this->db->sql_escape($name) . "'"; + $this->db->sql_query($sql); + $group_id = $this->db->sql_fetchfield('group_id'); + + if (!$group_id) + { + throw new phpbb_db_migration_exception('GROUP_NOT_EXIST', $name); + } + + // If the group has a role set for them we will remove the requested permissions from that role. + $sql = 'SELECT auth_role_id + FROM ' . ACL_GROUPS_TABLE . ' + WHERE group_id = ' . $group_id . ' + AND auth_role_id <> 0'; + $this->db->sql_query($sql); + $role_id = $this->db->sql_fetchfield('auth_role_id'); + if ($role_id) + { + $sql = 'SELECT role_name + FROM ' . ACL_ROLES_TABLE . ' + WHERE role_id = ' . $role_id; + $this->db->sql_query($sql); + $role_name = $this->db->sql_fetchfield('role_name'); + + return $this->permission_unset($role_name, $auth_option, 'role'); + } + + $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . ' + WHERE ' . $this->db->sql_in_set('auth_option_id', $to_remove); + $this->db->sql_query($sql); + break; + } + + $this->auth->acl_clear_prefetch(); + + return false; + } +} -- cgit v1.2.1 From e3737978f76a962385a26de910959607d0ae0d30 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Wed, 9 Jan 2013 14:27:01 -0600 Subject: [feature/migrations] Fixing returns of callables and handling data state Lots of comments and some other miscellaneous fixes. PHPBB3-9737 --- phpBB/includes/db/migration/tool/permission.php | 106 ++++++++++++------------ 1 file changed, 55 insertions(+), 51 deletions(-) (limited to 'phpBB/includes/db/migration/tool/permission.php') diff --git a/phpBB/includes/db/migration/tool/permission.php b/phpBB/includes/db/migration/tool/permission.php index ebe404bc62..97fdbc0df9 100644 --- a/phpBB/includes/db/migration/tool/permission.php +++ b/phpBB/includes/db/migration/tool/permission.php @@ -7,24 +7,38 @@ * */ +/** +* Migration permission management tool +* +* @package db +*/ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_interface { /** @var phpbb_auth */ - protected $auth = null; + protected $auth; /** @var phpbb_cache_service */ - protected $cache = null; + protected $cache; /** @var dbal */ - protected $db = null; + protected $db; /** @var string */ - protected $phpbb_root_path = null; + protected $phpbb_root_path; /** @var string */ - protected $php_ext = null; + protected $php_ext; - public function __construct(phpbb_db_driver $db, $cache, phpbb_auth $auth, $phpbb_root_path, $php_ext) + /** + * Constructor + * + * @param phpbb_db_driver $db + * @param mixed $cache + * @param phpbb_auth $auth + * @param string $phpbb_root_path + * @param string $php_ext + */ + public function __construct(phpbb_db_driver $db, phpbb_cache_service $cache, phpbb_auth $auth, $phpbb_root_path, $php_ext) { $this->db = $db; $this->cache = $cache; @@ -48,7 +62,6 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte * * @param string $auth_option The name of the permission (auth) option * @param bool $global True for checking a global permission setting, False for a local permission setting - * * @return bool true if it exists, false if not */ public function exists($auth_option, $global = true) @@ -86,8 +99,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte * * @param string $auth_option The name of the permission (auth) option * @param bool $global True for checking a global permission setting, False for a local permission setting - * - * @return result + * @return null */ public function add($auth_option, $global = true, $copy_from = false) { @@ -152,7 +164,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte } $this->db->sql_freeresult($result); - if (sizeof($sql_ary)) + if (!empty($sql_ary)) { $this->db->sql_multi_insert($table, $sql_ary); } @@ -160,8 +172,6 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte $auth_admin->acl_clear_prefetch(); } - - return false; } /** @@ -171,8 +181,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte * * @param string $auth_option The name of the permission (auth) option * @param bool $global True for checking a global permission setting, False for a local permission setting - * - * @return result + * @return null */ public function remove($auth_option, $global = true) { @@ -197,7 +206,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte $row = $this->db->sql_fetchrow($result); $this->db->sql_freeresult($result); - $id = $row['auth_option_id']; + $id = (int) $row['auth_option_id']; // If it is a local and global permission, do not remove the row! :P if ($row['is_global'] && $row['is_local']) @@ -210,17 +219,17 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte else { // Delete time - $this->db->sql_query('DELETE FROM ' . ACL_GROUPS_TABLE . ' WHERE auth_option_id = ' . $id); - $this->db->sql_query('DELETE FROM ' . ACL_ROLES_DATA_TABLE . ' WHERE auth_option_id = ' . $id); - $this->db->sql_query('DELETE FROM ' . ACL_USERS_TABLE . ' WHERE auth_option_id = ' . $id); - $this->db->sql_query('DELETE FROM ' . ACL_OPTIONS_TABLE . ' WHERE auth_option_id = ' . $id); + $tables = array(ACL_GROUPS_TABLE, ACL_ROLES_DATA_TABLE, ACL_USERS_TABLE, ACL_OPTIONS_TABLE); + foreach ($tables as $table) + { + $this->db->sql_query('DELETE FROM ' . $table . ' + WHERE auth_option_id = ' . $id); + } } // Purge the auth cache $this->cache->destroy('_acl_options'); $this->auth->acl_clear_prefetch(); - - return false; } /** @@ -228,6 +237,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte * * @param string $role_name The new role name * @param sting $role_type The type (u_, m_, a_) + * @return null */ public function role_add($role_name, $role_type = '', $role_description = '') { @@ -235,18 +245,18 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte FROM ' . ACL_ROLES_TABLE . " WHERE role_name = '" . $this->db->sql_escape($role_name) . "'"; $this->db->sql_query($sql); - $role_id = $this->db->sql_fetchfield('role_id'); + $role_id = (int) $this->db->sql_fetchfield('role_id'); if ($role_id) { throw new phpbb_db_migration_exception('ROLE_ALREADY_EXISTS', $old_role_name); } - $sql = 'SELECT MAX(role_order) AS max + $sql = 'SELECT MAX(role_order) AS max_role_order FROM ' . ACL_ROLES_TABLE . " WHERE role_type = '" . $this->db->sql_escape($role_type) . "'"; $this->db->sql_query($sql); - $role_order = $this->db->sql_fetchfield('max'); + $role_order = (int) $this->db->sql_fetchfield('max_role_order'); $role_order = (!$role_order) ? 1 : $role_order + 1; $sql_ary = array( @@ -258,8 +268,6 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte $sql = 'INSERT INTO ' . ACL_ROLES_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary); $this->db->sql_query($sql); - - return false; } /** @@ -267,6 +275,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte * * @param string $old_role_name The old role name * @param string $new_role_name The new role name + * @return null */ public function role_update($old_role_name, $new_role_name = '') { @@ -274,7 +283,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte FROM ' . ACL_ROLES_TABLE . " WHERE role_name = '" . $this->db->sql_escape($old_role_name) . "'"; $this->db->sql_query($sql); - $role_id = $this->db->sql_fetchfield('role_id'); + $role_id = (int) $this->db->sql_fetchfield('role_id'); if (!$role_id) { @@ -285,14 +294,13 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte SET role_name = '" . $this->db->sql_escape($new_role_name) . "' WHERE role_name = '" . $this->db->sql_escape($old_role_name) . "'"; $this->db->sql_query($sql); - - return false; } /** * Remove a permission role * * @param string $role_name The role name to remove + * @return null */ public function role_remove($role_name) { @@ -300,7 +308,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte FROM ' . ACL_ROLES_TABLE . " WHERE role_name = '" . $this->db->sql_escape($role_name) . "'"; $this->db->sql_query($sql); - $role_id = $this->db->sql_fetchfield('role_id'); + $role_id = (int) $this->db->sql_fetchfield('role_id'); if (!$role_id) { @@ -316,8 +324,6 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte $this->db->sql_query($sql); $this->auth->acl_clear_prefetch(); - - return false; } /** @@ -329,6 +335,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte * @param string|array $auth_option The auth_option or array of auth_options you would like to set * @param string $type The type (role|group) * @param bool $has_permission True if you want to give them permission, false if you want to deny them permission + * @return null */ public function permission_set($name, $auth_option = array(), $type = 'role', $has_permission = true) { @@ -344,13 +351,13 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte $result = $this->db->sql_query($sql); while ($row = $this->db->sql_fetchrow($result)) { - $new_auth[] = $row['auth_option_id']; + $new_auth[] = (int) $row['auth_option_id']; } $this->db->sql_freeresult($result); - if (!sizeof($new_auth)) + if (empty($new_auth)) { - return false; + return; } $current_auth = array(); @@ -364,7 +371,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte FROM ' . ACL_ROLES_TABLE . " WHERE role_name = '" . $this->db->sql_escape($name) . "'"; $this->db->sql_query($sql); - $role_id = $this->db->sql_fetchfield('role_id'); + $role_id = (int) $this->db->sql_fetchfield('role_id'); if (!$role_id) { @@ -387,7 +394,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte FROM ' . GROUPS_TABLE . " WHERE group_name = '" . $this->db->sql_escape($name) . "'"; $this->db->sql_query($sql); - $group_id = $this->db->sql_fetchfield('group_id'); + $group_id = (int) $this->db->sql_fetchfield('group_id'); if (!$group_id) { @@ -401,7 +408,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte AND auth_role_id <> 0 AND forum_id = 0'; $this->db->sql_query($sql); - $role_id = $this->db->sql_fetchfield('auth_role_id'); + $role_id = (int) $this->db->sql_fetchfield('auth_role_id'); if ($role_id) { $sql = 'SELECT role_name @@ -437,7 +444,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte 'role_id' => $role_id, 'auth_option_id' => $auth_option_id, 'auth_setting' => $has_permission, - ); + ); } } @@ -453,7 +460,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte 'group_id' => $group_id, 'auth_option_id' => $auth_option_id, 'auth_setting' => $has_permission, - ); + ); } } @@ -462,8 +469,6 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte } $this->auth->acl_clear_prefetch(); - - return false; } /** @@ -474,6 +479,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte * @param string $name The name of the role/group * @param string|array $auth_option The auth_option or array of auth_options you would like to set * @param string $type The type (role|group) + * @return null */ public function permission_unset($name, $auth_option = array(), $type = 'role') { @@ -489,13 +495,13 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte $result = $this->db->sql_query($sql); while ($row = $this->db->sql_fetchrow($result)) { - $to_remove[] = $row['auth_option_id']; + $to_remove[] = (int) $row['auth_option_id']; } $this->db->sql_freeresult($result); - if (!sizeof($to_remove)) + if (empty($to_remove)) { - return false; + return; } $type = (string) $type; // Prevent PHP bug. @@ -507,11 +513,11 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte FROM ' . ACL_ROLES_TABLE . " WHERE role_name = '" . $this->db->sql_escape($name) . "'"; $this->db->sql_query($sql); - $role_id = $this->db->sql_fetchfield('role_id'); + $role_id = (int) $this->db->sql_fetchfield('role_id'); if (!$role_id) { - throw new phpbb_db_migration_exception('ROLE_NOT_EXIST', $name); + throw new phpbb_db_migration_exception('ROLE_NOT_EXIST', $name); } $sql = 'DELETE FROM ' . ACL_ROLES_DATA_TABLE . ' @@ -524,7 +530,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte FROM ' . GROUPS_TABLE . " WHERE group_name = '" . $this->db->sql_escape($name) . "'"; $this->db->sql_query($sql); - $group_id = $this->db->sql_fetchfield('group_id'); + $group_id = (int) $this->db->sql_fetchfield('group_id'); if (!$group_id) { @@ -537,7 +543,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte WHERE group_id = ' . $group_id . ' AND auth_role_id <> 0'; $this->db->sql_query($sql); - $role_id = $this->db->sql_fetchfield('auth_role_id'); + $role_id = (int) $this->db->sql_fetchfield('auth_role_id'); if ($role_id) { $sql = 'SELECT role_name @@ -556,7 +562,5 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte } $this->auth->acl_clear_prefetch(); - - return false; } } -- cgit v1.2.1 From f56e400cd36b6d17ffde90a91e6e221cb83d2dbd Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Wed, 9 Jan 2013 15:41:04 -0600 Subject: [feature/migrations] Comments PHPBB3-9737 --- phpBB/includes/db/migration/tool/permission.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'phpBB/includes/db/migration/tool/permission.php') diff --git a/phpBB/includes/db/migration/tool/permission.php b/phpBB/includes/db/migration/tool/permission.php index 97fdbc0df9..7b45b24361 100644 --- a/phpBB/includes/db/migration/tool/permission.php +++ b/phpBB/includes/db/migration/tool/permission.php @@ -366,7 +366,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte switch ($type) { - case 'role' : + case 'role': $sql = 'SELECT role_id FROM ' . ACL_ROLES_TABLE . " WHERE role_name = '" . $this->db->sql_escape($name) . "'"; @@ -389,7 +389,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte $this->db->sql_freeresult($result); break; - case 'group' : + case 'group': $sql = 'SELECT group_id FROM ' . GROUPS_TABLE . " WHERE group_name = '" . $this->db->sql_escape($name) . "'"; @@ -435,7 +435,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte $sql_ary = array(); switch ($type) { - case 'role' : + case 'role': foreach ($new_auth as $auth_option_id) { if (!isset($current_auth[$auth_option_id])) @@ -451,7 +451,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte $this->db->sql_multi_insert(ACL_ROLES_DATA_TABLE, $sql_ary); break; - case 'group' : + case 'group': foreach ($new_auth as $auth_option_id) { if (!isset($current_auth[$auth_option_id])) @@ -508,7 +508,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte switch ($type) { - case 'role' : + case 'role': $sql = 'SELECT role_id FROM ' . ACL_ROLES_TABLE . " WHERE role_name = '" . $this->db->sql_escape($name) . "'"; @@ -525,7 +525,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte $this->db->sql_query($sql); break; - case 'group' : + case 'group': $sql = 'SELECT group_id FROM ' . GROUPS_TABLE . " WHERE group_name = '" . $this->db->sql_escape($name) . "'"; -- cgit v1.2.1 From 3d4c00619f1c96df7a4c6f8bc1c03eb21abf49d7 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Wed, 9 Jan 2013 18:24:32 -0600 Subject: [feature/migrations] Reverse data functionality If data step fails, attempt to roll back any previous calls from the migration that failed. Fix some failing tests PHPBB3-9737 --- phpBB/includes/db/migration/tool/permission.php | 55 +++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'phpBB/includes/db/migration/tool/permission.php') diff --git a/phpBB/includes/db/migration/tool/permission.php b/phpBB/includes/db/migration/tool/permission.php index 7b45b24361..a25fdb345e 100644 --- a/phpBB/includes/db/migration/tool/permission.php +++ b/phpBB/includes/db/migration/tool/permission.php @@ -563,4 +563,59 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte $this->auth->acl_clear_prefetch(); } + + /** + * Reverse an original install action + * + * First argument is the original call to the class (e.g. add, remove) + * After the first argument, send the original arguments to the function in the original call + * + * @return null + */ + public function reverse() + { + $arguments = func_get_args(); + $original_call = array_shift($arguments); + + $call = false; + switch ($original_call) + { + case 'add': + $call = 'remove'; + break; + + case 'remove': + $call = 'add'; + break; + + case 'permission_set': + $call = 'permission_unset'; + break; + + case 'permission_unset': + $call = 'permission_set'; + break; + + case 'role_add': + $call = 'role_remove'; + break; + + case 'role_remove': + $call = 'role_add'; + break; + + case 'role_update': + // Set to the original value if the current value is what we compared to originally + $arguments = array( + $arguments[1], + $arguments[0], + ); + break; + } + + if ($call) + { + return call_user_func_array(array(&$this, $call), $arguments); + } + } } -- cgit v1.2.1 From 77df9109b61ec93c28a8120a8d81a045961b24ac Mon Sep 17 00:00:00 2001 From: Nathaniel Guse Date: Mon, 4 Feb 2013 13:46:23 -0600 Subject: [feature/migrations] Remove default values from necessary parameters Clean up some comments PHPBB3-9737 --- phpBB/includes/db/migration/tool/permission.php | 33 +++++++++++++------------ 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'phpBB/includes/db/migration/tool/permission.php') diff --git a/phpBB/includes/db/migration/tool/permission.php b/phpBB/includes/db/migration/tool/permission.php index a25fdb345e..001d090f5a 100644 --- a/phpBB/includes/db/migration/tool/permission.php +++ b/phpBB/includes/db/migration/tool/permission.php @@ -61,7 +61,8 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte * Check if a permission (auth) setting exists * * @param string $auth_option The name of the permission (auth) option - * @param bool $global True for checking a global permission setting, False for a local permission setting + * @param bool $global True for checking a global permission setting, + * False for a local permission setting * @return bool true if it exists, false if not */ public function exists($auth_option, $global = true) @@ -98,7 +99,8 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte * Add a permission (auth) option * * @param string $auth_option The name of the permission (auth) option - * @param bool $global True for checking a global permission setting, False for a local permission setting + * @param bool $global True for checking a global permission setting, + * False for a local permission setting * @return null */ public function add($auth_option, $global = true, $copy_from = false) @@ -180,7 +182,8 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte * Remove a permission (auth) option * * @param string $auth_option The name of the permission (auth) option - * @param bool $global True for checking a global permission setting, False for a local permission setting + * @param bool $global True for checking a global permission setting, + * False for a local permission setting * @return null */ public function remove($auth_option, $global = true) @@ -239,7 +242,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte * @param sting $role_type The type (u_, m_, a_) * @return null */ - public function role_add($role_name, $role_type = '', $role_description = '') + public function role_add($role_name, $role_type, $role_description = '') { $sql = 'SELECT role_id FROM ' . ACL_ROLES_TABLE . " @@ -277,7 +280,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte * @param string $new_role_name The new role name * @return null */ - public function role_update($old_role_name, $new_role_name = '') + public function role_update($old_role_name, $new_role_name) { $sql = 'SELECT role_id FROM ' . ACL_ROLES_TABLE . " @@ -332,12 +335,14 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte * Allows you to set permissions for a certain group/role * * @param string $name The name of the role/group - * @param string|array $auth_option The auth_option or array of auth_options you would like to set + * @param string|array $auth_option The auth_option or array of + * auth_options you would like to set * @param string $type The type (role|group) - * @param bool $has_permission True if you want to give them permission, false if you want to deny them permission + * @param bool $has_permission True if you want to give them permission, + * false if you want to deny them permission * @return null */ - public function permission_set($name, $auth_option = array(), $type = 'role', $has_permission = true) + public function permission_set($name, $auth_option, $type = 'role', $has_permission = true) { if (!is_array($auth_option)) { @@ -477,11 +482,12 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte * Allows you to unset (remove) permissions for a certain group/role * * @param string $name The name of the role/group - * @param string|array $auth_option The auth_option or array of auth_options you would like to set + * @param string|array $auth_option The auth_option or array of + * auth_options you would like to set * @param string $type The type (role|group) * @return null */ - public function permission_unset($name, $auth_option = array(), $type = 'role') + public function permission_unset($name, $auth_option, $type = 'role') { if (!is_array($auth_option)) { @@ -565,12 +571,7 @@ class phpbb_db_migration_tool_permission implements phpbb_db_migration_tool_inte } /** - * Reverse an original install action - * - * First argument is the original call to the class (e.g. add, remove) - * After the first argument, send the original arguments to the function in the original call - * - * @return null + * {@inheritdoc} */ public function reverse() { -- cgit v1.2.1