{"id":2497,"date":"2021-09-09T17:59:44","date_gmt":"2021-09-09T22:59:44","guid":{"rendered":"https:\/\/xlinesoft.com\/blog\/?p=2497"},"modified":"2022-10-15T09:26:56","modified_gmt":"2022-10-15T14:26:56","slug":"providing-access-to-web-application-via-unique-link","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2021\/09\/09\/providing-access-to-web-application-via-unique-link\/","title":{"rendered":"Providing access to web application via unique link"},"content":{"rendered":"<p>Some web applications need to provide quick access to certain pages or documents. For instance, you can share a file via such a link or send a link to an invoice to be paid to your customer like the one on the screenshot below.<\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/09\/scr_hash_link.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/09\/scr_hash_link-600x331.png\" alt=\"\" width=\"600\" height=\"331\" class=\"alignnone size-medium wp-image-2498\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/09\/scr_hash_link-600x331.png 600w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/09\/scr_hash_link-768x424.png 768w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/09\/scr_hash_link-1024x565.png 1024w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/09\/scr_hash_link.png 1096w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>Let&#8217;s see how you can implement this kind of link in your own project. The key is to create add a new text field to the table with invoices or documents that will store a long unique record identifier which will take a long time to guess. For practical reasons, this can be varchar(100) text field. In our example, this field name will <strong>hash<\/strong>.<\/p>\n<p>And here is the sample URL that will provide the access to a single invoice:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"js\" name=\"mshighlighter\" >\r\ninvoices_view.php?hash=4f92e96540bc661ffaf9e63245342c486ca9e19<\/textarea><\/pre>\n<\/div>\n<p>This is a two-step process. First, when we create a new record, we also need to populate the hash field with some unique value. And second, when someone opens a link like this, we <\/p>\n<h3>1. Populate hash field<\/h3>\n<p>In <strong>BeforeAdd<\/strong> event you can use the code like this:<\/p>\n<p><b>PHP<\/b><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"php\" name=\"mshighlighter\" >\r\n$values[\"hash\"] = generatePassword(50);<\/textarea><\/pre>\n<\/div>\n<p><b>C#<\/b><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"js\" name=\"mshighlighter\" >\r\nvalues[\"hash\"] = CommonFunctions.generatePassword(50);<\/textarea><\/pre>\n<\/div>\n<p>There are many ways to generate a long random string, we use the built-in Runner&#8217;s function <strong>generatePassword()<\/strong> but you can use any other method. <\/p>\n<h3>2. Process hash value when someone opens the link<\/h3>\n<p>In our example we provide access to the View page, so the code below needs to go to <strong>View page: BeforeProcess<\/strong> event. Make sure to use the correct table and field names like <strong>invoices<\/strong> and <strong>id<\/strong>.<\/p>\n<p><b>PHP:<\/b><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"php\" name=\"mshighlighter\" >\r\n\/\/ hash is required for the GUEST user\r\nif ( Security::isGuest() && !postvalue(\"hash\") ) \r\n{\r\n\t\techo \"the hash number is required\";\t\r\n\t\texit();\r\n} \r\n\t\r\nif (postvalue(\"hash\"))\r\n{\r\n\t$data[\"hash\"] = postvalue(\"hash\");\r\n\t$rs = DB::Select(\"invoices\", $data );\r\n\t$record = $rs->fetchAssoc();\r\n\t\r\n\tif(!$record)\r\n\t{\r\n\t\techo \"The hash number is incorrect\";\t\r\n\t\texit();\r\n\t}\r\n\t\r\n\t$keys = array();\r\n\t$keys[\"id\"] = $record[\"id\"];\r\n\t$pageObject->setKeys( $keys );\t\t\r\n}<\/textarea><\/pre>\n<\/div>\n<p><b>C#:<\/b><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"js\" name=\"mshighlighter\" >\r\nif((XVar)(Security.isGuest())  && (XVar)(!(XVar)(MVCFunctions.postvalue(new XVar(\"hash\")))))\r\n\t{\r\n\t\tMVCFunctions.Echo(\"the hash number is required\");\r\n\t\tMVCFunctions.ob_flush();\r\n\t\tHttpContext.Current.Response.End();\r\n\t\tthrow new RunnerInlineOutputException();\r\n\t}\r\n\tif(XVar.Pack(MVCFunctions.postvalue(new XVar(\"hash\"))))\r\n\t{\r\n\t\tdata.InitAndSetArrayItem(MVCFunctions.postvalue(new XVar(\"hash\")), \"hash\");\r\n\t\trs = XVar.Clone(DB.Select(new XVar(\"invoices\"), (XVar)(data)));\r\n\t\trecord = XVar.Clone(rs.fetchAssoc());\r\n\t\tif(XVar.Pack(!(XVar)(record)))\r\n\t\t{\r\n\t\t\tMVCFunctions.Echo(\"The hash number is incorrect\");\r\n\t\t\tMVCFunctions.ob_flush();\r\n\t\t\tHttpContext.Current.Response.End();\r\n\t\t\tthrow new RunnerInlineOutputException();\r\n\t\t}\r\n\t\tkeys = XVar.Clone(XVar.Array());\r\n\t\tkeys.InitAndSetArrayItem(record[\"id\"], \"id\");\r\n\t\tpageObject.setKeys((XVar)(keys));\r\n\t}\r\n\treturn null;<\/textarea><\/pre>\n<\/div>\n<h3>3. View page permissions for GUEST users<\/h3>\n<p>Make sure that GUEST account has access to the invoice View page. This can be done via Static or Dynamic User Group Permissions. <\/p>\n<p>This is it, you can now send your users links to individual invoices that they can access without logging in. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some web applications need to provide quick access to certain pages or documents. For instance, you can share a file via such a link or send a link to an invoice to be paid to your customer like the one on the screenshot below. Let&#8217;s see how you can implement this kind of link in your own project. The key is to create add a new text field to the table with invoices or documents that will store a long unique record identifier which will&#8230;<span class=\"clearfix clearfix-post\"><\/span><a href=\"https:\/\/xlinesoft.com\/blog\/2021\/09\/09\/providing-access-to-web-application-via-unique-link\/\" class=\"more-link\">Continue Reading <span class=\"screen-reader-text\">&#8220;Providing access to web application via unique link&#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,1,8],"tags":[],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2497"}],"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=2497"}],"version-history":[{"count":14,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2497\/revisions"}],"predecessor-version":[{"id":2841,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2497\/revisions\/2841"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=2497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=2497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=2497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}