کد:
type
TUser = class
public
private
var
FUserID : Integer;
FUsername : string;
FPassword : string;
FFullName : string;
FTotalPoint : Integer;
FEMail : string;
procedure SetUserID(val: Integer);
procedure SetUsername(val: string);
procedure SetPassword(val: string);
procedure SetFulName(val: string);
procedure SetTotalPoint(val: Integer);
procedure SetEMail(val: string);
function SendMail(aSMTP: TIdSMTP; aMailMessage: TIdMessage; aSubject, aBody: WideString): string;
public
property UserID : Integer read FUserID write SetUserID;
property Username : string read FUsername write SetUsername;
property Password : string read FPassword write SetPassword;
property FullName : string read FFullName write SetFulName;
property TotalPoint : Integer read FTotalPoint write SetTotalPoint;
property EMail : string read FEMail write SetEMail;
constructor Create;
destructor Destroy; override;
function Login: TUser;
function GetUserInfo(aUsername: string; aUserID: Integer = 0; aEMail: string = ''): TUser;
function RegisterUser: TUser;
function UpdateUser: boolean;
function ChangePassword(NewPassword: string): boolean;
procedure SendNewPasswordEmail(aSMTP: TIdSMTP; aMailMessage: TIdMessage);
end;
procedure TUser.SetUserID(val: Integer);
begin
FUserID := val;
end;
procedure TUser.SetUsername(val: string);
begin
FUsername := val;
end;
procedure TUser.SetPassword(val: string);
begin
FPassword := val;
end;
procedure TUser.SetFulName(val: string);
begin
FFullName := val;
end;
procedure TUser.SetTotalPoint(val: Integer);
begin
FTotalPoint := val;
end;
procedure TUser.SetEMail(val: string);
begin
FEMail := val;
end;
constructor TUser.Create;
begin
inherited Create;
FUserID := 0;
FUsername := '';
FPassword := '';
FFullName := '';
FTotalPoint := 0;
FEMail := '';
end;
destructor TUser.Destroy;
begin
inherited Destroy;
end;
function TUser.Login: TUser;
var
V: Integer;
begin
Result := nil;
V := fOpenReturnInt('Select Count(*) from Users where Username = ' + QuotedStr(Username) + ' and Password = ' +
QuotedStr(Password));
if V > 0 then
Result := GetUserInfo(Username);
end;
function TUser.GetUserInfo(aUsername: string; aUserID: Integer = 0; aEMail: string = ''): TUser;
var
WHR_Clause: String;
V : TADOQuery;
I : Integer;
begin
Result := nil;
if aUserID <> 0 then
WHR_Clause := ' UserID = ' + IntToStr(aUserID)
else if aUsername <> '' then
WHR_Clause := ' Username = ' + QuotedStr(aUsername)
else if aEMail <> '' then
WHR_Clause := ' EMail = ' + QuotedStr(aEMail);
V := fOpen('Select * from Users where ' + WHR_Clause);
if V.RecordCount > 0 then
begin
Result := TUser.Create;
with Result do
begin
UserID := V.FieldByName('UserID').AsInteger;
FullName := V.FieldByName('FullName').AsString;
Username := V.FieldByName('Username').AsString;
Password := V.FieldByName('Password').AsString;
TotalPoint := V.FieldByName('TotalPoint').AsInteger;
EMail := V.FieldByName('EMail').AsString;
end;
end;
FreeAndNil(V);
end;
function TUser.RegisterUser: TUser;
var
V: Integer;
begin
Result := nil;
V := fOpenReturnInt('Select Count(*) from Users where Username = ' + QuotedStr(Username));
if V = 0 then
begin
V := fOpenReturnInt('INSERT INTO Users (Username, Password, FullName, TotalPoint, EMail) VALUES (' +
QuotedStr(Username) + ',' + QuotedStr(Password) + ', N' + QuotedStr(FullName) + ',0,' +
QuotedStr(EMail) + '); Select Scope_Identity();');
if V > 0 then
begin
Result := Self.GetUserInfo(Self.Username);
Self := Result;
end;
end;
end;
function TUser.UpdateUser: boolean;
var
V: Integer;
begin
Result := False;
V := fRun('Update Users SET FullName = N' + QuotedStr(FullName) + ' where UserID = ' + IntToStr(UserID));
if V > 0 then
Result := True;
end;
function TUser.ChangePassword(NewPassword: string): boolean;
var
V: Integer;
begin
Result := False;
V := fRun('Update Users set Password = ' + QuotedStr(NewPassword) + ' where UserID = ' + IntToStr(UserID));
if V > 0 then
Result := True;
end;
procedure TUser.SendNewPasswordEmail(aSMTP: TIdSMTP; aMailMessage: TIdMessage);
var
Subject, Body: WideString;
NewPass : String;
begin
NewPass := RandomPassword(10);
Subject := ':: ' + IWServerController.AppName + ' :: تغییر رمز عبور ::';
Body := 'کاربر گرامی ، ' + FullName + 'اقدام به درخواست رمز عبور جدید کرده اید . ' + IWServerController.AppName +
' شما از طریق کنترل پنل خود در سایت <br>' +
'رمز عبور جدید شما : ' + NewPass;
Self.ChangePassword(NewPass);
SendMail(aSMTP, aMailMessage, Subject, Body);
end;
function TUser.SendMail(aSMTP: TIdSMTP; aMailMessage: TIdMessage; aSubject, aBody: WideString): string;
var
S: String;
begin
// setup SMTP
aSMTP.Host := 'smtp.gmail.com';
aSMTP.Port := 25; // for smtp.gmail.com
// setup mail message
aMailMessage.From.Address := 'Email@gmail.com';
aMailMessage.Recipients.EMailAddresses := EMail;
aMailMessage.ContentType := 'text/html';
aMailMessage.Subject := 'Subject Email';
S := '<table style="FONT-SIZE: 12px; font-name: Tahoma" dir="rtl" border="1" cellspacing="1" cellpadding="1" width="100%">';
S := S + '<tr><td>' + aBody + '</td></tr>';
S := S + '</table>';
aMailMessage.CharSet := 'UTF-8';
aMailMessage.Body.Text := S;
// send mail
try
try
aSMTP.Connect;
aSMTP.Send(aMailMessage);
except
on E: Exception do
// Error Handling
end;
finally
if aSMTP.Connected then
aSMTP.Disconnect;
end;
end;
علاقه مندي ها (Bookmarks)