Re: Membership of DB Role when User Gains Access via Active Direct



Hi Antonio

I found the IS_MEMBER function after I posted the question. It sort of
works. I say sort of because it returns membership based on the CURRENT_USER
rather than the SYSTEM_USER. For most people it will work but it doesn't
work for my login. I assume this is because I am a sysadmin.

I want to test whether someone is a member of the role 'OutletOwners'. I am
a member of the role through my login DOMAIN\simonlogin'. However my
CURRENT_USER name is 'dbo'. So IS_MEMBER thinks I am not a member of
'OutletOwners' because 'dbo' is not a member. I got around this by querying
the sysmembers table directly for users like me.

IS_MEMBER also doesn't work if the AD Group which has rights to the database
has a different name in the database to its Windows Group name (eg if grant
access to the DB via " EXEC sp_grantdbaccess 'DOMAIN\ADGroupName', 'NameInDB'
" then make 'NameInDB' a member of the role you want to test). I haven't
found a way around this except to put a warning in the documentation to only
add AD groups to the DB using their Windows names, not a new DB user name.

If it is any use, here is the complete code for the user-defined function I
wrote:

--=== CODE BEGINS ====

CREATE FUNCTION dbo.usf_IsRoleMember (
@pstrDBRole VARCHAR(30)
)
RETURNS SMALLINT AS
BEGIN
DECLARE @strUser VARCHAR(50),
@intUserID SMALLINT,
@intRoleID SMALLINT,
@intResult SMALLINT

SET @pstrDBRole = LTRIM(RTRIM(COALESCE(@pstrDBRole, '')))
SET @intResult = 0

-- ROLE EXISTS IN CURRENT DB.
IF EXISTS(SELECT * FROM sysusers WHERE name = @pstrDBRole)
BEGIN
-- Returns true if users' login is a member of a windows group that
-- is in turn a member of the specified database role.
SET @intResult = COALESCE(IS_MEMBER(@pstrDBRole), -1)

-- IS_MEMBER tests whether CURRENT_USER is a member of the role. Sometimes
-- a user may be a member of the role through their login while their
-- CURRENT_USER name is different from the login (eg CURRENT_USER may
-- be 'dbo'). In that case IS_MEMBER will indicate user is not a
-- member of the role.

-- DOUBLE CHECK WHETHER USER IS MEMBER OF ROLE.
IF @intResult = 0
BEGIN
-- NOTE: USER_ID with no argument will return the ID of the session user.
-- Normally this is the same as the id of the system user. Sometimes
-- they may differ, eg when user is a member of sysadmin. In that case
-- USER_ID() will return the ID of the database owner while
-- USER_ID(SYSTEM_USER) will return the ID of the user's login.
SET @intUserID = USER_ID(SYSTEM_USER)

SET @intRoleID = USER_ID(@pstrDBRole)
IF EXISTS(SELECT * FROM sysmembers WHERE groupuid = @intRoleID AND
memberuid = @intUserID)
SET @intResult = 1
END
END
-- ROLE DOES NOT EXIST IN CURRENT DB.
ELSE
SET @intResult = -1

RETURN @intResult
END

--=== CODE ENDS====

--
Cheers
Si
.



Relevant Pages

  • Re: Trend Analysis.
    ... I just want some sort of idea of how many calls were open on ... Regarding the count for each member of staff, that may be something I have ... >> I need to do a retrospective analysis of the number of Helpdesk Calllogs ... >> Bill. ...
    (microsoft.public.access.queries)
  • Re: How could I have lived so long without XM Radio?
    ... some sort of IC. ... each thought the other was a member. ... "regulars", some of whom have met in person, and many who haven't. ... folks in this newsgroup tend to treat each other as real ...
    (rec.music.makers.guitar.acoustic)
  • Re: "Sorting" assignment
    ... it to every other member, throwing each compared member into bin A or ... bin B depending on whether it is greater than, ... If the array is already sorted, this means that you end up ... quick sort is fast but not for ordered ...
    (comp.programming)
  • Re: How could I have lived so long without XM Radio?
    ... some sort of IC. ... folks looking over their shoulders for evidence of the imaginary "IC" each ... thought the other was a member. ... "regulars", some of whom have met in person, and many who haven't. ...
    (rec.music.makers.guitar.acoustic)
  • Re: How could I have lived so long without XM Radio?
    ... some sort of IC. ... It's also kinda fun to read that two folks ... the other was a member. ... Yes, it's true Wilbur. ...
    (rec.music.makers.guitar.acoustic)