Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus - MacOS X - How to Verify Admin password

2 Posts
1 Users
0 Reactions
1,573 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

Sometimes, you'd like to verify if a user is actually an Admin, for example to prevent passwords to be modified or seen, or to modify certain settings in your app.

I have not found an easy way to do this straight in Lazarus Pascal (I'm open to suggestions!), so I wrote a little function that does this for me.

It will have your Mac ask you for the Admin password, if entered correctly (your app will not be able to read the password!), the function will return TRUE.
If it fails or if "Cancel" was pressed, then the function will return a FALSE.

For this function your work, you will need to include the "process" unit in your "Uses" clause!

function CheckIfAdmin:boolean;
var s: string;
begin
  RunCommand('/usr/bin/osascript',['-e','do shell script "/bin/echo ''OK''" with administrator privileges'],s);
  if s='OK'#10 then
    result := true
  else
    result := false;
end;

It simply calls OsaScript to run "Echo 'OK'" with administrator privileges. 
If the password was entered correctly, the "RunCommand" will return "OK" in the string "s". Well, it's an "OK" followed by a line feed. So 'OK'#10.

A simple example on how to use this:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if CheckIfAdmin then
    ShowMessage('Admin!')
  else
    ShowMessage('Not Admin!!!');
end;

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

Voor wie een screenshot wil zien ... zie aanhangsel! 


   
ReplyQuote
Share: