This detailed sample is really interesting because it shows you how to use security profiles in addition to scripting. You, moreover, have a working demonstration below.
The goal of this script is
The result:
if a user wants to view our password protected pages, he/she will be prompted for the password.
if he/she provides the correct password, he/she will be granted an access to the pages.
otherwise an error page is displayed.
To cache the password, we will use a persistent global variable name “CachePassword1”.
The secret password is “reality”.
First step: writing the script
Since we use a security profile, we will write a Boolean function that can be used as a condition for our security profile. First of all, the script part.
Go to the Script Manager, create a blank script named “MySecurity1” and copy/paste the following code:
// MySecurity1
// A script to password protect pages.
// Our secret password:
const
OurPassword="reality";
function CheckSecurity1: Boolean;
var
S: String;
begin
{ This function is called by the security profile.
If it returns True, we configure the actions so that
the user may NOT view the pages.
If it returns False, then the secure pages may be displayed. }
// 1) Check if the password was cached.
S := GetGlobalVar("CachePassword1", "");
if S="" then
begin
// The password was never asked.
// So we ask it.
S := InputBox("Password protected page"#13#10 +
"This page is password protected; please enter the password in order to continue",
"Security", "");
end;
// 2) Check whether the password is correct.
Result := S <> OurPassword;
// Cache the password if valid.
if not Result then SetGlobalVar("CachePassword1", S, True);
// Result = true => pages locked.
// Result = false => pages unlocked.
end;
Thanks to this code, we have a Boolean function that we can use in a security profile.
Press Save to save the script.
Second step: create a security profile
Third step: select the pages you want to protect
Final
You can now compile your publication and test it.
Do you want to test this sample?
The following page was password protected using the same method. Try to use a wrong password and then use the same password as above to access it.
If you enter a wrong password, you will be redirected to a custom error page (use the Locking Pages option above to do that).
Note: by default the password is cached on your system. To clear the password and try this sample again, just click on this button:
You can of course customize this example:
instead of using a prompt dialog box, you could create a login system.
you can use anything else than a password to protect your files. Just modify the Boolean function.
Copyright G.D.G. Software 2019. All rights reserved