public User GetUserByEmail(string email) { using (var context = helper.Create()) return(context.Users .Include(x => x.UserAddress) .Include(x => x.UserPersonalInformation) .FirstOrDefault(x => x.Email == email)); }
public ActionResult Save(string Name, string UserName, string UserPassword, string ConfirmPassword, string Phone, string Email, string Sex) { var ctx = ContextHelper.Create(); User u = new User(); var query = ctx.User.AsQueryable(); //query = query.FirstOrDefault(x => x.UserName==UserName); var model = ctx.User.FirstOrDefault(x => x.UserName == UserName); if (String.IsNullOrEmpty(UserName)) { return(Content("<script >alert('用户名不能为空!');window.history.go(-1);</script>")); } if (model != null) { return(Content("<script >alert('用户名已存在!');window.history.go(-1);</script>")); } else { u.Name = Name; u.UserName = UserName; u.UserPassword = UserPassword; u.ConfirmPassword = ConfirmPassword; u.Phone = Phone; u.Mail = Email; u.Gender = Convert.ToInt32(Sex); u.CreatTime = DateTime.Now; u.LogTime = DateTime.Now; u.LastTime = DateTime.Now; ctx.User.Add(u); ctx.SaveChangesAsync(); //return Json(new Success()); return(Content("<script >alert('注册成功!');window.history.go(-1);</script>")); } }
/// <summary> /// Within this context, cache hit expectations are always enforced - rather than on debug builds only. /// </summary> /// <returns></returns> public static IDisposable EnforceCacheHitRules(bool enforceRules = true) { bool oldValue = _enforceCacheHitRules.Value; _enforceCacheHitRules.Value = enforceRules; return(ContextHelper.Create(() => { _enforceCacheHitRules.Value = oldValue; })); }
/// <summary> /// Impersonate an admin of this tenant and return the context object, which must be disposed. /// </summary> public IDisposable GetTenantAdminContext( ) { if (TenantAdminIdentityInfo == null) { using (GetSystemAdminContext( )) { Role role = Entity.Get <Role>("core:administratorRole"); UserAccount userAccount = role.RoleMembers.FirstOrDefault( ); if (userAccount == null) { throw new Exception($"Tenant '{TenantName}' has no administrator."); } TenantAdminIdentityInfo = new IdentityInfo(userAccount.Id, userAccount.Name); } } // Set context var tenantInfo = new EDC.ReadiNow.Metadata.Tenants.TenantInfo(TenantId); RequestContextData contextData = new RequestContextData(TenantAdminIdentityInfo, tenantInfo, CultureHelper.GetUiThreadCulture(CultureType.Neutral)); RequestContext.SetContext(contextData); // Return delegate to revoke context return(ContextHelper.Create(RequestContext.FreeContext)); }
public static IDisposable AssociateNewCachesWithTenant(long tenantId) { long old = _associateNewCachesWithTenant; _associateNewCachesWithTenant = tenantId; return(ContextHelper.Create(() => { _associateNewCachesWithTenant = old; })); }
public ActionResult Index() { var ctx = ContextHelper.Create(); var user = ctx.User.AsQueryable(); //user = user.FirstOrDefault(x => x.DeleteFlag == false); user = user.Where(x => x.DeleteFlag == false); var list = user.ToList(); return(View(list)); }
/// <summary> /// Updates the 'entry point' setting within a context block. /// </summary> /// <param name="entryPoint">The name of the entry point.</param> /// <param name="append">if set to <c>true</c> [append].</param> /// <returns></returns> private static IDisposable SetEntryPoint(string entryPoint, bool append) { var context = RequestContext.GetContext( ); string oldEntryPoint = _entryPoint; _entryPoint = append ? (oldEntryPoint ?? "") + "/" + entryPoint : entryPoint; return(ContextHelper.Create(() => { _entryPoint = oldEntryPoint; })); }
public ActionResult Contact() { var ctx = ContextHelper.Create(); ctx.Database.CreateIfNotExists();//创建数据库 Contact u = new Contact(); u.Name = "LLLL"; ctx.Contact.Add(u); ctx.SaveChangesAsync(); //return Json(new Success()); return(Content("<script >alert('提交留言成功,谢谢对我们支持,我们会根据您提供联系方式尽快与您取的联系!');window.history.go(-1);</script>")); }
/// <summary> /// Returns a context block that expects all calls to the entity model to be cache hits. /// Basically code within this block is declaring that it has pre-filled the cache, and this mechanism /// is a diagnostic tool to prevent preload queries accidentally missing stuff. /// ONLY enforce it in debug builds!! Otherwise multi-threading cache invalidation may bite you.. /// </summary> /// <param name="expectCacheHits">True to insist, false to negate.</param> /// <returns></returns> public static IDisposable ExpectCacheHits(bool expectCacheHits = true) { if (_enforceCacheHitRules.Value) { bool oldValue = _cacheExpectsHits; _cacheExpectsHits = expectCacheHits; return(ContextHelper.Create(() => { _cacheExpectsHits = oldValue; })); } else { return(null); } }
public ActionResult Save(Hui_Demo.Models.Entities.Post model) { var ctx = ContextHelper.Create(); try { if (model.Title != "调研专业负责人" && model.Title != "评价专业负责人" && model.Title != "专业人员" && model.Title != "管理员") { var entity = new Post(); entity.Title = model.Title; entity.Description = model.Description; entity.SFFZY = model.SFFZY; entity.TypeFlag = model.TypeFlag; // entity.IsSystem = false; // entity.Extra = model.Extra; entity.IsSystem = 0; entity.DeleteFlag = false; ctx.Post.Add(entity); } else { return(Json(new Error("职务中已经存在此职位!"), JsonRequestBehavior.AllowGet)); } var Errors = ctx.GetValidationErrors(); if (Errors.Count() > 0) { string Message = ""; foreach (var E in Errors) { foreach (var e in E.ValidationErrors) { Message += String.Format("{0}:{1}", e.PropertyName, e.ErrorMessage); Message += Environment.NewLine; } } return(Json(new Error(Message))); } else { ctx.SaveChanges(); //UserLog("添加职务:" + model.Title); //return Json(new Success().TabID("PostList"), JsonRequestBehavior.AllowGet); //return Json(new Success(), JsonRequestBehavior.AllowGet); return(Content("<script >alert('提交留言成功,谢谢对我们支持,我们会根据您提供联系方式尽快与您取的联系!');window.location.reload();</script>")); } } catch (Exception ex) { return(Json(new Error(ex.Message), JsonRequestBehavior.AllowGet)); } }
/// <summary> /// Return an IDisposable block that acquires and releases the mutex. /// </summary> public IDisposable AcquireRelease(out bool acquired) { bool acquiredLocal = Acquire( ); acquired = acquiredLocal; return(ContextHelper.Create(() => { if (acquiredLocal) { Release(); } })); }
public ActionResult Index(string Title) { var ctx = ContextHelper.Create(); var query = ctx.Post.AsQueryable(); //AsQueryable是在数据库中查询再返回数据,in if (!String.IsNullOrEmpty(Title)) //根据名字查询 { query = query.Where(x => x.Title.Contains(Title)); ViewBag.Title = Title; } var list = query.ToList(); return(View(list)); }
public async Task <ActionResult> Save(string Name, string Phone, string Message, string Email) { var ctx = ContextHelper.Create(); Contact u = new Contact(); u.Name = Name; u.Phone = Phone; u.Email = Email; u.Message = Message; ctx.Contact.Add(u); await ctx.SaveChangesAsync(); //return Json(new Success()); return(Content("<script >alert('提交留言成功,谢谢对我们支持,我们会根据您提供联系方式尽快与您取的联系!');window.history.go(-1);</script>")); }
public ActionResult Delete(string ID) { var ctx = ContextHelper.Create(); List <int> ids = new List <int>(); foreach (var i in ID.Split(',')) { int id = Convert.ToInt32(i); ids.Add(id); } var list = ctx.Post.Where(x => ids.Contains(x.ID)); ctx.Post.RemoveRange(list); ctx.SaveChangesAsync(); return(Json(new Success(), JsonRequestBehavior.AllowGet)); }
public void Create_Always_CallUserManagerCreate() { using (var context = ContextHelper.Create()) { var model = GetValidCreateUser(); var mock = A.Fake <UserManager <User> >(); var service = Factory.CreateUserService(context, mock); Helper.Suppress(() => service.Create(model, null)); A.CallTo(() => mock.CreateAsync(A <User> ._, model.Password)).MustHaveHappened(); } }
public void Update_WhenModuleProhibited_Throws() { using (var context = ContextHelper.Create()) { var userModule = context.AddUserModule(x => { x.Permission = Permissions.Prohibit; }); var model = GetValidUpdateUserModule(context, userModule); var service = Factory.CreateUserModuleService(context); var ex = Assert.Throws <Exception>( () => service.Update(model, null)); StringAssert.Contains("access", ex.Message.ToLower()); } }
public void Update_WhenUserMissing_Throws() { using (var context = ContextHelper.Create()) { var model = GetValidUpdateUserData(context); model.UserId = "missing"; var service = Factory.CreateUserService(context); var ex = Assert.Throws <Exception>( () => service.Update(model, null)); StringAssert.Contains("not found", ex.Message.ToLower()); } }
public ActionResult Update(User model) { var ctx = ContextHelper.Create(); var entity = ctx.User.FirstOrDefault(x => x.ID == model.ID); entity.Name = model.Name; entity.Phone = model.Phone; entity.Age = model.Age; entity.Company = model.Company; entity.Gender = model.Gender; entity.Mail = model.Mail; entity.Portrait = model.Portrait; ctx.SaveChangesAsync(); return(null); }
public ActionResult Delete(string IDs) { List <int> idList = new List <int>(); foreach (var i in IDs.Split(',')) { var id = Convert.ToInt32(i); idList.Add(id); } var ctx = ContextHelper.Create(); var list = ctx.User.Where(x => idList.Contains(x.ID)); ctx.User.RemoveRange(list); ctx.SaveChangesAsync(); return(null); }
public ActionResult Index(string UserName, string StartTime, string EndTime) { var timer = InitTime(ref StartTime, ref EndTime); var ctx = ContextHelper.Create(); var query = ctx.SysLog.AsQueryable(); //AsQueryable是在数据库中查询再返回数据,in if (!String.IsNullOrEmpty(UserName)) //根据名字查询 { query = query.Where(x => x.UserName.Contains(UserName)); ViewBag.UserName = UserName; } query = query.Where(x => x.LogTime >= timer.StartTime && x.LogTime <= timer.EndTime); var list = query.ToList(); return(View(list)); }
public void Update_WhenNonexistentModule_IgnoresIt() { using (var context = ContextHelper.Create()) { var model = GetValidUpdateUserData(context); model.Modules.Add(GetValidUserModule()); var service = Factory.CreateUserService(context); Helper.Suppress(() => service.Update(model, null)); var userModuleWasAdded = context.UserModules.Any(); Assert.False(userModuleWasAdded); } }
public ActionResult Update(string Name, string Phone, int Age, int Gender, string Portrait) //更新个人信息 { var ctx = ContextHelper.Create(); User u = new User(); var user = AccountHelper.GetUser(); var query = ctx.User.AsQueryable(); //query = query.FirstOrDefault(x => x.UserName==UserName); var model = ctx.User.FirstOrDefault(x => x.ID == user.ID); u.Name = Name; u.Phone = Phone; u.Age = Age; u.Gender = Gender; u.Portrait = Portrait; ctx.SaveChangesAsync(); //return Json(new Success()); return(Content("<script >alert('修改成功!');window.history.go(-1);</script>")); }
public void Create_WhenUserNameIsTaken_ValidationError() { using (var context = ContextHelper.Create()) { var user = context.AddUser(); var model = GetValidCreateUser(); model.UserName = user.UserName; var service = Factory.CreateUserService(context); var mock = A.Fake <IValidationErrors>(); Helper.Suppress(() => service.Create(model, mock)); A.CallTo(() => mock.Add(A <string> ._, A <string> .That.Contains("taken"))).MustHaveHappened(); } }
public ActionResult DeleteFlag(string IDs) { //假删 List <int> idList = new List <int>(); foreach (var i in IDs.Split(',')) { int id = Convert.ToInt32(i); idList.Add(id); } var ctx = ContextHelper.Create(); var list = ctx.Post.Where(x => idList.Contains(x.ID)); // ctx.Dish.Where foreach (var i in list) { i.DeleteFlag = true; } ctx.SaveChangesAsync(); return(null); }
public ActionResult Edit(string Name, string Phone,int Age,int Gender, ) //更新个人信息 { var ctx = ContextHelper.Create(); User u = new User(); var query = ctx.User.AsQueryable(); //query = query.FirstOrDefault(x => x.UserName==UserName); var model = ctx.User.FirstOrDefault(x => x.UserName == UserName); u.Name = Name; u.Phone = Phone; u.UserPassword = UserPassword; u.ConfirmPassword = ConfirmPassword; ctx.User.Add(u); ctx.SaveChangesAsync(); //return Json(new Success()); return Content("<script >alert('注册成功!');window.history.go(-1);</script>"); }
public void Update_WhenModulesPermissionDidntChange_DoesNotUpdateIt() { using (var context = ContextHelper.Create()) { var user = context.AddUser(); var module = context.AddUserModule(); var granter = context.AddUser(); var model = GetValidUpdateUserData(user); var moduleModel = GetValidUserModule(module); moduleModel.GranterId = granter.Id; model.Modules.Add(moduleModel); var service = Factory.CreateUserService(context); Helper.Suppress(() => service.Update(model, null)); var userModuleFrromDb = context.UserModules.First(); Assert.AreNotEqual(userModuleFrromDb.GranterId, granter.Id); } }
public ActionResult Login(int?TypeFlag, string UserName, string UserPassword) { UserName = UserName.Trim(); UserPassword = UserPassword.Trim(); //Code = Code.Trim().ToLower(); ; var ctx = ContextHelper.Create(); var UserSession = AccountHelper.GetUser(); var SysLog = new SysLog(); var User = ctx.User.FirstOrDefault(x => x.UserName == UserName && x.DeleteFlag == false); if (User != null) { if (User.UserPassword == UserPassword) { HttpContext.Application.Lock(); string SessionID = Session.SessionID; string CurrentUser = "******" + User.UserName; // HttpContext.Application.Add(CurrentUser,SessionID); HttpContext.Application.Set(CurrentUser, SessionID); HttpContext.Application.UnLock(); AccountHelper.SaveUser(User); //if (Code == Session["VCode"].ToString().ToLower()) //{ //} //else //{ // ViewBag.Message = "验证码输入错误!"; // ViewBag.UserName = UserName.Trim(); // ViewBag.UserPassword = UserPassword.Trim(); // return View(); //} if (User.TypeFlag == Enum.UserTypeFlag.外部) { User.LastTime = User.LogTime; User.LogTime = DateTime.Now; SysLog.Name = User.Name; SysLog.Title = User.UserName; SysLog.LogTime = DateTime.Now; ctx.SysLog.Add(SysLog); ctx.SaveChanges(); return(RedirectToAction("Index", "Home")); } else { User.LastTime = User.LogTime; User.LogTime = DateTime.Now; SysLog.Name = User.Name; SysLog.Title = User.UserName; SysLog.LogTime = DateTime.Now; ctx.SysLog.Add(SysLog); User.LastTime = User.LogTime; User.LogTime = DateTime.Now; ctx.SaveChanges(); return(RedirectToAction("Index", "Home", new { Area = "Manage" })); } } else { ViewBag.Message = "用户名或者密码错误!"; return(View()); } } else { ViewBag.Message = "用户名或者密码错误!"; return(View()); } }
public IList <Product> GetAllProducts() { using (var context = helper.Create()) return(context.Products.ToList()); }
public ActionResult Login(int?TypeFlag, string UserName, string UserPassword, string Code) { SysHelper sys = new SysHelper(); var ctx = ContextHelper.Create(); var SysLog = new Hui_Demo.Models.Entities.SysLog(); var User = ctx.User.FirstOrDefault(x => x.UserName == UserName && x.DeleteFlag == false); if (User != null) { if (User.UserPassword == UserPassword) { HttpContext.Application.Lock(); //使用Lock就能确保了在某一时段所有连接到服务器的用户之中只有一个用户能获得存取或修改 //该Application变量的权限(即对该公共变量进行锁定操作)。其它任何用户想要获得这样的权限 //就必须等当前权限用户结束其锁定或者当前ASP程序终止执行。 string SessionID = Session.SessionID; string CurrentUser = "******" + User.UserName; // HttpContext.Application.Add(CurrentUser,SessionID); HttpContext.Application.Set(CurrentUser, SessionID); HttpContext.Application.UnLock(); AccountHelper.SaveUser(User); Code = Code.ToString().ToLower(); if (Code == Session["VCode"].ToString().ToLower()) { } else { return(Content("<script >alert('验证码错误!');history.go(-1);</script>")); } if (User.TypeFlag == Enum.UserTypeFlag.外部) { User.LogTime = User.LastTime; //Logtime 记录的是上上次的时间 User.LastTime = DateTime.Now; SysLog.Name = User.Name; SysLog.UserName = User.UserName; SysLog.LogTime = DateTime.Now; SysLog.Title = sys.GetHostAddress(); SysLog.TypeFlag = User.TypeFlag; ctx.SysLog.Add(SysLog); ctx.SaveChanges(); return(RedirectToAction("Index", "Home")); } else { SysLog.Name = User.Name; SysLog.UserName = User.UserName; SysLog.LogTime = DateTime.Now; SysLog.Title = sys.GetHostAddress(); SysLog.TypeFlag = User.TypeFlag; ctx.SysLog.Add(SysLog); User.LogTime = User.LastTime; //Logtime 记录的是上上次的时间 User.LastTime = DateTime.Now; ctx.SaveChanges(); return(RedirectToAction("Index", "Home", new { Area = "Manage" })); } } else { return(Content("<script >alert('用户名或者密码错误!');history.go(-1);</script>")); } } else { return(Content("<script >alert('用户名或者密码错误!');history.go(-1)</script>")); } }