Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Determine unique combinations of permissions assigned to users Expand / Collapse
Author
Message
Posted Friday, October 12, 2012 8:27 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 31, 2014 7:42 PM
Points: 39, Visits: 221
I need to determine the unique combinations of group permissions assigned to users. Currently, permissions are assigned to a user. I'm working on changing our model to assign permissions to a role, and assigning a role to a user.

I believe I need to generate a query to PIVOT/Select DISTINCT with Row_Number/UNPIVOT the data, but I haven't been able to structure the query.

We have nearly 15000 users and over 1000 groups, so I can't manually type the PIVOT/UNPIVOT column names. On the plus side, I don't care what the intermediate columns names will be as they'll only be used in this statement.

I'm starting with:
Name GroupName
Tom Admin
Tom Accounting
Tom Support
Tom Legal
Dick Accounting
Dick Legal
Harry Support
Mary Support
NULL Restricted


and need to finish with this
RoleNum GroupName
1 Admin
1 Accounting
1 Support
1 Legal
2 Accounting
2 Legal
3 Support

Notice the Support group only shows up once even though two people belong to that group, and the Restricted group does not appear because no one is currently assigned.

I've created some sample tables to play with if anyone is interested in helping.
CREATE TABLE [dbo].[groups](
[GroupID] [int] IDENTITY(1,1) NOT NULL,
[GroupName] [varchar](50) NULL
) ON [PRIMARY]

GO
CREATE TABLE [dbo].[person](
[PersonID] [int] IDENTITY(1,1) NOT NULL,
[NAME] [varchar](10) NULL
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[PersonGroups](
[PersonID] [int] NULL,
[GroupID] [int] NULL
) ON [PRIMARY]

GO

SET IDENTITY_INSERT [dbo].[groups] ON;

BEGIN TRANSACTION;
INSERT INTO [dbo].[groups]([GroupID], [GroupName])
SELECT 1, N'Admin' UNION ALL
SELECT 2, N'Accounting' UNION ALL
SELECT 3, N'Support' UNION ALL
SELECT 4, N'Legal' UNION ALL
SELECT 5, N'Restricted'
COMMIT;
RAISERROR (N'[dbo].[groups]: Insert Batch: 1.....Done!', 10, 1) WITH NOWAIT;
GO

SET IDENTITY_INSERT [dbo].[groups] OFF;

SET IDENTITY_INSERT [dbo].[person] ON;

BEGIN TRANSACTION;
INSERT INTO [dbo].[person]([PersonID], [NAME])
SELECT 1, N'Tom' UNION ALL
SELECT 2, N'Dick' UNION ALL
SELECT 3, N'Harry' UNION ALL
SELECT 4, N'Mary'
COMMIT;
RAISERROR (N'[dbo].[person]: Insert Batch: 1.....Done!', 10, 1) WITH NOWAIT;
GO

SET IDENTITY_INSERT [dbo].[person] OFF;

BEGIN TRANSACTION;
INSERT INTO [dbo].[PersonGroups]([PersonID], [GroupID])
SELECT 1, 1 UNION ALL
SELECT 1, 2 UNION ALL
SELECT 1, 3 UNION ALL
SELECT 1, 4 UNION ALL
SELECT 2, 2 UNION ALL
SELECT 2, 4 UNION ALL
SELECT 3, 3 UNION ALL
SELECT 4, 3
COMMIT;
RAISERROR (N'[dbo].[PersonGroups]: Insert Batch: 1.....Done!', 10, 1) WITH NOWAIT;
GO


Post #1372392
Posted Saturday, October 13, 2012 6:16 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 5:27 AM
Points: 369, Visits: 1,214
If dynamic pivot will solve your case, here it is: http://sqlwithmanoj.wordpress.com/2011/01/25/dynamic-pivot/
But I think you should rethink your concept.

-- users with object-level permissions in current db
sp_helprotect NULL, NULL, NULL, 'o'

-- users with server-level permissions in current db
sp_helprotect NULL, NULL, NULL, 's'

-- Object-level permissions for all databases
IF OBJECT_ID('tempdb..#p','U') is not null DROP TABLE #P
create table #p
( DBname nvarchar(500), Owner sysname, Object sysname, Grantee sysname, Grantor sysname,
ProtectType varchar(100), Action varchar(100), [Column] varchar(100)
)
EXEC sp_msforeachdb '
INSERT INTO #p(Owner, Object, Grantee, Grantor, ProtectType, Action, [Column]) exec ?.dbo.sp_helprotect NULL, NULL, NULL, ''o''
UPDATE #p SET DBName=''?'' WHERE DBName IS NULL
'
select * from #p

-- Roles granted
IF OBJECT_ID('tempdb..#g','U') is not null DROP TABLE #G
CREATE TABLE #g
( DBName varchar(100), UserName nvarchar(500), GroupName nvarchar(500), LoginName nvarchar(500),
DefDBName nvarchar(500),
DefSchemaName nvarchar(500),
UserID int, SID image
)
exec sp_msforeachdb
'
insert into #g(UserName, GroupName, LoginName,
DefDBName,
DefSchemaName,
UserID, SID) EXEC sp_helpuser
UPDATE #g SET DBName=''?'' WHERE DBName IS NULL
'
SELECT * FROM #G

It's straightforward to select distinct from those.

HTH,
Vedran


_____________________________________________________
Microsoft Certified Master: SQL Server 2008
XDetails Addin - for SQL Developers
blog.sqlxdetails.com - Transaction log myths
Post #1372477
Posted Sunday, October 14, 2012 6:44 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:49 PM
Points: 3,648, Visits: 5,326
No need to use dynamic SQL or a PIVOT for this.

;WITH GroupsGrouped AS (
SELECT DISTINCT Groups=STUFF((
SELECT ',' + CAST(GroupID AS VARCHAR(5))
FROM PersonGroups b
WHERE a.PersonID = b.PersonID
ORDER BY GroupID
FOR XML PATH('')), 1, 1, '')
FROM personGroups a
GROUP BY PersonID)
SELECT RoleID, GroupName
FROM (
SELECT RoleID=ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
,Groups
FROM GroupsGrouped) a
CROSS APPLY dbo.DelimitedSplit8K(Groups, ',') b
INNER JOIN Groups c ON c.GroupID = b.Item


The trick is to create a delimited string (I chose comma for my delimiter) of each combination of groups for a user. You can then apply a ROW_NUMBER() to get the RoleID and unravel the whole thing using a delimited string splitter (DelimitedSplit8K), like the one popularized here by Jeff Moden: http://www.sqlservercentral.com/articles/Tally+Table/72993/



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1372548
Posted Tuesday, October 16, 2012 10:06 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 31, 2014 7:42 PM
Points: 39, Visits: 221
@dwain.c

Excellent, that is just what I needed.

After working with it for a bit this morning, I've determined I need to assign the groups string in a checksum rather than a row_number. Now I don't need to worry about row_number assigments changing erratically when new group combinations are used. The checksum values won't be affected by permissions changes.

Thanks again.
Wes
Post #1373381
Posted Tuesday, October 16, 2012 6:38 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:49 PM
Points: 3,648, Visits: 5,326
whenriksen (10/16/2012)
@dwain.c

Excellent, that is just what I needed.

After working with it for a bit this morning, I've determined I need to assign the groups string in a checksum rather than a row_number. Now I don't need to worry about row_number assigments changing erratically when new group combinations are used. The checksum values won't be affected by permissions changes.

Thanks again.
Wes


Happy to hear that my suggestion seems to have gotten you on the right track.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1373590
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse