#region namespace
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using System.Configuration;
using umbraco.BusinessLogic;
using System.Security.Cryptography;
using System.Web.Util;
using System.Collections.Specialized;
using System.Configuration.Provider;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.member;
using System.Collections;
#endregion
namespace umbraco.providers.members {
public class UmbracoRoleProvider : RoleProvider {
#region
private string _ApplicationName = Member.UmbracoRoleProviderName;
#endregion
#region Properties
///
/// Gets or sets the name of the application to store and retrieve role information for.
///
///
/// The name of the application to store and retrieve role information for.
public override string ApplicationName {
get {
return _ApplicationName;
}
set {
if (string.IsNullOrEmpty(value))
throw new ProviderException("ApplicationName cannot be empty.");
if (value.Length > 0x100)
throw new ProviderException("Provider application name too long.");
_ApplicationName = value;
}
}
#endregion
#region Initialization Method
///
/// Initializes the provider.
///
/// The friendly name of the provider.
/// A collection of the name/value pairs representing the provider-specific attributes specified in the configuration for this provider.
/// The name of the provider is null.
/// An attempt is made to call
/// on a provider
/// after the provider has already been initialized.
/// The name of the provider has a length of zero.
public override void Initialize(string name, NameValueCollection config) {
// Initialize values from web.config
if (config == null) throw new ArgumentNullException("config");
if (name == null || name.Length == 0) name = "UmbracoMemberRoleProvider";
if (String.IsNullOrEmpty(config["description"])) {
config.Remove("description");
config.Add("description", "Umbraco Member Role provider");
}
// Initialize the abstract base class.
base.Initialize(name, config);
this._ApplicationName = config["applicationName"];
if (string.IsNullOrEmpty(this._ApplicationName))
this._ApplicationName = SecUtility.GetDefaultAppName();
}
#endregion
#region Methods
///
/// Adds the specified user names to the specified roles for the configured applicationName.
///
/// A string array of user names to be added to the specified roles.
/// A string array of the role names to add the specified user names to.
public override void AddUsersToRoles(string[] usernames, string[] roleNames) {
ArrayList roles = new ArrayList();
foreach (string role in roleNames)
try {
roles.Add(MemberGroup.GetByName(role).Id);
} catch {
throw new ProviderException(String.Format("No role with name '{0}' exists", role));
}
foreach (string username in usernames) {
Member m = Member.GetMemberFromLoginName(username);
foreach (int roleId in roles)
m.AddGroup(roleId);
}
}
///
/// Adds a new role to the data source for the configured applicationName.
///
/// The name of the role to create.
public override void CreateRole(string roleName) {
MemberGroup.MakeNew(roleName, User.GetUser(0));
}
///
/// Removes a role from the data source for the configured applicationName.
///
/// The name of the role to delete.
/// If true, throw an exception if roleName has one or more members and do not delete roleName.
///
/// true if the role was successfully deleted; otherwise, false.
///
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) {
MemberGroup group = MemberGroup.GetByName(roleName);
if (group == null)
throw new ProviderException(String.Format("No role with name '{0}' exists", roleName));
else if (throwOnPopulatedRole && group.GetMembersAsIds().Length > 0)
throw new ProviderException(String.Format("Can't delete role '{0}', there are members assigned to the role", roleName));
else {
foreach (Member m in group.GetMembers())
m.RemoveGroup(group.Id);
group.delete();
return true;
}
}
///
/// Gets an array of user names in a role where the user name contains the specified user name to match.
///
/// The role to search in.
/// The user name to search for.
///
/// A string array containing the names of all the users where the user name matches usernameToMatch and the user is a member of the specified role.
///
public override string[] FindUsersInRole(string roleName, string usernameToMatch) {
ArrayList members = new ArrayList();
MemberGroup group = MemberGroup.GetByName(roleName);
if (group == null)
throw new ProviderException(String.Format("No role with name '{0}' exists", roleName));
else {
foreach (Member m in group.GetMembers(usernameToMatch))
members.Add(m.LoginName);
return (string[])members.ToArray(typeof(string));
}
}
///
/// Gets a list of all the roles for the configured applicationName.
///
///
/// A string array containing the names of all the roles stored in the data source for the configured applicationName.
///
public override string[] GetAllRoles() {
ArrayList roles = new ArrayList();
foreach (MemberGroup mg in MemberGroup.GetAll)
roles.Add(mg.Text);
return (string[])roles.ToArray(typeof(string));
}
///
/// Gets a list of the roles that a specified user is in for the configured applicationName.
///
/// The user to return a list of roles for.
///
/// A string array containing the names of all the roles that the specified user is in for the configured applicationName.
///
public override string[] GetRolesForUser(string username) {
ArrayList roles = new ArrayList();
Member m = Member.GetMemberFromLoginName(username);
if (m != null) {
IDictionaryEnumerator ide = m.Groups.GetEnumerator();
while (ide.MoveNext())
roles.Add(((MemberGroup)ide.Value).Text);
return (string[])roles.ToArray(typeof(string));
} else
throw new ProviderException(String.Format("No member with username '{0}' exists", username));
}
///
/// Gets a list of users in the specified role for the configured applicationName.
///
/// The name of the role to get the list of users for.
///
/// A string array containing the names of all the users who are members of the specified role for the configured applicationName.
///
public override string[] GetUsersInRole(string roleName) {
ArrayList members = new ArrayList();
MemberGroup group = MemberGroup.GetByName(roleName);
if (group == null)
throw new ProviderException(String.Format("No role with name '{0}' exists", roleName));
else {
foreach (Member m in group.GetMembers())
members.Add(m.LoginName);
return (string[])members.ToArray(typeof(string));
}
}
///
/// Gets a value indicating whether the specified user is in the specified role for the configured applicationName.
///
/// The user name to search for.
/// The role to search in.
///
/// true if the specified user is in the specified role for the configured applicationName; otherwise, false.
///
public override bool IsUserInRole(string username, string roleName) {
Member m = Member.GetMemberFromLoginName(username);
if (m == null)
throw new ProviderException(String.Format("No user with name '{0}' exists", username));
else {
MemberGroup mg = MemberGroup.GetByName(roleName);
if (mg == null)
throw new ProviderException(String.Format("No Membergroup with name '{0}' exists", roleName));
else
return mg.HasMember(m.Id);
}
}
///
/// Removes the specified user names from the specified roles for the configured applicationName.
///
/// A string array of user names to be removed from the specified roles.
/// A string array of role names to remove the specified user names from.
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) {
ArrayList roles = new ArrayList();
foreach (string role in roleNames)
try {
roles.Add(MemberGroup.GetByName(role).Id);
} catch {
throw new ProviderException(String.Format("No role with name '{0}' exists", role));
}
foreach (string username in usernames) {
Member m = Member.GetMemberFromLoginName(username);
foreach (int roleId in roles)
m.RemoveGroup(roleId);
}
}
///
/// Gets a value indicating whether the specified role name already exists in the role data source for the configured applicationName.
///
/// The name of the role to search for in the data source.
///
/// true if the role name already exists in the data source for the configured applicationName; otherwise, false.
///
public override bool RoleExists(string roleName) {
MemberGroup mg = MemberGroup.GetByName(roleName);
return mg != null;
}
#endregion
}
}