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

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 : 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.             this.AddOaTable();
  20.             this.AddKmTable();
  21.         }
  22.     }
  23.     //動態生成選單表格
  24.     protected void AddOaTable()
  25.     {
  26.         Table mainTable = new Table();
  27.         mainTable.BorderWidth = Unit.Pixel(1);
  28.         mainTable.HorizontalAlign = HorizontalAlign.Center;
  29.         PnlOa.Controls.Add(mainTable);
  30.         SqlConnection conn = dbConnection.getConnection();
  31.         conn.Open();
  32.         ArrayList ids = new ArrayList();
  33.         ArrayList names = new ArrayList();
  34.         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);
  35.         SqlDataReader dr = cmd.ExecuteReader();
  36.         while (dr.Read())
  37.         {
  38.             ids.Add(dr["id"].ToString());
  39.             names.Add(dr["name"].ToString());
  40.         }
  41.         dr.Close();
  42.         for (int i = 0; i < ids.Count; i++)
  43.         {
  44.             TableRow tr1 = new TableRow();
  45.             tr1.BorderWidth = Unit.Pixel(1);
  46.             TableCell td1 = new TableCell();
  47.             td1.Width = Unit.Pixel(150);
  48.             tr1.HorizontalAlign = HorizontalAlign.Left;
  49.             CheckBox cb = new CheckBox();
  50.             cb.ID = "chk" + ids[i].ToString();
  51.             cb.Text = names[i].ToString();
  52.             cb.InputAttributes["parent"] = "1";
  53.             cb.InputAttributes["value"] = ids[i].ToString();
  54.             cb.InputAttributes["onclick"] = "check("+ids[i]+")";
  55.             td1.Controls.Add(cb);
  56.             tr1.Cells.Add(td1);
  57.             td1.BorderWidth = Unit.Pixel(1);
  58.             ArrayList ids1 = new ArrayList();
  59.             ArrayList names1 = new ArrayList();
  60.             cmd = new SqlCommand("select id, name from OA_SYS_CHILD_MENU where parent_id=" + ids[i] + " and is_open='Y' order by name", conn);
  61.             dr = cmd.ExecuteReader();
  62.             while (dr.Read())
  63.             {
  64.                 ids1.Add(dr["id"].ToString());
  65.                 names1.Add(dr["name"].ToString());
  66.             }
  67.             dr.Close();
  68.             Table tb1 = new Table();
  69.             TableCell td2 = new TableCell();
  70.             td2.BorderWidth = Unit.Pixel(1);
  71.             td2.Width = Unit.Pixel(750);
  72.             for (int j = 0; j < ids1.Count; j++)
  73.             {
  74.                 TableRow tr2 = new TableRow();
  75.                 TableCell td3 = new TableCell();
  76.                 td3.BorderWidth = Unit.Pixel(1);
  77.                 td3.Width = Unit.Pixel(200);
  78.                 tr2.HorizontalAlign = HorizontalAlign.Left;
  79.                 cb = new CheckBox();
  80.                 cb.InputAttributes["parent"] = ids[i].ToString();
  81.                 cb.InputAttributes["value"] = ids1[j].ToString();
  82.                 cb.InputAttributes["onclick"] = "check(" + ids1[j] + ")";
  83.                 cb.ID = "chk" + ids1[j].ToString();
  84.                 cb.Text = names1[j].ToString();
  85.                 td3.Controls.Add(cb);
  86.                 tr2.Cells.Add(td3);
  87.                 ArrayList ids2 = new ArrayList();
  88.                 ArrayList names2 = new ArrayList();
  89.                 cmd = new SqlCommand("select id, name from OA_SYS_CHILD_MENU where parent_id=" + ids1[j] + " and is_open='Y' order by name", conn);
  90.                 dr = cmd.ExecuteReader();
  91.                 while (dr.Read())
  92.                 {
  93.                     ids2.Add(dr["id"].ToString());
  94.                     names2.Add(dr["name"].ToString());
  95.                 }
  96.                 dr.Close();
  97.                 Table tb2 = new Table();
  98.                 TableCell td5 = new TableCell();
  99.                 td5.BorderWidth = Unit.Pixel(1);
  100.                 td5.Width = Unit.Pixel(500);
  101.                 for (int k = 0; k < ids2.Count; k++)
  102.                 {
  103.                     TableRow tr3 = new TableRow();
  104.                     TableCell td4 = new TableCell();
  105.                     td4.BorderWidth = Unit.Pixel(1);
  106.                     td4.Width = Unit.Pixel(220);
  107.                     tr3.HorizontalAlign = HorizontalAlign.Left;
  108.                     cb = new CheckBox();
  109.                     cb.InputAttributes["parent"] = ids1[j].ToString();
  110.                     cb.InputAttributes["value"] = ids2[k].ToString();
  111.                     cb.InputAttributes["onclick"] = "check(" + ids2[k] + ")";
  112.                     cb.ID = "chk" + ids2[k].ToString();
  113.                     cb.Text = names2[k].ToString();
  114.                     td4.Controls.Add(cb);
  115.                     tr3.Cells.Add(td4);
  116.                     tb2.Rows.Add(tr3);
  117.                     ArrayList ids3 = new ArrayList();
  118.                     ArrayList names3 = new ArrayList();
  119.                     cmd = new SqlCommand("select id, name from OA_SYS_CHILD_MENU where parent_id=" + ids2[k] + " and is_open='Y' order by name", conn);
  120.                     dr = cmd.ExecuteReader();
  121.                     while (dr.Read())
  122.                     {
  123.                         ids3.Add(dr["id"].ToString());
  124.                         names3.Add(dr["name"].ToString());
  125.                     }
  126.                     dr.Close();
  127.                     TableCell td6 = new TableCell();
  128.                     td6.VerticalAlign = VerticalAlign.Top;
  129.                     Table tb3 = new Table();
  130.                     for (int l = 0; l < ids3.Count; l++)
  131.                     {
  132.                         TableRow tr4 = new TableRow();
  133.                         TableCell td7 = new TableCell();
  134.                         td7.BorderWidth = Unit.Pixel(1);
  135.                         td7.Width = Unit.Pixel(280);
  136.                         td7.VerticalAlign = VerticalAlign.Top;
  137.                         tr3.HorizontalAlign = HorizontalAlign.Left;
  138.                         cb = new CheckBox();
  139.                         cb.InputAttributes["parent"] = ids2[k].ToString();
  140.                         cb.InputAttributes["value"] = ids3[l].ToString();
  141.                         cb.InputAttributes["onclick"] = "check(" + ids3[l] + ")";
  142.                         cb.ID = "chk" + ids3[l].ToString();
  143.                         cb.Text = names3[l].ToString();
  144.                         td7.Controls.Add(cb);
  145.                         tr4.Cells.Add(td7);
  146.                         tb3.Rows.Add(tr4);
  147.                     }
  148.                     td6.Controls.Add(tb3);
  149.                     tr3.Cells.Add(td6);
  150.                 }
  151.                 td5.Controls.Add(tb2);
  152.                 tr2.Cells.Add(td5);
  153.                 tb1.Rows.Add(tr2);
  154.             }
  155.             td2.Controls.Add(tb1);
  156.             tr1.Cells.Add(td2);
  157.             mainTable.Rows.Add(tr1);
  158.         }
  159.         mainTable.Rows.Add(new TableRow());
  160.         conn.Close();
  161.     }
  162.     protected void AddKmTable()
  163.     {
  164.         Table mainTable = new Table();
  165.         mainTable.ID = "TblKm";
  166.         mainTable.BorderWidth = Unit.Pixel(1);
  167.         mainTable.HorizontalAlign = HorizontalAlign.Center;
  168.         PnlKm.Controls.Add(mainTable);
  169.         SqlConnection conn = dbConnection.getConnection();
  170.         conn.Open();
  171.         ArrayList ids = new ArrayList();
  172.         ArrayList names = new ArrayList();
  173.         SqlCommand cmd = new SqlCommand("select id, name from OA_SYS_CHILD_MENU where parent_id=77 and is_open='Y'  order by name", conn);
  174.         SqlDataReader dr = cmd.ExecuteReader();
  175.         while (dr.Read())
  176.         {
  177.             ids.Add(dr["id"].ToString());
  178.             names.Add(dr["name"].ToString());
  179.         }
  180.         dr.Close();
  181.         for (int i = 0; i < ids.Count; i++)
  182.         {
  183.             TableRow tr1 = new TableRow();
  184.             tr1.BorderWidth = Unit.Pixel(1);
  185.             TableCell td1 = new TableCell();
  186.             td1.Width = Unit.Pixel(150);
  187.             tr1.HorizontalAlign = HorizontalAlign.Left;
  188.             CheckBox cb = new CheckBox();
  189.             cb.ID = "chk" + ids[i].ToString();
  190.             cb.Text = names[i].ToString();
  191.             cb.InputAttributes["parent"] = "1";
  192.             cb.InputAttributes["value"] = ids[i].ToString();
  193.             cb.InputAttributes["onclick"] = "check(" + ids[i] + ")";
  194.             td1.Controls.Add(cb);
  195.             tr1.Cells.Add(td1);
  196.             td1.BorderWidth = Unit.Pixel(1);
  197.             ArrayList ids1 = new ArrayList();
  198.             ArrayList names1 = new ArrayList();
  199.             cmd = new SqlCommand("select id, name from OA_SYS_CHILD_MENU where parent_id=" + ids[i] + " and is_open='Y' order by name", conn);
  200.             dr = cmd.ExecuteReader();
  201.             while (dr.Read())
  202.             {
  203.                 ids1.Add(dr["id"].ToString());
  204.                 names1.Add(dr["name"].ToString());
  205.             }
  206.             dr.Close();
  207.             Table tb1 = new Table();
  208.             TableCell td2 = new TableCell();
  209.             td2.BorderWidth = Unit.Pixel(1);
  210.             td2.Width = Unit.Pixel(750);
  211.             int max_dir = 0;
  212.             for (int j = 0; j < ids1.Count; j++)
  213.             {
  214.                 TableRow tr2 = new TableRow();
  215.                 TableCell td3 = new TableCell();
  216.                 td3.BorderWidth = Unit.Pixel(1);
  217.                 td3.Width = Unit.Pixel(200);
  218.                 tr2.HorizontalAlign = HorizontalAlign.Left;
  219.                 cb = new CheckBox();
  220.                 cb.InputAttributes["parent"] = ids[i].ToString();
  221.                 cb.InputAttributes["value"] = ids1[j].ToString();
  222.                 cb.InputAttributes["onclick"] = "check(" + ids1[j] + ")";
  223.                 cb.ID = "chk" + ids1[j].ToString();
  224.                 cb.Text = names1[j].ToString();
  225.                 td3.Controls.Add(cb);
  226.                 tr2.Cells.Add(td3);
  227.                 ArrayList ids2 = new ArrayList();
  228.                 ArrayList names2 = new ArrayList();
  229.                 cmd = new SqlCommand("select id, name from OA_SYS_CHILD_MENU where parent_id=" + ids1[j] + " and is_open='Y' order by name", conn);
  230.                 dr = cmd.ExecuteReader();
  231.                 while (dr.Read())
  232.                 {
  233.                     ids2.Add(dr["id"].ToString());
  234.                     names2.Add(dr["name"].ToString());
  235.                 }
  236.                 dr.Close();
  237.                 Table tb2 = new Table();
  238.                 TableCell td5 = new TableCell();
  239.                 td5.BorderWidth = Unit.Pixel(1);
  240.                 td5.Width = Unit.Pixel(500);
  241.                 if (!ids[i].Equals("16"))
  242.                 {
  243.                     for (int k = 0; k < ids2.Count; k++)
  244.                     {
  245.                         TableRow tr3 = new TableRow();
  246.                         TableCell td4 = new TableCell();
  247.                         td4.BorderWidth = Unit.Pixel(1);
  248.                         td4.Width = Unit.Pixel(220);
  249.                         tr3.HorizontalAlign = HorizontalAlign.Left;
  250.                         cb = new CheckBox();
  251.                         cb.InputAttributes["parent"] = ids1[j].ToString();
  252.                         cb.InputAttributes["value"] = ids2[k].ToString();
  253.                         cb.InputAttributes["onclick"] = "check(" + ids2[k] + ")";
  254.                         cb.ID = "chk" + ids2[k].ToString();
  255.                         cb.Text = names2[k].ToString();
  256.                         td4.Controls.Add(cb);
  257.                         tr3.Cells.Add(td4);
  258.                         tb2.Rows.Add(tr3);
  259.                         ArrayList ids3 = new ArrayList();
  260.                         ArrayList names3 = new ArrayList();
  261.                         cmd = new SqlCommand("select id, name from OA_SYS_CHILD_MENU where parent_id=" + ids2[k] + " and is_open='Y' order by name", conn);
  262.                         dr = cmd.ExecuteReader();
  263.                         while (dr.Read())
  264.                         {
  265.                             ids3.Add(dr["id"].ToString());
  266.                             names3.Add(dr["name"].ToString());
  267.                         }
  268.                         dr.Close();
  269.                         TableCell td6 = new TableCell();
  270.                         td6.VerticalAlign = VerticalAlign.Top;
  271.                         Table tb3 = new Table();
  272.                         for (int l = 0; l < ids3.Count; l++)
  273.                         {
  274.                             TableRow tr4 = new TableRow();
  275.                             TableCell td7 = new TableCell();
  276.                             td7.BorderWidth = Unit.Pixel(1);
  277.                             td7.Width = Unit.Pixel(280);
  278.                             td7.VerticalAlign = VerticalAlign.Top;
  279.                             tr3.HorizontalAlign = HorizontalAlign.Left;
  280.                             cb = new CheckBox();
  281.                             cb.InputAttributes["parent"] = ids2[k].ToString();
  282.                             cb.InputAttributes["value"] = ids3[l].ToString();
  283.                             cb.InputAttributes["onclick"] = "check(" + ids3[l] + ")";
  284.                             cb.ID = "chk" + ids3[l].ToString();
  285.                             cb.Text = names3[l].ToString();
  286.                             td7.Controls.Add(cb);
  287.                             tr4.Cells.Add(td7);
  288.                             tb3.Rows.Add(tr4);
  289.                         }
  290.                         td6.Controls.Add(tb3);
  291.                         tr3.Cells.Add(td6);
  292.                     }
  293.                 }
  294.                 else
  295.                 {
  296.                     if (Convert.ToInt32(ids1[j]) > max_dir)
  297.                     {
  298.                         max_dir = Convert.ToInt32(ids1[j]);
  299.                     }
  300.                     TableRow trDir = new TableRow();
  301.                     TableCell tdDir = new TableCell();
  302.                     tdDir.VerticalAlign = VerticalAlign.Top;
  303.                     RadioButtonList rb = new RadioButtonList();
  304.                     rb.ID = "rb" + ids1[j];
  305.                     rb.RepeatDirection = RepeatDirection.Horizontal;
  306.                     ListItem item = new ListItem();
  307.                     item.Selected = true;
  308.                     item.Value = "view" + ids1[j];
  309.                     item.Text = "瀏覽<br><br>";
  310.                     rb.Items.Add(item);
  311.                     item = new ListItem();
  312.                     item.Value = "edit" + ids1[j];
  313.                     item.Text = "編輯<br><br>";
  314.                     rb.Items.Add(item);
  315.                     tdDir.Controls.Add(rb);
  316.                     trDir.Cells.Add(tdDir);
  317.                     tb2.Rows.Add(trDir);
  318.                     MaxDir.Value = max_dir.ToString();
  319.                 }
  320.                 td5.Controls.Add(tb2);
  321.                 tr2.Cells.Add(td5);
  322.                 tb1.Rows.Add(tr2);
  323.             }
  324.             td2.Controls.Add(tb1);
  325.             tr1.Cells.Add(td2);
  326.             mainTable.Rows.Add(tr1);
  327.         }
  328.         mainTable.Rows.Add(new TableRow());
  329.         conn.Close();
  330.     }
  331.     protected void BtnOk_Click(object sender, EventArgs e)
  332.     {
  333.         SqlConnection conn = dbConnection.getConnection();
  334.         conn.Open();
  335.         SqlTransaction tx = conn.BeginTransaction();
  336.         try
  337.         {
  338.             SqlCommand cmd = new SqlCommand("select count(*) from OA_USER_GROUP_MAIN where name='" + TxtName.Text + "'", conn);
  339.             cmd.Transaction = tx;
  340.             int count = Convert.ToInt32(cmd.ExecuteScalar());
  341.             if (count == 0)
  342.             {
  343.                 int main_id = 1;
  344.                 cmd = new SqlCommand("select max(id) from OA_USER_GROUP_MAIN", conn);
  345.                 try
  346.                 {
  347.                     cmd.Transaction = tx;
  348.                     main_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  349.                 }
  350.                 catch { }
  351.                 DateMgr mgr = new DateMgr();
  352.                 string time = mgr.getDateTime();
  353.                 User user = new User();
  354.                 if (Session["user"] != null)
  355.                 {
  356.                     user = (User)Session["user"];
  357.                 }
  358.                 cmd = new SqlCommand("insert into OA_USER_GROUP_MAIN(id, name, department_id, cre_user, cre_date)" +
  359.                     "values(" + main_id + ",'" + TxtName.Text + "',0,'" + user.Emp_name + "','" + time + "')", conn);
  360.                 cmd.Transaction = tx;
  361.                 cmd.ExecuteNonQuery();
  362.                 string[] menus = Menus.Value.Split(new char[] { ',' });
  363.                 for (int i = 0; i < menus.Length; i++)
  364.                 {
  365.                     cmd = new SqlCommand("insert into OA_USER_GROUP_DETAIL(main_id, menu_id)" +
  366.                         "values(" + main_id + ", '" + menus[i] + "')", conn);
  367.                     cmd.Transaction = tx;
  368.                     cmd.ExecuteNonQuery();
  369.                 }
  370.                 //添加規章制度瀏覽權限設定
  371.                 string[] dir_edit = DirEdit.Value.Split(new char[] { ';' });
  372.                 for (int i = 0; i < dir_edit.Length-1; i++)
  373.                 {
  374.                     cmd = new SqlCommand("update OA_USER_GROUP_DETAIL set function_id='edit' where menu_id=" + dir_edit[i]+" and main_id="+main_id, conn);
  375.                     cmd.Transaction = tx;
  376.                     cmd.ExecuteNonQuery();
  377.                 }
  378.                 tx.Commit();
  379.             }
  380.             else
  381.             {
  382.                 this.AddOaTable();
  383.                 this.AddKmTable();
  384.                 TxtName.Focus();
  385.                 Response.Write("<script language='javascript'>alert('已存在相同名稱的權限群組, 請重新輸入');</script>");
  386.             }
  387.         }
  388.         catch (Exception ex)
  389.         {
  390.             tx.Rollback();        
  391.             Response.Write(ex.Message);
  392.         }
  393.         Response.Redirect("authority_list.aspx");
  394.         conn.Close();
  395.     }
  396. }