Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Database Role Membership

Author  Topic 

stephenbaer
Yak Posting Veteran

71 Posts

Posted - 2008-08-19 : 16:50:50
I did some research since posting a previous topic, and I discovered a few things, but I'm still not able to do what I'd like.
I'm looking at several system views, which I assume contain the info I need, but I can't quite figure out how they are related.

The views in question:
sys.database_principals
sys.database_role_members

I've figured out that there are several system SPs that will list all the roles, check role membership, etc. For instance:
EXEC sp_helprolemember 'Sales'will return all members of the 'sales' database role. That's swell, but I want it the other way around.

How do I make a view that lists role membership for a user?


----------------
-Stephen

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-19 : 16:53:07
EXEC sp_helpuser 'UserNameGoesHere'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

stephenbaer
Yak Posting Veteran

71 Posts

Posted - 2008-08-19 : 16:56:22
AHA! Man, that was easier than I thought.
Observe:
---------
SELECT sys.database_principals.name
FROM sys.database_principals AS logins INNER JOIN
sys.database_role_members ON logins.principal_id = sys.database_role_members.member_principal_id INNER JOIN
sys.database_principals ON sys.database_role_members.role_principal_id = sys.database_principals.principal_id
WHERE (sys.database_principals.principal_id < 16384) AND (logins.name = N'sbaer')
---------
Yes, I know the < 16384 isn't really complete. I need to exclude just the reserved ids, but that was a quick test.
One question though...What's with the "N" in (logins.name = N'sbaer')?

----------------
-Stephen
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-19 : 16:58:02
It's to handle unicode data.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

stephenbaer
Yak Posting Veteran

71 Posts

Posted - 2008-08-19 : 16:59:08
um, yours was slighly more elegant. Thanks Tara.

----------------
-Stephen
Go to Top of Page

stephenbaer
Yak Posting Veteran

71 Posts

Posted - 2008-08-19 : 17:40:11
Why does the above work, but not this?
-----------
SELECT sys.database_principals.name
FROM sys.database_principals AS logins INNER JOIN
sys.database_role_members ON logins.principal_id = sys.database_role_members.member_principal_id INNER JOIN
sys.database_principals ON sys.database_role_members.role_principal_id = sys.database_principals.principal_id
WHERE (sys.database_principals.principal_id < 16384) AND (logins.name = CURRENT_USER)
---------

----------------
-Stephen
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-19 : 17:58:44
What does this return:
select current_user

Why don't you just use sp_helpuser?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

stephenbaer
Yak Posting Veteran

71 Posts

Posted - 2008-08-19 : 17:58:44
OHHHH! For me, Current_user returns dbo! hmmm

----------------
-Stephen
Go to Top of Page

stephenbaer
Yak Posting Veteran

71 Posts

Posted - 2008-08-19 : 18:09:33
quote:
Originally posted by tkizer


Why don't you just use sp_helpuser?



Because I can't use it in an IN clause, at least I don't think so...
Also, the front end is odbc and SP's are troublesome, so I'm trying to make a view. What can I say, I'm a noob.
Here's the full query:

ALTER VIEW [dbo].[new]
AS
SELECT dbo.Resident.*, dbo.Facility.FacilityAbbrev
FROM dbo.Facility INNER JOIN
dbo.Resident ON dbo.Facility.FacilityID = dbo.Resident.FacilityID
WHERE FacilityAbbrev
IN (SELECT sys.database_principals.name
FROM sys.database_principals AS logins INNER JOIN
sys.database_role_members ON logins.principal_id = sys.database_role_members.member_principal_id INNER JOIN
sys.database_principals ON sys.database_role_members.role_principal_id = sys.database_principals.principal_id
WHERE (sys.database_principals.principal_id < 16384) AND (logins.name = CURRENT_USER)
))


----------------
-Stephen
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-19 : 18:16:48
Perhaps you should describe what you're trying to do as there is almost certainly a better approach to this.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

stephenbaer
Yak Posting Veteran

71 Posts

Posted - 2008-08-19 : 18:34:20
I'm sure there is...
Ok, here's the long story in the shortest form possible...
We have a database, which contains the names of our clients. All the clients are minors. As such, there are very strict laws concerning need-to-know. Each client resides at a given facility, as indicated by FacilityID and FacilityAbbrev, in the query above. Only the staff members at that facility, and those in the higher echelons should see info on residents at that particulr facility. Staff of other facilities should not. Right now, the front-end goes through some convoluted AD group checking, and creates a string to use as the datasource for forms, etc. Unfortunately, this means I have to create new views for special cases, such as right now, when we are down a supervisor, and a different supervisor temporarily needs access to the other facility's info, so she can fill in. That meant I had to create a new view that shows both facilities, and alter the front end to set that view as the new data source for said person. It was a pain. I thought I might implement row-level security in the database, using roles matching the facilities. Then, when someone needs access to multiple facilities (but not ALL of them), I can just add said person to the corresponding roles, and viola! Then I'll remove them once they don't need the access.

----------------
-Stephen
Go to Top of Page

stephenbaer
Yak Posting Veteran

71 Posts

Posted - 2008-08-20 : 00:43:43
Just as an FYI, the view posted works great---except for me .
If I log on as anyone else, it works! Which brings me to...How do I make current_user return my actual user name instead of dbo?
I know I can use the execute as user thing, but a view won't allow that. Anyone?

----------------
-Stephen
Go to Top of Page
   

- Advertisement -