SHARE
TWEET

Untitled

a guest May 31st, 2017 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. { -- Todo :
  3.            fix const userfile to config.datapath + userfilename
  4.            add acs checking possably use pipeline style acs checking
  5.            add more user vars as more is written
  6.            add encrypt string to encrypt password from sysop
  7.  
  8.            -sd
  9. }
  10.  
  11. unit UserManager;
  12.  
  13. {$mode objfpc}{$H+}
  14.  
  15. interface
  16.  
  17. uses
  18.   Classes, SysUtils, IniFiles, Windows, SyncObjs;
  19.  
  20. const
  21.   userfile = 'userdata.ini'; // replace this with config string +'userdata.ini'
  22.  
  23. type
  24.  
  25.   TUserAccount = class(TObject)
  26.   protected
  27.     FName: string;
  28.     FRealName: string;
  29.     FPassword: string;
  30.     FACSString: string;
  31.     FFlagAttributes: string;
  32.     FEmailAddress : String;
  33.   public
  34.     constructor Create;
  35.     destructor Destroy; override;
  36.     function CheckPassword(const APassword: string): boolean;
  37.     function ACSCheck(const AValue: string): boolean;
  38.   published
  39.     property UserName: string read FName write FName;
  40.     property UserRealName: string read FRealName write FRealName;
  41.     property Password: string read FPassword write FPassword;
  42.     property UserACS: string read FACSString write FACSString;
  43.     property UserFlags: string read FFlagAttributes write FFlagAttributes;
  44.     property EmailAddress : string read FEmailAddress write FEmailAddress;
  45.   end;
  46.  
  47.   TUserAccountManager = class(TObject)
  48.   protected
  49.     FLock: TRTLCriticalSection;
  50.     FLastError: integer;
  51.     FLastErrorStr: string;
  52.     FIFile: TInifile;
  53.     procedure FSetError(ACode: integer; AError: string);
  54.     procedure FResetError;
  55.   public
  56.     constructor Create;
  57.     destructor Destroy; override;
  58.     function GetAccountByUserName(const AUserName: string): TUserAccount;
  59.     function DoesAccountExist(const AUserName: string): boolean;
  60.     procedure AddAccount(const AAccount: TUserAccount);
  61.     procedure DeleteAcccount(const AUserName: string);
  62.     procedure UpdateAccount(const AAccount: TUserAccount);
  63.   published
  64.     property LastError: integer read FLastError default 0;
  65.     property LastErrorStr: string read FLastErrorStr;
  66.  
  67.   end;
  68.  
  69.  
  70.  
  71.  
  72. implementation
  73.  
  74. constructor TUserAccountManager.Create;
  75. begin
  76.   if not FileExists(userfile) then
  77.     FSetError(1, 'TUserAccountManager.Create : User File Does NOT Exist');
  78.   FIFile := TInifile.Create(userfile);
  79.   InitializeCriticalSection(FLock);
  80. end;
  81.  
  82. destructor TUserAccountManager.Destroy;
  83. begin
  84.   FIFile.Free;
  85.   DeleteCriticalSection(FLock);
  86. end;
  87.  
  88. procedure TUserAccountManager.FSetError(ACode: integer; AError: string);
  89. begin
  90.   FLastError := ACode;
  91.   FLastErrorStr := AError;
  92. end;
  93.  
  94. procedure TUserAccountManager.FResetError;
  95. begin
  96.   FLastError := 0;
  97.   FlastErrorStr := '';
  98. end;
  99.  
  100. function TUserAccountManager.DoesAccountExist(const AUserName: string): boolean;
  101. begin
  102.   EnterCriticalSection(FLock);
  103.   try
  104.     Result := FIFile.SectionExists(AUserName);
  105.   finally
  106.     LeaveCriticalSection(FLock);
  107.   end;
  108. end;
  109.  
  110. function TUserAccountManager.GetAccountByUserName(const AUserName: string): TUserAccount;
  111. begin
  112.   if not DoesAccountExist(AUserName) then
  113.   begin
  114.     FSetError(2, 'TUserAccountManager.GetAccountByUserName : User Does Not Exist');
  115.     exit;
  116.   end;
  117.   EnterCriticalSection(FLock);
  118.   try
  119.     Result := TUserAccount.Create;
  120.     Result.UserRealName := FIFile.ReadString(AUserName, 'USER_REAL_NAME', '');
  121.     Result.UserName := FIFile.ReadString(AUserName, 'USER_HANDLE', '');
  122.     Result.Password := FIFile.ReadString(AUserName, 'USER_PASSWORD', '');
  123.     Result.EmailAddress:= FIFile.ReadString(AUserName,'USER_EMAIL,'');
  124.    Result.UserACS := FIFile.ReadString(AUserName, 'USER_ACS', '');
  125.    Result.UserFlags := FIFile.ReadString(AUserName, 'USER_FLAGS', '');
  126.  finally
  127.    LeaveCriticalSection(FLock);
  128.  end;
  129. end;
  130.  
  131.  
  132. procedure TUserAccountManager.AddAccount(const AAccount: TUserAccount);
  133. begin
  134.  if DoesAccountExist(AAccount.UserName) then
  135.  begin
  136.    FSetError(3, 'TUserAccountManager.AddAccount : User Already Exist');
  137.    exit;
  138.  end;
  139.  EnterCriticalSection(FLock);
  140.  try
  141.    FIFile.WriteString(AAccount.UserName, 'USER_HANDLE', AAccount.UserName);
  142.    FIFile.WriteString(AAccount.UserName, 'USER_REAL_NAME', AAccount.UserRealName);
  143.    FIFile.WriteString(AAccount.UserName, 'USER_EMAIL',AAccount.EmailAddress);
  144.    FIFile.WriteString(AAccount.UserName, 'USER_PASSWORD', AAccount.Password);
  145.    FIFile.WriteString(AAccount.UserName, 'USER_ACS', AAccount.UserACS);
  146.    FIFile.WriteString(AAccount.UserName, 'USER_FLAGS', AAccount.UserFlags);
  147.  finally
  148.    LeaveCriticalSection(FLock);
  149.  end;
  150. end;
  151.  
  152. procedure TUserAccountManager.DeleteAcccount(const AUserName: string);
  153. begin
  154.  if not DoesAccountExist(AUserName) then
  155.  begin
  156.    FSetError(2, 'TUserAccountManager.DeleteAccount : User Does Not Exist');
  157.    Exit;
  158.  end;
  159.  EnterCriticalSection(FLock);
  160.  try
  161.    FIFile.EraseSection(AUserName);
  162.    if DoesAccountExist(AUserName) then
  163.      FSetError(4, 'TUserAccountManager.DeleteAccount : User Exist After Deletion');
  164.  finally
  165.    LeaveCriticalSection(FLock);
  166.  end;
  167. end;
  168.  
  169. procedure TUserAccountManager.UpdateAccount(const AAccount: TUserAccount);
  170. begin
  171.  if not DoesAccountExist(AAccount.UserName) then
  172.  begin
  173.    FSetError(3, 'TUserAccountManager.UpdateAccount : User Does Not Exist');
  174.    exit;
  175.  end;
  176.  EnterCriticalSection(FLock);
  177.  try
  178.    FIFile.WriteString(AAccount.UserName, 'USER_HANDLE', AAccount.UserName);
  179.    FIFile.WriteString(AAccount.UserName, 'USER_REAL_NAME', AAccount.UserRealName);
  180.    FIFile.WriteString(AAccount.UserName, 'USER_PASSWORD', AAccount.Password);
  181.    FIFile.WriteString(AAccount.UserName, 'USER_EMAUL',AAccount.EmailAddress);
  182.    FIFile.WriteString(AAccount.UserName, 'USER_ACS', AAccount.UserACS);
  183.    FIFile.WriteString(AAccount.UserName, 'USER_FLAGS', AAccount.UserFlags);
  184.  finally
  185.    LeaveCriticalSection(FLock);
  186.  end;
  187. end;
  188.  
  189.  
  190. constructor TUserAccount.Create;
  191. begin
  192.  // lets set defaults
  193.  UserName := 'Unknown';
  194.  UserRealName := 'Unknown';
  195.  Password := '';
  196.  UserACS := '';
  197.  UserFlags := '';
  198. end;
  199.  
  200. destructor TUserAccount.Destroy;
  201. begin
  202. end;
  203.  
  204.  
  205. function TUserAccount.CheckPassword(const APassword: string): boolean;
  206. begin
  207.  Result := match(EncryptString(Uppercase(APassword), Password));
  208.  
  209. end;
  210.  
  211.  
  212. function TUserAccount.ACSCheck(const AValue: string): boolean;
  213. begin
  214.  Result := True;
  215. // fix me!! iniquity / rg / tg / pipeline acs style system research time
  216. end;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top