public void SaveUser(USER user) { using (DeepBlueEntities context = new DeepBlueEntities()) { if (user.UserID == 0) { context.USERs.AddObject(user); } else { //Update user,user account values // Define an ObjectStateEntry and EntityKey for the current object. EntityKey key; object originalItem; key = default(EntityKey); key = context.CreateEntityKey("USERs", user); if (context.TryGetObjectByKey(key, out originalItem)) { context.ApplyCurrentValues(key.EntitySetName, user); } } context.SaveChanges(); } }
public ActionResult UpdateUser(FormCollection collection) { EditUserModel model=new EditUserModel(); ResultModel resultModel=new ResultModel(); this.TryUpdateModel(model); string ErrorMessage=UserNameAvailable(model.Login,model.UserId); if(String.IsNullOrEmpty(ErrorMessage)==false) { ModelState.AddModelError("Login",ErrorMessage); } ErrorMessage=EmailAvailable(model.Email,model.UserId); if(String.IsNullOrEmpty(ErrorMessage)==false) { ModelState.AddModelError("Email",ErrorMessage); } if(model.ChangePassword) { if(string.IsNullOrEmpty(model.Password)) ModelState.AddModelError("Password","Password is required"); } if(ModelState.IsValid) { USER user=AdminRepository.FindUser(model.UserId); if(user==null) { user=new USER(); user.CreatedDate=DateTime.Now; } user.EntityID=Authentication.CurrentEntity.EntityID; user.LastUpdatedDate=DateTime.Now; user.FirstName=model.FirstName; user.LastName=model.LastName; user.MiddleName=model.MiddleName; user.PhoneNumber=model.PhoneNumber; if(model.ChangePassword) { user.PasswordSalt=SecurityExtensions.CreateSalt(); user.PasswordHash=model.Password.CreateHash(user.PasswordSalt); } user.Login=model.Login; user.Email=model.Email; user.Enabled=model.Enabled; user.IsAdmin=model.IsAdmin; IEnumerable<ErrorInfo> errorInfo=AdminRepository.SaveUser(user); if(errorInfo!=null) { resultModel.Result+=ValidationHelper.GetErrorInfo(errorInfo); } else { resultModel.Result="True||"+user.UserID; } } else { foreach(var values in ModelState.Values.ToList()) { foreach(var err in values.Errors.ToList()) { if(string.IsNullOrEmpty(err.ErrorMessage)==false) { resultModel.Result+=err.ErrorMessage+"\n"; } } } } return View("Result",resultModel); }
private IEnumerable<ErrorInfo> Validate(USER user) { return ValidationHelper.Validate(user); }
public ActionResult UpdateEntity(FormCollection collection) { ICacheManager cacheManager=new MemoryCacheManager(); ENTITY model=new ENTITY(); ResultModel resultModel=new ResultModel(); this.TryUpdateModel(model); bool isNewEntity=false; string ErrorMessage=EntityNameAvailable(model.EntityName,model.EntityID); if(String.IsNullOrEmpty(ErrorMessage)==false) { ModelState.AddModelError("EntityName",ErrorMessage); } ErrorMessage=EntityCodeAvailable(model.EntityCode,model.EntityID); if(String.IsNullOrEmpty(ErrorMessage)==false) { ModelState.AddModelError("EntityCode",ErrorMessage); } if(ModelState.IsValid) { ENTITY entity=AdminRepository.FindEntity(model.EntityID); if(entity==null) { entity=new ENTITY(); entity.CreatedDate=DateTime.Now; isNewEntity=true; } entity.EntityName=model.EntityName; entity.EntityCode=model.EntityCode; entity.Enabled=model.Enabled; IEnumerable<ErrorInfo> errorInfo=AdminRepository.SaveEntity(entity); if(errorInfo!=null) { resultModel.Result+=ValidationHelper.GetErrorInfo(errorInfo); } else { resultModel.Result="True||"+entity.EntityID; // Create default user if(isNewEntity) { string username="******"; string password="******"; string firstname="admin"; string lastname="admin"; string email="*****@*****.**"; USER user=new USER(); user.Login=username; user.PasswordSalt=SecurityExtensions.CreateSalt(); user.PasswordHash=password.CreateHash(user.PasswordSalt); user.Email=email; user.FirstName=firstname; user.LastName=lastname; user.EntityID=entity.EntityID; user.CreatedDate=DateTime.Now; user.LastUpdatedDate=DateTime.Now; user.Enabled=true; user.IsAdmin=true; AdminRepository.SaveUser(user); } } // Create entity menu if(AdminRepository.GetEntityMenuCount(entity.EntityID)<=0) { AdminRepository.SaveEntityMenu(entity.EntityID); // Remove entity menu cache cacheManager.RemoveByPattern(string.Format(MenuHelper.ENTITYMENUKEY,entity.EntityID)); } } else { foreach(var values in ModelState.Values.ToList()) { foreach(var err in values.Errors.ToList()) { if(string.IsNullOrEmpty(err.ErrorMessage)==false) { resultModel.Result+=err.ErrorMessage+"\n"; } } } } return View("Result",resultModel); }