Commit 32ea4716cf8a54003616c4cedb65166098bf305c

Authored by Jonathan Byrne
1 parent 7841525d

KTS-1390

"A more precise error message needed when creating a duplicate of an already existing Permission"

Fixed
Added a more detailed error message system.

Committed By: Jonathan Byrne
Reviewed By: Kevin Fourie

git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@7674 c91229c3-7414-0410-bfa2-8a42b809f60b
i18n/knowledgeTree.pot
@@ -4062,8 +4062,32 @@ msgstr "" @@ -4062,8 +4062,32 @@ msgstr ""
4062 msgid "Error creating allocation" 4062 msgid "Error creating allocation"
4063 msgstr "" 4063 msgstr ""
4064 4064
4065 -#: plugins/ktcore/admin/managePermissions.php:76  
4066 -msgid "Error creating permission" 4065 +#: plugins/ktcore/admin/managePermissions.php:81
  4066 +msgid "An error occured while creating your permission"
  4067 +msgstr ""
  4068 +
  4069 +#: plugins/ktcore/admin/managePermissions.php:86
  4070 +msgid "An error occured while creating your permission: The System Name was not provided."
  4071 +msgstr ""
  4072 +
  4073 +#: plugins/ktcore/admin/managePermissions.php:92
  4074 +msgid "An error occured while creating your permission: The Display Name was not provided."
  4075 +msgstr ""
  4076 +
  4077 +#: plugins/ktcore/admin/managePermissions.php:98
  4078 +msgid "An error occured while creating your permission: The Display Name and System Name weren't provided."
  4079 +msgstr ""
  4080 +
  4081 +#: plugins/ktcore/admin/managePermissions.php:120
  4082 +msgid "An error occured while creating your permission: The Display Name and System Name you have provided both already exist."
  4083 +msgstr ""
  4084 +
  4085 +#: plugins/ktcore/admin/managePermissions.php:129
  4086 +msgid "An error occured while creating your permission: A permission with the same System Name already exists."
  4087 +msgstr ""
  4088 +
  4089 +#: plugins/ktcore/admin/managePermissions.php:138
  4090 +msgid "An error occured while creating your permission: A permission with the same Display Name already exists."
