Solved

IB7.1 Number of users logged into the DB, and who they are.

Posted on 2003-11-01
4
584 Views
Last Modified: 2013-12-09
Interbase 7.1
Delphi 7
Looking for a code sample on how to determine the number of users currently logged into a specific database (not just the server).  Any other supporting logon information would also be helpful.   Do not want to give the users SYSDBA access to get this information.   This information is to be used to control the number of valid users within an application.
0
Comment
Question by:chart11610
  • 2
4 Comments
 
LVL 6

Assisted Solution

by:BAlexandrov
BAlexandrov earned 100 total points
Comment Utility
I have done this one time  - it was for puprose to be able to view whish users are currently connected. The application itself had users - a table with information about each user but without the password.
When new user is created - it creates new user on the server also with the suffix - application name.
When user logon - it is authenticated to the server(user name that he have supplied + application name and the password) - with logging in to the database.
Then it is easy to get list of currently active users on the server, filter this list with users only from our application (comparing with the users table in our database) and list only them.

Drawbacks - you have to include in your app hidden administrative password - an account that will have rights to make new servers accounts.

Just as example..

Bojidar Alexandrov
0
 
LVL 19

Accepted Solution

by:
Gary Benade earned 150 total points
Comment Utility
below is a function to retrieve the usernames into a StringList using the Interbase API.
I am actually a c programmer so I may have made some syntax errors.

procedure TForm1.GetConnStats( FUserNames: TStringList)

var
Status_Vector: ISC_STATUS_VECTOR;
db_handle: isc_db_handle;
DPB_Buffer: array [0..255] of Char;
DPB_Index: Integer;
DBName_Cstr: array [0..255] of Char;
temp_buffer: array[0..255] of Char;

{Options buffer and index}

DBStatOpts: array [0..15] of Byte;
NumOpts: Integer;

{Result buffer array, index and pointer}

Result_Buffer: array [0..1023] of Byte;
RB_index: Integer;
RB_ptr: Pointer;

{Variables for handling each return item}
Item: Byte;
ItemLen: ISC_LONG;
Page_Size: Longint;
Num_Pages: Longint;
i, user_length: Integer;

begin

{Initialise the buffer index and set the first character value which is always the parameter buffer version number}
DPB_Index := 0;
DPB_Buffer[DPB_Index] := Char(isc_dpb_version1);
Inc(DPB_Index);

{Set the user name parameter identifier and copy the name from the user name edit field on the form}
DPB_Buffer[DPB_Index] := Char(isc_dpb_user_name);
Inc(DPB_Index);
DPB_Buffer[DPB_Index] := Char(Length(Edit_Username.Text));
Inc(DPB_Index);
StrPCopy(@DPB_Buffer[DPB_Index], Edit_Username.Text);
Inc(DPB_Index, Length(Edit_Username.Text));

{Set the password parameter identifier and copy the name from the password edit field on the form}
DPB_Buffer[DPB_Index] := Char(isc_dpb_password);
Inc(DPB_Index);
DPB_Buffer[DPB_Index] := Char(Length(Edit_Password.Text));
Inc(DPB_Index);
StrPCopy(@DPB_Buffer[DPB_Index], Edit_Password.Text);
Inc(DPB_Index, Length(Edit_Password.Text));

{Copy the database name from the edit field on the form to a C-string style character array}
StrPCopy(DBName_Cstr, Edit_DBName.Text);

{Initialise the database handle to nil - mandatory}
db_handle := nil;

{Attach to the database}
isc_attach_database(@Status_Vector, Length(Edit_DBName.Text), @DBName_Cstr, @db_handle, DPB_Index, DPB_Buffer);


{ Username extraction starts here }

{Exit if we’re not connected}
if db_handle = nil then
Exit;

{Set up the information options required}
NumOpts := 0;
DBStatOpts[NumOpts] := isc_info_page_size;
Inc(NumOpts);
DBStatOpts[NumOpts] := isc_info_allocation;
Inc(NumOpts);

{Issue the call to get the information required about usernames}
if isc_database_info (@Status_Vector, @db_handle, NumOpts, @DBStatOpts, SizeOf(Result_Buffer), @Result_Buffer) <> 0 then
isc_print_status(@Status_Vector);

RB_Index := 0;
FUserNames.Clear;

while Result_Buffer[RB_Index] = Char(isc_info_user_names)
do
begin
Inc(RB_Index, 3); { skip "isc_info_user_names byte" & two unknown bytes of structure}
user_length := Long(Result_Buffer[RB_Index]);
Inc(RB_Index,1);  
Move(Result_Buffer[RB_Index], temp_buffer[0], user_length);
Inc(RB_Index, user_length);  
temp_buffer[user_length] := #0;
FUserNames.Add(String(temp_buffer));
end; { while }

{ Username extraction section ends here }

{Detach to the database}
if db_handle <> nil then isc_detach_database( @Status_Vector, @db_handle);

end;
0
 
LVL 6

Expert Comment

by:BAlexandrov
Comment Utility
Great job, hobbit72!
But this can be done via ibx components (or other)...
The main problem is that this is list of users connected to the _server_. To _any_ database on this server.
The problem is how to get users connected only to your application's own database.

Bojidar Alexandrov
0
 
LVL 10

Expert Comment

by:kacor
Comment Utility
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
      to split points between BAlexandrov (100) and hobbit72 (150)
Please leave any comments here within the next four (4) days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

If you will get a useful answer to your newer question next time, please accept it after evaluating. By this way the experts will get sooner his expert points for supporting you. If you have question about, please look at  http://www.experts-exchange.com/help.jsp.

kacor
EE Cleanup Volunteer
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction: I have seen many questions on EE and elsewhere, asking about how to find either gaps in lists of numbers (id field, usually) ranges of values or dates overlapping date ranges combined date ranges I thought it would be a good …
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now