1

I need to know the user id and groups of the currently logged-in user in a client application of RAD server to do some further logic. But I couldn't find anything about that in the documentation. Can anyone please share any ideas? Thanks.

MW3226
  • 53
  • 5

2 Answers2

0

To get the user user's username:

function GetUserNameStringEx(UsernameFormat: EXTENDED_NAME_FORMAT): string;
var
    szBuffer: UnicodeString;
    nSize: Cardinal;
const
    UNLEN = 256; //characters
begin
(*
    Get the user and computer name

        NameFullyQualifiedDN  'CN=Ian Boyd,OU=Contoso Users,DC=contoso,DC=com'
        NameSamCompatible     'CONTOSO\ian'             <--the only one supported if the user account is not in a domain
        NameDisplay           'Ian'
        NameUniqueId          '{e756aad8-2630-4661-a362-abe75b96146a}'
        NameCanonical         'contoso.com/Contoso Users/Ian Boyd'
        NameUserPrincipal     'ian@Contoso.com'
        NameCanonicalEx       'contoso.com/Contoso Users'#$A'Ian Boyd'
        NameServicePrincipal  ''
        NameDnsDomain         'CONTOSO.COM\ian'

    The value *cannot* be NameUnknown
*)

    Result := '';

    SetLength(szBuffer, 4096); //UNLEN+1);
    nSize   := Length(szBuffer);

    if not GetUserNameEx(UsernameFormat, PWideChar(@szBuffer[1]), {var}nSize) then
        Exit;

    SetLength(szBuffer, nSize);
    Result := szBuffer;
end;

To get the current user's Security Identifier (SID)

function GetCurrentUserSID: string;
type
  PTOKEN_USER = ^TOKEN_USER;
  _TOKEN_USER = record
     User: TSidAndAttributes;
  end;
  TOKEN_USER = _TOKEN_USER;

var
    hToken: THandle;
    cbBuf: Cardinal;
    ptiUser: PTOKEN_USER;
    bSuccess: Boolean;
begin
    Result := '';

    // Get the calling thread's access token.
    if not OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, True, hToken) then
    begin
        if (GetLastError <> ERROR_NO_TOKEN) then
            Exit;

        // Retry against process token if no thread token exists.
        if not OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hToken) then
            Exit;
    end;
    try
        // Obtain the size of the user information in the token.
        bSuccess := GetTokenInformation(hToken, TokenUser, nil, 0, cbBuf);
        ptiUser  := nil;
        try
            while (not bSuccess) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) do
            begin
                ReallocMem(ptiUser, cbBuf);
                bSuccess := GetTokenInformation(hToken, TokenUser, ptiUser, cbBuf, cbBuf);
            end;
            Result := SidToString(ptiUser.User.Sid);
        finally
            FreeMem(ptiUser);
        end;
    finally
        CloseHandle(hToken);
    end;
end;

Getting groups

(but be aware, some groups are simply labels, some are allow, some are deny)

var
    hAccessToken: THandle;
    ptgGroups: PTokenGroups;
    dwInfoBufferSize: DWORD;
    x: Integer;
    bSuccess: BOOL;
begin
    bSuccess := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True, hAccessToken);
    if not bSuccess then
    begin
        if GetLastError = ERROR_NO_TOKEN then
                bSuccess := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hAccessToken);
    end;

    if not bSuccess then
       Exit;

    GetMem(ptgGroups, 1024);
    bSuccess := GetTokenInformation(hAccessToken, TokenGroups, ptgGroups, 1024, dwInfoBufferSize);
    CloseHandle(hAccessToken);
    if not bSuccess then
       Exit;

    {$R-}
    for x := 0 to ptgGroups^.GroupCount - 1 do
    begin
        Here you can do something with:

        ptgGroups^.Groups[x]
    end;
    FreeMem(ptgGroups);
end;
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • Thanks Ian. But I'm afraid it's not what I need. The context I'm talking about is in the client application of RAD Server (Embarcadero's RAD Server) but not Windows Server. – MW3226 Apr 05 '22 at 06:07
0

One way to get information about users/groups from RAD Server is to use it's Administrative API, such as RetrieveUser. It's a lot, but reading up on the Internal API logic will go a long way for you. Good luck.

Note: Use the TEMSClientAPI, the TBackendUsers or the TBackendAuth components to sign up, log in, retrieve, update, or delete RAD Server Users information from your RAD Server Database

BackendAuth has a property called UserDetails.

pjackson
  • 35
  • 7
  • We're you able to get what you needed for the user and group information? – pjackson Nov 20 '22 at 18:19
  • @MW3226, Here's a great article about using RAD Server's Users DB. [link]https://blogs.embarcadero.com/rad-server-and-the-included-data-storage-for-rad-server-users/ – pjackson Nov 20 '22 at 18:25