Giving different users different Permissions on database - sql-server

I have a table containing my user (Admins) and another table for other accounts.
each of these accounts are in a accountsgroup table.
I want to give a list of accountsgroups access to a user. This access is different for each user.
Something like this:
Users:
user1
user2
accountsGroups:
group1
group2
group3
group4
user1 has access to group1 and group2
user2 has access to group1 and group3
How to give them these permissions?
Do I have to add another table?

One way to solve your problem is to add a third table.
This table is sometimes called an "associative table", or "association table".
It can look a bit like this: useraccountgroups = { id, user, group }.
The records, then, will look like this: (1, "user1", "group1"), (2, "user1", "group2").
Hope this helps.

Related

Database link in oracle

I have a USER1 in DATABASE1 and USER2 and USER3 in DATABASE2. I am given the credentials of USER2 from which i can select only few tables on USER3. Now the requirement is, i have to create a databaselink in USER1 to query the tables in USER3. Can someone help, how to create the database link for this type of requirement?
Here's one options: in USER2 schema create views to tables owned by USER3:
create view v_tab1 as select * from user3.tab1;
As USER1, create a database link to USER2:
create database link dbl_user2
connect to user2
identified by its_password
using 'database2_alias';
Now you can access USER2 objects, which includes both tables and views:
select * from tabx#dbl_user2; -- selects from USER2's table
select * from v_tab1#dbl_user2; -- selects from USER3's table, via a view owned by USER2

Simple database design

I am so poor on database design. I want to ask you to make sure !
Example I have two table look like this:
1) 2)
tb_users: | tb_users:
---------- | -------------
- user_id | - user_id
- user_name | - user_name
- role_id | ===============
============ | tb_role:
tb_roles: | --------------
----------- | - role_id
- role_id | - role_name
- role_name | - user_id
============ | ================
Which one is right, 1) or 2)?
Thank for any answer. I really don't understand. If you have any key to remember, please tell me also.
The second one, but read this whole answer please for a comment at the end.
Look at it this way: "Users" can stand alone. They do not need roles. However, "Roles" require users or they have no meaning.
Having said that, I think both are wrong. The third option is to have 2 main tables, and a 3rd table that allows you to join the others.
The User table uniquely identifies users. The Role table identifies roles. The User_Role table says which user has which role.
So something like this:
USER
----
User_Id
User_Name
ROLE
----
Role_Id
Role_Name
USER_ROLE
---------
User_Id
Role_Id
That allows for each user to have as many roles as required, and it allows for roles to be assigned to as many users as required.

Ideas/theories on grouping in SQL Server

I am looking for some help/ideas on how to structure (table wise) infinitely nest groups in SQL.
EX.
group1 will contain a,b,c
group2 will contain d,e,f
group3 will contain h,i,j
group4 will contain k.l.m
groupA will contain group1,group2
groupB will contain group3
groupA1 will contain groupA,groupB,group4
each lowest level group will refer to a list of scans in a different table (in this example lets say group1 group2 group3 and group4 are the lowest level)
this should be able to support an infinite number of groups
I know this is vague but i am trying to find out how to structure and manage something like this...
I am trying for both tables and queries. So far I have this:
Scan Table
((uniqueID),barcode,user,date,group)
Groups Table
(groupID,groupName,groupRef)
but i am having trouble "Creating" GroupA
in terms of queries i would need to know what are the lower level groups and get a list of all items in a group.
Based on your example it looks like a parent-child structure would do it:
CREATE TABLE #ParentChild (Parent VARCHAR(30), Child VARCHAR(30))
INSERT INTO #ParentChild
VALUES
('group1','a'),
('group1','b'),
('group1','c'),
('group2','d'),
('group2','e'),
('group2','f'),
('group3','h'),
('group3','i'),
('group3','j'),
('group4','k'),
('group4','l'),
('group4','m'),
('groupA','group1'),
('groupA','group2'),
('groupB','group3'),
('groupA1','groupA'),
('groupA1','groupB'),
('groupA1','group4')
This will allow you to store an (almost) infinite number of groups. The "limit" depends on the SQL Server version (e.g. SQL Server 2008R2: File size (data): 16 terabytes) which should be good enough ;-)
As for your specific questions:
--"what are the lower level groups?"
--"i.e., give me all the groups, except those that contain another group
SELECT Parent
FROM #ParentChild
EXCEPT
SELECT t1.Parent
FROM #ParentChild t1
INNER JOIN #ParentChild t2
ON t1.Child = t2.Parent
--"get a list of all items in a group"
SELECT Child
FROM #ParentChild
WHERE Parent = #Group
Another option would be to store the data using the hierarchyid hierarchyid data type
You're talking about Hierarchical Data. Sql Server has built-in support for this. You should read through this article:
http://msdn.microsoft.com/en-us/library/bb677173.aspx
That article discusses using the new hierarchyid type, as well as Parent/Child alternative.

What is a sensible approach for enabling/disabling features on a database object?

Say I have a User database table with the regular username, password, email fields. What is a sensible way to add additional boolean fields that enable/disable features for any given user.
e.g.,
user_can_view_page_x
user_can_send_emails
user_can_send_pms
etc
Adding a bunch of boolean columns to the existing user table seems like the wrong way to go.
Yes, I would think that this is the wrong approach.
I would rather create a
User_Features Table
with columns something like
UserID
FeatureName
And check if a given user has the feature in question enabled/entered in the table.
You could even go as far as creating a Users_Groups table, where users are also assosiated with groups and features can be inherited/disallowed from group settings.
I would use three tables.
One is your existing user table:
USER table
----
user_id (pk)
name
email
...
Another is a table containing possible user privileges:
PRIVILEGE table
----
privilege_id (pk)
name
Lastly is a join table containing an entry for each privilege setting for each user:
USER_PRIVILEGE table
----
user_id (pk) (fk)
privilege_id (pk) (fk)
allowed
Here is some sample data for two users, one with the send email privilege and the send pms privilege and another with a view page privilege:
USER data
USER_ID NAME EMAIL
------- ----- -------------------
1 USER1 user1#somewhere.com
2 USER2 user2#somewhere.com
PRIVILEGE data
PRIVILEGE_ID NAME
------------ -----------
1 view_page_x
2 send_email
3 send_pms
USER_PRIVILEGE data
USER_ID PRIVILEGE_ID ALLOWED
------- ------------ -------
1 1 'N'
1 2 'Y'
1 3 'Y'
2 1 'Y'
2 2 'N'
2 3 'N'

Entity Framework 4: Mapping several assocation tables to entities

I am kind of new to Entity Framework and ORMs. I have a simple database schema that is kind of like this.
User:
Id
Name
Group:
Id
Name
Role:
Id
Name
There are many-many between groups and users. Also, there is many-many between users and roles. However, Roles are per group. So we could have the following:
User A belongs to Group 1 with Roles a,b,c and belongs to Group 2, but has Roles d,e,f.
So we have some association tables like so:
UserRoles:
UserId -> User.Id
RoleId -> Role.Id
UserGroups
UserId -> User.Id
GroupId -> Group.Id
GroupRoles:
GroupId -> Group.Id
RoleId -> Role.Id
So, in my entities I want to have a Role entitiy, a User entity with a collection of Roles and a Group entity with a collection of Users and a collection of Roles.
When I load a group, I want to only load the users in that group and only that users roles for that group.
So my question:
In the above example. How do I make it so when I load Group 1, I want to see User A with Roles a,b,c and NOT Roles d,e,f.
Thanks,
JR
You need to call something like Group.User.Roles to get all Roles the user belongs to in the Group. something like:
var group1 = objectContext.Groups.Where(x => x.GroupId == 1);
var userARoles = group1.Users.Where(x => x.UserId == "A").Roles;
Does this help you?

Resources