unit srvLogin_Impl;

{----------------------------------------------------------------------------}
{ This unit was automatically generated by the RemObjects SDK after reading  }
{ the RODL file associated with this project .                               }
{                                                                            }
{ This is where you are supposed to code the implementation of your objects. }
{----------------------------------------------------------------------------}

interface

uses
  {vcl:} Classes, SysUtils, 
  {RemObjects:} uROClientIntf, uROTypes, uROServer, uROServerIntf, uROSessions,
  {Required:} uRORemoteDataModule, uDAInterfaces, DARemoteService_Impl, DataAbstract_Intf,
  {Generated:} VARELA_Intf, uDAClasses, uDADataTable, uDABINAdapter;

type
  { TsrvLogin }
  TsrvLogin = class(TDARemoteService, IsrvLogin)
    schLogin: TDASchema;
    DABINAdapter: TDABINAdapter;
  private
  protected
    function Login(const UserID: String; const Password: String; out LoginInfo: TLoginInfo): Boolean;
    procedure Logout;
    function UpdateUsers(const UsersDelta: Binary): Binary;
    function SetUserPassword(const UserID: String; const NewPassword: String): Boolean;
    function Ping : Boolean;
  end;

implementation

{$R *.dfm}
uses
  {Generated:} VARELA_Invk, uDataModuleServer, Dialogs, IB, Variants;


procedure Create_srvLogin(out anInstance : IUnknown);
begin
  anInstance := TsrvLogin.Create(NIL);
end;

{ TsrvLogin }

function TsrvLogin.Login(const UserID, Password: String;
  out LoginInfo: TLoginInfo): Boolean;
var
  dsUserInfo,
  dsRoles,
  dsPrivilegios : IDADataset;
begin
  LoginInfo := NIL;
  Result := False;

  dsUserInfo := schLogin.NewDataset(Connection, 'GetLoginInfo', ['UserID', 'Password'], [UserID, Password]);

  if (dsUserInfo.RecordCount = 1) then
  try
    Result := TRUE;

    // Packages the user information that will be sent back to the client.
    // This information will be used in many places client-side and when issing service calls
    // so it's good practice to cache it there
    LoginInfo := TLoginInfo.Create;
    FillROStruct(dsUserInfo, LoginInfo);
    LoginInfo.UserID := UserID;
    LoginInfo.SessionID := GUIDToString(Session.SessionID);
    //LoginInfo.Roles.Add(VarToStr(dsUserInfo.FieldValues[3]));

    // Collects the privileges for this user as a comma separated list of strings
{    privs := Schema.NewDataset(Connection, ds_GetUserPrivileges, ['UserInc'], [LoginInfo.UserInc]);

    while not privs.EOF do begin
      LoginInfo.Privileges := LoginInfo.Privileges+Trim(VarToStr(privs.FieldValues[0]))+',';
      privs.Next;
    end;
    LoginInfo.Privileges := Copy(LoginInfo.Privileges,1,Length(LoginInfo.Privileges)-1);


    // Stores all the information we retrieved in the current session since it might be
    // necessary for other services}
    Session.SaveStruct(LoginInfo);
  except
    FreeAndNIL(LoginInfo);
    raise
  end;
end;

procedure TsrvLogin.Logout;
begin
  DestroySession;
end;

function TsrvLogin.Ping: Boolean;
begin
  Result := True;
end;

function TsrvLogin.SetUserPassword(const UserID,
  NewPassword: String): Boolean;
var
  dsCommand : IDASQLCommand;
  i : Integer;
begin
  Result := False;
  dsCommand := schLogin.NewCommand(Connection, 'SetUserPassword');
  with dsCommand do
  begin
    ParamByName('UserID').AsString := UserID;
    ParamByName('Password').AsString := NewPassword;
    i := Execute;
  end;
  if i > 0 then
    Result := True;
end;

function TsrvLogin.UpdateUsers(const UsersDelta: Binary): Binary;
begin
  //
end;

initialization
  TROClassFactory.Create('srvLogin', Create_srvLogin, TsrvLogin_Invoker);

finalization

end.
