root / Client / UfrmAccountControl.pas @ 13:c78b5eafa10e
History | View | Annotate | Download (11.2 kB)
| 1 | (*
|
|---|---|
| 2 | * CDDL HEADER START |
| 3 | * |
| 4 | * The contents of this file are subject to the terms of the |
| 5 | * Common Development and Distribution License, Version 1.0 only |
| 6 | * (the "License"). You may not use this file except in compliance |
| 7 | * with the License. |
| 8 | * |
| 9 | * You can obtain a copy of the license at |
| 10 | * http://www.opensource.org/licenses/cddl1.php. |
| 11 | * See the License for the specific language governing permissions |
| 12 | * and limitations under the License. |
| 13 | * |
| 14 | * When distributing Covered Code, include this CDDL HEADER in each |
| 15 | * file and include the License file at |
| 16 | * http://www.opensource.org/licenses/cddl1.php. If applicable, |
| 17 | * add the following below this CDDL HEADER, with the fields enclosed |
| 18 | * by brackets "[]" replaced with your own identifying * information: |
| 19 | * Portions Copyright [yyyy] [name of copyright owner] |
| 20 | * |
| 21 | * CDDL HEADER END |
| 22 | * |
| 23 | * |
| 24 | * Portions Copyright 2008 Andreas Schneider |
| 25 | *) |
| 26 | unit UfrmAccountControl;
|
| 27 | |
| 28 | {$mode objfpc}{$H+} |
| 29 | |
| 30 | interface
|
| 31 | |
| 32 | uses
|
| 33 | Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ComCtrls, |
| 34 | VirtualTrees, VTHeaderPopup, UEnhancedMemoryStream, UEnums; |
| 35 | |
| 36 | type
|
| 37 | |
| 38 | { TfrmAccountControl }
|
| 39 | |
| 40 | TfrmAccountControl = class(TForm)
|
| 41 | ilToolbar: TImageList; |
| 42 | ilAccesslevel: TImageList; |
| 43 | tbMain: TToolBar; |
| 44 | tbRefresh: TToolButton; |
| 45 | tbAddUser: TToolButton; |
| 46 | tbEditUser: TToolButton; |
| 47 | tbDeleteUser: TToolButton; |
| 48 | tbSeparator1: TToolButton; |
| 49 | vstAccounts: TVirtualStringTree; |
| 50 | procedure tbEditUserClick(Sender: TObject);
|
| 51 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); |
| 52 | procedure FormCreate(Sender: TObject);
|
| 53 | procedure FormDestroy(Sender: TObject);
|
| 54 | procedure FormShow(Sender: TObject);
|
| 55 | procedure tbAddUserClick(Sender: TObject);
|
| 56 | procedure tbDeleteUserClick(Sender: TObject);
|
| 57 | procedure tbRefreshClick(Sender: TObject);
|
| 58 | procedure vstAccountsDblClick(Sender: TObject);
|
| 59 | procedure vstAccountsFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
|
| 60 | procedure vstAccountsGetImageIndex(Sender: TBaseVirtualTree;
|
| 61 | Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex; |
| 62 | var Ghosted: Boolean; var ImageIndex: Integer); |
| 63 | procedure vstAccountsGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
|
| 64 | Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
|
| 65 | protected
|
| 66 | procedure OnModifyUserResponse(ABuffer: TEnhancedMemoryStream);
|
| 67 | procedure OnDeleteUserResponse(ABuffer: TEnhancedMemoryStream);
|
| 68 | procedure OnListUsersPacket(ABuffer: TEnhancedMemoryStream);
|
| 69 | function FindNode(AUsername: string): PVirtualNode; |
| 70 | public
|
| 71 | { public declarations }
|
| 72 | end;
|
| 73 | |
| 74 | var
|
| 75 | frmAccountControl: TfrmAccountControl; |
| 76 | |
| 77 | implementation
|
| 78 | |
| 79 | uses
|
| 80 | UdmNetwork, UPacket, UPacketHandlers, UAdminHandling, UfrmEditAccount; |
| 81 | |
| 82 | type
|
| 83 | PAccountInfo = ^TAccountInfo; |
| 84 | TAccountInfo = record
|
| 85 | Username: string;
|
| 86 | AccessLevel: TAccessLevel; |
| 87 | Regions: TStringList; |
| 88 | end;
|
| 89 | |
| 90 | { TModifyUserPacket }
|
| 91 | |
| 92 | TModifyUserPacket = class(TPacket)
|
| 93 | constructor Create(AUsername, APassword: string; AAccessLevel: TAccessLevel); |
| 94 | end;
|
| 95 | |
| 96 | { TDeleteUserPacket }
|
| 97 | |
| 98 | TDeleteUserPacket = class(TPacket)
|
| 99 | constructor Create(AUsername: string); |
| 100 | end;
|
| 101 | |
| 102 | { TRequestUserListPacket }
|
| 103 | |
| 104 | TRequestUserListPacket = class(TPacket)
|
| 105 | constructor Create;
|
| 106 | end;
|
| 107 | |
| 108 | { TModifyUserPacket }
|
| 109 | |
| 110 | constructor TModifyUserPacket.Create(AUsername, APassword: string; |
| 111 | AAccessLevel: TAccessLevel); |
| 112 | begin
|
| 113 | inherited Create($03, 0); |
| 114 | FStream.WriteByte($05);
|
| 115 | FStream.WriteStringNull(AUsername); |
| 116 | FStream.WriteStringNull(APassword); |
| 117 | FStream.WriteByte(Byte(AAccessLevel)); |
| 118 | end;
|
| 119 | |
| 120 | { TDeleteUserPacket }
|
| 121 | |
| 122 | constructor TDeleteUserPacket.Create(AUsername: string); |
| 123 | begin
|
| 124 | inherited Create($03, 0); |
| 125 | FStream.WriteByte($06);
|
| 126 | FStream.WriteStringNull(AUsername); |
| 127 | end;
|
| 128 | |
| 129 | { TRequestUserListPacket }
|
| 130 | |
| 131 | constructor TRequestUserListPacket.Create;
|
| 132 | begin
|
| 133 | inherited Create($03, 0); |
| 134 | FStream.WriteByte($07);
|
| 135 | end;
|
| 136 | |
| 137 | { TfrmAccountControl }
|
| 138 | |
| 139 | procedure TfrmAccountControl.FormCreate(Sender: TObject);
|
| 140 | begin
|
| 141 | vstAccounts.NodeDataSize := SizeOf(TAccountInfo); |
| 142 | |
| 143 | AssignAdminPacketHandler($05, TPacketHandler.Create(0, @OnModifyUserResponse)); |
| 144 | AssignAdminPacketHandler($06, TPacketHandler.Create(0, @OnDeleteUserResponse)); |
| 145 | AssignAdminPacketHandler($07, TPacketHandler.Create(0, @OnListUsersPacket)); |
| 146 | end;
|
| 147 | |
| 148 | procedure TfrmAccountControl.FormClose(Sender: TObject;
|
| 149 | var CloseAction: TCloseAction);
|
| 150 | begin
|
| 151 | CloseAction := caHide; |
| 152 | end;
|
| 153 | |
| 154 | procedure TfrmAccountControl.tbEditUserClick(Sender: TObject);
|
| 155 | var
|
| 156 | selected: PVirtualNode; |
| 157 | accountInfo: PAccountInfo; |
| 158 | begin
|
| 159 | selected := vstAccounts.GetFirstSelected; |
| 160 | if selected <> nil then |
| 161 | begin
|
| 162 | accountInfo := vstAccounts.GetNodeData(selected); |
| 163 | with frmEditAccount do |
| 164 | begin
|
| 165 | edUsername.Text := accountInfo^.Username; |
| 166 | edUsername.Color := clBtnFace; |
| 167 | edUsername.ReadOnly := True; |
| 168 | edPassword.Text := '';
|
| 169 | lblPasswordHint.Visible := True; |
| 170 | SetAccessLevel(accountInfo^.AccessLevel); |
| 171 | if ShowModal = mrOK then |
| 172 | dmNetwork.Send(TModifyUserPacket.Create(edUsername.Text, |
| 173 | edPassword.Text, GetAccessLevel)); |
| 174 | end;
|
| 175 | end;
|
| 176 | end;
|
| 177 | |
| 178 | procedure TfrmAccountControl.FormDestroy(Sender: TObject);
|
| 179 | begin
|
| 180 | if AdminPacketHandlers[$05] <> nil then FreeAndNil(AdminPacketHandlers[$05]); |
| 181 | if AdminPacketHandlers[$06] <> nil then FreeAndNil(AdminPacketHandlers[$06]); |
| 182 | if AdminPacketHandlers[$07] <> nil then FreeAndNil(AdminPacketHandlers[$07]); |
| 183 | end;
|
| 184 | |
| 185 | procedure TfrmAccountControl.FormShow(Sender: TObject);
|
| 186 | begin
|
| 187 | tbRefreshClick(Sender); |
| 188 | end;
|
| 189 | |
| 190 | procedure TfrmAccountControl.tbAddUserClick(Sender: TObject);
|
| 191 | begin
|
| 192 | with frmEditAccount do |
| 193 | begin
|
| 194 | edUsername.Text := '';
|
| 195 | edUsername.Color := clWindow; |
| 196 | edUsername.ReadOnly := False; |
| 197 | edPassword.Text := '';
|
| 198 | lblPasswordHint.Visible := False; |
| 199 | cbAccessLevel.ItemIndex := 2;
|
| 200 | if ShowModal = mrOK then |
| 201 | dmNetwork.Send(TModifyUserPacket.Create(edUsername.Text, edPassword.Text, |
| 202 | GetAccessLevel)); |
| 203 | end;
|
| 204 | end;
|
| 205 | |
| 206 | procedure TfrmAccountControl.tbDeleteUserClick(Sender: TObject);
|
| 207 | var
|
| 208 | selected: PVirtualNode; |
| 209 | accountInfo: PAccountInfo; |
| 210 | begin
|
| 211 | selected := vstAccounts.GetFirstSelected; |
| 212 | if selected <> nil then |
| 213 | begin
|
| 214 | accountInfo := vstAccounts.GetNodeData(selected); |
| 215 | if MessageDlg('Confirmation', Format('Do you really want to delete "%s"?', |
| 216 | [accountInfo^.Username]), mtConfirmation, [mbYes, mbNo], 0) = mrYes then |
| 217 | dmNetwork.Send(TDeleteUserPacket.Create(accountInfo^.Username)); |
| 218 | end;
|
| 219 | end;
|
| 220 | |
| 221 | procedure TfrmAccountControl.tbRefreshClick(Sender: TObject);
|
| 222 | begin
|
| 223 | dmNetwork.Send(TRequestUserListPacket.Create); |
| 224 | end;
|
| 225 | |
| 226 | procedure TfrmAccountControl.vstAccountsDblClick(Sender: TObject);
|
| 227 | begin
|
| 228 | tbEditUserClick(Sender); |
| 229 | end;
|
| 230 | |
| 231 | procedure TfrmAccountControl.vstAccountsFreeNode(Sender: TBaseVirtualTree;
|
| 232 | Node: PVirtualNode); |
| 233 | var
|
| 234 | accountInfo: PAccountInfo; |
| 235 | begin
|
| 236 | accountInfo := vstAccounts.GetNodeData(Node); |
| 237 | accountInfo^.Username := '';
|
| 238 | if accountInfo^.Regions <> nil then FreeAndNil(accountInfo^.Regions); |
| 239 | end;
|
| 240 | |
| 241 | procedure TfrmAccountControl.vstAccountsGetImageIndex(Sender: TBaseVirtualTree;
|
| 242 | Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex; |
| 243 | var Ghosted: Boolean; var ImageIndex: Integer); |
| 244 | var
|
| 245 | accountInfo: PAccountInfo; |
| 246 | begin
|
| 247 | if Column = 0 then |
| 248 | begin
|
| 249 | accountInfo := Sender.GetNodeData(Node); |
| 250 | case accountInfo^.AccessLevel of |
| 251 | alNone: ImageIndex := 0;
|
| 252 | alView: ImageIndex := 1;
|
| 253 | alNormal: ImageIndex := 2;
|
| 254 | alAdministrator: ImageIndex := 3;
|
| 255 | end;
|
| 256 | end;
|
| 257 | end;
|
| 258 | |
| 259 | procedure TfrmAccountControl.vstAccountsGetText(Sender: TBaseVirtualTree;
|
| 260 | Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType; |
| 261 | var CellText: WideString);
|
| 262 | var
|
| 263 | accountInfo: PAccountInfo; |
| 264 | begin
|
| 265 | accountInfo := Sender.GetNodeData(Node); |
| 266 | case Column of |
| 267 | 1: CellText := accountInfo^.Username;
|
| 268 | 2: CellText := GetAccessLevelString(accountInfo^.AccessLevel);
|
| 269 | else
|
| 270 | CellText := '';
|
| 271 | end;
|
| 272 | end;
|
| 273 | |
| 274 | procedure TfrmAccountControl.OnModifyUserResponse(ABuffer: TEnhancedMemoryStream);
|
| 275 | var
|
| 276 | node: PVirtualNode; |
| 277 | modifyStatus: TModifyUserStatus; |
| 278 | username: string;
|
| 279 | accountInfo: PAccountInfo; |
| 280 | i, regions: Integer; |
| 281 | begin
|
| 282 | modifyStatus := TModifyUserStatus(ABuffer.ReadByte); |
| 283 | username := ABuffer.ReadStringNull; |
| 284 | case modifyStatus of |
| 285 | muAdded: |
| 286 | begin
|
| 287 | node := vstAccounts.AddChild(nil);
|
| 288 | accountInfo := vstAccounts.GetNodeData(node); |
| 289 | accountInfo^.Username := username; |
| 290 | accountInfo^.AccessLevel := TAccessLevel(ABuffer.ReadByte); |
| 291 | accountInfo^.Regions := TStringList.Create; |
| 292 | regions := ABuffer.ReadByte; |
| 293 | for i := 0 to regions - 1 do |
| 294 | accountInfo^.Regions.Add(ABuffer.ReadStringNull); |
| 295 | |
| 296 | Messagedlg('Success', Format('The user "%s" has been added.', [username]), |
| 297 | mtInformation, [mbOK], 0);
|
| 298 | end;
|
| 299 | muModified: |
| 300 | begin
|
| 301 | node := FindNode(username); |
| 302 | if node <> nil then |
| 303 | begin
|
| 304 | accountInfo := vstAccounts.GetNodeData(node); |
| 305 | accountInfo^.AccessLevel := TAccessLevel(ABuffer.ReadByte); |
| 306 | accountInfo^.Regions.Clear; |
| 307 | regions := ABuffer.ReadByte; |
| 308 | for i := 0 to regions - 1 do |
| 309 | accountInfo^.Regions.Add(ABuffer.ReadStringNull); |
| 310 | |
| 311 | Messagedlg('Success', Format('The user "%s" has been modified.', [username]), |
| 312 | mtInformation, [mbOK], 0);
|
| 313 | end;
|
| 314 | end;
|
| 315 | muInvalidUsername: |
| 316 | MessageDlg('Error', Format('The username "%s" is not valid.', [username]), |
| 317 | mtError, [mbOK], 0);
|
| 318 | end;
|
| 319 | end;
|
| 320 | |
| 321 | procedure TfrmAccountControl.OnDeleteUserResponse(ABuffer: TEnhancedMemoryStream);
|
| 322 | var
|
| 323 | node: PVirtualNode; |
| 324 | deleteStatus: TDeleteUserStatus; |
| 325 | username: string;
|
| 326 | begin
|
| 327 | deleteStatus := TDeleteUserStatus(ABuffer.ReadByte); |
| 328 | username := ABuffer.ReadStringNull; |
| 329 | case deleteStatus of |
| 330 | duDeleted: |
| 331 | begin
|
| 332 | node := FindNode(username); |
| 333 | if node <> nil then |
| 334 | begin
|
| 335 | vstAccounts.DeleteNode(node); |
| 336 | Messagedlg('Success', Format('The user "%s" has been deleted.', [username]), |
| 337 | mtInformation, [mbOK], 0);
|
| 338 | end;
|
| 339 | end;
|
| 340 | duNotFound: |
| 341 | MessageDlg('Error', Format('The user "%s" could not be deleted. Maybe ' + |
| 342 | 'your list is out of date or you tried to delete yourself.', [username]),
|
| 343 | mtError, [mbOK], 0);
|
| 344 | end;
|
| 345 | end;
|
| 346 | |
| 347 | procedure TfrmAccountControl.OnListUsersPacket(ABuffer: TEnhancedMemoryStream);
|
| 348 | var
|
| 349 | node: PVirtualNode; |
| 350 | accountInfo: PAccountInfo; |
| 351 | i, j, count, regions: Word; |
| 352 | begin
|
| 353 | vstAccounts.BeginUpdate; |
| 354 | vstAccounts.Clear; |
| 355 | count := ABuffer.ReadWord; |
| 356 | for i := 1 to count do |
| 357 | begin
|
| 358 | node := vstAccounts.AddChild(nil);
|
| 359 | accountInfo := vstAccounts.GetNodeData(node); |
| 360 | accountInfo^.Username := ABuffer.ReadStringNull; |
| 361 | accountInfo^.AccessLevel := TAccessLevel(ABuffer.ReadByte); |
| 362 | accountInfo^.Regions := TStringList.Create; |
| 363 | regions := ABuffer.ReadByte; |
| 364 | for j := 0 to regions - 1 do |
| 365 | accountInfo^.Regions.Add(ABuffer.ReadStringNull); |
| 366 | end;
|
| 367 | vstAccounts.EndUpdate; |
| 368 | end;
|
| 369 | |
| 370 | function TfrmAccountControl.FindNode(AUsername: string): PVirtualNode; |
| 371 | var
|
| 372 | node: PVirtualNode; |
| 373 | accountInfo: PAccountInfo; |
| 374 | begin
|
| 375 | Result := nil;
|
| 376 | node := vstAccounts.GetFirst; |
| 377 | while (node <> nil) and (Result = nil) do |
| 378 | begin
|
| 379 | accountInfo := vstAccounts.GetNodeData(node); |
| 380 | if accountInfo^.Username = AUsername then |
| 381 | Result := node; |
| 382 | node := vstAccounts.GetNext(node); |
| 383 | end;
|
| 384 | end;
|
| 385 | |
| 386 | initialization
|
| 387 | {$I UfrmAccountControl.lrs}
|
| 388 | |
| 389 | end.
|
| 390 |