/// <summary> /// Initializes the provider. /// </summary> /// <param name="config">A collection of the name/value pairs representing the provider-specific /// attributes specified in the configuration for this provider.</param> /// <param name="name">The friendly name of the provider.</param> /// <exception cref="ArgumentNullException">The name of the provider is null.</exception> /// <exception cref="InvalidOperationException">An attempt is made to call <see cref="Initialize(System.String,System.Collections.Specialized.NameValueCollection)"></see> on a provider after the provider has already been initialized.</exception> /// <exception cref="ArgumentException">The name of the provider has a length of zero.</exception> public override void Initialize(string name, NameValueCollection config) { // Initialize values from Web.config. if (null == config) { throw (new ArgumentNullException("config")); } if (string.IsNullOrEmpty(name)) { name = "NHibernateRoleProvider"; } if (string.IsNullOrEmpty(config["description"])) { config.Remove("description"); config.Add("description", "NHibernate Role Provider"); } // Call the base class implementation. base.Initialize(name, config); if (_aspnetMembershipService == null) { IApplicationContext context = ContextRegistry.GetContext(); _aspnetMembershipService = (IAspnetMembershipService)context.GetObject("IAspnetMembershipService"); } if (_aspnetApplicationService == null) { IApplicationContext context = ContextRegistry.GetContext(); _aspnetApplicationService = (IAspnetApplicationService)context.GetObject("IAspnetApplicationService"); } if (_aspnetRoleService == null) { IApplicationContext context = ContextRegistry.GetContext(); _aspnetRoleService = (IAspnetRoleService)context.GetObject("IAspnetRoleService"); } string appName = ConfigurationUtil.GetConfigValue(config["applicationName"], System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath); application = _aspnetApplicationService.CreateOrLoadApplication(appName); if (config["writeExceptionsToEventLog"] != null) { if (config["writeExceptionsToEventLog"].ToUpper() == "TRUE") { pWriteExceptionsToEventLog = true; } } // Load configuration data. //application = _applicationService.CreateOrLoadApplication(appName); //application = // NHibernateProviderEntityHelper.CreateOrLoadApplication( // ConfigurationUtil.GetConfigValue(config["applicationName"], HostingEnvironment.ApplicationVirtualPath)); }
/// <summary> /// gets a value indicating whether the specified user is in the specified role for the configured application name. /// </summary> /// <param name="username">the name of the user to search for.</param> /// <param name="rolename">the name of the role to search in.</param> /// <returns> /// <c>true</c> if the specified user is in the specified role for the configured application name; /// otherwise, <c>false</c>. /// </returns> public override bool IsUserInRole(string username, string rolename) { // assume the given role is not associated to the given user. bool isinrole = false; //// check against the data store if the role has been assigned to the given user. try { if (_aspnetMembershipService == null) { IApplicationContext context = ContextRegistry.GetContext(); _aspnetMembershipService = (IAspnetMembershipService)context.GetObject("IAspnetMembershipService"); } if (_aspnetRoleService == null) { IApplicationContext context = ContextRegistry.GetContext(); _aspnetRoleService = (IAspnetRoleService)context.GetObject("IAspnetRoleshipService"); } AspnetMembership user = _aspnetMembershipService.GetMembership(username); AspnetRole role = _aspnetRoleService.GetAspnetRole(rolename); if ((null != user) && (null != role)) { //TODO wint linq :int appuserrole = _aspnetRoleService.IsUserInrole(application.ApplicationId, user.UserId, role.RoleId); int appuserrole = user.AspnetRoles.Count(n => n.RoleId.Equals(role.RoleId)); isinrole = (0 < appuserrole); } } catch (Exception ex) { if (WriteExceptionsToEventLog) { WriteToEventLog(ex, "IsUserInRole"); } else { throw ExceptionUtil.NewProviderException(this, Resources.Role_UnableToFindUserInRole, ex); } } // return the result of the operation. return isinrole; }
/// <summary> /// Gets a list of all the roles for the configured application name. /// </summary> /// <returns> /// A string array containing the names of all the roles stored in the data store for the configured application name. /// </returns> public override string[] GetAllRoles() { // Prepare a placeholder for the roles. string[] roleNames = new string[0]; // Load the list of roles for the configured application name. try { if (_aspnetRoleService == null) { IApplicationContext context = ContextRegistry.GetContext(); _aspnetRoleService = (IAspnetRoleService)context.GetObject("IAspnetRoleService"); } IList<String> list = _aspnetRoleService.GetRoleNames(application); if (0 < list.Count) { roleNames = new string[list.Count]; int i = 0; foreach (string roleName in list) { roleNames[i++] = roleName; } } } catch (Exception ex) { if (WriteExceptionsToEventLog) { WriteToEventLog(ex, "GetAllRoles"); } else { throw ExceptionUtil.NewProviderException(this, Resources.Role_UnableToGetAllRoles, ex); } } // Return the result of the operation. return roleNames; }