BasePage.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. //using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. //using System.Xml.Linq;
  12. namespace Com.Jpsoft.Hospital.Web.Common
  13. {
  14. public enum PageFuntionType
  15. {
  16. Insert = 1, Delete, Update, List, Detail, Print, Confirm, Input, Export
  17. }
  18. public class BasePage : System.Web.UI.Page
  19. {
  20. protected override void OnInit(EventArgs e)
  21. {
  22. if (!IsPostBack)
  23. {
  24. CheckRight();
  25. }
  26. base.OnInit(e);
  27. }
  28. private bool _autoCheckRight;
  29. /// <summary>
  30. /// 是否自动判断权限
  31. /// </summary>
  32. protected bool AutoCheckRight
  33. {
  34. get { return _autoCheckRight; }
  35. set { _autoCheckRight = value; }
  36. }
  37. private PageFuntionType _checkRightParam;
  38. /// <summary>
  39. /// 权限判断参数
  40. /// </summary>
  41. public PageFuntionType CheckRightParam
  42. {
  43. get { return _checkRightParam; }
  44. set { _checkRightParam = value; }
  45. }
  46. private string TransEnumType(PageFuntionType type)
  47. {
  48. if (type.ToString() != "0")
  49. return type.ToString().ToLower() + ".aspx";
  50. else
  51. return "";
  52. }
  53. public bool IsAdmin()
  54. {
  55. int uid = 0;
  56. if (!GetSession(out uid))
  57. {
  58. HttpContext.Current.Response.Redirect("~/Module/Sys/MsgBox/NoLogin.htm");
  59. }
  60. Com.Jpsoft.Hospital.BLL.jp_sys_userEx user = new Com.Jpsoft.Hospital.BLL.jp_sys_userEx();
  61. Com.Jpsoft.Hospital.Model.jp_sys_user model = user.GetModel(uid);
  62. if (model == null)
  63. return false;
  64. if (model.user_islock)
  65. return false;
  66. if (model.user_isadmin)
  67. return true;
  68. return false;
  69. }
  70. protected void CheckRight()
  71. {
  72. int uid = 0;
  73. string url = HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"];
  74. string appPath = HttpContext.Current.Request.ApplicationPath;
  75. if (appPath.Length == 1)
  76. {
  77. url = url.Substring(appPath.Length); //网站在根目录(网站)下
  78. }
  79. else
  80. {
  81. url = url.Substring(appPath.Length + 1); //网站在虚拟目录下
  82. }
  83. SetCheckRightParam(url);
  84. Com.Jpsoft.Hospital.BLL.jp_sys_user user = new Com.Jpsoft.Hospital.BLL.jp_sys_user();
  85. Com.Jpsoft.Hospital.BLL.jp_sys_userrole re_userrole = new Com.Jpsoft.Hospital.BLL.jp_sys_userrole();
  86. Com.Jpsoft.Hospital.BLL.jp_sys_rightEx right = new Com.Jpsoft.Hospital.BLL.jp_sys_rightEx();
  87. //判断Session是否为空
  88. //if (Session["uid"] == null)
  89. //{
  90. // HttpContext.Current.Response.Redirect("~/Module/Sys/MsgBox/NoLogin.htm");
  91. //}
  92. ////判断Session是否有效
  93. //if (!int.TryParse(Session["uid"].ToString(), out uid))
  94. //{
  95. // HttpContext.Current.Response.Redirect("~/Module/Sys/MsgBox/NoLogin.htm");
  96. //}
  97. if (!GetSession(out uid))
  98. {
  99. HttpContext.Current.Response.Redirect("~/Module/Sys/MsgBox/NoLogin.htm");
  100. return;
  101. }
  102. if (!right.IsHavRight(uid, url, TransEnumType(CheckRightParam)))
  103. {
  104. HttpContext.Current.Response.Redirect("~/Module/Sys/MsgBox/NoRight.htm");
  105. }
  106. }
  107. private void SetCheckRightParam(string url)
  108. {
  109. if (url.IndexOf("/") > 0)
  110. {
  111. url = url.Substring(url.LastIndexOf("/") + 1);
  112. switch (url)
  113. {
  114. case "Insert.aspx":
  115. CheckRightParam = PageFuntionType.Insert;
  116. break;
  117. case "Delete.aspx":
  118. CheckRightParam = PageFuntionType.Delete;
  119. break;
  120. case "Update.aspx":
  121. CheckRightParam = PageFuntionType.Update;
  122. break;
  123. case "List.aspx":
  124. CheckRightParam = PageFuntionType.List;
  125. break;
  126. case "Detail.aspx":
  127. CheckRightParam = PageFuntionType.Detail;
  128. break;
  129. case "Print.aspx":
  130. CheckRightParam = PageFuntionType.Print;
  131. break;
  132. case "Confirm.aspx":
  133. CheckRightParam = PageFuntionType.Confirm;
  134. break;
  135. case "Input.aspx":
  136. CheckRightParam = PageFuntionType.Input;
  137. break;
  138. case "Export.aspx":
  139. CheckRightParam = PageFuntionType.Export;
  140. break;
  141. }
  142. }
  143. }
  144. protected bool CheckRightByFun(Com.Jpsoft.Hospital.Web.Common.PageFuntionType fun)
  145. {
  146. Com.Jpsoft.Hospital.BLL.jp_sys_user user = new Com.Jpsoft.Hospital.BLL.jp_sys_user();
  147. Com.Jpsoft.Hospital.BLL.jp_sys_userrole re_userrole = new Com.Jpsoft.Hospital.BLL.jp_sys_userrole();
  148. Com.Jpsoft.Hospital.BLL.jp_sys_rightEx right = new Com.Jpsoft.Hospital.BLL.jp_sys_rightEx();
  149. int uid = 0;
  150. if (!GetSession(out uid))
  151. {
  152. HttpContext.Current.Response.Redirect("~/Module/Sys/MsgBox/NoLogin.htm");
  153. }
  154. if (!right.IsHavRight(uid, TransEnumType(fun)))
  155. {
  156. return false;
  157. }
  158. return true;
  159. }
  160. #region 操作Session
  161. public static void SetSession(int uid)
  162. {
  163. HttpContext.Current.Session["uid"] = uid;
  164. }
  165. /// <summary>
  166. /// 取Session值
  167. /// </summary>
  168. /// <param name="uid"></param>
  169. /// <returns>成功,返回一个Session值,失败,返回0</returns>
  170. public static bool GetSession(out int uid)
  171. {
  172. if (HttpContext.Current.Session["uid"] != null)
  173. {
  174. uid = Convert.ToInt32(HttpContext.Current.Session["uid"]);
  175. return true;
  176. }
  177. uid = 0;
  178. return false;
  179. }
  180. public static void ClearSession()
  181. {
  182. //HttpContext.Current.Session["uid"] == null;
  183. }
  184. #endregion
  185. #region 取QueryString
  186. public static bool FetchQueryString(string key, out string value)
  187. {
  188. if (HttpContext.Current.Request.QueryString[key] != null && HttpContext.Current.Request.QueryString[key] != string.Empty)
  189. {
  190. value = HttpContext.Current.Request.QueryString[key];
  191. return true;
  192. }
  193. else
  194. {
  195. value = String.Empty;
  196. return false;
  197. }
  198. }
  199. public static bool FetchQueryString(string key, out int value)
  200. {
  201. if (HttpContext.Current.Request.QueryString[key] != null && HttpContext.Current.Request.QueryString[key] != string.Empty)
  202. {
  203. string _value = HttpContext.Current.Request.QueryString[key];
  204. if (int.TryParse(_value, out value))
  205. {
  206. return true;
  207. }
  208. else
  209. {
  210. return false;
  211. }
  212. }
  213. else
  214. {
  215. value = 0;
  216. return false;
  217. }
  218. }
  219. #endregion
  220. #region 获取安全sql
  221. /// <summary>
  222. /// 获取安全sql
  223. /// </summary>
  224. /// <param name="str"></param>
  225. /// <returns></returns>
  226. public static string GetSafeSql(string str)
  227. {
  228. return str.Replace("'", "''");
  229. }
  230. #endregion
  231. }
  232. }