📄 rolecontroller.cs
字号:
/// -----------------------------------------------------------------------------
/// <summary>
/// This overload obtains a role and optionaly synchronizes the DNN and AspNet Roles
/// </summary>
/// <param name="PortalId">The Id of the Portal</param>
/// <param name="RoleName">Name of the role to be found</param>
/// <param name="SynchronizeRoles"></param>
/// <returns>A RoleInfo object is the role is found</returns>
/// <history>
/// [cnurse] 05/24/2005 Documented
/// </history>
/// -----------------------------------------------------------------------------
public RoleInfo GetRoleByName(int portalId, string roleName, bool synchronizeRoles)
{
if (synchronizeRoles)
{
this.SynchronizeRoles(portalId);
}
return ((RoleInfo) CBO.FillObject(DataProvider.Instance().GetRoleByName(portalId, roleName), typeof(RoleInfo)));
}
/// -----------------------------------------------------------------------------
/// <summary>
/// Returns an array of rolenames for a Portal
/// </summary>
/// <param name="PortalId">The Id of the Portal</param>
/// <returns>A String Array of Role Names</returns>
/// <history>
/// [cnurse] 05/24/2005 Documented
/// </history>
/// -----------------------------------------------------------------------------
public string[] GetRoleNames(int portalID)
{
string originalAppName = Common.Globals.GetApplicationName();
try
{
Common.Globals.SetApplicationName(portalID);
return Microsoft.ScalableHosting.Security.Roles.GetAllRoles();
}
catch (Exception exception) // an unexpected error occurred
{
Exceptions.LogException(exception);
}
finally
{
Common.Globals.SetApplicationName(originalAppName);
}
return new string[0];
}
/// -----------------------------------------------------------------------------
/// <summary>
/// Gets an ArrayList of Roles
/// </summary>
/// <returns>An ArrayList of Roles</returns>
/// <history>
/// [cnurse] 05/24/2005 Documented
/// </history>
/// -----------------------------------------------------------------------------
public ArrayList GetRoles()
{
return CBO.FillCollection(DataProvider.Instance().GetRoles(), typeof(RoleInfo));
}
/// -----------------------------------------------------------------------------
/// <summary>
/// Gets a List of Roles for a given User
/// </summary>
/// <param name="UserId">The Id of the User</param>
/// <param name="PortalId">The Id of the Portal</param>
/// <returns>A String Array of Role Names</returns>
/// <history>
/// [cnurse] 05/24/2005 Documented
/// </history>
/// -----------------------------------------------------------------------------
public string[] GetRolesByUser(int userId, int portalId)
{
string originalApplicationName = Common.Globals.GetApplicationName();
try
{
Common.Globals.SetApplicationName(portalId);
UserController userController = new UserController();
UserInfo userInfo = userController.GetUser(portalId, userId);
return Microsoft.ScalableHosting.Security.Roles.GetRolesForUser(userInfo.Membership.Username);
}
finally
{
//Reset the Application Name
Common.Globals.SetApplicationName(originalApplicationName);
}
}
/// -----------------------------------------------------------------------------
/// <summary>
/// Gets a list of Services for a Portal
/// </summary>
/// <param name="PortalId">The Id of the Portal</param>
/// <returns>An ArrayList of UserRoleInfo objects</returns>
/// <history>
/// [Nik Kalyani] 10/15/2004 Created multiple signatures to eliminate Optional parameters
/// </history>
/// -----------------------------------------------------------------------------
public ArrayList GetServices(int portalId)
{
return GetServices(portalId, - 1);
}
/// -----------------------------------------------------------------------------
/// <summary>
/// Gets a list of Services for a User
/// </summary>
/// <param name="PortalId">The Id of the Portal</param>
/// <param name="UserId">The Id of the User</param>
/// <returns>An ArrayList of UserRoleInfo objects</returns>
/// <history>
/// [Nik Kalyani] 10/15/2004 Created multiple signatures to eliminate Optional parameters
/// </history>
/// -----------------------------------------------------------------------------
public ArrayList GetServices(int portalId, int userId)
{
return CBO.FillCollection(DataProvider.Instance().GetServices(portalId, userId), typeof(UserRoleInfo));
}
/// -----------------------------------------------------------------------------
/// <summary>
/// Gets a User/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>A UserRoleInfo object</returns>
/// <history>
/// [cnurse] 05/24/2005 Documented
/// </history>
/// -----------------------------------------------------------------------------
public UserRoleInfo GetUserRole(int portalID, int userId, int roleId)
{
return ((UserRoleInfo) CBO.FillObject(DataProvider.Instance().GetUserRole(portalID, userId, roleId), typeof(UserRoleInfo)));
}
/// -----------------------------------------------------------------------------
/// <summary>
/// Gets a List of UserRoles by UserName and RoleName
/// </summary>
/// <param name="PortalID">The Id of the Portal</param>
/// <param name="Username">The username of the user</param>
/// <param name="Rolename">The role name</param>
/// <returns>An ArrayList of UserRoleInfo objects</returns>
/// <history>
/// [cnurse] 05/24/2005 Documented
/// </history>
/// -----------------------------------------------------------------------------
public ArrayList GetUserRolesByUsername(int portalID, string userName, string roleName)
{
return CBO.FillCollection(DataProvider.Instance().GetUserRolesByUsername(portalID, userName, roleName), typeof(UserRoleInfo));
}
/// -----------------------------------------------------------------------------
/// <summary>
/// Gets a List of UserRoles by RoleName
/// </summary>
/// <param name="PortalID">The Id of the Portal</param>
/// <param name="Rolename">The role name</param>
/// <returns>An ArrayList of UserRoleInfo objects</returns>
/// <history>
/// [cnurse] 05/24/2005 Documented
/// </history>
/// -----------------------------------------------------------------------------
public ArrayList GetUsersInRole(int portalID, string roleName)
{
string originalApplicationName = Common.Globals.GetApplicationName();
string[] users;
ArrayList roleUsers = new ArrayList();
try
{
Common.Globals.SetApplicationName(portalID);
users = Microsoft.ScalableHosting.Security.Roles.GetUsersInRole(roleName);
}
finally
{
//Reset the Application Name
Common.Globals.SetApplicationName(originalApplicationName);
}
RoleController roleController = new RoleController();
ArrayList arrUserRoles;
int i;
for (i = 0; i < users.Length; i++)
{
arrUserRoles = roleController.GetUserRolesByUsername(portalID, users[i], roleName);
foreach (UserRoleInfo userRoleInfo in arrUserRoles)
{
roleUsers.Add(userRoleInfo);
}
}
return roleUsers;
}
/// -----------------------------------------------------------------------------
/// <summary>
/// Synchronize the DNN Roles and AspNet Roles
/// </summary>
/// <param name="PortalID"></param>
/// <history>
/// [cnurse] 05/23/2005 Documented
/// </history>
/// -----------------------------------------------------------------------------
public void SynchronizeRoles (int portalID)
{
string originalApplicationName = Common.Globals.GetApplicationName();
string[] roles;
RoleController roleController = new RoleController();
try
{
Common.Globals.SetApplicationName(portalID);
roles = Microsoft.ScalableHosting.Security.Roles.GetAllRoles();
}
finally
{
//Reset the Application Name
Common.Globals.SetApplicationName(originalApplicationName);
}
int i;
for (i = 0; i < roles.Length ; i++)
{
if (roleController.GetRoleByName(portalID, roles[i]) == null)
{
RoleInfo roleInfo = new RoleInfo();
roleInfo.PortalID = portalID;
roleInfo.RoleName = roles[i];
roleController.AddRole(roleInfo, true);
}
}
}
/// -----------------------------------------------------------------------------
/// <summary>
/// Persists a role to the Data Store
/// </summary>
/// <param name="roleInfo">The role to persist</param>
/// <history>
/// [cnurse] 05/24/2005 Documented
/// </history>
/// -----------------------------------------------------------------------------
public void UpdateRole (RoleInfo roleInfo)
{
DataProvider.Instance().UpdateRole(roleInfo.RoleID, roleInfo.Description, roleInfo.ServiceFee,
roleInfo.BillingPeriod.ToString(), roleInfo.BillingFrequency, roleInfo.TrialFee, roleInfo.TrialPeriod,
roleInfo.TrialFrequency, roleInfo.IsPublic, roleInfo.AutoAssignment);
AutoAssignUsers(roleInfo, roleInfo.RoleID, false);
}
/// -----------------------------------------------------------------------------
/// <summary>
/// Updates a Service (UserRole)
/// </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>
/// <history>
/// [Nik Kalyani] 10/15/2004 Created multiple signatures to eliminate Optional parameters
/// </history>
/// -----------------------------------------------------------------------------
public void UpdateService (int portalId, int userId, int roleId)
{
UpdateService(portalId, userId, roleId, false);
}
/// -----------------------------------------------------------------------------
/// <summary>
/// Updates a Service (UserRole)
/// </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="Cancel">A flag that indicates whether to cancel (delete) the userrole</param>
/// <history>
/// [Nik Kalyani] 10/15/2004 Created multiple signatures to eliminate Optional parameters
/// </history>
/// -----------------------------------------------------------------------------
public void UpdateService (int portalId, int userId, int roleId, bool cancel)
{
if (cancel)
{
DeleteUserRole(portalId, userId, roleId);
}
else
{
IDataReader dr;
int userRoleId = - 1;
DateTime expiryDate = DateTime.Now;
bool trialUsed = false;
int period = 0;
string frequency = string.Empty;
dr = DataProvider.Instance().GetUserRole(portalId, userId, roleId);
if (dr.Read())
{
userRoleId = Convert.ToInt32(dr["UserRoleId"]);
if (! Convert.IsDBNull(dr["ExpiryDate"]))
{
expiryDate = Convert.ToDateTime(dr["ExpiryDate"]);
}
if (! Convert.IsDBNull(dr["IsTrialUsed"]))
{
trialUsed = Convert.ToBoolean(dr["IsTrialUsed"]);
}
}
dr.Close();
dr = DataProvider.Instance().GetRole(roleId, portalId);
if (dr.Read())
{
if (!trialUsed && Convert.ToString(dr["TrialFrequency"]) != "N")
{
period = Convert.ToInt32(dr["TrialPeriod"]);
frequency = dr["TrialFrequency"].ToString();
}
else
{
period = Convert.ToInt32(dr["BillingPeriod"]);
frequency = dr["BillingFrequency"].ToString();
}
}
dr.Close();
if (expiryDate < DateTime.Now)
{
expiryDate = DateTime.Now;
}
switch (frequency)
{
case "N":
expiryDate = Null.NullDate;
break;
case "O":
expiryDate = DateTime.MaxValue;//new DateTime(9999, 12, 31);
break;
case "D":
expiryDate = Convert.ToDateTime(expiryDate).AddDays(period);
break;
case "W":
expiryDate = Convert.ToDateTime(expiryDate).AddDays(period*7);
break;
case "M":
expiryDate = Convert.ToDateTime(expiryDate).AddMonths(period);
break;
case "Y":
expiryDate = Convert.ToDateTime(expiryDate).AddYears(period);
break;
}
if (userRoleId != - 1)
{
DataProvider.Instance().UpdateUserRole(userRoleId, expiryDate);
}
else
{
AddUserRole(portalId, userId, roleId, expiryDate);
}
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -