www.pudn.com > EmailServer.zip > AddEditUser.pas


unit AddEditUser; 
 
(******************************************************************************) 
(*                                                                            *) 
(* Hermes Add / Edit User Dialog Box                                          *) 
(* Part of Hermes SMTP/POP3 Server.                                           *) 
(* Copyright(C) 2000 by Alexander J. Fanti, All Rights Reserver Worldwide.    *) 
(*                                                                            *) 
(* Created January 11, 2000 by Alexander J. Fanti.  See License.txt           *) 
(*                                                                            *) 
(* Used by: ManageUsers                                                       *) 
(* Uses: DataU1                                                               *) 
(*                                                                            *) 
(* Description: This Modal dialog window pulls double duty, allowing both     *) 
(*              adding and editing of users       .                           *) 
(*              Adding: Let user specify User Name and all other user data    *) 
(*              Editing: Let user re-specify user data, but not change Name   *) 
(*                                                                            *) 
(* Revisions: 1/21/2000  AJF  Commented and re-wrote OK button procedure to   *) 
(*                       clarify behavior                                     *) 
(*                                                                            *) 
(******************************************************************************) 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
  StdCtrls, Buttons, FileCtrl, 
  DataU1; 
 
type 
  TfrmAddEditUser = class(TForm) 
    Label1: TLabel; 
    txtUserID: TEdit; 
    Label2: TLabel; 
    txtUserPassword: TEdit; 
    btnOK: TBitBtn; 
    btnCancel: TBitBtn; 
    GroupBox1: TGroupBox; 
    Label3: TLabel; 
    txtRealName: TEdit; 
    cbxUB_DoNotReportUserExists_SMTP: TCheckBox; 
    cbxForwardTo: TCheckBox; 
    txtForwardTo: TEdit; 
    procedure btnOKClick(Sender: TObject); 
    procedure FormCreate(Sender: TObject); 
    procedure FormDestroy(Sender: TObject); 
    procedure FormShow(Sender: TObject); 
  private 
    { Private declarations } 
    UserInfo : TPop3UserInformation; // The user information we're 
                                     // adding or editing 
    procedure DisplayInfo;      // Display the user information in the dialog 
  public 
    { Public declarations } 
    function Add : Boolean;                   // Add a new user 
    function Edit(UserID : String) : Boolean; // Edit an existing user 
    // Both functions return true if successful, false if not (cancelled) 
  end; 
 
var 
  frmAddEditUser: TfrmAddEditUser; 
 
implementation 
 
{$R *.DFM} 
 
procedure TfrmAddEditUser.FormCreate(Sender: TObject); 
begin 
  // Create the User Information object that will hold user information and 
  // provide functions such as loading and saving user data to the disk. 
  UserInfo := TPop3UserInformation.Create; 
end; 
 
procedure TfrmAddEditUser.FormShow(Sender: TObject); 
begin 
  // If we can, set focus on the User Name edit box, unless it's disabled 
  // (edit mode) in which case, we focus on the User Password box. 
  if txtUserID.Enabled then txtUserID.SetFocus else txtUserPassword.SetFocus; 
end; 
 
procedure TfrmAddEditUser.FormDestroy(Sender: TObject); 
begin 
  // Free the User Information object... 
  UserInfo.Free; 
end; 
 
procedure TfrmAddEditUser.DisplayInfo; 
begin 
  // Display the User Information (from the UserInformation object) 
  // in the dialog 
  txtUserPassword.Text := UserInfo.Password; 
  txtForwardTo.Text := UserInfo.ForwardToAddress; 
  cbxForwardTo.Checked := txtForwardTo.Text <> ''; 
  txtRealName.Text := UserInfo.RealName; 
  cbxUB_DoNotReportUserExists_SMTP.Checked := 
    UserInfo.UB_DoNotReportUserExists_SMTP; 
end; 
 
function TfrmAddEditUser.Add : Boolean; 
begin 
  // We want to use the dialog as an "Add" dialog... 
 
  Caption := 'Add User';      // Set the dialog caption to reflect usage 
  txtUserID.Enabled := True;  // enable the User ID edit box so the user 
                              // can specify a user ID 
  txtUserID.Text := '';       // clear the user ID we're editing 
  UserInfo.Initialize;        // Initialize the User information 
                              // (clean the slate) 
 
  DisplayInfo;                // Display the user information (default user) 
  Result := ShowModal = mrOK; // let the user do their thing 
