public bool CheckIfUserHasAccessToOperateParohii(int? cif, Parohii parohii) { var loggedUserId = this.User.Identity.GetUserId(); var loggedUser = this.Data.Users .All() .FirstOrDefault(u => u.Id == loggedUserId); if (loggedUser.ProtopopiateId != null) { if (cif != null) { var parohiiFromDb = this.Data.Parohiis .All() .FirstOrDefault(p => p.Cif == cif); if (parohiiFromDb.ProtopopiateId == loggedUser.ProtopopiateId) { return true; } } else { if (parohii.ProtopopiateId == loggedUser.ProtopopiateId) { return true; } } } return false; }
public ActionResult Edit(Parohii parohii) { if (!CheckIfUserHasAccessToOperateParohii(null, parohii) && !this.User.IsInRole("Admin")) { return RedirectToAction("Index"); } if (ModelState.IsValid) { db.Entry(parohii).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(parohii); }
public ActionResult Create(Parohii parohii) { if (!CheckIfUserHasAccessToOperateParohii(null, parohii) && !this.User.IsInRole("Admin")) { return RedirectToAction("Index"); } if (ModelState.IsValid) { var mitropolii = this.Data.Mitropoliis.All().FirstOrDefault(m => m.MitropoliiId == 2); var eparhii = this.Data.Eparhiis.All().FirstOrDefault(e => e.EparhiiId == 21); if (mitropolii != null) { parohii.MitropoliiId = 2; } if (eparhii != null) { parohii.EparhiiId = 21; } db.Parohiis.Add(parohii); db.SaveChanges(); return RedirectToAction("Index"); } return View(parohii); }