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,
  {Ancestor Implementation:} DARemoteService_Impl,
  {Used RODLs:} DataAbstract_Intf,
  {Generated:} FactuGES_Intf, uDAClasses, uDAInterfaces, uDAEngine,
  uDADataTable, uDABINAdapter, uROClient;

const
  PERFIL_ADMINISTRADORES = 'Administradores';

type
  { TsrvLogin }
  TsrvLogin = class(TDARemoteService, IsrvLogin)
    DABINAdapter: TDABINAdapter;
    schLogin: TDASchema;
    procedure DARemoteServiceBeforeAcquireConnection(Sender: TDARemoteService;
      var ConnectionName: string);
    procedure DARemoteServiceCreate(Sender: TObject);
  private
  protected
    function Login(const User: String; const Password: String; out LoginInfo: TRdxLoginInfo): Boolean;
    procedure Logout;
    function Ping: Boolean;
  end;

implementation

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

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

{ srvLogin }
{ TsrvLogin }

procedure TsrvLogin.DARemoteServiceBeforeAcquireConnection(
  Sender: TDARemoteService; var ConnectionName: string);
begin
  ConnectionName := dmServer.ConnectionName;
end;

procedure TsrvLogin.DARemoteServiceCreate(Sender: TObject);
begin
  SessionManager := dmServer.SessionManager;
end;

function TsrvLogin.Login(const User, Password: String;
  out LoginInfo: TRdxLoginInfo): Boolean;
var
  dsUser,
  dsPerfiles,
  dsEmpresas : IDADataset;
  InternalLoginInfo : TRdxLoginInfo;
begin
  LoginInfo := NIL;
  Result := False;

  dsUser := schLogin.NewDataset(Connection, 'UsuarioPermitido', ['Usuario', 'Pass'], [User, Password]);

  if (dsUser.RecordCount = 1) then
  begin
    try
      LoginInfo := TRdxLoginInfo.Create;
      with LoginInfo do
      begin
        UserID := dsUser.FieldValues[0];
        Usuario := User;
        SessionID := GUIDToString(Session.SessionID);
        Perfiles := TDAStringArray.Create;
        Empresas := TRdxEmpresasArray.Create;
      end;

      // Asigna los perfiles del usuario
      LoginInfo.Perfiles.Clear;
      dsPerfiles := schLogin.NewDataset(Connection, 'PerfilesUsuario', ['ID_USUARIO'], [LoginInfo.UserID]);
      while not dsPerfiles.EOF do
      begin
        LoginInfo.Perfiles.Add(VarToStr(dsPerfiles.FieldValues[0]));
        dsPerfiles.Next;
      end;

      // Asigna las empresas del usuario
      LoginInfo.Empresas.Clear;
      dsEmpresas := schLogin.NewDataset(Connection, 'EmpresasUsuario', ['ID_USUARIO'], [LoginInfo.UserID]);
      while not dsEmpresas.EOF do
      begin
        LoginInfo.Empresas.Add(dsEmpresas.FieldValues[0]);
        dsEmpresas.Next;
      end;

      // Guardamos una copia de LoginInfo en el servidor para usarlo
      // en otros servicios
      InternalLoginInfo := TRdxLoginInfo.Create;
      InternalLoginInfo.Assign(LoginInfo);
      SesionesHelper.SaveSessionObject(Session, SESION_LOGININFO, InternalLoginInfo);
      Result := True;
    except
      on e : exception do
      begin
        FreeAndNIL(LoginInfo);
        ShowMessage(e.Message);
        raise
      end;
    end;
  end
  else begin
    { Invalid login. The temporary session is not to be kept. }
    DestroySession;
  end;
end;

procedure TsrvLogin.Logout;
begin
  DestroySession;
end;

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

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

finalization

end.
