From 94a6dd5d29901a9e72ffa4c163b229d1864afea0 Mon Sep 17 00:00:00 2001 From: chmst Date: Sat, 6 Apr 2024 19:05:17 +0200 Subject: [PATCH 01/12] Delete user access level - check for levels in use --- .../com_users/src/Model/LevelModel.php | 140 ++++++++++-------- administrator/language/en-GB/com_users.ini | 3 +- 2 files changed, 84 insertions(+), 59 deletions(-) diff --git a/administrator/components/com_users/src/Model/LevelModel.php b/administrator/components/com_users/src/Model/LevelModel.php index f511143d530a3..d3f32c929460e 100644 --- a/administrator/components/com_users/src/Model/LevelModel.php +++ b/administrator/components/com_users/src/Model/LevelModel.php @@ -32,10 +32,89 @@ class LevelModel extends AdminModel { /** - * @var array A list of the access levels in use. - * @since 1.6 + * Method to delete one or more access levels. + * + * @param array $pks An array of record primary keys. + * + * @return boolean True if successful, false if an error occurs. + * + * @since __DEPLOY_VERSION__ */ - protected $levelsInUse = null; + public function delete(&$pks) + { + $pks = (array) $pks; + $table = $this->getTable(); + + $itemsInUse = []; + + // Iterate the items to delete each one. + foreach ($pks as $i => $pk) { + if ($table->load($pk)) { + + // Check if the access level is being used by any content. + $db = $this->getDatabase(); + $query = $db->getQuery(true) + ->select('DISTINCT access'); + + // Get all tabels that have the access field + $checkTables = $db->getTableList(); + $prefix = $db->getPrefix(); + + $itemsInUse[$pk] = []; + foreach ($checkTables as $checktable) { + + // Get all of the columns in the table + $fields = $db->getTableColumns($checktable); + + /** + * We are looking for the access field. If custom tables are using something other + * than the 'access' field they are on their own unfortunately. + * Also make sure the table prefix matches the live db prefix (eg, it is not a "bak_" table) + */ + if (strpos($checktable, $prefix) === 0 && isset($fields['access'])) { + // Lookup the distinct values of the field. + $query->clear('from') + ->from($db->quoteName($checktable)); + $db->setQuery($query); + + try { + $values = $db->loadColumn(); + } catch (\RuntimeException $e) { + $this->setError($e->getMessage()); + + return false; + } + + // Check if the table uses this access level + if (in_array($pk, $values)) { + + // Add the table to the list of tables that use this access level + $levelsInUse[$pk][] = $checktable; + + // Remove the access level from the list of items to delete + unset($pks[$i]); + } + } + } + } + } + + if (!empty($levelsInUse)) { + $msg = Text::_('COM_USERS_ERROR_VIEW_LEVEL_IN_USE'); + $msg .= ''; + + Factory::getApplication()->enqueueMessage($msg, 'error'); + } + + return parent::delete($pks); + } /** * Method to test whether a record can be deleted. @@ -65,61 +144,6 @@ protected function canDelete($record) } } - // Check if the access level is being used by any content. - if ($this->levelsInUse === null) { - // Populate the list once. - $this->levelsInUse = []; - - $db = $this->getDatabase(); - $query = $db->getQuery(true) - ->select('DISTINCT access'); - - // Get all the tables and the prefix - $tables = $db->getTableList(); - $prefix = $db->getPrefix(); - - foreach ($tables as $table) { - // Get all of the columns in the table - $fields = $db->getTableColumns($table); - - /** - * We are looking for the access field. If custom tables are using something other - * than the 'access' field they are on their own unfortunately. - * Also make sure the table prefix matches the live db prefix (eg, it is not a "bak_" table) - */ - if (strpos($table, $prefix) === 0 && isset($fields['access'])) { - // Lookup the distinct values of the field. - $query->clear('from') - ->from($db->quoteName($table)); - $db->setQuery($query); - - try { - $values = $db->loadColumn(); - } catch (\RuntimeException $e) { - $this->setError($e->getMessage()); - - return false; - } - - $this->levelsInUse = array_merge($this->levelsInUse, $values); - - // @todo Could assemble an array of the tables used by each view level list those, - // giving the user a clue in the error where to look. - } - } - - // Get uniques. - $this->levelsInUse = array_unique($this->levelsInUse); - - // Ok, after all that we are ready to check the record :) - } - - if (\in_array($record->id, $this->levelsInUse)) { - $this->setError(Text::sprintf('COM_USERS_ERROR_VIEW_LEVEL_IN_USE', $record->id, $record->title)); - - return false; - } - return parent::canDelete($record); } diff --git a/administrator/language/en-GB/com_users.ini b/administrator/language/en-GB/com_users.ini index 16f622a8c5871..1e4dbeab9f28f 100644 --- a/administrator/language/en-GB/com_users.ini +++ b/administrator/language/en-GB/com_users.ini @@ -104,7 +104,8 @@ COM_USERS_ERROR_LEVELS_NOLEVELS_SELECTED="No View Permission Level(s) selected." COM_USERS_ERROR_NO_ADDITIONS="The selected user(s) are already assigned to the selected group." COM_USERS_ERROR_NOT_IN_GROUP="The selected user(s) are not in the selected group." COM_USERS_ERROR_ONLY_ONE_GROUP="A user must belong to at least one group." -COM_USERS_ERROR_VIEW_LEVEL_IN_USE="You can't delete the view access level '%d:%s' because it is being used by content." +COM_USERS_ERROR_VIEW_LEVEL_IN_USE="You can't delete the view access level(s)" +COM_USERS_ERROR_VIEW_LEVEL_IN_USE_DETAILS="Level with ID %d is being used in the database tables: %s." COM_USERS_FIELDS_USER_FIELDS_TITLE="Users: Fields" COM_USERS_FIELDS_USER_FIELD_ADD_TITLE="Users: New Field" COM_USERS_FIELDS_USER_FIELD_EDIT_TITLE="Users: Edit Field" From 075ab71ea74b69b405f1faf36bdddd14bc433953 Mon Sep 17 00:00:00 2001 From: chmst Date: Sat, 6 Apr 2024 19:41:00 +0200 Subject: [PATCH 02/12] cs --- administrator/components/com_users/src/Model/LevelModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/components/com_users/src/Model/LevelModel.php b/administrator/components/com_users/src/Model/LevelModel.php index d3f32c929460e..9e8ec1e0c3102 100644 --- a/administrator/components/com_users/src/Model/LevelModel.php +++ b/administrator/components/com_users/src/Model/LevelModel.php @@ -58,7 +58,7 @@ public function delete(&$pks) // Get all tabels that have the access field $checkTables = $db->getTableList(); - $prefix = $db->getPrefix(); + $prefix = $db->getPrefix(); $itemsInUse[$pk] = []; foreach ($checkTables as $checktable) { From e449b223be9ba5b5d6e385d1c481d7e12c066172 Mon Sep 17 00:00:00 2001 From: chmst Date: Sat, 6 Apr 2024 19:41:44 +0200 Subject: [PATCH 03/12] cs --- administrator/components/com_users/src/Model/LevelModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/components/com_users/src/Model/LevelModel.php b/administrator/components/com_users/src/Model/LevelModel.php index 9e8ec1e0c3102..9c0d18c61caa1 100644 --- a/administrator/components/com_users/src/Model/LevelModel.php +++ b/administrator/components/com_users/src/Model/LevelModel.php @@ -86,7 +86,7 @@ public function delete(&$pks) } // Check if the table uses this access level - if (in_array($pk, $values)) { + if (\in_array($pk, $values)) { // Add the table to the list of tables that use this access level $levelsInUse[$pk][] = $checktable; From 203f678260d00acbe7f3ce14e6df0c5f814f8975 Mon Sep 17 00:00:00 2001 From: Christiane Maier-Stadtherr Date: Sun, 7 Apr 2024 09:44:01 +0200 Subject: [PATCH 04/12] Update administrator/components/com_users/src/Model/LevelModel.php Co-authored-by: Brian Teeman --- administrator/components/com_users/src/Model/LevelModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/components/com_users/src/Model/LevelModel.php b/administrator/components/com_users/src/Model/LevelModel.php index 9c0d18c61caa1..4a826ff0384f9 100644 --- a/administrator/components/com_users/src/Model/LevelModel.php +++ b/administrator/components/com_users/src/Model/LevelModel.php @@ -34,7 +34,7 @@ class LevelModel extends AdminModel /** * Method to delete one or more access levels. * - * @param array $pks An array of record primary keys. + * @param array $pks An array of primary keys. * * @return boolean True if successful, false if an error occurs. * From 2d715886d4614ad107aaf6387a0c025305c7f679 Mon Sep 17 00:00:00 2001 From: Christiane Maier-Stadtherr Date: Sun, 7 Apr 2024 09:44:34 +0200 Subject: [PATCH 05/12] Update administrator/components/com_users/src/Model/LevelModel.php Co-authored-by: Brian Teeman --- administrator/components/com_users/src/Model/LevelModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/components/com_users/src/Model/LevelModel.php b/administrator/components/com_users/src/Model/LevelModel.php index 4a826ff0384f9..53f2add346884 100644 --- a/administrator/components/com_users/src/Model/LevelModel.php +++ b/administrator/components/com_users/src/Model/LevelModel.php @@ -56,7 +56,7 @@ public function delete(&$pks) $query = $db->getQuery(true) ->select('DISTINCT access'); - // Get all tabels that have the access field + // Get all tables that have the access field $checkTables = $db->getTableList(); $prefix = $db->getPrefix(); From 44f9fd78992b7f56e9622770770bd0acd00ec8de Mon Sep 17 00:00:00 2001 From: chmst Date: Sun, 7 Apr 2024 09:47:03 +0200 Subject: [PATCH 06/12] comment --- administrator/components/com_users/src/Model/LevelModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/components/com_users/src/Model/LevelModel.php b/administrator/components/com_users/src/Model/LevelModel.php index 53f2add346884..f20e8adcee59c 100644 --- a/administrator/components/com_users/src/Model/LevelModel.php +++ b/administrator/components/com_users/src/Model/LevelModel.php @@ -51,7 +51,7 @@ public function delete(&$pks) foreach ($pks as $i => $pk) { if ($table->load($pk)) { - // Check if the access level is being used by any content. + // Check if the access level is being used. $db = $this->getDatabase(); $query = $db->getQuery(true) ->select('DISTINCT access'); From faceb6da9b40d579a89ea1bb082fe5ead52df8d2 Mon Sep 17 00:00:00 2001 From: chmst Date: Sun, 7 Apr 2024 09:53:52 +0200 Subject: [PATCH 07/12] cs --- administrator/components/com_users/src/Model/LevelModel.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/administrator/components/com_users/src/Model/LevelModel.php b/administrator/components/com_users/src/Model/LevelModel.php index f20e8adcee59c..222face7a309b 100644 --- a/administrator/components/com_users/src/Model/LevelModel.php +++ b/administrator/components/com_users/src/Model/LevelModel.php @@ -50,7 +50,6 @@ public function delete(&$pks) // Iterate the items to delete each one. foreach ($pks as $i => $pk) { if ($table->load($pk)) { - // Check if the access level is being used. $db = $this->getDatabase(); $query = $db->getQuery(true) @@ -62,7 +61,6 @@ public function delete(&$pks) $itemsInUse[$pk] = []; foreach ($checkTables as $checktable) { - // Get all of the columns in the table $fields = $db->getTableColumns($checktable); @@ -87,7 +85,6 @@ public function delete(&$pks) // Check if the table uses this access level if (\in_array($pk, $values)) { - // Add the table to the list of tables that use this access level $levelsInUse[$pk][] = $checktable; From 352e2736380c97d7888d164f923bcb79afa933a6 Mon Sep 17 00:00:00 2001 From: Christiane Maier-Stadtherr Date: Tue, 9 Apr 2024 14:29:36 +0200 Subject: [PATCH 08/12] Update administrator/components/com_users/src/Model/LevelModel.php Co-authored-by: Quy --- administrator/components/com_users/src/Model/LevelModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/components/com_users/src/Model/LevelModel.php b/administrator/components/com_users/src/Model/LevelModel.php index 222face7a309b..13512a8fcbcdd 100644 --- a/administrator/components/com_users/src/Model/LevelModel.php +++ b/administrator/components/com_users/src/Model/LevelModel.php @@ -45,7 +45,7 @@ public function delete(&$pks) $pks = (array) $pks; $table = $this->getTable(); - $itemsInUse = []; + $levelsInUse = []; // Iterate the items to delete each one. foreach ($pks as $i => $pk) { From 615eb4e8d8c67d3b21c0260bf52ab5610abe49b0 Mon Sep 17 00:00:00 2001 From: chmst Date: Tue, 9 Apr 2024 14:43:27 +0200 Subject: [PATCH 09/12] fix array levelsin use --- administrator/components/com_users/src/Model/LevelModel.php | 1 - 1 file changed, 1 deletion(-) diff --git a/administrator/components/com_users/src/Model/LevelModel.php b/administrator/components/com_users/src/Model/LevelModel.php index 13512a8fcbcdd..aa309de2d49b9 100644 --- a/administrator/components/com_users/src/Model/LevelModel.php +++ b/administrator/components/com_users/src/Model/LevelModel.php @@ -59,7 +59,6 @@ public function delete(&$pks) $checkTables = $db->getTableList(); $prefix = $db->getPrefix(); - $itemsInUse[$pk] = []; foreach ($checkTables as $checktable) { // Get all of the columns in the table $fields = $db->getTableColumns($checktable); From 8ed9f3826c6cb3794004e4f89712cdf52cf2e5c4 Mon Sep 17 00:00:00 2001 From: chmst Date: Thu, 23 May 2024 16:16:20 +0200 Subject: [PATCH 10/12] Use enque message for output --- .../components/com_users/src/Model/LevelModel.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/administrator/components/com_users/src/Model/LevelModel.php b/administrator/components/com_users/src/Model/LevelModel.php index aa309de2d49b9..dafb8585acb61 100644 --- a/administrator/components/com_users/src/Model/LevelModel.php +++ b/administrator/components/com_users/src/Model/LevelModel.php @@ -96,17 +96,13 @@ public function delete(&$pks) } if (!empty($levelsInUse)) { - $msg = Text::_('COM_USERS_ERROR_VIEW_LEVEL_IN_USE'); - $msg .= '
    '; + $app = Factory::getApplication(); + $app->enqueueMessage(Text::_('COM_USERS_ERROR_VIEW_LEVEL_IN_USE'), 'error'); foreach ($levelsInUse as $levelId => $usedIn) { - $text = Text::sprintf('COM_USERS_ERROR_VIEW_LEVEL_IN_USE_DETAILS', $levelId, implode(', ', $usedIn)); - $msg .= '
  • ' . $text . '
  • '; + $msg = Text::sprintf('COM_USERS_ERROR_VIEW_LEVEL_IN_USE_DETAILS', $levelId, implode(', ', $usedIn)); + $app->enqueueMessage($msg, 'error'); } - - $msg .= '
'; - - Factory::getApplication()->enqueueMessage($msg, 'error'); } return parent::delete($pks); From f8ee84c6d52d541eeae36667db760a8fa86e02f9 Mon Sep 17 00:00:00 2001 From: chmst Date: Fri, 24 May 2024 10:57:34 +0200 Subject: [PATCH 11/12] check empty array pks --- administrator/components/com_users/src/Model/LevelModel.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/administrator/components/com_users/src/Model/LevelModel.php b/administrator/components/com_users/src/Model/LevelModel.php index dafb8585acb61..0761109820993 100644 --- a/administrator/components/com_users/src/Model/LevelModel.php +++ b/administrator/components/com_users/src/Model/LevelModel.php @@ -104,6 +104,11 @@ public function delete(&$pks) $app->enqueueMessage($msg, 'error'); } } + + if (empty($pks)) { + // Nothing left to delete + return true; + } return parent::delete($pks); } From 550e25f4b89e58a7fa294aa293b5d2f16d1f8f36 Mon Sep 17 00:00:00 2001 From: chmst Date: Fri, 2 Aug 2024 22:04:59 +0200 Subject: [PATCH 12/12] cs --- administrator/components/com_users/src/Model/LevelModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/components/com_users/src/Model/LevelModel.php b/administrator/components/com_users/src/Model/LevelModel.php index 0761109820993..9164435c26b72 100644 --- a/administrator/components/com_users/src/Model/LevelModel.php +++ b/administrator/components/com_users/src/Model/LevelModel.php @@ -104,7 +104,7 @@ public function delete(&$pks) $app->enqueueMessage($msg, 'error'); } } - + if (empty($pks)) { // Nothing left to delete return true;