Topic: Create logout button

Hello everyone,

I'm trying to create a simple logout button but I cannot find the actual login form.

Any help?

Thank you.

Re: Create logout button

Hi,
I'm assuming that you're using the role-based access control functionality.
The form where you enter your user-id and password (frmdbcorelogin) is a 'system form' and not accessible graphically like normal user-created forms.
It can however be accessed and edited using a script;  you would need to create a new button, place it on the form and associate the appropriate action with it.
My question would be why you need it when you have the standard Windows 'close form' button (X) already on the form?
Derek.

Re: Create logout button

Hello derek and thanks for your response,

I found this application very simple and easy to use and I wanted to keep tracking of our company's repair center. And as you mentioned correctly, we are using the user-role control system so not everyone can have administrator's rights.
Although I make a backup almost every day but I just wanted to have an alternative login system instead of closing the whole application to just switch users (we have viewer to see only the records, moderator to insert data and admin to create and design)
But if this is pain to make it happen I'll find another alternative.

Thank you once again.

4 (edited by k245 2022-07-30 08:20:49)

Re: Create logout button

frmDbCoreLogin.ShowModal

You can change the user and his role without displaying the authorization form.

procedure Form1_Button4_OnClick (Sender: TObject; var Cancel: boolean);
begin
  Application.User.id :=3;
  Application.User.RoleId  := 2;
end;

procedure Form1_Button2_OnClick (Sender: TObject; var Cancel: boolean);
begin
  Application.User.id :=2;
  Application.User.RoleId  := 1;
end;
Визуальное программирование: блог и телеграм-канал.

5 (edited by ktommy 2022-07-30 18:57:38)

Re: Create logout button

Thank you for your replies.

That's what I was thinking to make changes to the user authorities without the need of closing the application, but I found something else which also is useful at least in my situation.
I've disabled the main menu options that are shown in the main form (form1) by default using the code I've found posted in another article:

Form1.mniFile.Visible := False;
Form1.mniOptions.Visible := False;
Form1.mniAbout.Visible := False;
Form1.mniReport.Visible := False;

and create a checkbox item that is visible only to the administrator and whenever it is checked it changes the menu visibility to true and show all the options again and modify whatever I want to and by unchecking it, it returns to the custom menu that I created. This is the code that I used maybe someone else will need it:

procedure Form1_CheckBox1_OnClick (Sender: TObject);

begin

If Form1.CheckBox1.Checked Then
begin
   Form1.mniFile.Visible := True;
   Form1.mniOptions.Visible := True;
   Form1.mniAbout.Visible := True;
   Form1.mniReport.Visible := True;
end
Else
  begin
   Form1.mniFile.Visible := False;
   Form1.mniOptions.Visible := False;
   Form1.mniAbout.Visible := False;
   Form1.mniReport.Visible := False;
   end
end;

I was wondering if you can create dynamic submenu items. I found some codes from another sites but they don't work for some reason. Anyway I attach photo of how do I imagine the menu.

Thank all of you for your help once again.

Post's attachments

Attachment icon hJH7H8Y.png 8.44 kb, 70 downloads since 2022-07-30 

Re: Create logout button

Hi KTommy,
I'm slightly confused (happens frequently - LOL!).
Do you need the ability to switch users without logging out of the system or do you really just want different users to see different options when they log in depending on their assigned user role?
Whenever I hear that people are switching user-ids so that they can access different functions, it sets alarm bells ringing and makes me wonder if their user role has not been correctly defined.
But you're closer to your project so there may well be other issues at play.
Anyway, for what it's worth, please see the attached as 2 options (one which switches users (if you really need it) and one which just turns off the standard menu options depending on user roles). 
The passwords are the same as the userids.
In answer to your dynamic sub-menus question, yes it can be done but I think most people end up just putting different buttons on discrete 'menu panels' and, again, display different 'menu panels' depending on user role.
Hope this helps rather than hinder - LOL!
Derek.

Post's attachments

Attachment icon ktommy.zip 672.54 kb, 228 downloads since 2022-07-30 

Re: Create logout button

Good evening derek,

Ha ha ha ha. Not to worry, I'm confused with you as well many times (customers-clients, our company stuff,etc.).
Well to be honest, the ability to switch users is more, how can I say, efficient and time saver because every user sees a different menu (buttons, forms, etc.) and instead of closing the application and reopen it to switch the user it would be easier just to logout. This will make the login form open. I'll try to write script that will automatically log out user after few minutes of inactivity (yes maybe it sounds crazy but I want to have an extra caution). Another fact is that our engineers mostly shares pcs and every user has its own desktop and sees different shared folders in network so I wanted to create something similar.

Thank you for your attached sample. It's a life saver to me. Thanks a million.

Re: Create logout button

I have an auto/manual log out function which I've attached below.  I came up with this because the SQLite DB is on a mapped drive and the IT department have a periodic cache scraper programme running. This plays havoc with SQLite cuasing users to either be logged out or never logged out even after they have closed the program. Obviously SQLite is not designed to run on mapped/shared drives.


I have an extra table called user_log  which contains logging on time and last logged in time.

//Forced logout by user.


procedure frmMain_btnLogOut_OnClick (Sender: TObject; var Cancel: boolean);

begin

   ClearLogs;

   MessageDlgTimeOut('You have logged out'+#13#13+' Program will CLOSE in 4 seconds','LOG OUT INFORMATION',0,4000);
  
   frmMain.Close;
end;

//Clear Logs


//user_log
var
  logged_in : boolean = False;

procedure ClearLogs;
begin
    SqlExecute('DELETE FROM user_log WHERE username = "' + Application.User.Username + '"');
end;

Combined with this I also have an auto program close for each user (they can set their own time span), as people were going home and not closing the program down causing major database corruption.

On a clear disk you can seek forever