Solved

How to know in code if a windows network user is member of a network group?

Posted on 2006-11-08
2
342 Views
Last Modified: 2010-04-04
Dear experts,
I need to use Windows identification to assign user rights in my software.
How can i know if the current user is a member of a certain group,
in order to allow him some rights allowed only to this group?
I need some sample code in Delphi 3 if possible.
Thanks in advance
0
Comment
Question by:icad01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 17900023

Example user group listing done and tested in D3. Requires admin or account operator privs to execute.

Regards,
Russell

----

example usage:

var listGroup: TStringList;
begin
 
  listGroup:=TStringList.Create;
  try
     GetUserGroups('administrator', listGroup);
     ShowMessage(listGroup.Text);
  finally
     listGroup.Free;
  end;

end;

--- the code ---

////////////////////////////////////////////////////////////////////////////////
//   LanMan constants
////////////////////////////////////////////////////////////////////////////////
const
  MAX_PREFERRED_LENGTH       =  DWORD(-1);
  LG_INCLUDE_INDIRECT        = $0001;

////////////////////////////////////////////////////////////////////////////////
//   LanMan types
////////////////////////////////////////////////////////////////////////////////
type
  NET_API_STATUS             =  DWORD;

  LOCALGROUP_USERS_INFO_0    =  packed record
     lgrui0_name:            LPWSTR;
  end;
  LPLOCALGROUP_USERS_INFO_0  =  ^LOCALGROUP_USERS_INFO_0;
  TLocalGroupUserInfo0       = LOCALGROUP_USERS_INFO_0;
  PLocalGroupUserInfo0       =  ^TLocalGroupUserInfo0;

////////////////////////////////////////////////////////////////////////////////
//   LanMan functions
////////////////////////////////////////////////////////////////////////////////
function   NetUserGetLocalGroups(ServerName: LPCWSTR; UserName: LPCWSTR; Level: DWORD; Flags: DWORD; var BufPtr: Pointer; PrefMaxLen: DWORD; EntriesRead: PDWORD; TotalEntries: PDWORD): NET_API_STATUS; stdcall; external 'netapi32.dll';
function   NetApiBufferFree(pBuf: Pointer): NET_API_STATUS; stdcall; external 'netapi32.dll';

////////////////////////////////////////////////////////////////////////////////
//   DomainUserName should be passed in the format of DOMAIN\USERNAME unless the user
//   account is local to the system executing the function. Local accounts
//   should be passed in as USERNAME only
////////////////////////////////////////////////////////////////////////////////
function GetUserGroups(DomainUserName: String; GroupList: TStrings): Integer;
var  lpBuffer:      PLocalGroupUserInfo0;
     lpGroup:       PLocalGroupUserInfo0;
     dwRead:        DWORD;
     dwTotal:       DWORD;
     dwIndex:       Integer;
begin

  // Check list
  if Assigned(GroupList) then
  begin
     // Lock the list
     GroupList.BeginUpdate;
     // Resource protection
     try
        // Clear the list
        GroupList.Clear;
        // Get the local groups that this user / group might belong to
        if (NetUserGetLocalGroups(nil, PWideChar(WideString(DomainUserName)), 0, LG_INCLUDE_INDIRECT, Pointer(lpBuffer), MAX_PREFERRED_LENGTH, @dwRead, @dwTotal) = 0) then
        begin
           // Resource protection
           try
              // Save start of pointer
              lpGroup:=lpBuffer;
              // Enumerate the groups
              for dwIndex:=0 to Pred(dwRead) do
              begin
                 // Copy the account name
                 GroupList.Add(WideCharToString(lpGroup^.lgrui0_name));
                 // Push next group name
                 Inc(lpGroup);
              end;
           finally
              // Free the allocated buffer
              NetApiBufferFree(lpBuffer);
           end;
        end;
     finally
        // Unlock list
        GroupList.EndUpdate;
     end;
     // Return list count
     result:=GroupList.Count;
  end
  else
     // No list passed
     result:=(-1);

end;
0
 

Author Comment

by:icad01
ID: 17928881
Dear rllibby,
Sorry for the delay.
Thanks a lot for sharing this great and helpfull  code!


0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question