end; 
 
function TfrmAddEditUser.Edit(UserID : String) : Boolean; 
begin 
  // We want to use the dialog as an "Edit" dialog... 
 
  Caption := 'Edit User';        // Set the dialog caption to reflect usage 
  txtUserID.Enabled := False;    // disable the User ID edit box so the user 
                                 // can't change the User ID 
  txtUserID.Text := UserID;      // Display the ID of the User we'll edit 
  UserInfo.LoadFromFile(UserID); // read the user information from disk 
 
  DisplayInfo;                // Display the user information (default user) 
  Result := ShowModal = mrOK; // let the user do their thing 
end; 
 
procedure TfrmAddEditUser.btnOKClick(Sender: TObject); 
var 
  UserID : String;    // This is the UserID we're adding or editing... 
  Editing : Boolean;  // We'll set this to true to know we're editing... 
                      // it just makes it that much more readable... 
begin 
  // First, we need to "read" the user data from the dialog.  We'll clear the 
  // User Information object first, then get the dialog data 
  UserInfo.Initialize; 
  UserInfo.Password := txtUserPassword.Text; 
  if (cbxForwardTo.Checked) and (Trim(txtForwardTo.Text) <> '') then 
    UserInfo.ForwardToAddress := txtForwardTo.Text 
  else UserInfo.ForwardToAddress := ''; 
  UserInfo.RealName := txtRealName.Text; 
  UserInfo.UB_DoNotReportUserExists_SMTP := 
    cbxUB_DoNotReportUserExists_SMTP.Checked; 
 
  UserID := Trim(txtUserID.Text); 
  Editing := not txtUserID.Enabled;  // we know we're editing by the condition 
                                     // of the UserID Edit Box (Enabled <> Edit) 
 
  // Here, we can make the checks we'll have to do whether we're adding or 
  // editing a User ID 
  // Like being specified at all... 
  if UserID = '' then begin 
    ShowMessage('The User requires an ID'); 
    ModalResult := mrNone;      Exit; 
  end; 
 
  // We behave differently based on editing or adding... 
  if Editing then begin 
    // We're editing a user, it should already exist... 
    if not INI.User_Exists(UserID) then begin 
      // This should never happen, but if it does... 
      ShowMessage('The User ID must exist to be edited.'); 
      ModalResult := mrCancel;      Exit; 
    end; 
    // Save the User Information to disk, replacing the current data 
    UserInfo.SaveToFile(UserID); 
  end else begin 
    // We're creating a new User... 
 
    // First we need to be certain the UserID doesn't contain any 
    // invalid characters 
    if not IsNameValid(UserID) then begin 
      ShowMessage('The Used ID cannot contain ' + INVALIDNAMECHARACTERS); 
      ModalResult := mrNone;    Exit; 
    end; 
    // Then we check to be sure the name's available (it hasn't been used 
    // already for an Alias ID or Mailing List Name) 
    if INI.Alias_Exists(UserID) then begin 
      ShowMessage('The User ID is already taken by an Alias.'); 
      ModalResult := mrNone;    Exit; 
    end; 
    if INI.List_Exists(UserID) then begin 
      ShowMessage('The User ID is already taken by a Mail List.'); 
      ModalResult := mrNone;    Exit; 
    end; 
 
    // Finally, we verify the UserID does not exist, and we create it. 
    if INI.User_Exists(UserID) then begin 
      ShowMessage('The User ID already exists, please specify a different ID.'); 
      ModalResult := mrNone;      Exit; 
    end; 
 
    // if we've gotten this far, we can safely create the User. 
    // at each check along the way, we would have been thrown out of this 
    // procedure by the "ModalResult := mrNone;      Exit;" line which 
    // would have left the user looking at the dialog again. 
    if INI.User_Create(UserID) then UserInfo.SaveToFile(UserID) 
      else ShowMessage('Unable to create User Folder and File.'); 
  end; 
end; 
 
 
end.