⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rolecontroller.cs

📁 SharpNuke源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections;
using System.Data;
using System.Text;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Data;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using DotNetNuke.Services.Exceptions;

//
// DotNetNuke -  http://www.dotnetnuke.com
// Copyright (c) 2002-2005
// by Shaun Walker ( sales@perpetualmotion.ca ) of Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//

namespace DotNetNuke.Security.Roles
{
	
	/// -----------------------------------------------------------------------------
	/// Project:    DotNetNuke
	/// Namespace:  DotNetNuke.Security.Roles
	/// Class:      RoleController
	/// -----------------------------------------------------------------------------
	/// <summary>
	/// The RoleController class provides Business Layer methods for Roles
	/// </summary>
	/// <history>
	///     [cnurse]    05/23/2005  made compatible with .NET 2.0
	/// </history>
	/// -----------------------------------------------------------------------------
	public class RoleController
	{
		
		#region "Private Methods"
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// Auto Assign existing users to a role
		/// </summary>
		/// <param name="roleInfo">The Role to Auto assign</param>
		/// <param name="RoleId">The Id of the Role</param>
		/// <param name="SynchronizationMode">not used</param>
		/// <history>
		/// 	[cnurse]	05/23/2005	Documented
		/// </history>
		/// -----------------------------------------------------------------------------
		private void AutoAssignUsers (RoleInfo roleInfo, int roleId, bool synchronizationMode)
		{
			
			if (roleInfo.AutoAssignment)
			{
				// loop through users for portal and add to role
				IDataReader dr = DataProvider.Instance().GetUsers(roleInfo.PortalID);
				while (dr.Read())
				{
					try
					{
						AddUserRole(roleInfo.PortalID, Convert.ToInt32(dr["UserId"]), roleId, Null.NullDate);
					}
					catch (Exception)
					{
						// user already belongs to role
					}
				}
				dr.Close();
			}
		}
		
		#endregion
		
		#region "Public Methods"
		
