/// <summary> /// 根据UserID获得没有被分配过的角色 /// </summary> /// <param name="UserID"></param> /// <returns></returns> public List<LabMS.Model.RoleTable> GetRoleTableExpect(int UserID) { LabMS.BLL.UserRole BUR = new LabMS.BLL.UserRole(); List<LabMS.Model.UserRole> list = new List<LabMS.Model.UserRole>(); string str = "UserID="+UserID; list = BUR.GetModelList(str); LabMS.BLL.RoleTable BRT = new LabMS.BLL.RoleTable(); List<LabMS.Model.RoleTable> listRoleT = new List<LabMS.Model.RoleTable>(); string strsql = ""; int RoleID; if (list.Count == 0)//没有分配角色 { strsql = "1=1"; } else { for (int i = 0; i < list.Count; i++) { RoleID =int.Parse(list[i].RoleID.Value.ToString()); if (i == list.Count - 1) { strsql += " RoleID!=" + RoleID; } else { strsql += " RoleID!="+RoleID+" and "; } } } listRoleT = BRT.GetModelList(strsql); return listRoleT; }
/// <summary> /// 根据RoleId取得角色名称RoleName /// </summary> /// <param name="RoleId"></param> /// <returns></returns> public string GetRoleNameByRoleID(int RoleId) { string str = "RoleId="+RoleId; LabMS.BLL.RoleTable BRT = new LabMS.BLL.RoleTable(); string name = BRT.GetModelList(str)[0].RoleName; return name; }
/// <summary> /// 删除 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnCancle_Click(object sender, EventArgs e) { LabMS.BLL.RoleTable BRT = new LabMS.BLL.RoleTable(); bool success = false; CheckBox chk; for (int i = 0; i < gvRole.Rows.Count; i++) { chk = ((CheckBox)gvRole.Rows[i].FindControl("Chk")); if (chk.Checked) { int RoleId = int.Parse(gvRole.DataKeys[i].Value.ToString()); try { BRT.Delete(RoleId); success = true; } catch { } } } if (success == true) { Common.JShelper.JSAlert(this.Page, "", "删除角色成功!"); InitBind(); } else { Common.JShelper.JSAlert(this.Page, "", "删除角色失败!"); } }
/// <summary> /// 根据RoleName找到RoleID的值 /// </summary> /// <param name="RoleName"></param> /// <returns></returns> public int GetRoleIDByRoleName(string RoleName) { int RoleID; string str = "RoleName='"+RoleName+"'"; LabMS.BLL.RoleTable BRt = new LabMS.BLL.RoleTable(); RoleID=int.Parse(BRt.GetModelList(str)[0].RoleId.ToString()); return RoleID; }
/// <summary> /// 保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSave_Click(object sender, EventArgs e) { lbErr.Visible = false; if (string.IsNullOrEmpty(tbRoleName.Text.Trim())) { Common.JShelper.JSAlert(this.Page, "err", "角色名不能为空!"); tbRoleName.Focus(); } else if (string.IsNullOrEmpty(tbRoleDetails.Text.Trim())) { Common.JShelper.JSAlert(this.Page,"err","角色描述不能为空!"); } else { LabMS.BLL.RoleTable BRT = new LabMS.BLL.RoleTable(); LabMS.Model.RoleTable Mrt = new LabMS.Model.RoleTable(); Mrt.RoleName = tbRoleName.Text.Trim(); Mrt.RoleDetails = tbRoleDetails.Text.Trim(); try { int iRid = BRT.Add(Mrt); if (iRid == 0) { lbErr.Visible = true; lbErr.Text = "角色添加失败"; return; } List<string> liststrFunction = GetPrivaily(); LabMS.BLL.RoleFucntion brf = new LabMS.BLL.RoleFucntion(); LabMS.Model.RoleFucntion mrf = new LabMS.Model.RoleFucntion(); for (int i = 0; i < liststrFunction.Count; ++i) { mrf.FCode = liststrFunction[i].ToString(); mrf.RoleID = iRid; brf.Add(mrf); } Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>alert('新增角色成功!');window.location.href='RoleList.aspx';</script>"); } catch { } } }
/// <summary> /// 保存修改 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(tbRoleName.Text.Trim())) { Common.JShelper.JSAlert(this.Page, "err", "角色名不能为空!"); tbRoleName.Focus(); } else if (string.IsNullOrEmpty(tbRoleDetails.Text.Trim())) { Common.JShelper.JSAlert(this.Page, "err", "角色描述不能为空!"); } else { LabMS.BLL.RoleTable BRT = new LabMS.BLL.RoleTable(); LabMS.Model.RoleTable Mrt = new LabMS.Model.RoleTable(); int RoleId = int.Parse(Request.QueryString["RoleId"].ToString()); Mrt.RoleId = RoleId; Mrt.RoleName = EBrt.GetRoleNameByRoleID(RoleId); Mrt.RoleName = tbRoleName.Text.Trim(); Mrt.RoleDetails = tbRoleDetails.Text.Trim(); try { BRT.Update(Mrt); ExtendBLL.RoleFucntion erf = new ExtendBLL.RoleFucntion(); erf.delbyWhere("RoleID=" + RoleId.ToString()); List<string> liststrFunction = GetPrivaily(); LabMS.BLL.RoleFucntion brf = new LabMS.BLL.RoleFucntion(); LabMS.Model.RoleFucntion mrf = new LabMS.Model.RoleFucntion(); for (int i = 0; i < liststrFunction.Count; ++i) { mrf.FCode = liststrFunction[i].ToString(); mrf.RoleID = RoleId; brf.Add(mrf); } //Common.JShelper.JSAlert(this.Page, "", "修改角色成功!"); Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>alert('修改角色成功!');window.location.href='RoleList.aspx';</script>"); } catch { } } }
/// <summary> /// 根据UserID找出已经赋给此用户的角色信息 /// </summary> /// <param name="UserID"></param> /// <returns></returns> public List<LabMS.Model.RoleTable> GetUserRoleByUserID(int UserID) { LabMS.BLL.UserRole BUR = new LabMS.BLL.UserRole(); List<LabMS.Model.UserRole> listMUR = new List<LabMS.Model.UserRole>(); #region 用户是否存在 string str1 = "UserID="+UserID; LabMS.BLL.UserTable BUT = new LabMS.BLL.UserTable(); List<LabMS.Model.UserTable> list = new List<LabMS.Model.UserTable>(); list = BUT.GetModelList(str1); if (list.Count == 0) { throw(new Exception("1"));//用户不存在 } #endregion #region 用户是否有角色 string str = "UserID="+UserID; listMUR=BUR.GetModelList(str);//在UserRole表中找出RoleID if (listMUR.Count == 0) { throw(new Exception("2"));//用户没有对应角色 } #endregion LabMS.BLL.RoleTable BRT = new LabMS.BLL.RoleTable(); List<LabMS.Model.RoleTable> listMRT = new List<LabMS.Model.RoleTable>(); int RoleID; string sqlstr = ""; for (int i = 0; i < listMUR.Count; i++) { RoleID =int.Parse(listMUR[i].RoleID.Value.ToString()); if (i == listMUR.Count - 1) { sqlstr += " RoleId=" + RoleID; } else { sqlstr += " RoleId="+RoleID+" or "; } } listMRT = BRT.GetModelList(sqlstr); return listMRT; }
/// <summary> /// 从数据库中查出所有角色的信息 /// </summary> private void InitBind() { LabMS.BLL.RoleTable BRT = new LabMS.BLL.RoleTable(); gvRole.DataSource = BRT.GetAllList().Tables[0].DefaultView; gvRole.DataBind(); }
/// <summary> /// 搜索 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void search_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(tbRoleNameSerach.Text.Trim())) { Common.JShelper.JSAlert(this.Page, "", "请输入要搜索的角色名称!"); tbRoleNameSerach.Focus(); } else { LabMS.BLL.RoleTable BRT = new LabMS.BLL.RoleTable(); string str = "RoleName like '%" + tbRoleNameSerach.Text.Trim() + "%'"; gvRole.DataSource = BRT.GetModelList(str); gvRole.DataBind(); } }