authority_edit.aspx.cs
上传用户:xrffrp
上传日期:2022-03-25
资源大小:22155k
文件大小:22k
源码类别:

OA系统

开发平台:

ASP/ASPX

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;
  12. public partial class web_system_authority_edit : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.         LoginDAO.CheckLogin(Session, Response, "../", 2);
  17.         if (!IsPostBack)
  18.         {
  19.             ArrayList ids = new ArrayList();
  20.             if (Request.QueryString["g_id"] != null)
  21.             { 
  22.                 ids = this.getMenuIds(Request.QueryString["g_id"]);
  23.             }
  24.             this.AddOaTable(ids);
  25.             this.AddKmTable(ids);
  26.         }
  27.     }
  28.     protected ArrayList getMenuIds(string group_id)
  29.     {
  30.         ArrayList ids = new ArrayList();
  31.         SqlConnection conn = dbConnection.getConnection();
  32.         conn.Open();
  33.         SqlCommand cmd = new SqlCommand("select menu_id from OA_USER_GROUP_DETAIL where main_id="+group_id, conn);
  34.         SqlDataReader dr = cmd.ExecuteReader();
  35.         while (dr.Read())
  36.         {
  37.             ids.Add(dr["menu_id"].ToString());
  38.         }
  39.         dr.Close();
  40.         cmd = new SqlCommand("select name from OA_USER_GROUP_MAIN where id="+group_id, conn);
  41.         TxtName.Text = cmd.ExecuteScalar().ToString();
  42.         conn.Close();
  43.         return ids;
  44.     }
  45.     protected ArrayList GetDirEdit(string main_id)
  46.     {
  47.         ArrayList dirEdits = new ArrayList();
  48.         SqlConnection conn = dbConnection.getConnection();
  49.         conn.Open();
  50.         SqlCommand cmd = new SqlCommand("select menu_id from OA_USER_GROUP_DETAIL where main_id=" +
  51.             main_id + " and function_id='edit'", conn);
  52.         SqlDataReader dr = cmd.ExecuteReader();
  53.         while (dr.Read())
  54.         {
  55.             dirEdits.Add(dr["menu_id"].ToString());
  56.         }
  57.         dr.Close();
  58.         conn.Close();
  59.         return dirEdits;
  60.     }
  61.     //動態生成選單表格
  62.     protected void AddOaTable(ArrayList menus)
  63.     {
  64.         Table mainTable = new Table();
  65.         mainTable.BorderWidth = Unit.Pixel(1);
  66.         mainTable.HorizontalAlign = HorizontalAlign.Center;
  67.         PnlOa.Controls.Add(mainTable);
  68.         SqlConnection conn = dbConnection.getConnection();
  69.         conn.Open();
  70.         ArrayList ids = new ArrayList();
  71.         ArrayList names = new ArrayList();
  72.         SqlCommand cmd = new SqlCommand("select id, name from OA_SYS_CHILD_MENU where id=78 or id=22 or id=24 or id=161 order by name", conn);
  73.         SqlDataReader dr = cmd.ExecuteReader();
  74.         while (dr.Read())
  75.         {
  76.             ids.Add(dr["id"].ToString());
  77.             names.Add(dr["name"].ToString());
  78.         }
  79.         dr.Close();
  80.         for (int i = 0; i < ids.Count; i++)
  81.         {
  82.             TableRow tr1 = new TableRow();
  83.             tr1.BorderWidth = Unit.Pixel(1);
  84.             TableCell td1 = new TableCell();
  85.             td1.Width = Unit.Pixel(150);
  86.             tr1.HorizontalAlign = HorizontalAlign.Left;
  87.             CheckBox cb = new CheckBox();
  88.             cb.ID = "chk" + ids[i].ToString();
  89.             cb.Text = names[i].ToString();
  90.             cb.InputAttributes["parent"] = "1";
  91.             cb.InputAttributes["value"] = ids[i].ToString();
  92.             cb.InputAttributes["onclick"] = "check(" + ids[i] + ")";
  93.             cb.Checked = false;
  94.             for (int m = 0; m < menus.Count; m++)
  95.             { 
  96.                 if(menus[m].Equals(ids[i]))
  97.                 {
  98.                     cb.Checked = true;
  99.                     break;
  100.                 }
  101.             }
  102.             td1.Controls.Add(cb);
  103.             tr1.Cells.Add(td1);
  104.             td1.BorderWidth = Unit.Pixel(1);
  105.             ArrayList ids1 = new ArrayList();
  106.             ArrayList names1 = new ArrayList();
  107.             cmd = new SqlCommand("select id, name from OA_SYS_CHILD_MENU where parent_id=" + ids[i] + " and is_open='Y' order by name", conn);
  108.             dr = cmd.ExecuteReader();
  109.             while (dr.Read())
  110.             {
  111.                 ids1.Add(dr["id"].ToString());
  112.                 names1.Add(dr["name"].ToString());
  113.             }
  114.             dr.Close();
  115.             Table tb1 = new Table();
  116.             TableCell td2 = new TableCell();
  117.             td2.BorderWidth = Unit.Pixel(1);
  118.             td2.Width = Unit.Pixel(750);
  119.             for (int j = 0; j < ids1.Count; j++)
  120.             {
  121.                 TableRow tr2 = new TableRow();
  122.                 TableCell td3 = new TableCell();
  123.                 td3.BorderWidth = Unit.Pixel(1);
  124.                 td3.Width = Unit.Pixel(200);
  125.                 tr2.HorizontalAlign = HorizontalAlign.Left;
  126.                 cb = new CheckBox();
  127.                 cb.InputAttributes["parent"] = ids[i].ToString();
  128.                 cb.InputAttributes["value"] = ids1[j].ToString();
  129.                 cb.InputAttributes["onclick"] = "check(" + ids1[j] + ")";
  130.                 cb.ID = "chk" + ids1[j].ToString();
  131.                 cb.Text = names1[j].ToString();
  132.                 cb.Checked = false;
  133.                 for (int m = 0; m < menus.Count; m++)
  134.                 {
  135.                     if (menus[m].Equals(ids1[j]))
  136.                     {
  137.                         cb.Checked = true;
  138.                         break;
  139.                     }
  140.                 }
  141.                 td3.Controls.Add(cb);
  142.                 tr2.Cells.Add(td3);
  143.                 ArrayList ids2 = new ArrayList();
  144.                 ArrayList names2 = new ArrayList();
  145.                 cmd = new SqlCommand("select id, name from OA_SYS_CHILD_MENU where parent_id=" + ids1[j] + " and is_open='Y' order by name", conn);
  146.                 dr = cmd.ExecuteReader();
  147.                 while (dr.Read())
  148.                 {
  149.                     ids2.Add(dr["id"].ToString());
  150.                     names2.Add(dr["name"].ToString());
  151.                 }
  152.                 dr.Close();
  153.                 Table tb2 = new Table();
  154.                 TableCell td5 = new TableCell();
  155.                 td5.BorderWidth = Unit.Pixel(1);
  156.                 td5.Width = Unit.Pixel(500);
  157.                 for (int k = 0; k < ids2.Count; k++)
  158.                 {
  159.                     TableRow tr3 = new TableRow();
  160.                     TableCell td4 = new TableCell();
  161.                     td4.BorderWidth = Unit.Pixel(1);
  162.                     td4.Width = Unit.Pixel(220);
  163.                     tr3.HorizontalAlign = HorizontalAlign.Left;
  164.                     cb = new CheckBox();
  165.                     cb.InputAttributes["parent"] = ids1[j].ToString();
  166.                     cb.InputAttributes["value"] = ids2[k].ToString();
  167.                     cb.InputAttributes["onclick"] = "check(" + ids2[k] + ")";
  168.                     cb.ID = "chk" + ids2[k].ToString();
  169.                     cb.Text = names2[k].ToString();
  170.                     cb.Checked = false;
  171.                     for (int m = 0; m < menus.Count; m++)
  172.                     {
  173.                         if (menus[m].Equals(ids2[k]))
  174.                         {
  175.                             cb.Checked = true;
  176.                             break;
  177.                         }
  178.                     }
  179.                     td4.Controls.Add(cb);
  180.                     tr3.Cells.Add(td4);
  181.                     tb2.Rows.Add(tr3);
  182.                     ArrayList ids3 = new ArrayList();
  183.                     ArrayList names3 = new ArrayList();
  184.                     cmd = new SqlCommand("select id, name from OA_SYS_CHILD_MENU where parent_id=" + ids2[k] + " and is_open='Y' order by name", conn);
  185.                     dr = cmd.ExecuteReader();
  186.                     while (dr.Read())
  187.                     {
  188.                         ids3.Add(dr["id"].ToString());
  189.                         names3.Add(dr["name"].ToString());
  190.                     }
  191.                     dr.Close();
  192.                     TableCell td6 = new TableCell();
  193.                     td6.VerticalAlign = VerticalAlign.Top;
  194.                     Table tb3 = new Table();
  195.                     for (int l = 0; l < ids3.Count; l++)
  196.                     {
  197.                         TableRow tr4 = new TableRow();
  198.                         TableCell td7 = new TableCell();
  199.                         td7.BorderWidth = Unit.Pixel(1);
  200.                         td7.Width = Unit.Pixel(280);
  201.                         td7.VerticalAlign = VerticalAlign.Top;
  202.                         tr3.HorizontalAlign = HorizontalAlign.Left;
  203.                         cb = new CheckBox();
  204.                         cb.InputAttributes["parent"] = ids2[k].ToString();
  205.                         cb.InputAttributes["value"] = ids3[l].ToString();
  206.                         cb.InputAttributes["onclick"] = "check(" + ids3[l] + ")";
  207.                         cb.ID = "chk" + ids3[l].ToString();
  208.                         cb.Text = names3[l].ToString();
  209.                         cb.Checked = false;
  210.                         for (int m = 0; m < menus.Count; m++)
  211.                         {
  212.                             if (menus[m].Equals(ids3[l]))
  213.                             {
  214.                                 cb.Checked = true;
  215.                                 break;
  216.                             }
  217.                         }
  218.                         td7.Controls.Add(cb);
  219.                         tr4.Cells.Add(td7);
  220.                         tb3.Rows.Add(tr4);
  221.                     }
  222.                     td6.Controls.Add(tb3);
  223.                     tr3.Cells.Add(td6);
  224.                 }
  225.                 td5.Controls.Add(tb2);
  226.                 tr2.Cells.Add(td5);
  227.                 tb1.Rows.Add(tr2);
  228.             }
  229.             td2.Controls.Add(tb1);
  230.             tr1.Cells.Add(td2);
  231.             mainTable.Rows.Add(tr1);
  232.         }
  233.         mainTable.Rows.Add(new TableRow());
  234.         conn.Close();
  235.     }
  236.     protected void AddKmTable(ArrayList menus)
  237.     {
  238.         ArrayList dirEdits = this.GetDirEdit(Request.QueryString["g_id"]);
  239.         Table mainTable = new Table();
  240.         mainTable.BorderWidth = Unit.Pixel(1);
  241.         mainTable.HorizontalAlign = HorizontalAlign.Center;
  242.         PnlKm.Controls.Add(mainTable);
  243.         SqlConnection conn = dbConnection.getConnection();
  244.         conn.Open();
  245.         ArrayList ids = new ArrayList();
  246.         ArrayList names = new ArrayList();
  247.         SqlCommand cmd = new SqlCommand("select id, name from OA_SYS_CHILD_MENU where parent_id=77 and is_open='Y' order by name", conn);
  248.         SqlDataReader dr = cmd.ExecuteReader();
  249.         while (dr.Read())
  250.         {
  251.             ids.Add(dr["id"].ToString());
  252.             names.Add(dr["name"].ToString());
  253.         }
  254.         dr.Close();
  255.         for (int i = 0; i < ids.Count; i++)
  256.         {
  257.             TableRow tr1 = new TableRow();
  258.             tr1.BorderWidth = Unit.Pixel(1);
  259.             TableCell td1 = new TableCell();
  260.             td1.Width = Unit.Pixel(150);
  261.             tr1.HorizontalAlign = HorizontalAlign.Left;
  262.             CheckBox cb = new CheckBox();
  263.             cb.ID = "chk" + ids[i].ToString();
  264.             cb.Text = names[i].ToString();
  265.             cb.InputAttributes["parent"] = "1";
  266.             cb.InputAttributes["value"] = ids[i].ToString();
  267.             cb.InputAttributes["onclick"] = "check(" + ids[i] + ")";
  268.             cb.Checked = false;
  269.             for (int m = 0; m < menus.Count; m++)
  270.             {
  271.                 if (menus[m].Equals(ids[i]))
  272.                 {
  273.                     cb.Checked = true;
  274.                     break;
  275.                 }
  276.             }
  277.             td1.Controls.Add(cb);
  278.             tr1.Cells.Add(td1);
  279.             td1.BorderWidth = Unit.Pixel(1);
  280.             ArrayList ids1 = new ArrayList();
  281.             ArrayList names1 = new ArrayList();
  282.             cmd = new SqlCommand("select id, name from OA_SYS_CHILD_MENU where parent_id=" + ids[i] + " and is_open='Y' order by name", conn);
  283.             dr = cmd.ExecuteReader();
  284.             while (dr.Read())
  285.             {
  286.                 ids1.Add(dr["id"].ToString());
  287.                 names1.Add(dr["name"].ToString());
  288.             }
  289.             dr.Close();
  290.             Table tb1 = new Table();
  291.             TableCell td2 = new TableCell();
  292.             td2.BorderWidth = Unit.Pixel(1);
  293.             td2.Width = Unit.Pixel(750);
  294.             int max_dir = 0;
  295.             for (int j = 0; j < ids1.Count; j++)
  296.             {
  297.                 TableRow tr2 = new TableRow();
  298.                 TableCell td3 = new TableCell();
  299.                 td3.BorderWidth = Unit.Pixel(1);
  300.                 td3.Width = Unit.Pixel(200);
  301.                 tr2.HorizontalAlign = HorizontalAlign.Left;
  302.                 cb = new CheckBox();
  303.                 cb.InputAttributes["parent"] = ids[i].ToString();
  304.                 cb.InputAttributes["value"] = ids1[j].ToString();
  305.                 cb.InputAttributes["onclick"] = "check(" + ids1[j] + ")";
  306.                 cb.ID = "chk" + ids1[j].ToString();
  307.                 cb.Text = names1[j].ToString();
  308.                 cb.Checked = false;
  309.                 for (int m = 0; m < menus.Count; m++)
  310.                 {
  311.                     if (menus[m].Equals(ids1[j]))
  312.                     {
  313.                         cb.Checked = true;
  314.                         break;
  315.                     }
  316.                 }
  317.                 td3.Controls.Add(cb);
  318.                 tr2.Cells.Add(td3);
  319.                 ArrayList ids2 = new ArrayList();
  320.                 ArrayList names2 = new ArrayList();
  321.                 cmd = new SqlCommand("select id, name from OA_SYS_CHILD_MENU where parent_id=" + ids1[j] + " and is_open='Y' order by name", conn);
  322.                 dr = cmd.ExecuteReader();
  323.                 while (dr.Read())
  324.                 {
  325.                     ids2.Add(dr["id"].ToString());
  326.                     names2.Add(dr["name"].ToString());
  327.                 }
  328.                 dr.Close();
  329.                 Table tb2 = new Table();
  330.                 TableCell td5 = new TableCell();
  331.                 td5.BorderWidth = Unit.Pixel(1);
  332.                 td5.Width = Unit.Pixel(500);
  333.                 if (!ids[i].Equals("16"))
  334.                 {
  335.                     for (int k = 0; k < ids2.Count; k++)
  336.                     {
  337.                         TableRow tr3 = new TableRow();
  338.                         TableCell td4 = new TableCell();
  339.                         td4.BorderWidth = Unit.Pixel(1);
  340.                         td4.Width = Unit.Pixel(220);
  341.                         tr3.HorizontalAlign = HorizontalAlign.Left;
  342.                         cb = new CheckBox();
  343.                         cb.InputAttributes["parent"] = ids1[j].ToString();
  344.                         cb.InputAttributes["value"] = ids2[k].ToString();
  345.                         cb.InputAttributes["onclick"] = "check(" + ids2[k] + ")";
  346.                         cb.ID = "chk" + ids2[k].ToString();
  347.                         cb.Text = names2[k].ToString();
  348.                         cb.Checked = false;
  349.                         for (int m = 0; m < menus.Count; m++)
  350.                         {
  351.                             if (menus[m].Equals(ids2[k]))
  352.                             {
  353.                                 cb.Checked = true;
  354.                                 break;
  355.                             }
  356.                         }
  357.                         td4.Controls.Add(cb);
  358.                         tr3.Cells.Add(td4);
  359.                         tb2.Rows.Add(tr3);
  360.                         ArrayList ids3 = new ArrayList();
  361.                         ArrayList names3 = new ArrayList();
  362.                         cmd = new SqlCommand("select id, name from OA_SYS_CHILD_MENU where parent_id=" + ids2[k] + " and is_open='Y' order by name", conn);
  363.                         dr = cmd.ExecuteReader();
  364.                         while (dr.Read())
  365.                         {
  366.                             ids3.Add(dr["id"].ToString());
  367.                             names3.Add(dr["name"].ToString());
  368.                         }
  369.                         dr.Close();
  370.                         TableCell td6 = new TableCell();
  371.                         td6.VerticalAlign = VerticalAlign.Top;
  372.                         Table tb3 = new Table();
  373.                         for (int l = 0; l < ids3.Count; l++)
  374.                         {
  375.                             TableRow tr4 = new TableRow();
  376.                             TableCell td7 = new TableCell();
  377.                             td7.BorderWidth = Unit.Pixel(1);
  378.                             td7.Width = Unit.Pixel(280);
  379.                             td7.VerticalAlign = VerticalAlign.Top;
  380.                             tr3.HorizontalAlign = HorizontalAlign.Left;
  381.                             cb = new CheckBox();
  382.                             cb.InputAttributes["parent"] = ids2[k].ToString();
  383.                             cb.InputAttributes["value"] = ids3[l].ToString();
  384.                             cb.InputAttributes["onclick"] = "check(" + ids3[l] + ")";
  385.                             cb.ID = "chk" + ids3[l].ToString();
  386.                             cb.Text = names3[l].ToString();
  387.                             cb.Checked = false;
  388.                             for (int m = 0; m < menus.Count; m++)
  389.                             {
  390.                                 if (menus[m].Equals(ids3[l]))
  391.                                 {
  392.                                     cb.Checked = true;
  393.                                     break;
  394.                                 }
  395.                             }
  396.                             td7.Controls.Add(cb);
  397.                             tr4.Cells.Add(td7);
  398.                             tb3.Rows.Add(tr4);
  399.                         }
  400.                         td6.Controls.Add(tb3);
  401.                         tr3.Cells.Add(td6);
  402.                     }
  403.                 }
  404.                 else
  405.                 {
  406.                     if (Convert.ToInt32(ids1[j]) > max_dir)
  407.                     {
  408.                         max_dir = Convert.ToInt32(ids1[j]);
  409.                     }
  410.                     TableRow trDir = new TableRow();
  411.                     TableCell tdDir = new TableCell();
  412.                     tdDir.VerticalAlign = VerticalAlign.Top;
  413.                     RadioButtonList rb = new RadioButtonList();
  414.                     rb.ID = "rb" + ids1[j];
  415.                     rb.RepeatDirection = RepeatDirection.Horizontal;
  416.                     ListItem item = new ListItem();
  417.                     item.Selected = true;
  418.                     item.Value = "view" + ids1[j];
  419.                     item.Text = "瀏覽<br><br>";
  420.                     rb.Items.Add(item);
  421.                     item = new ListItem();
  422.                     for (int o = 0; o < dirEdits.Count; o++)
  423.                     {
  424.                         if (dirEdits[o].ToString().Equals(ids1[j]))
  425.                         {
  426.                             item.Selected = true;
  427.                         }
  428.                     }
  429.                     item.Value = "edit" + ids1[j];
  430.                     item.Text = "編輯<br><br>";
  431.                     rb.Items.Add(item);
  432.                     tdDir.Controls.Add(rb);
  433.                     trDir.Cells.Add(tdDir);
  434.                     tb2.Rows.Add(trDir);
  435.                     MaxDir.Value = max_dir.ToString();
  436.                 }
  437.                 td5.Controls.Add(tb2);
  438.                 tr2.Cells.Add(td5);
  439.                 tb1.Rows.Add(tr2);
  440.             }
  441.             td2.Controls.Add(tb1);
  442.             tr1.Cells.Add(td2);
  443.             mainTable.Rows.Add(tr1);
  444.         }
  445.         mainTable.Rows.Add(new TableRow());
  446.         conn.Close();
  447.     }
  448.     protected void BtnOk_Click(object sender, EventArgs e)
  449.     {
  450.         SqlConnection conn = dbConnection.getConnection();
  451.         conn.Open();
  452.         SqlTransaction tx = conn.BeginTransaction();
  453.         try
  454.         {
  455.             string id = "0";
  456.             if (Request.QueryString["g_id"] != null)
  457.             {
  458.                 id = Request.QueryString["g_id"];
  459.             }
  460.             DateMgr mgr = new DateMgr();
  461.             string time = mgr.getDateTime();
  462.             User user = new User();
  463.             if (Session["user"] != null)
  464.             {
  465.                 user = (User)Session["user"];
  466.             }
  467.             SqlCommand cmd = new SqlCommand("update OA_USER_GROUP_MAIN set name='"+TxtName.Text+"', upd_user='"+user.Emp_name+"', upd_date='"+time+"' where id="+id, conn);
  468.             cmd.Transaction = tx;
  469.             cmd.ExecuteNonQuery();
  470.             
  471.             //刪除原來的權限群組
  472.             cmd = new SqlCommand("delete from OA_USER_GROUP_DETAIL where main_id="+id, conn);
  473.             cmd.Transaction = tx;
  474.             cmd.ExecuteNonQuery();
  475.             string[] menus = Menus.Value.Split(new char[] { ',' });
  476.             for (int i = 0; i < menus.Length; i++)
  477.             {
  478.                 try
  479.                 {
  480.                     cmd = new SqlCommand("insert into OA_USER_GROUP_DETAIL(main_id, menu_id)" +
  481.                         "values(" + id + ", " + menus[i] + ")", conn);
  482.                     cmd.Transaction = tx;
  483.                     cmd.ExecuteNonQuery();
  484.                 }
  485.                 catch { }
  486.             }
  487.             //添加規章制度瀏覽權限設定
  488.             string[] dir_edit = DirEdit.Value.Split(new char[] { ';' });
  489.             for (int i = 0; i < dir_edit.Length - 1; i++)
  490.             {
  491.                 cmd = new SqlCommand("update OA_USER_GROUP_DETAIL set function_id='edit' where menu_id=" + dir_edit[i] + " and main_id=" + id, conn);
  492.                 cmd.Transaction = tx;
  493.                 cmd.ExecuteNonQuery();
  494.             }
  495.             tx.Commit();
  496.         }
  497.         catch (Exception ex)
  498.         {
  499.             tx.Rollback();
  500.             Response.Write(ex.Message);
  501.         }
  502.         Response.Redirect("authority_list.aspx");
  503.         conn.Close();
  504.     }
  505. }