		/// <summary>
		/// Add a new role
		/// </summary>
		/// <param name="roleInfo">The Role to Add</param>
		/// <returns>The Id of the new role</returns>
		/// <returns></returns>
		/// <history>
		/// 	[cnurse]	05/23/2005	Documented
		/// </history>
		/// -----------------------------------------------------------------------------
		public int AddRole(RoleInfo roleInfo)
		{
			return AddRole(roleInfo, false);
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// This overload adds a role and optionally adds the info to the AspNet Roles
		/// </summary>
		/// <param name="roleInfo">The Role to Add</param>
		/// <param name="SynchronizationMode">Flag indicating whether the role should be
		/// added to the AspNet Roles</param>
		/// <returns>The Id of the new role</returns>
		/// <history>
		/// 	[cnurse]	05/23/2005	Documented
		/// </history>
		/// -----------------------------------------------------------------------------
		public int AddRole(RoleInfo roleInfo, bool synchronizationMode)
		{
			
			string originalAppName = Common.Globals.GetApplicationName();
			try
			{
				if (! synchronizationMode)
				{
					Common.Globals.SetApplicationName(roleInfo.PortalID);
					Microsoft.ScalableHosting.Security.Roles.CreateRole(roleInfo.RoleName);
				}
				
				int roleId = - 1;
				roleId = DataProvider.Instance().AddRole(roleInfo.PortalID, roleInfo.RoleName, roleInfo.Description, 
					roleInfo.ServiceFee, roleInfo.BillingPeriod.ToString(), roleInfo.BillingFrequency, 
					roleInfo.TrialFee, roleInfo.TrialPeriod, roleInfo.TrialFrequency, roleInfo.IsPublic, 
					roleInfo.AutoAssignment);
				
				AutoAssignUsers(roleInfo, roleId, synchronizationMode);
				
				return roleId;
			}
			finally
			{
				//Reset the Application Name
				Common.Globals.SetApplicationName(originalAppName);
			}
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// Adds a User to a Role
		/// </summary>
		/// <param name="PortalID">The Id of the Portal</param>
		/// <param name="UserId">The Id of the User</param>
		/// <param name="RoleId">The Id of the Role</param>
		/// <param name="ExpiryDate">The expiry Date of the Role membership</param>
		/// <history>
		/// 	[cnurse]	05/24/2005	Documented
		/// </history>
		/// -----------------------------------------------------------------------------
		public void AddUserRole (int portalID, int userId, int roleId, DateTime expiryDate)
		{
			
			string originalAppName = string.Empty;
			
			UserController userController = new UserController();
			UserInfo userInfo = userController.GetUser(portalID, userId);
			UserRoleInfo userRoleInfo = GetUserRole(portalID, userId, roleId);
			
			if (userRoleInfo == null)
			{
				originalAppName = Common.Globals.GetApplicationName();
				Common.Globals.SetApplicationName(portalID);
				DataProvider.Instance().AddUserRole(portalID, userId, roleId, expiryDate);
				userRoleInfo = GetUserRole(portalID, userId, roleId);
				try
				{
					Microsoft.ScalableHosting.Security.Roles.AddUserToRole(userInfo.Membership.Username, userRoleInfo.RoleName);
				}
				catch (Exception)
				{
					//Reset the Application Name
					Common.Globals.SetApplicationName(originalAppName);
				}
			}
			else
			{
				DataProvider.Instance().UpdateUserRole(userRoleInfo.UserRoleID, expiryDate);
			}
			
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// Delete a Role
		/// </summary>
		/// <param name="RoleId">The Id of the Role to delete</param>
		/// <param name="PortalId">The Id of the Portal</param>
		/// <history>
		/// 	[cnurse]	05/24/2005	Documented
		/// </history>
		/// -----------------------------------------------------------------------------
		public void DeleteRole (int roleId, int portalId)
		{
			string originalAppName = Common.Globals.GetApplicationName();
			try
			{
				RoleInfo roleInfo = GetRole(roleId, portalId);
				
				Common.Globals.SetApplicationName(portalId);
				
				//Remove all users from Role
				string[] users = Microsoft.ScalableHosting.Security.Roles.GetUsersInRole(roleInfo.RoleName);
				if (users.Length > 0)
				{
					Microsoft.ScalableHosting.Security.Roles.RemoveUsersFromRole(users, roleInfo.RoleName);
				}
				
				//Remove role
				Microsoft.ScalableHosting.Security.Roles.DeleteRole(roleInfo.RoleName);
				
				DataProvider.Instance().DeleteRole(roleId);
			}
			finally
			{
				//Reset the Application Name
				Common.Globals.SetApplicationName(originalAppName);
			}
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// Delete/Remove a User from a Role
		/// </summary>
		/// <param name="PortalID">The Id of the Portal</param>
		/// <param name="UserId">The Id of the User</param>
		/// <param name="RoleId">The Id of the Role</param>
		/// <returns></returns>
		/// <history>
		/// 	[cnurse]	05/24/2005	Documented
		/// </history>
		/// -----------------------------------------------------------------------------
		public bool DeleteUserRole(int portalId, int userId, int roleId)
		{
			string originalAppName = Common.Globals.GetApplicationName();
			try
			{
				UserController userController = new UserController();
				UserInfo userInfo = userController.GetUser(portalId, userId);
				UserRoleInfo userRoleInfo = GetUserRole(portalId, userId, roleId);
				
				PortalController portalController = new PortalController();
				bool delete = true;
				
				PortalInfo portalInfo = portalController.GetPortal(portalId);
				if (portalInfo != null)
				{
					if ((portalInfo.AdministratorId != userId || portalInfo.AdministratorRoleId != roleId) && portalInfo.RegisteredRoleId != roleId)
					{
						Common.Globals.SetApplicationName(portalId);
						Microsoft.ScalableHosting.Security.Roles.RemoveUserFromRole(userInfo.Membership.Username, userRoleInfo.RoleName);
						DataProvider.Instance().DeleteUserRole(userId, roleId);
					}
					else
					{
						delete = false;
					}
				}
				return delete;
			}
			finally
			{
				//Reset the Application Name
				Common.Globals.SetApplicationName(originalAppName);
			}
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// Get a list of roles for the Portal
		/// </summary>
		/// <param name="PortalId">The Id of the Portal</param>
		/// <returns>An ArrayList of RoleInfo objects</returns>
		/// <history>
		/// 	[cnurse]	05/24/2005	Documented
		/// </history>
		/// -----------------------------------------------------------------------------
		public ArrayList GetPortalRoles(int portalId)
		{
			return GetPortalRoles(portalId, false);
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// This overloads get a list of roles and optionally synchronizes the DNN and
		/// AspNet Roles
		/// </summary>
		/// <param name="PortalId">The Id of the Portal</param>
		/// <param name="SynchronizeRoles">Flagg that indicates whether the DNN and
		/// AspNet roles should be Synchronized</param>
		/// <returns>An ArrayList of RoleInfo objects</returns>
		/// <returns>An ArrayList of RoleInfo objects</returns>
		/// <history>
		/// 	[cnurse]	05/24/2005	Documented
		/// </history>
		/// -----------------------------------------------------------------------------
		public ArrayList GetPortalRoles(int portalId, bool synchronizeRoles)
		{
			if (synchronizeRoles)
			{
				this.SynchronizeRoles(portalId);
			}
			return CBO.FillCollection(DataProvider.Instance().GetPortalRoles(portalId), typeof(RoleInfo));
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// Gets a list of Roles for a User
		/// </summary>
		/// <param name="UserId">The Id of the User</param>
		/// <param name="PortalID">The Id of the Portal</param>
		/// <returns>A string that contains a list of Roles fro the User delimited by "|"</returns>
		/// <history>
		/// 	[cnurse]	05/24/2005	Documented
		/// </history>
		/// -----------------------------------------------------------------------------
		public string[] GetPortalRolesByUser(int userId, int portalId)
		{
			ArrayList roles = new ArrayList();
			IDataReader dr = null;
			try
			{
				dr = DataProvider.Instance().GetRolesByUser(userId, portalId);
				while (dr.Read())
				{
					roles.Add(System.Convert.ToString(dr["RoleName"]));
				}
				if (roles.Count > 0)
				{
					return (string[])roles.ToArray(typeof(string));
				}
				return null;
			}
			finally
			{
				if (dr != null)
				{
					dr.Close();
				}
			}
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// Fetch a single Role
		/// </summary>
		/// <param name="RoleID">The Id of the Role</param>
		/// <param name="PortalID">The Id of the Portal</param>
		/// <returns></returns>
		/// <remarks></remarks>
		/// <history>
		/// 	[cnurse]	05/24/2005	Documented
		/// </history>
		/// -----------------------------------------------------------------------------
		public RoleInfo GetRole(int roleID, int portalID)
		{
			return ((RoleInfo) CBO.FillObject(DataProvider.Instance().GetRole(roleID, portalID), typeof(RoleInfo)));
		}
		
		/// -----------------------------------------------------------------------------
		/// <summary>
		/// Obtains a role given the role name
		/// </summary>
		/// <param name="PortalId">Portal indentifier</param>
		/// <param name="RoleName">Name of the role to be found</param>
		/// <returns>A RoleInfo object is the role is found</returns>
		/// <history>
		/// 	[VMasanas]	27/08/2004	Created
		/// </history>
		/// -----------------------------------------------------------------------------
		public RoleInfo GetRoleByName(int portalId, string roleName)
		{
			return GetRoleByName(portalId, roleName, false);
		}
		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -