Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-11-08
2
Medium Priority
?
350 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

773 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