Ignore:
Timestamp:
Sep 13, 2010, 11:20:24 AM (14 years ago)
Author:
george
Message:
  • Přidáno: Druhá varianta projektu pro sestavení TCP serveru.
  • Přidáno: Vlastní obsluha vyjímek a zobrazení ve HTTP tvaru.
  • Opraveno: Zpracování session třídy THTTPSessionStorageMySQL.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        22UConfig.pas
        33bin
         4lib
  • trunk/Application/UUser.pas

    r32 r33  
    11unit UUser;
    22
    3 {$mode delphi}
     3{$mode Delphi}{$H+}
    44
    55interface
    66
    77uses
    8   Classes, SysUtils, USqlDatabase, UCGIApplication;
     8  Classes, SysUtils, synacode, USqlDatabase, UCommon, UHTTPServer;
    99
    1010type
    11   TSQLUserList = class
     11  EDuplicateItem = class(Exception);
     12  ENotFound = class(Exception);
     13
     14  { TWebUser }
     15
     16  TWebUser = class
    1217    Database: TSqlDatabase;
     18    HandlerData: THTTPHandlerData;
     19    procedure Delete(Id: Integer);
     20    procedure Add(Name, Password, Email: string);
     21    function GetIdByName(Name: string): Integer;
     22    function GetIdByNamePassword(Name: string; PassWord: string): Integer;
    1323  end;
    1424
    15   TUser = class
     25  { TWebOnlineUser }
    1626
    17   end;
    18 
    19   { TSQLOnlineUser }
    20 
    21   TSQLOnlineUser = class
     27  TWebOnlineUser = class
    2228    Database: TSqlDatabase;
     29    HandlerData: THTTPHandlerData;
    2330    Id: Integer;
    2431    User: Integer;
    25     CGI: TCGIApplication;
    26     procedure Login(Name, Password: string);
    27     function Logout: string;
    2832    procedure Update;
     33    procedure Login(User: Integer);
     34    procedure Logout;
    2935  end;
    3036
    3137implementation
    3238
    33 { TSQLOnlineUser }
     39resourcestring
     40  SDuplicateUserItem = 'User name already used.';
    3441
    35 procedure TSQLOnlineUser.Login(Name, Password: string);
     42{ TOnlineUser }
     43
     44procedure TWebOnlineUser.Update;
     45var
     46  DbRows: TDbRows;
     47  Id: Integer;
     48begin
     49  DbRows := Database.Query('SELECT * FROM `UserOnline` WHERE `SessionId`="' +
     50    HandlerData.Request.Cookies.Values['SessionId'] + '"');
     51  if DbRows.Count > 0 then begin
     52    // Update exited
     53    Id := StrToInt(DbRows[0].Values['Id']);
     54    DbRows.Free;
     55    DbRows := Database.Query('UPDATE `UserOnline` SET `ActivityTime` = NOW() WHERE `Id`=' + IntToStr(Id));
     56  end else begin
     57    // Create new record
     58    DbRows.Free;
     59    DbRows := Database.Query('INSERT INTO `UserOnline` (`User`, `ActivityTime`, `SessionId`) ' +
     60      'VALUES (1, NOW(), "' + HandlerData.Request.Cookies.Values['SessionId'] + '")');
     61    Id := Database.LastInsertId;
     62  end;
     63  DbRows.Destroy;
     64end;
     65
     66procedure TWebOnlineUser.Login(User: Integer);
    3667var
    3768  DbRows: TDbRows;
    3869begin
    3970  Logout;
    40   try
    41     DbRows := Database.Query('UPDATE `UserOnline` SET `User` = ' + IntToStr(User) + ', `LoginTime` = NOW() WHERE `SessionId`="' +
    42       CGI.Cookies.Values['SessionId'] + '"');
    43   finally
    44     DbRows.Free;
    45   end;
     71  DbRows := Database.Query('UPDATE `UserOnline` SET `User` = ' + IntToStr(User) + ', `LoginTime` = NOW() WHERE `SessionId`="' +
     72    HandlerData.Request.Cookies.Values['SessionId'] + '"');
     73  DbRows.Destroy;
    4674  Self.User := User;
    4775end;
    4876
    49 function TSQLOnlineUser.Logout: string;
     77procedure TWebOnlineUser.Logout;
    5078var
    5179  DbRows: TDbRows;
     
    5381  if Id = 1 then Update;
    5482  if User <> 1 then begin
    55     try
    56       DbRows := Database.Query('UPDATE `UserOnline` SET `User` = 1 WHERE `SessionId`="' +
    57         CGI.Cookies.Values['SessionId'] + '"');
    58     finally
    59       DbRows.Free;
    60     end;
     83    DbRows := Database.Query('UPDATE `UserOnline` SET `User` = 1 WHERE `SessionId`="' +
     84      HandlerData.Request.Cookies.Values['SessionId'] + '"');
     85    DbRows.Destroy;
    6186    User := 1;
    6287  end;
    6388end;
    6489
    65 procedure TSQLOnlineUser.Update;
     90{ TUser }
     91
     92procedure TWebUser.Delete(Id: Integer);
    6693begin
     94  Database.Query('DELETE FROM `User` WHERE `Id`=' + IntToStr(Id));
     95end;
    6796
     97procedure TWebUser.Add(Name, Password, Email: string);
     98var
     99  Salt: string;
     100  DbRows: TDbRows;
     101begin
     102  DbRows := Database.Query('SELECT `Id` FROM `User` WHERE `Name`="' + Name + '"');
     103  try
     104    if DbRows.Count = 0 then begin
     105      Salt := EncodeBase64(Copy(BinToHexString(SHA1(FloatToStr(Now))), 1, 8));
     106      Database.Query('INSERT INTO `User` (`Name`, `Password`, `Salt`, `Email`, `RegistrationTime`) VALUES ("' +
     107        Name + '", SHA1(CONCAT("' + Password + '", "' + Salt + '")), "' + Salt +
     108        '", "' + Email + '", NOW())');
     109    end else raise EDuplicateItem.Create(SDuplicateUserItem);
     110  finally
     111    DbRows.Destroy;
     112  end;
     113end;
     114
     115function TWebUser.GetIdByName(Name: string): Integer;
     116var
     117  DbRows: TDbRows;
     118begin
     119  DbRows := Database.Query('SELECT `Id` FROM `User` WHERE `Name`="' + Name + '"');
     120  try
     121    if DbRows.Count = 1 then Result := StrToInt(DbRows[0].ValuesAtIndex[0])
     122      else raise ENotFound.Create('User "' + Name + '" not found');
     123  finally
     124    DBRows.Destroy;
     125  end;
     126end;
     127
     128function TWebUser.GetIdByNamePassword(Name: string; PassWord: string): Integer;
     129var
     130  DbRows: TDbRows;
     131begin
     132  DbRows := Database.Query('SELECT `Id` FROM `User` WHERE `Name`="' + Name + '" AND ' +
     133    '`Password` = SHA1(CONCAT("' + Password + '", Salt))');
     134  try
     135    if DbRows.Count = 1 then Result := StrToInt(DbRows[0].ValuesAtIndex[0])
     136      else raise ENotFound.Create('User "' + Name + '" not found');
     137  finally
     138    DBRows.Destroy;
     139  end;
    68140end;
    69141
Note: See TracChangeset for help on using the changeset viewer.