using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.Core;
using Mesnac.Action.Base;
using Mesnac.Basic;
using Mesnac.Codd.Session;
using Mesnac.Controls.Base;
namespace Mesnac.Action.Default.Purview
{
///
/// 角色信息加载类
/// 创建人:郑立兵
/// 创建时间:2013-7-26
/// 说明:将角色信息绑定至组合框控件
///
public class BindRole : DatabaseAction, IAction
{
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime);
ICSharpCode.Core.LoggingService.Debug("初始化绑定角色下拉列表");
DbMCControl basRoleControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "BasRole").FirstOrDefault();
if (basRoleControl == null || !basRoleControl.BaseControl.GetType().IsSubclassOf(typeof(ListControl)))
{
ICSharpCode.Core.LoggingService.Warn("角色信息加载 缺少角色组合框控件...");
return;
}
DbHelper dbhelper = NewDbHelper(basRoleControl.DesignSource);
if (dbhelper == null)
{
return;
}
dbhelper.ClearParameter();
dbhelper.CommandType = CommandType.Text;
dbhelper.CommandText = "SELECT * FROM BasRole WHERE DeleteFlag='0' ORDER BY SeqIndex;";
basRoleControl.BaseControl.BindDataSource = dbhelper.ToDataTable();
}
}
///
/// 窗体级权限信息加载类
/// 创建人:郑立兵
/// 创建时间:2013-7-26
/// 说明:将窗体级权限信息加载至树形控件1级节点
///
public class LoadFormPurview : DatabaseAction, IAction
{
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime);
ICSharpCode.Core.LoggingService.Debug("向树控件初始化加载窗体级权限...");
DbMCControl basRolePurviewControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "BasRolePermission").FirstOrDefault();
if (basRolePurviewControl == null || !basRolePurviewControl.BaseControl.GetType().IsSubclassOf(typeof(TreeView)))
{
ICSharpCode.Core.LoggingService.Warn("窗体级权限加载 缺少权限树形控件...");
return;
}
DbHelper dbhelper = NewDbHelper(basRolePurviewControl.DesignSource);
if (dbhelper==null)
{
return;
}
dbhelper.ClearParameter();
dbhelper.CommandType = CommandType.Text;
if (Mesnac.Basic.UserInfo.Instance.RoleGUID == "-99")
{
dbhelper.CommandText = "SELECT * FROM BasForm WHERE DeleteFlag='0' ORDER BY FormText;";
}
else
{
// dbhelper.CommandText = @"select TA.* from BasForm TA
// inner join BasRolePermission TB on TA.GUID = TB.PermissionItemGUID
// where TA.DeleteFlag='0' and TB.RoleGUID = @RoleGUID
// order by TA.FormText";
dbhelper.CommandText = @"select TA.* from BasForm TA
inner join BasInitPermission TB on TA.GUID = TB.PermissionItemGUID
where TA.DeleteFlag='0'
order by TA.FormText";
dbhelper.AddParameter("@RoleGUID", Mesnac.Basic.UserInfo.Instance.RoleGUID);
}
DataTable dt = dbhelper.ToDataTable();
if (dt == null)
{
ICSharpCode.Core.LoggingService.Warn("窗体级权限加载 信息加载失败...");
return;
}
TreeView tv = basRolePurviewControl.BaseControl as TreeView;
tv.Nodes.Clear();
APurview frmPur;
TreeNode nodeL1;
foreach (DataRow dr in dt.Rows)
{
frmPur = new FormPurview(dr["GUID"].ToString(), dr["FormName"].ToString(), dr["FormText"].ToString(), dr["FormType"].ToString());
nodeL1 = new TreeNode(frmPur.PurviewText);
nodeL1.Name = frmPur.PurviewType + frmPur.GUID;
nodeL1.Tag = frmPur;
tv.Nodes.Add(nodeL1);
}
}
}
///
/// 控件级权限信息加载类
/// 创建人:郑立兵
/// 创建时间:2013-7-26
/// 说明:将控件级权限信息加载至树形控件2级节点
///
public class LoadFunctionPurview : DatabaseAction, IAction
{
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime);
ICSharpCode.Core.LoggingService.Debug("向树控件初始化加载控件级权限");
DbMCControl basRolePurviewControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "BasRolePermission").FirstOrDefault();
if (basRolePurviewControl == null || !basRolePurviewControl.BaseControl.GetType().IsSubclassOf(typeof(TreeView)))
{
ICSharpCode.Core.LoggingService.Warn("控件级权限加载 缺少权限树形控件...");
return;
}
DbHelper dbhelper = NewDbHelper(basRolePurviewControl.DesignSource);
if (dbhelper == null)
{
return;
}
dbhelper.ClearParameter();
dbhelper.CommandType = CommandType.Text;
if (Mesnac.Basic.UserInfo.Instance.RoleGUID == "-99")
{
dbhelper.CommandText = "SELECT * FROM BasFormFunction WHERE DeleteFlag='0' ORDER BY FunctionText";
}
else
{
// dbhelper.CommandText = @"select TA.* from BasFormFunction TA
// inner join BasRolePermission TB on TA.GUID = TB.PermissionItemGUID
// where TA.DeleteFlag='0' and TB.RoleGUID = @RoleGUID
// order by TA.FunctionText";
dbhelper.CommandText = @"select TA.* from BasFormFunction TA
inner join BasInitPermission TB on TA.GUID = TB.PermissionItemGUID
where TA.DeleteFlag='0'
order by TA.FunctionText";
dbhelper.AddParameter("@RoleGUID", Mesnac.Basic.UserInfo.Instance.RoleGUID);
}
DataTable dt = dbhelper.ToDataTable();
if (dt == null)
{
ICSharpCode.Core.LoggingService.Warn("控件级权限加载 信息加载失败...");
return;
}
TreeView tv = basRolePurviewControl.BaseControl as TreeView;
APurview formPur, funcPur;
TreeNode nodeL2;
foreach (TreeNode nodeL1 in tv.Nodes)
{
nodeL1.Nodes.Clear();
formPur = nodeL1.Tag as FormPurview;
foreach (DataRow dr in dt.Select("FormGUID='" + formPur.GUID + "'"))
{
funcPur = new FunctionPurview(dr["GUID"].ToString(), dr["FunctionName"].ToString(), dr["FunctionText"].ToString(), dr["FunctionType"].ToString());
nodeL2 = new TreeNode(funcPur.PurviewText);
nodeL2.Tag = funcPur;
nodeL2.Name = funcPur.PurviewType + funcPur.GUID;
nodeL1.Nodes.Add(nodeL2);
}
}
}
}
///
/// 初始化勾选角色的权限信息
/// 创建人:郑立兵
/// 创建时间:2013-7-29
/// 说明:根据角色的权限信息勾选权限树形控件
///
public class InitPurview : DatabaseAction, IAction
{
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime);
ICSharpCode.Core.LoggingService.Debug("在树控件中初始化勾选已有权限");
if (UserInfo.Instance.RoleGUID.Equals("-1") && UserInfo.MustLogin)
{
ICSharpCode.Core.LoggingService.Warn("权限树初始化勾选 用户未登录");
return;
}
if (UserInfo.Instance.RoleGUID.Equals("-99"))
{
Control btnLoadPermission = this.GetControlById("btnLoadPermission");
Control btnSaveInitPermission = this.GetControlById("btnSaveInitPermission");
if (btnLoadPermission != null)
{
btnLoadPermission.Enabled = true;
btnLoadPermission.Visible = true;
}
if (btnSaveInitPermission != null)
{
btnSaveInitPermission.Enabled = true;
btnSaveInitPermission.Visible = true;
}
}
DbMCControl basRoleControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "BasRole").FirstOrDefault();
DbMCControl basRolePurviewControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "BasRolePermission").FirstOrDefault();
if (basRoleControl == null || !basRoleControl.BaseControl.GetType().IsSubclassOf(typeof(ListControl)))
{
ICSharpCode.Core.LoggingService.Warn("权限树初始化勾选 缺少角色组合框控件...");
return;
}
if (basRolePurviewControl == null || !basRolePurviewControl.BaseControl.GetType().IsSubclassOf(typeof(TreeView)))
{
ICSharpCode.Core.LoggingService.Warn("权限树初始化勾选 缺少权限树形控件...");
return;
}
ListControl listCtrl = basRoleControl.BaseControl as ListControl;
TreeView tv = basRolePurviewControl.BaseControl as TreeView;
string roleGUID = listCtrl.SelectedValue == null ? string.Empty : listCtrl.SelectedValue.ToString();
DbHelper dbhelper = NewDbHelper(basRolePurviewControl.DesignSource);
if (dbhelper == null)
{
return;
}
dbhelper.ClearParameter();
dbhelper.CommandType = CommandType.Text;
dbhelper.CommandText = string.Format("SELECT * FROM BasRolePermission WHERE RoleGUID='{0}';", roleGUID);
DataTable dt = dbhelper.ToDataTable();
if (dt == null)
{
ICSharpCode.Core.LoggingService.Warn("权限树初始化勾选 角色的权限信息加载失败...");
return;
}
ClearTreeNodes(tv.Nodes);
foreach (DataRow dr in dt.Rows)
{
CheckTreeNode(tv.Nodes, dr["PermissionType"].ToString() + dr["PermissionItemGUID"].ToString());
}
}
private void ClearTreeNodes(TreeNodeCollection tnc)
{
foreach (TreeNode n in tnc)
{
n.Checked = false;
if (n.Nodes != null)
{
ClearTreeNodes(n.Nodes);
}
}
}
//private void CheckTreeNode(TreeNodeCollection tnc, string NodeName)
//{
// foreach (TreeNode n in tnc)
// {
// if (n.Name == NodeName)
// n.Checked = true;
// if (n.Nodes != null)
// {
// CheckTreeNode(n.Nodes, NodeName);
// }
// }
//}
private void CheckTreeNode(TreeNodeCollection tnc, string NodeName)
{
foreach (TreeNode n in tnc)
{
if (n.Nodes != null && n.Nodes.Count > 0)
{
foreach (TreeNode childNode in n.Nodes)
{
if (childNode.Name == NodeName && childNode.Checked == false)
{
childNode.Checked = true;
}
}
}
else
{
if (n.Name == NodeName && n.Checked == false)
{
n.Checked = true;
}
}
}
}
}
///
/// 保存初始化权限
///
public class SaveInitPurview : DatabaseAction, IAction
{
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime);
ICSharpCode.Core.LoggingService.Debug("保存初始化权限");
//if (UserInfo.Instance.RoleGUID.Equals("-1") && UserInfo.MustLogin)
//{
// ICSharpCode.Core.LoggingService.Warn("保存初始化权限 用户未登录");
// return;
//}
if (!UserInfo.Instance.RoleGUID.Equals("-99"))
{
ICSharpCode.Core.LoggingService.Warn("保存初始化权限 不是开发者账号!");
return;
}
DbMCControl basRoleControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "BasRole").FirstOrDefault();
DbMCControl basRolePurviewControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "BasRolePermission").FirstOrDefault();
if (basRoleControl == null || !basRoleControl.BaseControl.GetType().IsSubclassOf(typeof(ListControl)))
{
ICSharpCode.Core.LoggingService.Warn("保存角色权限 缺少角色组合框控件...");
return;
}
if (basRolePurviewControl == null || !basRolePurviewControl.BaseControl.GetType().IsSubclassOf(typeof(TreeView)))
{
ICSharpCode.Core.LoggingService.Warn("保存角色权限 缺少权限树形控件...");
return;
}
ListControl listCtrl = basRoleControl.BaseControl as ListControl;
TreeView tv = basRolePurviewControl.BaseControl as TreeView;
string roleId = listCtrl.SelectedValue == null ? string.Empty : listCtrl.SelectedValue.ToString();
DbHelper dbhelper = NewDbHelper(basRolePurviewControl.DesignSource);
if (dbhelper == null)
{
return;
}
ClearInitPermission(dbhelper);
SaveInitPermission(dbhelper, tv.Nodes);
string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_Default_Purview_SavePurview_msg1")); //信息保存完毕!
ShowMsg(msg1);
}
#region 初始权限
///
/// 清空初始权限
///
///
private void ClearInitPermission(DbHelper dbHelper)
{
string sqlstr = "truncate table BasInitPermission";
dbHelper.CommandText = sqlstr;
dbHelper.ClearParameter();
dbHelper.ExecuteNonQuery();
}
///
/// 保存初始权限(超级用户调用)
///
///
///
private void SaveInitPermission(DbHelper dbHelper, TreeNodeCollection tnc)
{
foreach (TreeNode n in tnc)
{
if (n.Checked)
{
string sqlstr = "insert into BasInitPermission(GUID,PermissionItemGUID,PermissionType) values(@GUID,@PermissionItemGUID,@PermissionType)";
dbHelper.CommandText = sqlstr;
dbHelper.ClearParameter();
APurview aPurview = n.Tag as APurview;
dbHelper.AddParameter("@GUID", System.Guid.NewGuid().ToString().ToUpper());
dbHelper.AddParameter("@PermissionItemGUID", aPurview.GUID);
dbHelper.AddParameter("@PermissionType", aPurview.PurviewType);
dbHelper.ExecuteNonQuery();
}
if (n.Nodes != null)
{
SaveInitPermission(dbHelper, n.Nodes);
}
}
}
#endregion
}
///
/// 保存角色的权限信息
/// 创建人:郑立兵
/// 创建时间:2013-7-29
/// 说明:根据角色的权限信息勾选权限树形控件
///
public class SavePurview : DatabaseAction, IAction
{
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime);
ICSharpCode.Core.LoggingService.Debug("执行给某角色分配保存权限");
if (UserInfo.Instance.RoleGUID.Equals("-1") && UserInfo.MustLogin)
{
ICSharpCode.Core.LoggingService.Warn("保存角色权限 用户未登录");
return;
}
DbMCControl basRoleControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "BasRole").FirstOrDefault();
DbMCControl basRolePurviewControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "BasRolePermission").FirstOrDefault();
if (basRoleControl == null || !basRoleControl.BaseControl.GetType().IsSubclassOf(typeof(ListControl)))
{
ICSharpCode.Core.LoggingService.Warn("保存角色权限 缺少角色组合框控件...");
return;
}
if (basRolePurviewControl == null || !basRolePurviewControl.BaseControl.GetType().IsSubclassOf(typeof(TreeView)))
{
ICSharpCode.Core.LoggingService.Warn("保存角色权限 缺少权限树形控件...");
return;
}
ListControl listCtrl = basRoleControl.BaseControl as ListControl;
TreeView tv = basRolePurviewControl.BaseControl as TreeView;
string roleId = listCtrl.SelectedValue == null ? string.Empty : listCtrl.SelectedValue.ToString();
DbHelper dbhelper = NewDbHelper(basRolePurviewControl.DesignSource);
if (dbhelper == null)
{
return;
}
save(dbhelper, tv.Nodes, roleId);
string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_Default_Purview_SavePurview_msg1")); //信息保存完毕!
ShowMsg(msg1);
}
private void save(DbHelper dbhelper, TreeNodeCollection tnc, string roleGUID)
{
foreach (TreeNode n in tnc)
{
if (n.Checked)
{
insert(dbhelper, n.Tag as APurview, roleGUID);
}
else
{
delete(dbhelper, n.Tag as APurview, roleGUID);
}
if (n.Nodes != null)
{
save(dbhelper, n.Nodes, roleGUID);
}
}
}
private void insert(DbHelper dbhelper, APurview aPurview, string roleGUID)
{
if (aPurview == null)
return;
StringBuilder sb = new StringBuilder();
sb.AppendLine("IF NOT EXISTS(SELECT * FROM BasRolePermission WHERE RoleGUID=@RoleGUID AND PermissionItemGUID=@PermissionItemGUID AND PermissionType=@PermissionType)");
sb.AppendLine("INSERT INTO BasRolePermission(GUID,RoleGUID,PermissionItemGUID,PermissionType)");
sb.AppendLine("VALUES(NEWID(),@RoleGUID,@PermissionItemGUID,@PermissionType)");
dbhelper.ClearParameter();
dbhelper.CommandType = CommandType.Text;
dbhelper.CommandText = sb.ToString();
dbhelper.AddParameter("@RoleGUID", roleGUID);
dbhelper.AddParameter("@PermissionItemGUID", aPurview.GUID);
dbhelper.AddParameter("@PermissionType", aPurview.PurviewType);
dbhelper.ExecuteNonQuery();
}
private void delete(DbHelper dbhelper, APurview aPurview, string roleGUID)
{
if (aPurview == null)
return;
dbhelper.ClearParameter();
dbhelper.CommandType = CommandType.Text;
dbhelper.CommandText = "DELETE FROM BasRolePermission WHERE RoleGUID=@RoleGUID AND PermissionItemGUID=@PermissionItemGUID AND PermissionType=@PermissionType";
dbhelper.AddParameter("@RoleGUID", roleGUID);
dbhelper.AddParameter("@PermissionItemGUID", aPurview.GUID);
dbhelper.AddParameter("@PermissionType", aPurview.PurviewType);
dbhelper.ExecuteNonQuery();
}
}
}