unit uUsersManager;

interface

uses
  uROSessions, FactuGES_Intf, uSesionesUtils, uDataModuleServer;

type
  TUserInfo = class(TObject)
  private
    FSession : TROSession;
    FLoginInfo : TRdxLoginInfo;
    function GetLoginInfo: TRdxLoginInfo;
    procedure SetLoginInfo(const Value: TRdxLoginInfo);
    function GetEsAdministrador: Boolean;
    function GetEmpresas: String;
  public
    constructor Create(ASession : TROSession); overload;
    constructor Create(ASessionID : TGUID); overload;
    destructor Destroy; override;
    property LoginInfo : TRdxLoginInfo read GetLoginInfo write SetLoginInfo;
    property EsAdministrador : Boolean read GetEsAdministrador;
    property Empresas : String read GetEmpresas;
  end;

implementation

uses Classes, srvLogin_Impl, SysUtils;


{ TUserInfo }

constructor TUserInfo.Create(ASession: TROSession);
begin
  FSession := ASession;
  FLoginInfo := TRdxLoginInfo(SesionesHelper.GetSessionObject(FSession, SESION_LOGININFO));
end;

constructor TUserInfo.Create(ASessionID: TGUID);
begin
  Create(dmServer.DBSessionManager.FindSession(ASessionID));
end;

destructor TUserInfo.Destroy;
begin
  inherited;
end;

function TUserInfo.GetEmpresas: String;
var
  I: Integer;
  AList : TStringList;
begin
  Result := '';
  AList := TStringList.Create;
  try
    AList.Sorted := True;
    AList.Delimiter := ',';
    AList.Duplicates := dupIgnore;

    for I := 0 to FLoginInfo.Empresas.Count - 1 do
      AList.Add(IntToStr(FLoginInfo.Empresas.Items[I]));
    Result := AList.DelimitedText;
  finally
    AList.Free;
    AList := NIL;
  end;
end;

function TUserInfo.GetEsAdministrador: Boolean;
var
  I: Integer;
begin
  Result := False;
  for I := 0 to FLoginInfo.Perfiles.Count - 1 do
    if FLoginInfo.Perfiles.Items[I] = PERFIL_ADMINISTRADORES then
    begin
      Result := True;
      Break;
    end;
end;

function TUserInfo.GetLoginInfo: TRdxLoginInfo;
begin
  Result := FLoginInfo;
end;

procedure TUserInfo.SetLoginInfo(const Value: TRdxLoginInfo);
begin
  FLoginInfo := Value;
  SesionesHelper.SaveSessionObject(FSession, SESION_LOGININFO, FLoginInfo);
end;

end.
