using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using PostgreSqlDotnetCore.Controllers; using PostgreSqlDotnetCore.Models; using System.Net; namespace PostgreSqlDotnetCore.Controllers { public class CityController : BaseController { public CityController(UserManager userManager) : base(userManager) { } // GET: Customer public async Task IndexAsync() { // check for permission UsersClass customerClass = await checkAuthorizationAsync(); ViewBag.isAuthenticated = User.Identity.IsAuthenticated; if (customerClass == null) { return RedirectToAction("AccessDenied", "Error"); } var citiess = await db.CitiesObj.ToListAsync(); // проба на 23.08 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); return View(citiess); } /* public async Task IndexAsync() { // check for permission UsersClass customerClass = await checkAuthorizationAsync(); if (customerClass == null) { return RedirectToAction("AccessDenied", "Error"); } //return View(Enumerable.Empty()); return View(db.CitiesObj.ToList()); } */ // GET: Customer/Details/5 //public ActionResult Details(int? id) public async Task Details(int? id) { if (id == null) { return RedirectToAction("NotExist", "Error"); } CitiesClass cityClass = db.CitiesObj.Find(id); UsersClass customerClass = await getCrrentUser(); ViewBag.isAuthenticated = customerClass; if (cityClass == null) { return RedirectToAction("NotExist", "Error"); } // no access for standard user ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); return View(cityClass); } // GET: Customer/Create //public ActionResult Create() //{ // return View(); //} //public ActionResult Create() public async Task CreateAsync() { UsersClass customerClass = await getCrrentUser(); // set if is authenticated ViewBag.isAuthenticated = customerClass; ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); return View(); } // POST: Customer/Create // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details see https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(include: "id,name")] CitiesClass cityClass) { if (ModelState.IsValid) { db.CitiesObj.Add(cityClass); db.SaveChanges(); return RedirectToAction("Index"); } return View(cityClass); } // GET: Customer/Edit/5 // public ActionResult Edit(int? id) public async Task Edit(int? id) { if (id == null) { return RedirectToAction("NotExist", "Error"); } CitiesClass cityClass = db.CitiesObj.Find(id); //22.08 ViewBag.isAuthenticated = await getCrrentUser(); if (cityClass == null) { return RedirectToAction("NotExist", "Error"); } // no access for standard user ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); return View(cityClass); } // POST: Customer/Edit/5 // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details see https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(include: "id,name")] CitiesClass cityClass) { if (ModelState.IsValid) { db.Entry(cityClass).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(cityClass); } // GET: Customer/Delete/5 // public ActionResult Delete(int? id) public async Task Delete(int? id) { UsersClass customerClass = await checkAuthorizationAsync(); ViewBag.isAuthenticated = await getCrrentUser(); if (id == null) { return RedirectToAction("NotExist", "Error"); } CitiesClass cityClass = db.CitiesObj.Find(id); if (cityClass == null) { return RedirectToAction("NotExist", "Error"); } // no access for standard user ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager); return View(cityClass); } // POST: Customer/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { CitiesClass cityClass = db.CitiesObj.Find(id); db.CitiesObj.Remove(cityClass); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } }