Before we give our users any access to our application, we need to consider the roles that we want our users to have.
One Year "Roles" into the Next
So what is a role? Supposedly, roles came about in SQL Server 2000 when the Windows team suggested that they would change the name of the NT groups to roles. The SQL Server team thought they would be proactive and changed the name to agree with the changes that the Windows team was going to implement, but alas Windows 2000 was released with groups instead of roles. Aww well guys, there is always next year!
A role is similar in concept to the idea of a group in NT. We can assign permissions to either a role or an individual user. If we assign permissions to a role, then every user that we add to that role will inherit the permissions from the role.
Inherit is the capability of an object (in this case a user) to derive all the attributes from another object (in this case a role).
By default, SQL Server 2000 has 10 basic database roles defined. These roles all allow for different levels of access to the database.
There is one very special role, however: the Public role. Every single user who is defined within the database belongs to the Public role. The Public role has almost no permissions except the very basic ones that allow a user to make a connection to the database and so forth. This role has some basic rules around it.
It is contained within every database.
It cannot be dropped.
Every user belongs to the Public role, including the sa account.
Because every user belongs to the role by default, you cannot add or remove users from the role.
The permissions that the Public role has, as mentioned, are very limited. You can alter this, but buyers beware. Because every user belongs to the role, you might accidentally assign too much control to someone who does not know how to use it.
So what sort of permissions does each role have?
db_owner—This is the most God-like role that a user can have. This role allows a user complete control over the database. The sa user is a member of the dbo_owner role; hence the reason the sa account can do anything it wants to the database.
db_securityadmin— This role allows a user to manage all the roles and members of those roles. This role can also manage assigning permissions to the roles. If we had a user that we trusted to manage the security of our database but didn't need full control, we would assign them this role.
db_accessadmin—This role is used to give a user the rights to add or remove users within the database. As with our db_securityadmin role, we would normally grant the user this role as well to ensure that they can give users the necessary access they need.
db_ddladmin—This role allows a user to manipulate all the objects within the database. For example, a user can create, modify, or drop the database's objects. This role has permission to run all SQL Server 2000 DDLs.
db_backupoperator— This role allows the users assigned to the role to perform database backups.
db_datawriter—This role allows a user to modify the data in all the user tables defined within the database.
db_datareader—This role allows a user to view the data from all the user tables defined within the database.
db_denydatawriter—This role prevents a user from modifying any of the data in the user tables within the database.
db_denydatareader—This role prevents a user from viewing any of the data in the user tables within the database.
A user can belong to none, one, or many of these roles. If a user belongs to more than one role, the user will have the collective of all the permissions of the roles with respect to the rules of precedence.
For example, if our user, SQLSpyNetUser, belonged to the db_datawriter and the db_backupoperator roles, they would be able to not only modify data within a table, but also perform database backups.
However, if our SQLSpyNetUser user belonged to the db_denydatawriter and the db_datawriter roles, then our user wouldn't be able to update data within our tables. What? Why? Because denying access to a database is more restrictive than granting access, and denying access has a higher precedence than granting access.
So be careful when assigning roles to your users. You might accidentally restrict users from a table or other database object that they really need access to.
Assigning Roles to the SQL Server Instance
Not only can we assign roles for our users within our SQLSpyNet database, but we can also assign users roles for the whole SQL Server 2000 instance. This allows us greater flexibility in deployment, especially in a large site. This means that we can allow only certain people the rights to, for example, back up our database. With this we can then ensure that no one tries to perform a backup without fully understanding what he or she is doing!
So what are the server roles that SQL Server 2000 has available?
You can view the server roles for your instance of SQL Server 2000 under Security, Server Roles. All the current server roles installed for your instance of SQL Server 2000 will be within the Server Roles folder.
sysadmin—This role has full control over the whole instance of SQL Server 2000. This is the role to which the sa account belongs.
securityadmin—This role can create and manage logins for the server.
serveradmin—This role can set the configuration options of the instance of SQL Server 2000. This role also has the ability to shut down the server.
setupadmin—This role has the ability to manage startup procedures and linked servers.
processadmin—This role has the ability to manage the processes that SQL Server 2000 has running. This means that this role can issue the KILL command, which finishes a user's session. If the user has any open transactions, their transaction is rolled back before the session is terminated.
diskadmin—This role can manage the files on disk, for example filegroups.
dbcreator—This role can create, alter, and drop databases.
Most of these roles allow members of the role to add other users to the role. For example, if our SQLSpyNetUser belonged to the dbcreator role, he could then add any other users to the role.
But be careful because not all roles allow the addition of new members to the role. For example, the db_datawriter role does not allow users to add new members to the role.
Okay, I know there are some actions that these roles perform that we haven't discussed yet, but hey, we will get there!
What happens when these roles do not really suit our business rules? Let's take a look at some options now.
Creating a Role Model
If I have 10 new users but am not really sure that I want to grant them as much access (or as little) as the built-in server/database roles provide, what can I do?
SQL Server 2000 allows us to create our own roles. If all of our users are to have similar access, the best idea is to create a role, add each user to the role, and then grant and deny the permissions on each object that they will be allowed to perform.
That sounds really cool! The perfect solution for my problem!
However, we currently have only one user. Is it really worth creating a whole role for just one user?
Well luckily for us, SQL Server 2000 allows us to grant/deny permissions on every object within the database to a single user/users. So, for example we might allow our SQLSpyNetUser to alter addresses in the Address table (SELECT, INSERT, UPDATE, and DELETE), but prevent them from altering any of the Spies details.
This sort of flexibility is available to us within SQL Server 2000 (and in fact all previous versions), but SQL Server 2000 also allows us to prevent our SQLSpyNetUser from viewing the AnnualSalary column in the Spy table! We have that fine of granularity control over the permissions that our users have.
For us to leverage the best from our application, we are now going to create a role for our database and then assign our user accounts to the role. Our role will be assigned basic permissions that allow our users assigned to the role to perform some tasks against our database.
First let's create the database role.
In Enterprise Manager, go to the SQLSpyNet database and then navigate your way to the Roles folder.
When you are there, right-click and select New Database Role. This will give you a screen similar to that shown in Figure 1.
Figure 1 Creating a new role for our SQLSpyNet database.
Like most database objects, we need to give our role a name with which SQL Server 2000 can use to identify the object. Let's give our new role a name of SQLSpyNetRole. Once again you can name this anything you like, but it is better to use a descriptive name.
Select Standard Role for the database role type, but notice that SQL Server offers these two roles:
Standard role—This type of role is generally similar to the groups in Windows NT. This role can contain users, and permissions can be applied. When a user connects (and they are a member of the role) they inherit the permissions of that role (as well as any other permissions the user might have).
Application role—These are special roles within SQL Server 2000 because they do not contain any members (or users). In some ways you can think of these special roles as user accounts. Application roles act like users because they require a password to be activated. However, when the role is activated (by supplying a password and using the sp_setapprole stored procedure), the connection loses all the permissions that the users, roles, and NT Groups have while the connection is active. It becomes the responsibility of the application to take over the task of user authentication, but because SQL Server 2000 must authenticate the application when it establishes a connection, the application must supply a password.
The next thing that we need to do is add our SQLSpyNetUser to the role, so click the Add button and you will see a screen similar to that shown in Figure 2.
Figure 2 Assigning our SQLSpyNetUser to the SQLSpyNetRole within Enterprise Manager.
This screen contains a list of our current users in the database, but because we have only one, this is all that is shown! If you have more than one user, simply click the users you want to add.
And there we have it! Our user is now a member of the SQLSpyNetRole and will inherit all the rights that the role has (which are none at the moment!).
Do not close the Database Role Properties window just yet! You can launch the Permissions window from this one, and we are going to assign the permissions very soon!
Now let's have a go at granting our SQLSpyNetRole some basic permissions within the database. There are two ways to accomplish this task—either through code or through using the graphical tools in Enterprise Manager. This time, however, we are going to use the graphical tools in Enterprise Manager. I just heard you say, "Whew!"