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
 query to use maskvalues and provide access permiss

Author  Topic 

jimoomba
Yak Posting Veteran

90 Posts

Posted - 2011-09-30 : 22:27:42
Hi All,

Iam having users table with a column of mask values 1--to view the only first page of the application, 2--view 2nd page..16--have admin rights ..etc
Could anyone provide me the sql query where i can give the rights to the users based n the mask values :

eg: 1. Suppose if i want to provide access rights to a user for first and second pages only....
2. What if i want to provide the admin rights to the user

Please send me the sql query

Thanks a lot:)

rams

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-10-01 : 00:16:46
You store the bitmask in an int or bigint field, as the sum of their permissions.

The value is returned in a query and typically examined in the front end with bitwise operators, but can be done in SQL too.

Say a value of:
1 means "Can read page 1"
2 means "Can read page 2"
4 means "Can read page 3"

So someone who can read page 1 and 3 (but not 2) gets a value of 5.

You check who can do what with a bitwise and (&). Any non-zero value is true. Here's a simple sample:
-- create user table
Create Table #users (id int identity (1, 1), name varchar(32));
insert #users (name) values ('Russell');
insert #users (name) values ('jimoomba');
insert #users (name) values ('Webfred');
insert #users (name) values ('Visakh');
GO

-- permission table
Create Table #permissions (id int, description varchar(16));
insert #permissions values(1, 'read 1');
insert #permissions values(2, 'read 2');
insert #permissions values(4, 'read 3');
insert #permissions values(8, 'read 4');
GO

-- map users to permissions
Create Table #userPermissions (userid int, permissionID int)
insert #userpermissions values(1, 1);
insert #userpermissions values(2, 2);
insert #userpermissions values(3, 5);
insert #userpermissions values(4, 15);
GO

-- query to see who can read page 3
SELECT u.name, 4 & up.permissionID as [can read page 3]
FROM #users u
JOIN #userPermissions up
On u.id = up.userid;
GO
-- any non-zero value is TRUE

drop table #userPermissions
drop table #permissions
drop table #users
Go to Top of Page

jimoomba
Yak Posting Veteran

90 Posts

Posted - 2011-10-01 : 14:14:45
Hi,

Thanks for your valuable information and i would like to know how to make the sum of bit masks for permissions using sql query in such a way that SUM of 1 and 3 should return a value of 3 as already 1 value is already there in 3.
Could yu please provide me the sql query if possible.

Thanks
Rams

rams
Go to Top of Page
   

- Advertisement -