using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using CMS.CMSHelper;
using CMS.GlobalHelper;
using CMS.SettingsProvider;
using CMS.SiteProvider;
using CMS.UIControls;
using CMS.ExtendedControls;
public partial class CMSModules_Permissions_Controls_PermissionsMatrix : CMSAdminControl
{
#region "Constants"
///
/// CSS class for highlighted permission matrix rows.
///
private const string HIGHLIGHTED_ROW_CSS = "Highlighted";
#endregion
#region "Variables"
private string mSelectedId = string.Empty;
private string mSelectedType = string.Empty;
private int mSiteId = 0;
private int mRoleId = 0;
private bool mGlobalRoles = false;
private UserInfo mSelectedUser = null;
private SiteInfo mSelectedSite = null;
private CurrentUserInfo currentUser = null;
#endregion
#region "Properties"
///
/// Messages placeholder
///
public override MessagesPlaceHolder MessagesPlaceHolder
{
get
{
return plcMess;
}
}
///
/// Indicates if control is used on live site.
///
public override bool IsLiveSite
{
get
{
return base.IsLiveSite;
}
set
{
plcMess.IsLiveSite = value;
base.IsLiveSite = value;
}
}
///
/// Gets or sets Site ID.
///
public int SiteID
{
get
{
return mSiteId;
}
set
{
mSiteId = value;
}
}
///
/// Gets or sets Role ID.
///
public int RoleID
{
get
{
return mRoleId;
}
set
{
mRoleId = value;
}
}
///
/// Gets or sets ID selected in the moduleSelector or docTypeSelector or customTableSelector according to the selected PermissionType.
///
public string SelectedID
{
get
{
return mSelectedId;
}
set
{
mSelectedId = value;
}
}
///
/// Gets or sets type constant according to the selected value in the moduleSelector or docTypeSelector or customTableSelector and the selected PermissionType.
///
public string SelectedType
{
get
{
return mSelectedType;
}
set
{
mSelectedType = value;
}
}
///
/// Currently selected user ID for report
///
public int SelectedUserID
{
get;
set;
}
///
/// Query name to get the data.
///
public string QueryName
{
get
{
return gridMatrix.QueryName;
}
set
{
gridMatrix.QueryName = value;
}
}
///
/// ID column of the row.
///
public string RowItemIDColumn
{
get
{
return gridMatrix.RowItemIDColumn;
}
set
{
gridMatrix.RowItemIDColumn = value;
}
}
///
/// Display name column of the row.
///
public string RowItemDisplayNameColumn
{
get
{
return gridMatrix.RowItemDisplayNameColumn;
}
set
{
gridMatrix.RowItemDisplayNameColumn = value;
}
}
///
/// Tooltip column of the row.
///
public string RowItemTooltipColumn
{
get
{
return gridMatrix.RowItemTooltipColumn;
}
set
{
gridMatrix.RowItemTooltipColumn = value;
}
}
///
/// Display name column of the column.
///
public string ColumnItemDisplayNameColumn
{
get
{
return gridMatrix.ColumnItemDisplayNameColumn;
}
set
{
gridMatrix.ColumnItemDisplayNameColumn = value;
}
}
///
/// ID column of the column.
///
public string ColumnItemIDColumn
{
get
{
return gridMatrix.ColumnItemIDColumn;
}
set
{
gridMatrix.ColumnItemIDColumn = value;
}
}
///
/// Tooltip column of the column.
///
public string ColumnItemTooltipColumn
{
get
{
return gridMatrix.ColumnItemTooltipColumn;
}
set
{
gridMatrix.ColumnItemTooltipColumn = value;
}
}
///
/// Tooltip column of the item.
///
public string ItemTooltipColumn
{
get
{
return gridMatrix.ItemTooltipColumn;
}
set
{
gridMatrix.ItemTooltipColumn = value;
}
}
///
/// Gets or sets the text displayed in the upper left corner, if filter is not shown.
///
public string CornerText
{
get
{
return gridMatrix.CornerText;
}
set
{
gridMatrix.CornerText = value;
}
}
///
/// Indicates whether show global roles.
///
public bool GlobalRoles
{
get
{
return mGlobalRoles;
}
set
{
mGlobalRoles = value;
}
}
///
/// Gets UserInfo object for selected user ID.
///
private UserInfo SelectedUser
{
get
{
if (mSelectedUser == null)
{
mSelectedUser = UserInfoProvider.GetUserInfo(SelectedUserID);
}
return mSelectedUser;
}
}
///
/// Gets SiteInfo object for selected site ID.
///
private SiteInfo SelectedSite
{
get
{
if (mSelectedSite == null)
{
mSelectedSite = SiteInfoProvider.GetSiteInfo(SiteID);
}
return mSelectedSite;
}
}
///
/// Indicates if only selected user roles should be displayed.
///
public bool UserRolesOnly
{
get;
set;
}
///
/// Indicates if filter was changed.
///
public bool FilterChanged
{
get;
set;
}
///
/// If true, the permissions are used as rows
///
public bool PermissionsAsRows
{
get;
set;
}
#endregion
#region "Events"
///
/// Occurs when data has been loaded. Allows manipulation with data.
///
public delegate void OnMatrixDataLoaded(DataSet ds);
public event OnMatrixDataLoaded OnDataLoaded;
#endregion
#region "Page Events"
protected void Page_Load(object sender, EventArgs e)
{
// Register js script
string script = "function NA() {alert(" + ScriptHelper.GetString(GetString("Administration-Permissions_Matrix.NotAdjustable")) + ");}";
ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "PermissionMatrix_NA", ScriptHelper.GetScript(script));
gridMatrix.OrderBy = "Matrix.RoleDisplayName, Matrix.RoleID, Matrix.PermissionOrder";
gridMatrix.ContentBefore = @"
";
gridMatrix.OnItemChanged += gridMatrix_OnItemChanged;
gridMatrix.DataLoaded += gridMatrix_DataLoaded;
if (PermissionsAsRows)
{
gridMatrix.CheckRowPermissions += gridMatrix_CheckPermissions;
gridMatrix.DisabledRowMark = "
";
}
else
{
gridMatrix.CheckColumnPermissions += gridMatrix_CheckPermissions;
gridMatrix.DisabledColumnMark = "
";
}
gridMatrix.ContentBeforeRowsCssClass = "ContentBefore";
gridMatrix.OnGetRowItemCssClass += gridMatrix_OnGetRowItemCssClass;
gridMatrix.NoRecordsMessage = GetString("general.emptymatrix");
currentUser = CMSContext.CurrentUser;
if (!CMSContext.CurrentUser.IsAuthorizedPerResource("CMS.Permissions", "Manage"))
{
gridMatrix.Enabled = false;
ShowWarning(string.Format(GetString("CMSSiteManager.AccessDeniedOnPermissionName"), "Manage"), null, null);
}
}
protected bool gridMatrix_CheckPermissions(object permId)
{
int permissionId = ValidationHelper.GetInteger(permId, 0);
// Check how the permission can be edited
PermissionNameInfo pni = PermissionNameInfoProvider.GetPermissionNameInfo(permissionId);
if (pni != null)
{
return currentUser.UserSiteManagerAdmin || !pni.PermissionEditableByGlobalAdmin;
}
return true;
}
protected override void OnPreRender(EventArgs e)
{
// Initialize value dependant properties of uni matrix
gridMatrix.ShowContentBeforeRows = SelectedUserID > 0;
gridMatrix.QueryName = GetQueryNameByType();
gridMatrix.QueryParameters = GetQueryParameters();
gridMatrix.WhereCondition = GetWhereCondition();
if (FilterChanged || (gridMatrix.Pager.CurrentPage <= 0))
{
gridMatrix.Pager.CurrentPage = 1;
}
if (gridMatrix.Pager.CurrentPage > gridMatrix.Pager.PageCount)
{
gridMatrix.Pager.CurrentPage = gridMatrix.Pager.PageCount;
}
if (!gridMatrix.HasData)
{
if (UserRolesOnly)
{
lblInfo.Text = GetString("general.norolemember");
}
}
else
{
// Inform user that global admin was selected
if ((SelectedUserID > 0) && SelectedUser.IsGlobalAdministrator)
{
lblGlobalAdmin.Text = GetString("Administration-Permissions_Matrix.GlobalAdministrator");
}
}
lblInfo.Visible = !string.IsNullOrEmpty(lblInfo.Text);
lblGlobalAdmin.Visible = !string.IsNullOrEmpty(lblGlobalAdmin.Text);
// Set content before rows and refresh matrix content
gridMatrix.ContentBeforeRows = GetBeforeRowsContent(SiteID, ValidationHelper.GetInteger(SelectedID, 0), SelectedType, SelectedUserID);
pnlUpdMat.Update();
base.OnPreRender(e);
}
#endregion
#region "Private Methods"
///
/// Gets where condition for the matrix according to the RoleID.
///
/// String representing where condition for the matrix.
private string GetWhereCondition()
{
string where = null;
if (RoleID > 0)
{
where = string.Format("RoleGroupID IS NULL AND RoleID = {0}", RoleID);
}
else
{
where = "RoleGroupID IS NULL";
}
if (UserRolesOnly && (SelectedUserID > 0))
{
// Get selected site name
string siteName = null;
if (SiteID > 0)
{
siteName = SelectedSite.SiteName.ToLowerCSafe();
}
else
{
siteName = UserInfo.GLOBAL_ROLES_KEY;
}
string rolesWhere = SelectedUser.GetRoleIdList((SiteID <= 0), true, siteName);
// Add roles where condition
if (!String.IsNullOrEmpty(rolesWhere))
{
where = SqlHelperClass.AddWhereCondition(where, "RoleID IN(" + rolesWhere + ")");
}
else
{
where = SqlHelperClass.NO_DATA_WHERE;
gridMatrix.StopProcessing = true;
}
}
return where;
}
///
/// Gets query parameters for the permission matrix.
///
/// Two dimensional object array of query parameters.
private QueryDataParameters GetQueryParameters()
{
QueryDataParameters parameters = new QueryDataParameters();
parameters.Add("@ID", SelectedID);
parameters.Add("@SiteID", (GlobalRoles ? 0 : SiteID == 0 ? CMSContext.CurrentSiteID : SiteID));
parameters.Add("@DisplayInMatrix", true);
return parameters;
}
///
/// Gets name of the query which will be used for the matrix according to the selected type.
///
/// String representing query name.
private string GetQueryNameByType()
{
switch (mSelectedType)
{
case "r":
return "cms.permission.getResourcePermissionMatrix";
default:
return "cms.permission.getClassPermissionMatrix";
}
}
///
/// Gets user effective permissions HTML content.
///
/// Site ID
/// ID of particular resource
/// Permission type
/// User ID
private string GetBeforeRowsContent(int siteId, int resourceId, string selectedType, int userId)
{
// Check if selected users exists
UserInfo user = SelectedUser;
if (user == null)
{
gridMatrix.ShowContentBeforeRows = false;
return null;
}
string columns = "PermissionID";
// Ensure tooltip column
if (!String.IsNullOrEmpty(gridMatrix.ItemTooltipColumn))
{
columns += ",Matrix." + gridMatrix.ItemTooltipColumn;
}
// Get permission data
DataSet dsPermissions = null;
switch (selectedType)
{
case "r":
dsPermissions = UserInfoProvider.GetUserResourcePermissions(user, siteId, resourceId, true, columns);
break;
default:
dsPermissions = UserInfoProvider.GetUserDataClassPermissions(user, siteId, resourceId, true, columns);
break;
}
// Initialize string builder
StringBuilder sb = new StringBuilder("");
if (!DataHelper.DataSourceIsEmpty(dsPermissions))
{
// Initialize variables used during rendering
string firstColumnsWidth = (gridMatrix.FirstColumnsWidth > 0) ? "width:" + gridMatrix.FirstColumnsWidth + (gridMatrix.UsePercentage ? "%;" : "px;") : "";
string imagesUrl = GetImageUrl("Design/Controls/UniMatrix/", IsLiveSite, true);
DataRowCollection rows = dsPermissions.Tables[0].Rows;
string userName = Functions.GetFormattedUserName(user.UserName, user.FullName);
// Get user name column
sb.Append("\n");
// Process permissions according to matrix order
foreach (int index in gridMatrix.ColumnOrderIndex)
{
DataRow dr = (DataRow)rows[index];
// Render cell
sb.Append(";) | \n");
}
bool manyColumns = gridMatrix.ColumnOrderIndex.Length >= 10;
// Add finish row
if (!manyColumns)
{
sb.Append(" | ");
}
}
return sb.ToString();
}
#endregion
#region "Event Handlers"
protected void gridMatrix_OnItemChanged(object sender, int rowItemId, int colItemId, bool allow)
{
// roleId and permissionId possitions differ according to the page where control is used
int roleId = (mRoleId > 0) ? colItemId : rowItemId;
int permissionId = (mRoleId > 0) ? rowItemId : colItemId;
// Check "Manage" permission
if (!CMSContext.CurrentUser.IsAuthorizedPerResource("CMS.Permissions", "Manage"))
{
CMSPage.RedirectToCMSDeskAccessDenied("CMS.Permissions", "Manage");
}
// Check permission for the given column
if (!gridMatrix_CheckPermissions(permissionId))
{
CMSPage.RedirectToCMSDeskAccessDenied("CMS.Permissions", "Manage");
}
if (allow)
{
RolePermissionInfoProvider.SetRolePermissionInfo(roleId, permissionId);
}
else
{
RolePermissionInfoProvider.DeleteRolePermissionInfo(roleId, permissionId);
}
// Reload content before rows
gridMatrix.ContentBeforeRows = GetBeforeRowsContent(SiteID, ValidationHelper.GetInteger(SelectedID, 0), SelectedType, SelectedUserID);
}
protected void gridMatrix_DataLoaded(DataSet ds)
{
if (OnDataLoaded != null)
{
OnDataLoaded(ds);
}
}
protected string gridMatrix_OnGetRowItemCssClass(object sender, DataRow dr)
{
string roleName = ValidationHelper.GetString(dr["RoleName"], String.Empty);
// Check if all necessary data are available
if (!String.IsNullOrEmpty(roleName) && (SelectedUser != null))
{
string siteName = "";
// Get site name if not global
if (SelectedSite != null)
{
siteName = SelectedSite.SiteName;
}
// Check if user is in specified role
if (SelectedUser.IsInRole(roleName, siteName, (SiteID <= 0), true))
{
return HIGHLIGHTED_ROW_CSS;
}
}
return String.Empty;
}
#endregion
}