{"id":2537,"date":"2021-11-15T13:02:47","date_gmt":"2021-11-15T18:02:47","guid":{"rendered":"https:\/\/xlinesoft.com\/blog\/?p=2537"},"modified":"2022-10-15T09:11:10","modified_gmt":"2022-10-15T14:11:10","slug":"providing-temporary-access-to-your-application","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2021\/11\/15\/providing-temporary-access-to-your-application\/","title":{"rendered":"Providing temporary access to your application"},"content":{"rendered":"<p>Let&#8217;s say you need to provide users temporary access to your application. You create a user, set their access to &#8216;temporary&#8217; and after 24 hours they should not be able to log on to your application. And, of course, you need this to happen automatically.  <\/p>\n<p>1. Add two more fields to the login table. <\/p>\n<p><strong>access<\/strong> &#8211; varchar(50). This field will store values like &#8220;temporary&#8221; or &#8220;inactive&#8221;. It can be left empty for existing users.<\/p>\n<p><strong>temporary_access_starts<\/strong> &#8211; datetime. Indicates when the temporary access starts. <\/p>\n<p>2. Add <strong>access<\/strong> field to Add\/Edit pages of the login table. Set &#8216;Edit as&#8217; type to &#8216;Lookup wizard&#8217; and add two hardcoded values there: &#8220;temporary&#8221; and &#8220;inactive&#8221;. Now the admin can create users with temporary access and make them inactive manually.<\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/11\/scr_access_field.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/11\/scr_access_field.png\" alt=\"\" width=\"597\" height=\"446\" class=\"alignnone size-full wp-image-2545\" \/><\/a><\/p>\n<p>3. Login table -> Add page -> Before Record Added event<\/p>\n<p>Here we simply initialize <strong>temporary_access_starts<\/strong> with the value of the current date\/time. <\/p>\n<p><strong>PHP code:<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"php\" name=\"mshighlighter\" >\r\nif($values[\"access\"] == \"temporary\"){\r\n\t$values[\"temporary_access_starts\"] = date(\"Y-m-d H:i:s\"); ;\r\n}\r\nreturn true;<\/textarea><\/pre>\n<\/div>\n<p><strong>C# code:<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"js\" name=\"mshighlighter\" >\r\nif(values[\"access\"] == \"temporary\")\r\n{\r\n\tvalues[\"temporary_access_starts\"] = CommonFunctions.date(new XVar(\"Y-m-d H:i:s\"));\r\n}\r\nreturn true;<\/textarea><\/pre>\n<\/div>\n<p>4. Login Page -> BeforeLogin event<\/p>\n<p>In this event we first check the value of user&#8217;s <strong>access<\/strong> field. If it says &#8220;inactive&#8221;, we reject the login. If it says &#8220;temporary&#8221; and the value of <strong>temporary_access_starts<\/strong> field is more than 24 hours old, then we also reject the login. All other users can log on. <\/p>\n<p><strong>PHP code:<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"php\" name=\"mshighlighter\" >\r\n$user = DB::Select(\"users\", array(\"username\" => $username))->fetchAssoc();\r\nif ($user && $user[\"access\"] == \"inactive\") {\r\n    return false;\r\n}\r\nif ($user  && $user[\"access\"] == \"temporary\") {\r\n    $date_cur = date_create_from_format('\"Y-m-d H:i:s\"', date('\"Y-m-d H:i:s\"'));\r\n    $date_end_access = date_create_from_format('\"Y-m-d H:i:s\"', date('\"Y-m-d H:i:s\"', strtotime($user[\"temporary_access_starts\"])));\r\n    $diff = date_diff($date_cur, $date_end_access);\r\n    \/\/ >24 hours\r\n    if ($diff->h > 24) return false;\r\n}\r\nreturn true;<\/textarea><\/pre>\n<\/div>\n<p><strong>C# code:<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"js\" name=\"mshighlighter\" >\r\ndynamic user = XVar.Array();\r\nuser = XVar.Clone(DB.Select(new XVar(\"users\"), (XVar)(new XVar(\"username\", username))).fetchAssoc());\r\nif((XVar)(user)  && (XVar)(user[\"access\"] == \"inactive\"))\r\n{\r\n\treturn false;\r\n}\r\nif((XVar)(user)  && (XVar)(user[\"access\"] == \"temporary\"))\r\n{\r\n   DateTime startTime = DateTime.Now;\r\n   DateTime endTime = Convert.ToDateTime(user[\"temporary_access_starts\"].ToString());\r\n   TimeSpan span = endTime.Subtract ( startTime );\r\n   if(span.Hours > 24) {\r\n      return false;\r\n   }\r\n}\r\nreturn true;<\/textarea><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say you need to provide users temporary access to your application. You create a user, set their access to &#8216;temporary&#8217; and after 24 hours they should not be able to log on to your application. And, of course, you need this to happen automatically. 1. Add two more fields to the login table. access &#8211; varchar(50). This field will store values like &#8220;temporary&#8221; or &#8220;inactive&#8221;. It can be left empty for existing users. temporary_access_starts &#8211; datetime. Indicates when the temporary access starts. 2. Add&#8230;<span class=\"clearfix clearfix-post\"><\/span><a href=\"https:\/\/xlinesoft.com\/blog\/2021\/11\/15\/providing-temporary-access-to-your-application\/\" class=\"more-link\">Continue Reading <span class=\"screen-reader-text\">&#8220;Providing temporary access to your application&#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],"tags":[],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2537"}],"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=2537"}],"version-history":[{"count":11,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2537\/revisions"}],"predecessor-version":[{"id":2836,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2537\/revisions\/2836"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=2537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=2537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=2537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}