Building a MVC2 Template, Part 9, Adding a Custom Role Provider

1 Comment

In this installment we’ll add the specifications and the skeleton code for a custom role provider. Start by adding a class file to the Providers folder in the Nehemiah.Specs folder. Give the file a name of RoleProviderSpecs.cs.

Here is our skeleton custom role provider. Just like the membership provider, these specs will be expanded upon in the next post about the custom role provider.

using Machine.Specifications;
using Nehemiah.Providers;
using System.Web.Security;

namespace Nehemiah.Specs.Providers
{

    [Subject("Role Provider")]
    public class add_users_to_roles_method
    {
        It should_add_multiple_users_to_multiple_roles;
    }

    [Subject("Role Provider")]
    public class create_role_method
    {
        It should_create_a_new_role;
    }

    [Subject("Role Provider")]
    public class delete_role_method
    {
        It should_delete_the_specified_role;
    }

    [Subject("Role Provider")]
    public class find_users_in_role_method
    {
        It should_find_all_users_in_the_specified_role;
    }

    [Subject("Role Provider")]
    public class get_all_roles_method
    {
        It should_return_an_array_of_all_roles;
    }

    [Subject("Role Provider")]
    public class get_roles_for_user_method
    {
        It should_return_an_array_of_all_roles_for_the_specified_user;
    }

    [Subject("Role Provider")]
    public class get_users_in_role_method
    {
        It should_return_an_array_of_all_users_in_the_specified_role;
    }

    [Subject("Role Provider")]
    public class is_user_in_role_method
    {
        It should_return_true_if_the_user_has_the_specified_role;
    }

    [Subject("Role Provider")]
    public class remove_users_from_roles_method
    {
        It should_remove_one_or_more_users_from_the_specified_roles;
    }

    [Subject("Role Provider")]
    public class role_exists_method
    {
        It should_return_true_if_the_role_exists;
    }

}

SolutionExplorer09-01

The screen shot above shows that we’ve added the RoleProviderSpecs.cs file to the Providers folder of the Nehemiah.Specs project and we’ve added a file named RoleProvider.cs to the Nehemiah.Providers project.

The code for the RolerProvider.cs file is listed below.

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web.Security;

namespace Nehemiah.Providers
{
    public class NehemiahRoleProvider : RoleProvider
    {

        #region - Properties -

        // The name of the application using the role provider. ApplicationName is used to scope
        // role data so that applications can choose whether to share role data with other applications.
        // This property can be read and written.
        public override string ApplicationName { get; set; }

        #endregion

        #region - Methods -

        public override void Initialize(string name, NameValueCollection config)
        {
            throw new NotImplementedException();
        }

        // Takes, as input, a list of user names and a list of role names and adds the specified users to
        // the specified roles.
        // AddUsersToRoles throws a ProviderException if any of the user names or role names do not exist.
        // If any user name or role name is null (Nothing in Visual Basic), AddUsersToRoles throws an
        // ArgumentNullException. If any user name or role name is an empty string, AddUsersToRoles throws
        // an ArgumentException.
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        // Takes, as input, a role name and creates the specified role.
        // CreateRole throws a ProviderException if the role already exists, the role name contains a comma,
        // or the role name exceeds the maximum length allowed by the data source.
        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        // Takes, as input, a role name and a Boolean value that indicates whether to throw an exception if there
        // are users currently associated with the role, and then deletes the specified role.
        // If the throwOnPopulatedRole input parameter is true and the specified role has one or more members,
        // DeleteRole throws a ProviderException and does not delete the role. If throwOnPopulatedRole is false,
        // DeleteRole deletes the role whether it is empty or not.
        //
        // When DeleteRole deletes a role and there are users assigned to that role, it also removes users from the role.
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        // Takes, as input, a search pattern and a role name and returns a list of users belonging to the specified role
        // whose user names match the pattern. Wildcard syntax is data-source-dependent and may vary from provider to
        // provider. User names are returned in alphabetical order.
        // If the search finds no matches, FindUsersInRole returns an empty string array (a string array with no elements).
        // If the role does not exist, FindUsersInRole throws a ProviderException.
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        // Returns the names of all existing roles. If no roles exist, GetAllRoles returns an empty string array (a string
        // array with no elements).
        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        // Takes, as input, a user name and returns the names of the roles to which the user belongs.
        // If the user is not assigned to any roles, GetRolesForUser returns an empty string array
        // (a string array with no elements). If the user name does not exist, GetRolesForUser throws a
        // ProviderException.
        public override string[] GetRolesForUser(string username)
        {
            throw new NotImplementedException();
        }

        // Takes, as input, a role name and returns the names of all users assigned to that role.
        // If no users are associated with the specified role, GetUserInRole returns an empty string array (a string array with
        // no elements). If the role does not exist, GetUsersInRole throws a ProviderException.
        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        // Takes, as input, a user name and a role name and determines whether the specified user
        // is associated with the specified role.
        // If the user or role does not exist, IsUserInRole throws a ProviderException.
        public override bool IsUserInRole(string username, string roleName)
        {
            throw new NotImplementedException();
        }

        // Takes, as input, a list of user names and a list of role names and removes the specified users from the specified roles.
        // RemoveUsersFromRoles throws a ProviderException if any of the users or roles do not exist, or if any user specified
        // in the call does not belong to the role from which he or she is being removed.
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        // Takes, as input, a role name and determines whether the role exists.
        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }

        #endregion

    }   // End Class

}       // End Namespace

NehemiahSpecs09-01

Save your code, compile, and open the Report.html file. You should now see that we have 63 specifications and 27 unimplemented specs.

Commit your changes. Open http://localhost:8080 and check for a successful TeamCity build.

References

Shout it


Kick It on DotNetKicks.com
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

One Comment (+add yours?)

Leave a Reply

Comment moderation is enabled. Your comment may take some time to appear.