Solved

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

Posted on 2006-11-08
2
335 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
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Firewall issues 6 85
Delphi Form ownership 4 54
Convert GUI app into console app for Win32 Env 5 58
creating threads in delphi 1 54
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now