4067 msgstr "" 4091 msgstr ""
4068 4092
4069 #: plugins/ktcore/admin/managePermissions.php:95 4093 #: plugins/ktcore/admin/managePermissions.php:95
plugins/ktcore/admin/managePermissions.php
@@ -44,13 +44,25 @@ require_once(KT_LIB_DIR . "/widgets/fieldWidgets.php"); @@ -44,13 +44,25 @@ require_once(KT_LIB_DIR . "/widgets/fieldWidgets.php");
44 class ManagePermissionsDispatcher extends KTAdminDispatcher { 44 class ManagePermissionsDispatcher extends KTAdminDispatcher {
45 var $sHelpPage = 'ktcore/admin/manage permissions.html'; 45 var $sHelpPage = 'ktcore/admin/manage permissions.html';
46 function do_main() { 46 function do_main() {
  47 + session_start();
47 $this->oPage->setTitle(_kt('Manage Permissions')); 48 $this->oPage->setTitle(_kt('Manage Permissions'));
48 $this->aBreadcrumbs[] = array('url' => $_SERVER['PHP_SELF'], 'name' => _kt('Manage Permissions')); 49 $this->aBreadcrumbs[] = array('url' => $_SERVER['PHP_SELF'], 'name' => _kt('Manage Permissions'));
49 50
50 $add_fields = array(); 51 $add_fields = array();
51 - $add_fields[] = new KTStringWidget(_kt('System Name'), _kt('The internal name used for the permission. This should never be changed.'), 'name', null, $this->oPage, true);  
52 - $add_fields[] = new KTStringWidget(_kt('Display Name'), _kt('A short name that is shown to users whenever permissions must be assigned.'), 'human_name', null, $this->oPage, true); 52 + $add_fields[] = new KTStringWidget(_kt('System Name'), _kt('The internal name used for the permission. This should never be changed.'), 'name', null, $this->oPage, true, 'name');
  53 + $add_fields[] = new KTStringWidget(_kt('Display Name'), _kt('A short name that is shown to users whenever permissions must be assigned.'), 'human_name', null, $this->oPage, true, 'human_name');
53 54
  55 + if($_SESSION['Permission']['NameValue'])
  56 + {
  57 + $this->sNameVal = $_SESSION['Permission']['NameValue'];
  58 + $_SESSION['Permission']['NameValue'] = '';
  59 + }
  60 + else if($_SESSION['Permission']['HumanNameValue'])
  61 + {
  62 + $this->sHumanNameVal = $_SESSION['Permission']['HumanNameValue'];
  63 + $_SESSION['Permission']['HumanNameValue'] = '';
  64 + }
  65 +
54 $oTemplating =& KTTemplating::getSingleton(); 66 $oTemplating =& KTTemplating::getSingleton();
55 $aPermissions =& KTPermission::getList(); 67 $aPermissions =& KTPermission::getList();
56 $oTemplate = $oTemplating->loadTemplate("ktcore/manage_permissions"); 68 $oTemplate = $oTemplating->loadTemplate("ktcore/manage_permissions");
@@ -63,17 +75,75 @@ class ManagePermissionsDispatcher extends KTAdminDispatcher { @@ -63,17 +75,75 @@ class ManagePermissionsDispatcher extends KTAdminDispatcher {
63 } 75 }
64 76
65 function do_newPermission() { 77 function do_newPermission() {
66 - $name = KTUtil::arrayGet($_REQUEST, 'name');  
67 - $human_name = KTUtil::arrayGet($_REQUEST, 'human_name');  
68 - if (empty($name) || empty($human_name)) {  
69 - return $this->errorRedirectToMain(_kt("Both names not given")); 78 + session_start();
  79 + $sName = KTUtil::arrayGet($_REQUEST, 'name');
  80 + $sHumanName = KTUtil::arrayGet($_REQUEST, 'human_name');
  81 + $sError = 'An error occured while creating your permission';
  82 +
  83 + //Checking that the System Name and Display Name fields aren't empty
  84 + if (empty($sName) && !empty($sHumanName))
  85 + {
  86 + $sError = 'An error occured while creating your permission: The System Name was not provided.';
  87 + $_SESSION['Permission']['HumanNameValue'] = $sHumanName;
  88 + return $this->errorRedirectToMain(_kt($sError));
70 } 89 }
71 - $oPerm = KTPermission::createFromArray(array(  
72 - 'name' => $name,  
73 - 'humanname' => $human_name, 90 + else if(!empty($sName) && empty($sHumanName))
  91 + {
  92 + $sError = 'An error occured while creating your permission: The Display Name was not provided.';
  93 + $_SESSION['Permission']['NameValue'] = $sName;
  94 + return $this->errorRedirectToMain(_kt($sError));
  95 + }
  96 + else if (empty($sName) && empty($sHumanName))
  97 + {
  98 + $sError = 'An error occured while creating your permission: The Display Name and System Name weren\'t provided.';
  99 + return $this->errorRedirectToMain(_kt($sError));
  100 + }
  101 +
  102 + //Checking that the System Name and Display Name aren't already in the database
  103 + $aPermissions = KTPermission::getList();
  104 + //$iNameErrorCount and $iHumanNameErrorCount are used to check whether only one name is duplicated or if two names are duplicated.
  105 + $iNameErrorCount = 0;
  106 + $iHumanNameErrorCount = 0;
  107 + foreach ($aPermissions as $aPermission)
  108 + {
  109 + if($sName == $aPermission->getName())
  110 + {
  111 + $iNameErrorCount ++;
  112 + }
  113 + if ($sHumanName == $aPermission->getHumanName())
  114 + {
  115 + $iHumanNameErrorCount ++;
  116 + }
  117 + }
  118 + if ($iNameErrorCount > 0 && $iHumanNameErrorCount > 0)
  119 + {
  120 + $sError = 'An error occured while creating your permission: The Display Name and System Name you have provided both already exist.';
  121 + return $this->errorRedirectToMain(_kt($sError));
  122 + }
  123 + else if ($iNameErrorCount > 0 && $iHumanNameErrorCount == 0)
  124 + {
  125 + if(!empty($sHumanName))
  126 + {
  127 + $_SESSION['Permission']['HumanNameValue'] = $sHumanName;
  128 + }
  129 + $sError = 'An error occured while creating your permission: A permission with the same System Name already exists.';
  130 + return $this->errorRedirectToMain(_kt($sError));
  131 + }
  132 + else if ($iNameErrorCount == 0 && $iHumanNameErrorCount > 0)
  133 + {
  134 + if(!empty($sName))
  135 + {
  136 + $_SESSION['Permission']['NameValue'] = $sName;
  137 + }
  138 + $sError = 'An error occured while creating your permission: A permission with the same Display Name already exists.';
  139 + return $this->errorRedirectToMain(_kt($sError));
  140 + }
  141 + $oPerm = KTPermission::createFromArray(array(
  142 + 'name' => $sName,
  143 + 'humanname' => $sHumanName,
74 )); 144 ));
75 if (PEAR::isError($oPerm)) { 145 if (PEAR::isError($oPerm)) {
76 - return $this->errorRedirectToMain(_kt("Error creating permission")); 146 + return $this->errorRedirectToMain(_kt($sError));
77 } 147 }
78 return $this->successRedirectToMain(_kt("Permission created")); 148 return $this->successRedirectToMain(_kt("Permission created"));
79 } 149 }
templates/ktcore/manage_permissions.smarty
  1 +{literal}
  2 +<script type="text/javascript">
  3 + function populateField(value, key){
  4 + var field = document.getElementById(key);
  5 + field.value = value;
  6 + }
  7 +</script>
  8 +{/literal}
1 <h2>{i18n}Existing permissions{/i18n}</h2> 9 <h2>{i18n}Existing permissions{/i18n}</h2>
2 10
3 <p class="descriptiveText">{i18n}Permissions are descriptors used to ascertain whether groups of users have access to certain functionality. The built-in permissions below facilitate the default functionality of the DMS and can't be changed. Plugin developers may choose to add additional permissions below that manage access to their plugins functionality.{/i18n}</p> 11 <p class="descriptiveText">{i18n}Permissions are descriptors used to ascertain whether groups of users have access to certain functionality. The built-in permissions below facilitate the default functionality of the DMS and can't be changed. Plugin developers may choose to add additional permissions below that manage access to their plugins functionality.{/i18n}</p>
@@ -54,4 +62,13 @@ class=&quot;ktAction ktDelete&quot;&gt;{i18n}Delete Permission{/i18n}&lt;/a&gt; @@ -54,4 +62,13 @@ class=&quot;ktAction ktDelete&quot;&gt;{i18n}Delete Permission{/i18n}&lt;/a&gt;
54 </tr> 62 </tr>
55 { /foreach } 63 { /foreach }
56 </table> 64 </table>
57 - 65 +{if $context->sNameVal != ''}
  66 +<script type="text/javascript">
  67 + populateField("{$context->sNameVal}", "name");
  68 +</script>
  69 +{/if}
  70 +{if $context->sHumanNameVal != ''}
  71 +<script type="text/javascript">
  72 + populateField("{$context->sHumanNameVal}", "human_name");
  73 +</script>
  74 +{/if}
58 \ No newline at end of file 75 \ No newline at end of file