Главная страница | назад





Article #25831: How do I use the InterBase API security functions to add, modify and delete users

Problem:
Since there are no VCL controls to add, modify, or delete users for an InterBase server I am not sure how to do this task. The BDE also does not allow me to add, modify or delete users. I need to be able to do this task within a program? How do I do it?
Solution:
InterBase has surfaced the following functions to add, modify and delete users:
ISC_STATUS isc_add_user(ISC_STATUS *status, USER_SEC_DATA * user_sec_data)
ISC_STATUS isc_modify_user( ISC_STATUS *status, USER_SEC_DATA *user_sec_data)
ISC_STATUS isc_delete_user( ISC_STATUS *status, USER_SEC_DATA *user_sec_data)
The following program below shows how to define these functions in a Delphi program and how to call them in a Delphi application:
unit add_user1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
const
sec_uid_spec = $01;
sec_gid_spec = $02;
sec_server_spec = $04;
sec_password_spec = $08;
sec_group_name_spec = $10;
sec_first_name_spec = $20;
sec_middle_name_spec = $40;
sec_last_name_spec = $80;
sec_dba_user_name_spec = $100;
sec_dba_password_spec = $200;
sec_protocol_tcpip = 1;
sec_protocol_netbeui = 2;
sec_protocol_spx = 3;
sec_protocol_local = 4;
type
ISC_STATUS = Integer;
P_ISC_STATUS = ^ISC_STATUS;
USER_SEC_DATA = record
sec_flags: Smallint;
uid: Integer;
gid: Integer;
protocol: Integer;
server: PChar;
user_name: PChar;
password: PChar;
group_name: PChar;
first_name: PChar;
middle_name: PChar;
last_name: PChar;
dba_user_name: PChar;
dba_password: PChar;
end;
P_USER_SEC_DATA = ^USER_SEC_DATA;
TForm1 = class(TForm)
add_user_name: TEdit;
add_password: TEdit;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Bevel1: TBevel;
Bevel2: TBevel;
Bevel3: TBevel;
mod_user_name: TEdit;
mod_password: TEdit;
Button2: TButton;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
del_user_name: TEdit;
del_password: TEdit;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
function add_user_by_name_password(user_name: PChar; password: PChar): ISC_STATUS;
function mod_user_by_name_password(user_name: PChar; password: PChar): ISC_STATUS;
function del_user_by_name_password(user_name: PChar; password: PChar): ISC_STATUS;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function isc_add_user(status: P_ISC_STATUS; user_sec_data: P_USER_SEC_DATA):
ISC_STATUS; stdcall; external 'gds32.dll';
function isc_modify_user(status: P_ISC_STATUS; user_sec_data: P_USER_SEC_DATA):
ISC_STATUS; stdcall; external 'gds32.dll';
function isc_delete_user(status: P_ISC_STATUS; user_sec_data: P_USER_SEC_DATA):
ISC_STATUS; stdcall; external 'gds32.dll';
function TForm1.add_user_by_name_password(user_name, password: PChar): ISC_STATUS;
var
p_status: P_ISC_STATUS;
status: array[0..19] of ISC_STATUS;
user_sec_data1: USER_SEC_DATA;
begin
// have p_status point at the ISC_STATUS array
p_status := @status[0];
// set up the USER_SEC_DATA elements
user_sec_data1.protocol := sec_protocol_tcpip;
user_sec_data1.server := 'aquaman';
user_sec_data1.user_name := user_name;
user_sec_data1.password := password;
user_sec_data1.dba_user_name := 'sysdba';
user_sec_data1.dba_password := 'masterkey';
user_sec_data1.sec_flags := sec_password_spec or sec_server_spec or
sec_dba_user_name_spec or sec_dba_password_spec;
result := isc_add_user(p_status, @user_sec_data1);
end;
function TForm1.mod_user_by_name_password(user_name: PChar; password: PChar): ISC_STATUS;
var
p_status: P_ISC_STATUS;
status: array[0..19] of ISC_STATUS;
user_sec_data1: USER_SEC_DATA;
begin
// have p_status point at the ISC_STATUS array
p_status := @status[0];
// set up the USER_SEC_DATA elements
user_sec_data1.protocol := sec_protocol_tcpip;
user_sec_data1.server := 'aquaman';
user_sec_data1.user_name := user_name;
user_sec_data1.password := password;
user_sec_data1.dba_user_name := 'sysdba';
user_sec_data1.dba_password := 'masterkey';
user_sec_data1.sec_flags := sec_password_spec or sec_server_spec or
sec_dba_user_name_spec or sec_dba_password_spec;
result := isc_modify_user(p_status, @user_sec_data1);
end;
function TForm1.del_user_by_name_password(user_name: PChar; password: PChar): ISC_STATUS;
var
p_status: P_ISC_STATUS;
status: array[0..19] of ISC_STATUS;
user_sec_data1: USER_SEC_DATA;
begin
// have p_status point at the ISC_STATUS array
p_status := @status[0];
// set up the USER_SEC_DATA elements
user_sec_data1.protocol := sec_protocol_tcpip;
user_sec_data1.server := 'aquaman';
user_sec_data1.user_name := user_name;
user_sec_data1.password := password;
user_sec_data1.dba_user_name := 'sysdba';
user_sec_data1.dba_password := 'masterkey';
user_sec_data1.sec_flags := sec_password_spec or sec_server_spec or
sec_dba_user_name_spec or sec_dba_password_spec;
result := isc_delete_user(p_status, @user_sec_data1);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
add_user_by_name_password(@add_user_name.Text[1], @add_password.Text[1]);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
mod_user_by_name_password(@mod_user_name.Text[1], @mod_password.Text[1]);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
del_user_by_name_password(@del_user_name.Text[1], @del_password.Text[1]);
end;
end.

Last Modified: 12-OCT-00