public IHttpActionResult Put(int ID, [FromBody] UserModel.UpdateModel upmodel) { if (!ModelState.IsValid || upmodel == null) { return(Content(HttpStatusCode.BadRequest, "Invald Update data.")); } else { try { UserModel model = new UserModel(); WebApplication2201802_dbEntities1 dbEntity = new WebApplication2201802_dbEntities1(); USERTABLE user = dbEntity.USERTABLE.Find(ID); user.UpdateAt = DateTime.Parse(DateTime.UtcNow.AddHours(8).ToString()); user.Arrived = "Yes"; model.Update(ID, user, upmodel); Debug.WriteLine("update success"); return(Ok()); } catch (Exception ex) { return(Content(HttpStatusCode.InternalServerError, "Update fail.")); } } }
// GET: USERTABLEs/Delete/5 public ActionResult Delete(int?id) { //cek parameter id if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } USERTABLE uSERTABLE = db.USERTABLEs.Find(id); //cek id user di database if (uSERTABLE == null) { // id user di database tidak ditemukan return(HttpNotFound()); } //cek super admin if (id == 1) { //super admin tidak bisa didelete return(RedirectToAction("Index", "USERTABLEs")); } return(View(uSERTABLE)); }
public IHttpActionResult Put(Int32 ID) { if (!ModelState.IsValid) { return(Content(HttpStatusCode.BadRequest, "Invald Clear data.")); } else { try { UserModel model = new UserModel(); WebApplication2201802_dbEntities1 dbEntity = new WebApplication2201802_dbEntities1(); USERTABLE user = dbEntity.USERTABLE.Find(ID); Debug.WriteLine("-------------" + user.UserName); user.UpdateAt = DateTime.Parse(DateTime.UtcNow.AddHours(8).ToString()); user.Arrived = "No"; user.TagID = Int64.Parse("0"); model.Clear(ID, user); Debug.WriteLine("Clear success"); return(Ok()); } catch (Exception ex) { return(Content(HttpStatusCode.InternalServerError, "Clear fail.")); } } }
public ActionResult DeleteConfirmed(int id) { USERTABLE uSERTABLE = db.USERTABLEs.Find(id); //hapus user db.USERTABLEs.Remove(uSERTABLE); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Login(USERTABLEVIEW userTableView) { //cek inputan user (kosong) if (userTableView.USER.USERNAME == null || userTableView.USER.USERPASSWORD == null) { if (userTableView.USER.USERNAME == null) { ViewBag.USERNAME = "******"; } if (userTableView.USER.USERPASSWORD == null) { ViewBag.USERPASSWORD = "******"; } return(View()); } userTableView.USER.STATUSID = 1; //cek username di database USERTABLE accountDB = db.USERTABLEs.Where(m => m.USERNAME == userTableView.USER.USERNAME && m.STATUSID == 1).FirstOrDefault(); if (accountDB != null) { //username ada //verifikasi password if (PasswordHelper.VerifyHash(userTableView.USER.USERPASSWORD, "SHA512", accountDB.USERPASSWORD)) { //simpan id user ke session Session["userId"] = Convert.ToInt32(accountDB.USERID); //cek role dari user if (db.USERROLEs.Where(m => m.USERID == accountDB.USERID && m.ROLEID == 1).Select(m => m.ROLEID).FirstOrDefault() != null) { //user merupakan admin Session["userRole"] = "admin"; } else { //user bukan admmin Session["userRole"] = "non-admin"; } return(RedirectToAction("Index", "CONTENTTABLEs", new { area = "" })); } else { //password salah ViewBag.ErrorMessage = "Login Failed"; return(View()); } } else { //username tidak ada ViewBag.ErrorMessage = "Login Failed"; return(View()); } }
public void Create(CreateModel dataModel) { using (WebApplication2201802_dbEntities1 dbEntity = new WebApplication2201802_dbEntities1()) { USERTABLE newUser = new USERTABLE(); newUser.UserId = dataModel.Id; newUser.UserName = dataModel.Name; newUser.AGE = dataModel.Age; newUser.Arrived = dataModel.Arrived; newUser.CreateAt = DateTime.Parse(DateTime.UtcNow.AddHours(8).ToString()); newUser.UpdateAt = DateTime.Parse(DateTime.UtcNow.AddHours(8).ToString()); //newOrder.Deleted = false; dbEntity.USERTABLE.Add(newUser); dbEntity.SaveChanges(); } }
public void Update(int id, USERTABLE newUser, UpdateModel upmodel) { using (WebApplication2201802_dbEntities1 dbEntity = new WebApplication2201802_dbEntities1()) { USERTABLE origin_User = dbEntity.USERTABLE.Find(id); Debug.WriteLine(origin_User.UserId + " " + origin_User.UserName + " " + origin_User.UpdateAt); //dbEntity.USERTABLE.AddOrUpdate(User); /*update_User.UpdatedAt = DateTime.Parse(DateTime.UtcNow.AddHours(8).ToString()); * * if (update_User.Enroll == 0) * { * update_User.Enroll = 1; * }*/ Debug.WriteLine("TagId : " + upmodel.TagId); Debug.WriteLine(newUser.UserId + " " + newUser.UserName + " " + newUser.UpdateAt); //dbEntity.Entry(newUser).CurrentValues.SetValues(origin_User); try { origin_User.UserId = newUser.UserId; origin_User.UserName = newUser.UserName; origin_User.AGE = newUser.AGE; origin_User.Arrived = newUser.Arrived; origin_User.CreateAt = newUser.CreateAt; origin_User.UpdateAt = newUser.UpdateAt; origin_User.TagID = upmodel.TagId; //Debug.WriteLine(dbEntity.Entry(update_User).CurrentValues); Debug.WriteLine(origin_User.UserId + " " + origin_User.UserName + " " + origin_User.UpdateAt + " " + origin_User.TagID); //dbEntity.USERTABLE.Attach(origin_User); //dbEntity.Entry(origin_User).State = EntityState.Modified; //dbEntity.SaveChangesAsync(); dbEntity.SaveChanges(); //Debug.WriteLine(origin_User.UserID + " " + origin_User.UserName + " " + origin_User.Enroll + " " + origin_User.UpdatedAt); } catch (Exception e) { Debug.WriteLine(e); } } }
public ActionResult Create() { //cek session login if (Session["userId"] == null) { return(RedirectToAction("Login", "USERTABLEs", new { area = "" })); } //cek role user if (Session["userRole"].ToString() != "admin") { return(RedirectToAction("Index", "CONTENTTABLEs", new { area = "" })); } USERTABLE uSERTABLE = new USERTABLE(); //masukkan data dari model role ke view model user var Results = from r in db.ROLETABLEs select new { r.ROLEID, r.ROLENAME, Checked = false }; var myViewModel = new USERTABLEVIEW2(); ViewBag.STATUSID = new SelectList(db.STATUSTABLEs, "STATUSID", "STATUSNAME", uSERTABLE.STATUSID); var myCheckBoxList = new List <CHECKBOXVIEW>(); foreach (var item in Results) { myCheckBoxList.Add(new CHECKBOXVIEW { Id = item.ROLEID, Name = item.ROLENAME, Checked = item.Checked }); } myViewModel.userRole = myCheckBoxList; return(View(myViewModel)); }
// GET: USERTABLEs/Details/5 public ActionResult Details(int?id) { // cek id if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } //cek session login if (Session["userId"] == null) { return(RedirectToAction("Login", "USERTABLEs", new { area = "" })); } //cek user role if (Session["userRole"].ToString() != "admin") { //bukan admin, lempar return(RedirectToAction("Index", "CONTENTTABLEs", new { area = "" })); } USERTABLE uSERTABLE = db.USERTABLEs.Where(m => m.USERID == id).Include(m => m.STATUSTABLE).FirstOrDefault(); //cek hasil query if (uSERTABLE == null) { return(HttpNotFound()); } //alasan keamanan uSERTABLE.USERPASSWORD = null; //cek role user var Results = from r in db.ROLETABLEs select new { r.ROLEID, r.ROLENAME, Checked = ((from ur in db.USERROLEs where (ur.USERID == id) & (ur.ROLEID == r.ROLEID) select ur).Count() > 0) }; var myViewModel = new USERTABLEVIEW2(); myViewModel.userId = id.Value; myViewModel.userName = uSERTABLE.USERNAME; myViewModel.userPassword = null; myViewModel.STATUSNAME = uSERTABLE.STATUSTABLE.STATUSNAME; ViewBag.STATUSID = new SelectList(db.STATUSTABLEs, "STATUSID", "STATUSNAME", uSERTABLE.STATUSID); //memasukkan role user ke checkbox var myCheckBoxList = new List <CHECKBOXVIEW>(); foreach (var item in Results) { myCheckBoxList.Add(new CHECKBOXVIEW { Id = item.ROLEID, Name = item.ROLENAME, Checked = item.Checked }); } myViewModel.userRole = myCheckBoxList; return(View(myViewModel)); }
// GET: USERTABLEs/Edit/5 public ActionResult Edit(int?id) { //cek session login if (Session["userId"] == null) { return(RedirectToAction("Login", "USERTABLEs", new { area = "" })); } //cek role user if (Session["userRole"].ToString() != "admin") { //bukan admin, dilempar ke index content return(RedirectToAction("Index", "CONTENTTABLEs", new { area = "" })); } //cek parameter id if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } //cari user melalui id USERTABLE uSERTABLE = db.USERTABLEs.Find(id); //cek user id di database if (uSERTABLE == null) { return(HttpNotFound()); } //user ber-ID 1 adalah super admin sehingga tidak diperbolehkan diedit kecuali dari user ber-ID 1 itu sendiri if (Convert.ToInt32(Session["userId"]) != 1 && id == 1) { return(RedirectToAction("Index", "USERTABLEs")); } //simpan data dari tabel role ke view model user var Results = from r in db.ROLETABLEs select new { r.ROLEID, r.ROLENAME, Checked = ((from ur in db.USERROLEs where (ur.USERID == id) & (ur.ROLEID == r.ROLEID) select ur).Count() > 0) }; var myViewModel = new USERTABLEVIEW2(); myViewModel.userId = id.Value; myViewModel.userName = uSERTABLE.USERNAME; myViewModel.userPassword = null; ViewBag.STATUSID = new SelectList(db.STATUSTABLEs, "STATUSID", "STATUSNAME", uSERTABLE.STATUSID); //madusukkan role ke checkbox var myCheckBoxList = new List <CHECKBOXVIEW>(); foreach (var item in Results) { myCheckBoxList.Add(new CHECKBOXVIEW { Id = item.ROLEID, Name = item.ROLENAME, Checked = item.Checked }); } myViewModel.userRole = myCheckBoxList; return(View(myViewModel)); }