Click here to Skip to main content
15,885,864 members
Articles / Web Development / ASP.NET

Eucalypto - ASP.NET CMS Library using NHibernate

Rate me:
Please Sign up or sign in to vote.
4.84/5 (36 votes)
10 Jun 2009MIT24 min read 318.9K   4.6K   260  
An ASP.NET server library for creating CMS website (forums, articles/wiki, news, users/roles, ...), using NHibernate for data access.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using System.Configuration;

namespace Eucalypto.Roles
{
    /// <summary>
    /// A implementation of a System.Web.Security.RoleProvider class that use the Eucalypto classes.
    /// See MSDN System.Web.Security.RoleProvider documentation for more informations about RoleProvider.
    /// </summary>
    public class EucalyptoRoleProvider : System.Web.Security.RoleProvider
    {
        private Configuration mConfiguration;

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            if (config == null)
                throw new ArgumentNullException("config");

            if (name == null || name.Length == 0)
                name = "EucalyptoRoleProvider";

            base.Initialize(name, config);


            this.mProviderName = name;
            this.mApplicationName = ExtractConfigValue(config, "applicationName", Configuration.DEFAULT_APP); //System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath

            string connName = ExtractConfigValue(config, "connectionStringName", null);
            ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings[connName];
            if (connectionStringSettings == null ||
                connectionStringSettings.ConnectionString == null ||
                connectionStringSettings.ConnectionString.Length == 0)
            {
                throw new EucalyptoException("Connection string cannot be blank.");
            }

            mConfiguration = new Configuration(connectionStringSettings);


            // Throw an exception if unrecognized attributes remain
            if (config.Count > 0)
            {
                string attr = config.GetKey(0);
                if (!String.IsNullOrEmpty(attr))
                    throw new System.Configuration.Provider.ProviderException("Unrecognized attribute: " +
                    attr);
            }
        }

        /// <summary>
        /// A helper function to retrieve config values from the configuration file and remove the entry.
        /// </summary>
        /// <returns></returns>
        private string ExtractConfigValue(System.Collections.Specialized.NameValueCollection config, string key, string defaultValue)
        {
            string val = config[key];
            if (val == null)
                return defaultValue;
            else
            {
                config.Remove(key);

                return val;
            }
        }


        #region Properties
        private string mProviderName;
        public string ProviderName
        {
            get { return mProviderName; }
            set { mProviderName = value; }
        }
        private string mApplicationName;
        public override string ApplicationName
        {
            get { return mApplicationName; }
            set { mApplicationName = value; }
        }
        #endregion

        #region Methods
        private void LogException(Exception exception, string action)
        {
            Log.Error(GetType(), "Exception on " + action, exception);
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                RoleDataStore roleStore = new RoleDataStore(transaction);

                //Find the roles
                Role[] roles = new Role[roleNames.Length];
                for (int i = 0; i < roleNames.Length; i++)
                {
                    //Find the role
                    Role role = roleStore.FindByName(ApplicationName, roleNames[i]);
                    if (role == null)
                        throw new RoleNotFoundException(roleNames[i]);

                    roles[i] = role;
                }


                UserInRoleDataStore usersInRolesStore = new UserInRoleDataStore(transaction);
                foreach (string userName in usernames)
                {
                    foreach (Role role in roles)
                    {
                        UserInRole userInRole = new UserInRole(ApplicationName, userName, role.Name);

                        usersInRolesStore.Insert(userInRole);
                    }
                }

                transaction.Commit();
            }
        }

        public override void CreateRole(string roleName)
        {
            //Check required for MSDN
            if (roleName == null || roleName == "")
                throw new System.Configuration.Provider.ProviderException("Role name cannot be empty or null.");
            if (roleName.IndexOf(',') > 0)
                throw new ArgumentException("Role names cannot contain commas.");
            if (RoleExists(roleName))
                throw new System.Configuration.Provider.ProviderException("Role name already exists.");

            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                RoleDataStore roleStore = new RoleDataStore(transaction);

                roleStore.Insert(new Role(ApplicationName, roleName));

                transaction.Commit();
            }
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                RoleDataStore roleStore = new RoleDataStore(transaction);
                UserInRoleDataStore userInRoleDataStore = new UserInRoleDataStore(transaction);

                //Find role
                Role role = roleStore.FindByName(ApplicationName, roleName);
                if (role == null)
                    throw new RoleNotFoundException(roleName);

                IList<UserInRole> listUserInRole = userInRoleDataStore.FindForRole(ApplicationName, roleName);

                if (throwOnPopulatedRole && listUserInRole.Count > 0)
                    throw new System.Configuration.Provider.ProviderException("Cannot delete a populated role.");

                foreach (UserInRole ur in listUserInRole)
                    userInRoleDataStore.Delete(ur.Id);

                roleStore.Delete(role.Id);

                transaction.Commit();
            }

            return true;
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            List<string> userNames = new List<string>();

            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                UserInRoleDataStore userInRoleDataStore = new UserInRoleDataStore(transaction);

                IList<UserInRole> listUserInRole = userInRoleDataStore.FindForRole(ApplicationName, roleName, usernameToMatch);

                foreach (UserInRole ur in listUserInRole)
                {
                    userNames.Add(ur.UserName);
                }
            }

            return userNames.ToArray();
        }

        public override string[] GetAllRoles()
        {
            List<string> rolesNames = new List<string>();

            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                RoleDataStore roleDataStore = new RoleDataStore(transaction);

                IList<Role> list = roleDataStore.FindAll(ApplicationName);

                foreach (Role ur in list)
                {
                    rolesNames.Add(ur.Name);
                }
            }

            return rolesNames.ToArray();
        }

        public override string[] GetRolesForUser(string username)
        {
            List<string> roleNames = new List<string>();

            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                UserInRoleDataStore userInRoleDataStore = new UserInRoleDataStore(transaction);

                IList<UserInRole> listUserInRole = userInRoleDataStore.FindForUser(ApplicationName, username);

                foreach (UserInRole ur in listUserInRole)
                {
                    roleNames.Add(ur.RoleName);
                }
            }

            return roleNames.ToArray();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            List<string> userNames = new List<string>();

            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                UserInRoleDataStore userInRoleDataStore = new UserInRoleDataStore(transaction);

                IList<UserInRole> listUserInRole = userInRoleDataStore.FindForRole(ApplicationName, roleName);

                foreach (UserInRole ur in listUserInRole)
                {
                    userNames.Add(ur.UserName);
                }
            }

            return userNames.ToArray();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                UserInRoleDataStore userInRoleDataStore = new UserInRoleDataStore(transaction);

                if (userInRoleDataStore.Find(ApplicationName, username, roleName) != null)
                    return true;
                else
                    return false;
            }
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                UserInRoleDataStore usersInRolesStore = new UserInRoleDataStore(transaction);

                foreach (string userName in usernames)
                {
                    foreach (string roleName in roleNames)
                    {
                        UserInRole userInRole = usersInRolesStore.Find(ApplicationName, userName, roleName);
                        if (userInRole == null)
                            throw new UserInRoleNotFoundException(userName, roleName);

                        usersInRolesStore.Delete(userInRole.Id);
                    }
                }

                transaction.Commit();
            }
        }

        public override bool RoleExists(string roleName)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                RoleDataStore store = new RoleDataStore(transaction);
                if (store.FindByName(ApplicationName, roleName) != null)
                    return true;
                else
                    return false;
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions