Support multiple objects selected in user manager > add object.

This commit is contained in:
Ansgar Becker
2016-08-29 18:37:00 +00:00
parent c20915c3c1
commit ade0bd3f44
2 changed files with 77 additions and 60 deletions

View File

@ -34,13 +34,13 @@ type
private private
{ Private declarations } { Private declarations }
FConnection: TDBConnection; FConnection: TDBConnection;
function GetSelectedObject: TDBObject; function GetSelectedObjects: TDBObjectList;
public public
{ Public declarations } { Public declarations }
property SelectedObject: TDBObject read GetSelectedObject; property SelectedObjects: TDBObjectList read GetSelectedObjects;
end; end;
function SelectDBO: TDBObject; function SelectDBObjects: TDBObjectList;
implementation implementation
@ -48,14 +48,14 @@ uses main, helpers;
{$R *.dfm} {$R *.dfm}
function SelectDBO: TDBObject; function SelectDBObjects: TDBObjectList;
var var
Dialog: TfrmSelectDBObject; Dialog: TfrmSelectDBObject;
begin begin
Dialog := TfrmSelectDBObject.Create(Mainform); Dialog := TfrmSelectDBObject.Create(Mainform);
Result := nil; Result := nil;
if Dialog.ShowModal = mrOK then if Dialog.ShowModal = mrOK then
Result := Dialog.SelectedObject; Result := Dialog.SelectedObjects;
Dialog.Free; Dialog.Free;
end; end;
@ -67,6 +67,7 @@ begin
Height := AppSettings.ReadInt(asSelectDBOWindowHeight); Height := AppSettings.ReadInt(asSelectDBOWindowHeight);
InheritFont(Font); InheritFont(Font);
TreeDBO.TreeOptions := MainForm.DBtree.TreeOptions; TreeDBO.TreeOptions := MainForm.DBtree.TreeOptions;
TreeDBO.TreeOptions.SelectionOptions := TreeDBO.TreeOptions.SelectionOptions + [toMultiSelect];
FixVT(TreeDBO); FixVT(TreeDBO);
FConnection := MainForm.ActiveConnection; FConnection := MainForm.ActiveConnection;
end; end;
@ -101,29 +102,37 @@ begin
end; end;
function TfrmSelectDBObject.GetSelectedObject: TDBObject; function TfrmSelectDBObject.GetSelectedObjects: TDBObjectList;
var var
Obj: TDBObject;
DBObj: PDBObject; DBObj: PDBObject;
Node: PVirtualNode;
begin begin
// Return currently selected object, either from tree node or from edit boxes // Return currently selected object, either from tree node or from edit boxes
Result := nil; Result := TDBObjectList.Create;
if editDb.Modified then begin if editDb.Modified then begin
Result := TDBObject.Create(FConnection); Obj := TDBObject.Create(FConnection);
Result.Database := editDb.Text; Obj.Database := editDb.Text;
Result.NodeType := lntDb; Obj.NodeType := lntDb;
end else if Assigned(TreeDBO.FocusedNode) then begin Result.Add(Obj);
DBObj := TreeDBO.GetNodeData(TreeDBO.FocusedNode); end else if TreeDBO.SelectedCount > 0 then begin
Result := TDBObject.Create(DBObj.Connection); Node := GetNextNode(TreeDBO, nil, True);
Result.Assign(DBObj^); while Assigned(Node) do begin
DBObj := TreeDBO.GetNodeData(Node);
Obj := TDBObject.Create(DBObj.Connection);
Obj.Assign(DBObj^);
// Database privileges can be wildcarded. Tables/columns not so. // Database privileges can be wildcarded. Tables/columns not so.
if Result.NodeType = lntDb then if Obj.NodeType = lntDb then
Result.Database := esc(Result.Database, True, False); Obj.Database := esc(Obj.Database, True, False);
if Result.NodeType = lntNone then begin if Obj.NodeType = lntNone then begin
Result.NodeType := lntDb; Obj.NodeType := lntDb;
Result.Database := '%'; Obj.Database := '%';
end;
Result.Add(Obj);
Node := GetNextNode(TreeDBO, Node, True);
end; end;
end; end;
// Let the result be nil to indicate we have no selected node // Let the result be empty to indicate we have no selected node
end; end;
procedure TfrmSelectDBObject.TreeDBOFocusChanged(Sender: TBaseVirtualTree; procedure TfrmSelectDBObject.TreeDBOFocusChanged(Sender: TBaseVirtualTree;

View File

@ -1073,29 +1073,36 @@ end;
procedure TUserManagerForm.btnAddObjectClick(Sender: TObject); procedure TUserManagerForm.btnAddObjectClick(Sender: TObject);
var var
DBObj: TDBObject; DBObjects: TDBObjectList;
DBObject: TDBObject;
Priv: TPrivObj; Priv: TPrivObj;
Node, Child: PVirtualNode; Node, Child: PVirtualNode;
ObjectExists: Boolean;
begin begin
// Add new privilege object // Add new privilege object(s)
DBObj := SelectDBO; DBObjects := SelectDBObjects;
if not Assigned(DBObj) then if (not Assigned(DBObjects)) or (DBObjects.Count = 0) then
Exit; Exit;
for DBObject in DBObjects do begin
// Check for unsupported object type, selectable in tree // Check for unsupported object type, selectable in tree
if not (DBObj.NodeType in [lntDb, lntTable, lntView, lntFunction, lntProcedure, lntColumn]) then begin if not (DBObject.NodeType in [lntDb, lntTable, lntView, lntFunction, lntProcedure, lntColumn]) then begin
ErrorDialog(f_('Objects of type %s cannot be part of privileges.', [_(DBObj.ObjType)])); ErrorDialog(f_('Objects of type %s cannot be part of privileges.', [_(DBObject.ObjType)]));
Exit; Continue;
end; end;
// Check if this would be a duplicate object // Check if this would be a duplicate object
ObjectExists := False;
for Priv in FPrivObjects do begin for Priv in FPrivObjects do begin
if Priv.DBObj.IsSameAs(DBObj) then begin if Priv.DBObj.IsSameAs(DBObject) then
ErrorDialog(_('Selected object is already accessible.')); ObjectExists := True;
Exit;
end; end;
if ObjectExists then begin
ErrorDialog(_('Selected object is already accessible.'));
Continue;
end; end;
Priv := TPrivObj.Create; Priv := TPrivObj.Create;
Priv.DBObj := DBObj; Priv.DBObj := DBObject;
case Priv.DBObj.NodeType of case Priv.DBObj.NodeType of
lntNone: Priv.AllPrivileges := PrivsGlobal; lntNone: Priv.AllPrivileges := PrivsGlobal;
lntDb: Priv.AllPrivileges := PrivsDb; lntDb: Priv.AllPrivileges := PrivsDb;
@ -1119,6 +1126,7 @@ begin
SelectNode(treePrivs, Node); SelectNode(treePrivs, Node);
Modified := True; Modified := True;
end; end;
end;
procedure TUserManagerForm.btnSaveClick(Sender: TObject); procedure TUserManagerForm.btnSaveClick(Sender: TObject);