// GET api/logapi public IEnumerable <CLog> Get(int page) { var user = (CSign)HttpContext.Current.Session[ConfigurationManager.AppSettings["AuthSaveKey"]]; if (user == null) { throw new HttpResponseException(new SiginFailureMessage()); } using (var dal = DalBuilder.CreateDal(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, 0)) { CLog[] logs; try { dal.Open(); logs = LogBll.List(dal, page); dal.Close(); } catch (Exception ex) { LogBll.Write(dal, new CLog { LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName), LogContent = string.Format("{0}#{1}", "Log.List", ex.Message), LogType = LogType.系统异常 }); throw new HttpResponseException(new SystemExceptionMessage()); } if (logs == null) { throw new HttpResponseException(new DataNotFoundMessage()); } return(logs); } }
// GET api/contactapi/5 public CCustContact Get(int id) { var user = (CSign)HttpContext.Current.Session[ConfigurationManager.AppSettings["AuthSaveKey"]]; if (user == null) { throw new HttpResponseException(new SiginFailureMessage()); } using (var dal = DalBuilder.CreateDal(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, 0)) { CCustContact custContact; try { dal.Open(); custContact = CustContactBll.Get(dal, id); dal.Close(); } catch (Exception ex) { LogBll.Write(dal, new CLog { LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName), LogContent = string.Format("{0}#{1}", "CustContact.Get", ex.Message), LogType = LogType.系统异常 }); throw new HttpResponseException(new SystemExceptionMessage()); } if (custContact == null) { throw new HttpResponseException(new DataNotFoundMessage()); } return(custContact); } }
// GET api/menuapi public IEnumerable <CMenuCategory> Get() { var user = (CSign)HttpContext.Current.Session[ConfigurationManager.AppSettings["AuthSaveKey"]]; if (user == null) { throw new HttpResponseException(new SiginFailureMessage()); } using (var dal = DalBuilder.CreateDal(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, 0)) { try { dal.Open(); } catch (Exception ex) { LogBll.Write(dal, new CLog { LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName), LogContent = string.Format("{0}#{1}", "Menu.List", ex.Message), LogType = LogType.系统异常 }); throw new HttpResponseException(new SystemExceptionMessage()); } var menus = FunctionBll.LoadMenu(dal, user.GroupCode); if (menus == null) { throw new HttpResponseException(new DataNotFoundMessage()); } return(menus); } }
/// <summary> /// 处理用户登录 /// </summary> /// <param name="filterContext"></param> public void OnAuthorization(AuthorizationContext filterContext) { if (filterContext.HttpContext == null) { throw new Exception("此特性只适合于Web应用程序使用!"); } if (filterContext.HttpContext.Session == null) { throw new Exception("服务器Session不可用!"); } if (filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true)) { return; } using (var dal = DalBuilder.CreateDal(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, 0)) { var httpCookie = filterContext.HttpContext.Request.Cookies["Token"]; if (filterContext.HttpContext.Session[_authSaveKey] != null) { /* if (httpCookie != null) * //更新Token * UpdateToken(filterContext, dal, (CAuthorityModel)filterContext.HttpContext.Session[_authSaveKey]);*/ return; } if (httpCookie != null) { try { dal.Open(); } catch { throw new HttpResponseException(new SystemExceptionMessage()); } //存在Token,进行Token登录 var authorityModel = new CSign(); if (SignBll.Signin(dal, httpCookie.Values["User"], httpCookie.Values["Value"], authorityModel)) { filterContext.HttpContext.Session.Add(ConfigurationManager.AppSettings["AuthSaveKey"], authorityModel); //更新Token UpdateToken(filterContext, dal, authorityModel); } else { filterContext.Result = new RedirectResult(_authUrl); } dal.Close(); } else { filterContext.Result = new RedirectResult(_authUrl); } } }
// POST api/customerapi public CCustomer Post(CCustomer value) { var user = (CSign)HttpContext.Current.Session[ConfigurationManager.AppSettings["AuthSaveKey"]]; if (user == null) { throw new HttpResponseException(new SiginFailureMessage()); } using ( var dal = DalBuilder.CreateDal(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, 0) ) { bool ok; try { dal.Open(); ok = CustomerBll.Create(dal, value, string.Format("{0}-{1}", user.UserCode, user.UserName)); } catch (Exception ex) { if (ex.Message.StartsWith("违反了 UNIQUE KEY 约束")) { throw new HttpResponseException(new PrimaryRepeatedMessge()); } LogBll.Write(dal, new CLog { LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName), LogContent = string.Format("{0}#{1}", "Customer.Post", ex.Message), LogType = LogType.系统异常 }); throw new HttpResponseException(new SystemExceptionMessage()); } if (!ok) { LogBll.Write(dal, new CLog { LogContent = string.Format("新建客户{0}-{1}", value.CustomerCode, value.CustomerName), LogType = LogType.操作失败, LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName) }); throw new HttpResponseException(new DealFailureMessage()); } LogBll.Write(dal, new CLog { LogContent = string.Format("新建客户{0}-{1}", value.CustomerCode, value.CustomerName), LogType = LogType.操作成功, LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName) }); dal.Close(); return(value); } }
public CSign Post(CSign value) { using (var dal = DalBuilder.CreateDal(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, 0)) { bool ok; try { dal.Open(); } catch (Exception ex) { LogBll.Write(dal, new CLog { LogUser = string.Format("{0}-{1}", value.UserCode, value.UserName), LogContent = string.Format("{0}#{1}", "Signin", ex.Message), LogType = LogType.系统异常 }); throw new HttpResponseException(new SystemExceptionMessage()); } var tokenCookie = HttpContext.Current.Request.Cookies["Token"]; if (value.Remain && tokenCookie != null && string.IsNullOrEmpty(value.UserCode) && string.IsNullOrEmpty(value.UPwd)) { //Token不为空 用户名和密码为空,则使用token登录 ok = SignBll.Signin(dal, tokenCookie.Values["User"], tokenCookie.Values["Value"], value); } else { //使用用户名密码登录 ok = SignBll.Signin(dal, value); } if (!ok) { throw new HttpResponseException(new DataNotFoundMessage()); } HttpContext.Current.Session[ConfigurationManager.AppSettings["AuthSaveKey"]] = value; //生成Token var token = Guid.NewGuid().ToString(); SignBll.UpdateToken(dal, token, value.UserCode); HttpContext.Current.Response.Cookies["Token"].Values["User"] = value.UserCode; HttpContext.Current.Response.Cookies["Token"].Values["Value"] = token; HttpContext.Current.Response.Cookies["Token"].Expires = DateTime.Now.AddDays(30); if (value.Remain) { return(value); } HttpContext.Current.Response.Cookies["Token"].Expires = DateTime.Now.AddDays(-1); SignBll.DropToken(dal, value.UserCode); dal.Close(); return(value); } }
public static IEnumerable <UserGroup> Get(int page, int maxCount) { var startIndex = (page - 1) * maxCount; using (var dal = DalBuilder.CreateDal(ConfigurationManager.ConnectionStrings["SYSDB"].ConnectionString, 0)) { int i; var rstDt = dal.Select( "select * from TUSERGROUP", startIndex, maxCount, out i); return(ObjectHelper.BuildObject <UserGroup>(rstDt)); } }
// GET api/userselecterapi public IEnumerable <CLenovoInputOption> Get(string dataSource, string condition) { var user = (CSign)HttpContext.Current.Session[ConfigurationManager.AppSettings["AuthSaveKey"]]; if (user == null) { throw new HttpResponseException(new SiginFailureMessage()); } using (var dal = DalBuilder.CreateDal(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, 0)) { CLenovoInputOption[] options; try { dal.Open(); switch (dataSource) { case "User": options = UserBll.GetLenovoInputOption(dal, condition); break; case "Dept": options = DeptBll.GetLenovoInputOption(dal, condition); break; case "UserGroup": options = UserGroupBll.GetLenovoInputOption(dal, condition); break; default: throw new HttpResponseException(new SystemExceptionMessage()); } dal.Close(); } catch (Exception ex) { LogBll.Write(dal, new CLog { LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName), LogContent = string.Format("{0}#{1}", "LenovoInputer.List", ex.Message), LogType = LogType.系统异常 }); throw new HttpResponseException(new SystemExceptionMessage()); } if (options == null) { throw new HttpResponseException(new DataNotFoundMessage()); } return(options); } }
// DELETE api/userapi/5 public void Delete(int id) { var user = (CSign)HttpContext.Current.Session[ConfigurationManager.AppSettings["AuthSaveKey"]]; if (user == null) { throw new HttpResponseException(new SiginFailureMessage()); } using (var dal = DalBuilder.CreateDal(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, 0)) { bool ok; CUser hisUser; try { dal.Open(); ok = UserBll.Delete(dal, id, out hisUser); } catch (Exception ex) { LogBll.Write(dal, new CLog { LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName), LogContent = string.Format("{0}#{1}", "User.Delete", ex.Message), LogType = LogType.系统异常 }); throw new HttpResponseException(new SystemExceptionMessage()); } if (!ok) { if (hisUser != null) { LogBll.Write(dal, new CLog { LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName), LogContent = string.Format("删除用户{0}-{1}", hisUser.UserCode, hisUser.UserName), LogType = LogType.操作失败 }); } throw new HttpResponseException(new DealFailureMessage()); } LogBll.Write(dal, new CLog { LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName), LogContent = string.Format("删除用户{0}-{1}", hisUser.UserCode, hisUser.UserName), LogType = LogType.操作成功 }); dal.Close(); } }
// PUT api/usergroupapi/5 public CUserGroup Put(int id, CUserGroup value) { var user = (CSign)HttpContext.Current.Session[ConfigurationManager.AppSettings["AuthSaveKey"]]; if (user == null) { throw new HttpResponseException(new SiginFailureMessage()); } using (var dal = DalBuilder.CreateDal(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, 0)) { bool ok; try { dal.Open(); ok = UserGroupBll.Update(dal, value, string.Format("{0}-{1}", user.UserCode, user.UserName)); } catch (Exception ex) { LogBll.Write(dal, new CLog { LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName), LogContent = string.Format("{0}#{1}", "UserGroup.Put", ex.Message), LogType = LogType.系统异常 }); throw new HttpResponseException(new SystemExceptionMessage()); } if (!ok) { LogBll.Write(dal, new CLog { LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName), LogContent = string.Format("修改用户组{0}-{1}", value.GroupCode, value.GroupName), LogType = LogType.操作失败 }); throw new HttpResponseException(new DealFailureMessage()); } LogBll.Write(dal, new CLog { LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName), LogContent = string.Format("修改用户组{0}-{1}", value.GroupCode, value.GroupName), LogType = LogType.操作成功 }); dal.Close(); return(value); } }
// GET api/uniquecheckapi/5 public CCheckResultModel Get(string dataSource, string value) { var user = (CSign)HttpContext.Current.Session[ConfigurationManager.AppSettings["AuthSaveKey"]]; if (user == null) { throw new HttpResponseException(new SiginFailureMessage()); } using (var dal = DalBuilder.CreateDal(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, 0)) { try { CCheckResultModel rst; dal.Open(); switch (dataSource) { case "User.UserCode": rst = UserBll.CheckUserCodeUnique(dal, value); break; case "Customer.CustomerCode": rst = CustomerBll.CheckCustomerCodeUnique(dal, value); break; default: throw new Exception("UniqueCheckInputer错误,没有找到指定DataSource"); } dal.Close(); return(rst); } catch (Exception ex) { LogBll.Write(dal, new CLog { LogUser = string.Format("{0}-{1}", user.UserCode, user.UserName), LogContent = string.Format("{0}#{1}", "UniqueCheck.Get", ex.Message), LogType = LogType.系统异常 }); throw new HttpResponseException(new SystemExceptionMessage()); } } }
public CAuthorityModel Post(CAuthorityModel value) { using (var dal = DalBuilder.CreateDal(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, 0)) { bool ok; try { dal.Open(); } catch { throw new HttpResponseException(new SystemExceptionMessage()); } var httpCookie = HttpContext.Current.Request.Cookies["Token"]; if (value.Remain && httpCookie != null && string.IsNullOrEmpty(value.UserCode) && string.IsNullOrEmpty(value.UPwd)) { //Token不为空 用户名和密码为空,则使用token登录 ok = AuthorityBll.Signin(dal, httpCookie.Value, value); } else { //使用用户名密码登录 ok = AuthorityBll.Signin(dal, value); } if (!ok) { throw new HttpResponseException(new DataNotFoundMessage()); } HttpContext.Current.Session["SignUser"] = value; //生成Token var token = Guid.NewGuid().ToString(); AuthorityBll.UpdateToken(dal, token, value.UserCode); HttpContext.Current.Response.Cookies["Token"].Value = token; HttpContext.Current.Response.Cookies["Token"].Expires = DateTime.Now.AddDays(30); if (value.Remain) { return(value); } HttpContext.Current.Response.Cookies["Token"].Expires = DateTime.Now.AddDays(-1); AuthorityBll.DropToken(dal, value.UserCode); return(value); } }
// GET api/menuapi public IEnumerable <CMenuCategory> Get() { using (var dal = DalBuilder.CreateDal(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString, 0)) { var user = (CSign)HttpContext.Current.Session[ConfigurationManager.AppSettings["AuthSaveKey"]]; try { dal.Open(); if (user == null) { var httpCookie = HttpContext.Current.Request.Cookies["Token"]; if (httpCookie != null) { //存在Token,进行Token登录 if (SignBll.Signin(dal, httpCookie.Values["User"], httpCookie.Values["Value"], user)) { HttpContext.Current.Session.Add(ConfigurationManager.AppSettings["AuthSaveKey"], user); //更新Token var token = Guid.NewGuid().ToString(); HttpContext.Current.Response.Cookies["Token"].Values["User"] = user.UserCode; HttpContext.Current.Response.Cookies["Token"].Values["Value"] = token; HttpContext.Current.Response.Cookies["Token"].Expires = DateTime.Now.AddDays(30); SignBll.UpdateToken(dal, token, user.UserCode); } else { throw new HttpResponseException(new SiginFailureMessage()); } } else { throw new HttpResponseException(new SiginFailureMessage()); } } var menus = FunctionBll.LoadMenu(dal, user.GroupCode); if (menus == null) { throw new HttpResponseException(new DataNotFoundMessage()); } return(menus); } catch (Exception ex) { LogBll.Write(dal, new CLog { LogUser = string.Format("{0}-{1}", user == null?string.Empty:user.UserCode, user == null?string.Empty:user.UserName), LogContent = string.Format("{0}#{1}", "Menu.List", ex.Message), LogType = LogType.系统异常 }); throw new HttpResponseException(new SystemExceptionMessage()); } finally { dal.Close(); } } }