角色成员资格信息提供类(HibernateRoleProvider.cs)
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using System.Collections.Specialized;
using System.Configuration.Provider;
using Guushuuse.SalaryPrj.Security.Service;

namespace Guushuuse.SalaryPrj.Security.Providers


{

/**//// <summary>
/// 对 ASP.NET 应用程序的角色成员资格信息在数据库中的存储进行管理。
/// </summary>
public class HibernateRoleProvider : RoleProvider

{
private string _applicationName;


属性#region 属性


/**//// <summary>
/// 获取或设置要存储和检索其角色信息的应用程序的名称
/// </summary>
public override string ApplicationName

{
get

{
return _applicationName;
}
set

{
if (String.IsNullOrEmpty(value))

{
throw new ArgumentNullException("Provider application name not null.");
}
if (value.Length > 255)

{
throw new ProviderException("Provider application name too long.");
}
_applicationName = value;

}
}

#endregion 属性


方法#region 方法


/**//// <summary>
/// 利用在 ASP.NET 应用程序的配置文件中指定的属性值初始化角色提供程序
/// </summary>
/// <param name="name"></param>
/// <param name="config"></param>
public override void Initialize(string name, NameValueCollection config)

{
if (config == null)

{
throw new ArgumentNullException("config");
}

if (String.IsNullOrEmpty(name))

{
name = "HibernateRoleProvider";
}

if (String.IsNullOrEmpty(config["description"]))

{
config.Remove("description");
config.Add("description", "GFC.Security Role Provider");
}

base.Initialize(name, config);


this._applicationName = config["applicationName"];
if (String.IsNullOrEmpty(this._applicationName))

{
this._applicationName = SecUtility.GetDefaultAppName();
}

if (this._applicationName.Length > 255)

{
throw new ProviderException("Provider application name is too long, max length is 255.");
}

if (!ServiceLocator.ApplicationService.ApplicationExists(this._applicationName))

{
ServiceLocator.ApplicationService.CreateApplication(this._applicationName);
}

config.Remove("applicationName");


if (config.Count > 0)

{
string attribUnrecognized = config.GetKey(0);
if (!String.IsNullOrEmpty(attribUnrecognized))

{
throw new ProviderException("Provider unrecognized attribute: " + attribUnrecognized);
}
}
}


/**//// <summary>
/// 将指定用户名添加到每个指定的角色
/// </summary>
/// <param name="usernames"></param>
/// <param name="roleNames"></param>
public override void AddUsersToRoles(string[] usernames, string[] roleNames)

{
SecUtility.CheckArrayParameter(ref usernames, true, true, true, 255, "usernames");
SecUtility.CheckArrayParameter(ref roleNames, true, true, true, 255, "roleNames");

int status = ServiceLocator.RoleService.AddUsersToRoles(this._applicationName, usernames, roleNames);

if (status != 0)

{
string errText = this.GetExceptionText(status);

throw new ProviderException(errText);
}

}


/**//// <summary>
/// 将新的角色添加到角色数据库。
/// </summary>
/// <param name="roleName"></param>
public override void CreateRole(string roleName)

{
SecUtility.CheckParameter(ref roleName, true, true, true, 255, "roleName");

int status = ServiceLocator.RoleService.CreateRole(this._applicationName, roleName);

if (status != 0)

{
string errText = this.GetExceptionText(status);

throw new ProviderException(errText);
}
}


/**//// <summary>
/// 从角色数据库移除一个角色
/// </summary>
/// <param name="roleName"></param>
/// <param name="throwOnPopulatedRole"></param>
/// <returns></returns>
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)

{
SecUtility.CheckParameter(ref roleName, true, true, true, 255, "roleName");

int status = ServiceLocator.RoleService.DeleteRole(this._applicationName, roleName, throwOnPopulatedRole);

if (status != 0)

{


return false;
}

return true;
}


/**//// <summary>
/// 获取属于某个角色且与指定的用户名相匹配的用户名的数组
/// </summary>
/// <param name="roleName"></param>
/// <param name="usernameToMatch"></param>
/// <returns></returns>
public override string[] FindUsersInRole(string roleName, string usernameToMatch)

{
SecUtility.CheckParameter(ref roleName, true, true, true, 255, "roleName");
SecUtility.CheckParameter(ref usernameToMatch, true, true, false, 255, "usernameToMatch");

return ServiceLocator.RoleService.FindUsersInRole(this._applicationName, roleName, usernameToMatch);
}


/**//// <summary>
/// 获取应用程序的所有角色的列表
/// </summary>
/// <returns></returns>
public override string[] GetAllRoles()

{
return ServiceLocator.RoleService.GetAllRoles(this._applicationName);
}


/**//// <summary>
/// 获取一个用户所属角色的列表
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
public override string[] GetRolesForUser(string username)

{
SecUtility.CheckParameter(ref username, true, false, true, 255, "username");

return ServiceLocator.RoleService.GetRolesForUser(this._applicationName, username);
}


/**//// <summary>
/// 获取属于指定角色的用户的列表
/// </summary>
/// <param name="roleName"></param>
/// <returns></returns>
public override string[] GetUsersInRole(string roleName)

{
SecUtility.CheckParameter(ref roleName, true, true, true, 255, "roleName");

return ServiceLocator.RoleService.GetUsersInRole(this._applicationName, roleName);
}


/**//// <summary>
/// 获取一个指示指定用户是否属于指定角色的值
/// </summary>
/// <param name="username"></param>
/// <param name="roleName"></param>
/// <returns></returns>
public override bool IsUserInRole(string username, string roleName)

{
SecUtility.CheckParameter(ref roleName, true, true, true, 255, "roleName");
SecUtility.CheckParameter(ref username, true, false, true, 255, "username");

return ServiceLocator.RoleService.IsUserInRole(this._applicationName, username, roleName);
}


/**//// <summary>
/// 移除指定角色中的指定用户名
/// </summary>
/// <param name="usernames"></param>
/// <param name="roleNames"></param>
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)

{
SecUtility.CheckArrayParameter(ref usernames, true, true, true, 255, "usernames");
SecUtility.CheckArrayParameter(ref roleNames, true, true, true, 255, "roleNames");

int status = ServiceLocator.RoleService.RemoveUsersFromRoles(this._applicationName, usernames, roleNames);

if (status != 0)

{
string errText = this.GetExceptionText(status);

throw new ProviderException(errText);
}

}


/**//// <summary>
/// 获取一个值,该值指示指定的角色名称是否已存在于角色数据库中
/// </summary>
/// <param name="roleName"></param>
/// <returns></returns>
public override bool RoleExists(string roleName)

{
SecUtility.CheckParameter(ref roleName, true, true, true, 255, "roleName");

return ServiceLocator.RoleService.RoleExists(this._applicationName, roleName);
}

#endregion 方法


private string GetExceptionText(int status)

{
string errText;
switch (status)

{
case 0:
return String.Empty;

case 1:
errText = "User not found.";
break;

case 2:
errText = "Role not found.";
break;

case 3:
errText = "This user already in role.";
break;

case 4:
errText = "Role is not empty.";
break;

case 5:
errText = "Role already exists.";
break;

default:
errText = "Provider error.";
break;
}
return errText;
}
}
}
