• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 556
  • Last Modified:

Change user on Interbase database during runtime?

Im using a TSQLConnection to access my database (interbase), and when my application
starts up, Im logging in as SYSDBA. But what I would like to do right after
that is:
1. Create a user
2. Give that user full access to all tables
3. Change user from SYSDBA to my created user

Is this possible to do with only a TSQLConnection?


I have managed to create a user by the following code:
with IBSecurityService1 do
  begin
  ServerName := DataBaseAliasString;
  LoginPrompt := False;
  Params.Add('user_name=sysdba');
  Params.Add('password=masterkey');
  Active := True;
    try
      UserName := edtUser.Text;
//      FirstName := Edit2.Text;
//      MiddleName := Edit3.Text;
//      LastName := Edit4.Text;
//      UserID := StrToInt(Edit5.Text);
//      GroupID := StrToInt(Edit6.Text);
      Password := edtPassword.Text;
      AddUser;
    finally
    Active := False;
    end;
  end;  

And after creating the user as shown above I have tried to close the
TSQLConnection and setting the params acordingly to my new user, and then
enabled the connected property again. But this does not work, Im still
logged in as SYSDBA.

Does anyone have any idea on how to do this? Any help or suggestion is much
appreciated.

In Short How do I change user during runtime?
0
jonas78
Asked:
jonas78
  • 2
  • 2
2 Solutions
 
kretzschmarCommented:
well, no delphi on hand yet,
but there should b e a property KeepConnection,
which is defaulted to true -> set it to false

meikl ;-)
0
 
esoftbgCommented:
I did a test with your example code and it really adds a user, but it may be has a problem with the rights. This evening I will test anymore ....
0
 
jonas78Author Commented:
Regarding access rights, thats already taken care of... here is an exampel of the current code:

procedure TMainForm.SpeedButton1Click(Sender: TObject);
var
  tempString: string;
begin
  with IBSecurityService1 do
  begin
    ServerName := 'C:\Database\DB.IB';
    LoginPrompt := False;
    Params.Add('user_name=sysdba');
    Params.Add('password=masterkey');
    Active := True;
    try
      UserName := 'jonas';
      Password := 'asdf';
      AddUser;

      with MainDM.SQLQueryGlobal do
      begin
        Close;
        SQL.Clear;
        SQL.Add('SELECT rdb$relation_name FROM Rdb$Relations');
        SQL.Add('WHERE rdb$system_flag = 0');
        Open;

        while not EOF do
        begin

          with IBSQL1 do
          begin
            SQL.Clear;
            tempString := Trim(MainDM.SQLQueryGlobal.Fields[0].AsString);
            SQL.Add('GRANT ALL ON ' + tempString + ' TO JONAS');
            IBSQL1.ExecQuery;
          end;

          Next;
        end;

        Close;
      end;

      with MAINDM.SQLConnection1 do
      begin
        if LoginPrompt = False then
        begin
          Connected := False;
          Close;

          Params.Values['User_Name'] := 'jonas';
          Params.Values['Password'] := 'asdf';

          Open;
          Connected := True;
        end;
      end;

    finally
      Active := False;
    end;
  end;

end;
0
 
esoftbgCommented:
Jonas, I tested your last code with my DataBase and IBComponents (not dbExpress) - it works fine.
Emil
0
 
jonas78Author Commented:
I screwed up!
The code works as esoftbg says...

So I splitted the points between you guys...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now