动态权限分配

假装没事ソ 提交于 2020-01-25 23:45:03

//后台代码
public partial class role_Role_Per : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            checkrol();
            checkroles();
        }
    }
    /// <summary>
    /// 获取所有权限
    /// </summary>
    private void checkrol()
    {
        DataTable users = rolebll.checkrole();//查询所有权限
        foreach (DataRow dr in users.Rows)//便利循环
        {
            string pid= dr["P_Id"].ToString();
            DataTable usert = rolebll.checkroles(int.Parse(pid));//查询子节点
            foreach (DataRow drs in usert.Rows)
            {
                ListItem li = new ListItem();
                li.Text=drs["P_Name"].ToString();
                li.Value=drs["P_Id"].ToString();
                CheckBoxList1.Items.Add(li);//数据绑定
            }
        }
    }
    /// <summary>
    /// 获取角色所拥有的权限
    /// </summary>
    private void checkroles()
    {
        string rid = Request.QueryString["id"].ToString();//得到角色id
        DataTable users = rolebll.chekrolerrid(int.Parse(rid));//查询此id的权限
        foreach (DataRow dr in users.Rows)
        {
            int pid = int.Parse(dr["RP_Pid"].ToString());
            foreach (ListItem li in CheckBoxList1.Items)//在Chwckboxlist上显示出来
            {
                if (int.Parse(li.Value) == pid)
                {
                    li.Selected = true;
                }
            }             
        }
    }
    protected void Button_Click(object sender, EventArgs e)
    {
        foreach (ListItem li in CheckBoxList1.Items)
        {
            int rid = int.Parse(Request.QueryString["id"].ToString());//得到角色id
            int pid = int.Parse(li.Value.ToString());//得到权限id
            if (li.Selected == true)//添加
            {
                if (rolebll.chekroleridandpid(rid, pid) == false)//判断是否存在此权限
                {
                    if (rolebll.insertrole(rid,pid)==1)//添加权限
                    {
                        Response.Write("<script>alert('分配成功!') </script>");
                    }
                }
            }
            else if (li.Selected == false)//删除
            {
                if (rolebll.chekroleridandpid(rid, pid) == true)//判断是否存在此权限
                {
                    if (rolebll.deleterole(rid, pid) == 1)//删除
                    {
                        Response.Write("<script>alert('分配成功!') </script>");
                    }
                }
            }
        }
    }
}

BLL层
/// <summary>
       /// 查询父节点
       /// </summary>
       /// <returns></returns>
        public static DataTable checkrole()
        {
            return roledal.checkrole();
        }
       /// <summary>
       /// 查询子节点
       /// </summary>
       /// <param name="pid"></param>
       /// <returns></returns>
        public static DataTable checkroles(int pid)
        {
            return roledal.checkroles(pid);
        }
       /// <summary>
       /// 查询角色拥有的权限
       /// </summary>
       /// <param name="rid"></param>
       /// <returns></returns>
        public static DataTable chekrolerrid(int rid)
        {
            return roledal.chekrolerid(rid);
        }
       /// <summary>
        /// 判断节点是否存在
       /// </summary>
       /// <param name="rid"></param>
       /// <param name="pid"></param>
       /// <returns></returns>
        public static bool chekroleridandpid(int rid, int pid)
        {
           return  roledal.checkroleridandpid(rid, pid);
        }
       /// <summary>
       /// 添加一个节点
       /// </summary>
       /// <param name="rid"></param>
       /// <param name="pid"></param>
       /// <returns></returns>
        public static int insertrole(int rid, int pid)
        {
            return roledal.insertrole(rid,pid);
        }
       /// <summary>
       /// 删除一个节点
       /// </summary>
       /// <param name="rid"></param>
       /// <param name="pid"></param>
       /// <returns></returns>
        public static int deleterole(int rid, int pid)
        {
            return roledal.delererole(rid, pid);
        }

DAL层
   /// <summary>
       /// 查询父节点
       /// </summary>
       /// <returns></returns>
        public static DataTable checkrole()
        {
            string sql = "select P_Id,P_Name from Permission where ParentNOdeId=0 ";
            return DBHelper.GetDataSet(sql);
        }
       /// <summary>
       /// 查询子节点
       /// </summary>
       /// <param name="pid"></param>
       /// <returns></returns>
        public static DataTable checkroles(int pid)
        {
            string sql = "select P_Id,P_Name from Permission where ParentNOdeId='" + pid + "'";
            return DBHelper.GetDataSet(sql);
        }
       /// <summary>
       /// 查询角色所拥有的权限
       /// </summary>
       /// <param name="rid"></param>
       /// <returns></returns>
        public static DataTable chekrolerid(int rid)
        {
            string sql = "select RP_Pid from Role_Per where RP_Rid='"+rid+"'";
            return DBHelper.GetDataSet(sql);
        }
       /// <summary>
       /// 判断节点是否存在
       /// </summary>
       /// <param name="rid"></param>
       /// <param name="pid"></param>
       /// <returns></returns>
        public static bool checkroleridandpid(int rid, int pid)
        {
            string sql = "select * from Role_Per where RP_Rid="+rid+"and RP_Pid="+pid+"";
            SqlDataReader dr= DBHelper.GetReader(sql);
            if (dr.Read())
            {
                dr.Close();
                return true;
            }
            else
            {
                dr.Close();
                return false;
            }
        }
       /// <summary>
       /// 添加一个节点
       /// </summary>
       /// <param name="rid"></param>
       /// <param name="pid"></param>
       /// <returns></returns>
        public static int insertrole(int rid, int pid)
        {
            string sql = "insert into Role_Per(RP_Rid,RP_Pid) values("+rid+","+pid+")";
            return DBHelper.ExecuteCommand(sql);
        }
       /// <summary>
       /// 删除一个节点
       /// </summary>
       /// <param name="rid"></param>
       /// <param name="pid"></param>
       /// <returns></returns>
        public static int delererole(int rid, int pid)
        {
            string sql = "delete Role_Per where RP_Rid="+rid+"and RP_Pid"+pid+"";
            return DBHelper.ExecuteCommand(sql);
        }

Model层和表就不写啦太麻烦啦

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!