{"id":2316,"date":"2020-09-14T11:30:51","date_gmt":"2020-09-14T16:30:51","guid":{"rendered":"https:\/\/xlinesoft.com\/blog\/?p=2316"},"modified":"2020-09-14T13:31:27","modified_gmt":"2020-09-14T18:31:27","slug":"password-protecting-additional-admin-actions","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2020\/09\/14\/password-protecting-additional-admin-actions\/","title":{"rendered":"Password-protecting additional admin actions"},"content":{"rendered":"<p>Some businesses may require two people to confirm certain actions like big transactions may require a supervisor&#8217;s approval. Another scenario &#8211; certain actions require entering the second password. This additional password can be changed daily and distributed among employees in the morning along with the secret handshake. Btw, the whole application doesn&#8217;t need to be password-protected, you can add the password to a certain action. <\/p>\n<p>In this article, we will show how to implement this additional password security feature. We will cover two scenarios here:<br \/>\n1. Password-protecting custom button<br \/>\n2. Password-protecting editing the field in inline mode<\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/09\/scr_password_protect_inline_edit.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/09\/scr_password_protect_inline_edit-300x163.png\" alt=\"\" width=\"300\" height=\"163\" class=\"alignnone size-medium wp-image-2323\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/09\/scr_password_protect_inline_edit-300x163.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/09\/scr_password_protect_inline_edit-768x417.png 768w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/09\/scr_password_protect_inline_edit-1024x556.png 1024w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/09\/scr_password_protect_inline_edit.png 1032w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n<!--more--><\/p>\n<h2>The code<\/h2>\n<h3>1. Creating &#8216;settings&#8217; table<\/h3>\n<p>This example is for MySQL. This SQL script creates the required table and columns and inserts the record with the password. The password by default is &#8216;password&#8217;.  <\/p>\n<pre name=\"code\" class=\"sql:nocontrols\">\r\nCREATE TABLE `settings` (\r\n  `id` int(11) NOT NULL,\r\n  `password` varchar(50) DEFAULT NULL\r\n);\r\n\r\nALTER TABLE `settings`\r\n  ADD PRIMARY KEY (`id`);\r\n\r\nALTER TABLE `settings`\r\n  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;\r\n\r\nINSERT INTO `settings` (`id`, `password`) VALUES (NULL, MD5('password'));\r\n<\/pre>\n<p>Room for improvement: use bcrypt instead of MD5 hashing. <\/p>\n<h3>2. AfterAppInit event code<\/h3>\n<p>The following code verifies the password and returns true or false in JSON format. If you use another table instead of &#8216;settings&#8217; make sure to update the code accordingly.<\/p>\n<p><strong>PHP<\/strong><\/p>\n<pre name=\"code\" class=\"php:nocontrols\">\r\nif( postvalue(\"a\") === \"check_password\" ){\r\n\t$db_password = DB::Query(\"select password from settings where id=1\")->value(0);\r\n\tprint json_encode( array(\"success\" => ( $db_password === md5( postvalue(\"password\") ) ) ) );\r\n\texit();\r\n}\r\n<\/pre>\n<p><strong>C#<\/strong><\/p>\n<pre name=\"code\" class=\"csharp:nocontrols\">\r\ndynamic action = MVCFunctions.postvalue(\"a\");\r\nif(action == new XVar(\"check_password\")){\r\n\tdynamic record;\r\n\tdynamic rs = DB.Select(\"settings\", \"id=1\");\r\n\trecord = rs.fetchAssoc();\r\n\tdynamic response = XVar.Array();\r\n\tresponse.InitAndSetArrayItem( (record[\"password\"] == MVCFunctions.md5(MVCFunctions.postvalue(\"password\")) ), \"success\");\r\n\tMVCFunctions.Echo(MVCFunctions.my_json_encode((XVar)(response)));\r\n\tMVCFunctions.Exit();\r\n}\r\n<\/pre>\n<h3>3. Password-protecting custom button<\/h3>\n<p>The following code goes to the button&#8217;s ClientBefore event.<\/p>\n<pre name=\"code\" class=\"javascript:nocontrols\">\r\nvar password = prompt('Enter a password');\r\n$.post(\"\",{a:\"check_password\",password:password},function(response) {\r\n\tvar result = JSON.parse(response);\r\n\tif(result.success){\r\n        \/\/ here goes your additional code ClientBefore code if any\r\n        \/\/ ...\r\n\r\n\t\tajax.submit();\r\n\t}\r\n\telse{\r\n\t\talert(\"Incorrect password\");\r\n\t}\r\n});\r\nreturn false;\r\n<\/pre>\n<p>Room for improvement: use Dialog API instead of Javascript&#8217;s prompt() function.<\/p>\n<h3>4. Password-protecting field changes<\/h3>\n<p>Enable Inline Edit for the table in question. Proceed to &#8216;View as\/Edit as&#8217; settings of the field you want to password-protect. Add Field event for &#8216;change&#8217;, choose &#8216;AJAX code snippet&#8217; and add the following code to ClientBefore part.<\/p>\n<pre name=\"code\" class=\"javascript:nocontrols\">\r\nvar password = prompt('Enter a password');\r\n$.post(\"\",{a:\"check_password\",password:password},function(response) {\r\n\tvar result = JSON.parse(response);\r\n\tif(!result.success){\r\n\t\talert(\"Incorrect password\");\r\n\t\tctrl.setValue(ctrl.defaultValue);\r\n\t}\r\n});\r\nreturn false;\r\n<\/pre>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some businesses may require two people to confirm certain actions like big transactions may require a supervisor&#8217;s approval. Another scenario &#8211; certain actions require entering the second password. This additional password can be changed daily and distributed among employees in the morning along with the secret handshake. Btw, the whole application doesn&#8217;t need to be password-protected, you can add the password to a certain action. In this article, we will show how to implement this additional password security feature. We will cover two scenarios here:&#8230;<span class=\"clearfix clearfix-post\"><\/span><a href=\"https:\/\/xlinesoft.com\/blog\/2020\/09\/14\/password-protecting-additional-admin-actions\/\" class=\"more-link\">Continue Reading <span class=\"screen-reader-text\">&#8220;Password-protecting additional admin actions&#8221;<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,10,1,8],"tags":[],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2316"}],"collection":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/comments?post=2316"}],"version-history":[{"count":10,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2316\/revisions"}],"predecessor-version":[{"id":2328,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2316\/revisions\/2328"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=2316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=2316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=2316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}