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;