source: PostgreSqlDotnetCore/Controllers/VetCenterController.cs@ 118e414

main
Last change on this file since 118e414 was 118e414, checked in by ElenaMoskova <elena.moskova99@…>, 5 weeks ago

fix access

implement multiple access pages with different roles
optimize present three structure of BlogPost and Answer

  • Property mode set to 100644
File size: 7.5 KB
RevLine 
[2aea0fd]1using Microsoft.AspNetCore.Identity;
2using Microsoft.AspNetCore.Mvc;
3using Microsoft.EntityFrameworkCore;
4using PostgreSqlDotnetCore.Models;
[d6040ef]5using Microsoft.AspNetCore.Mvc.Rendering;
[57fc402]6using System.Threading.Tasks;
[2aea0fd]7
8namespace PostgreSqlDotnetCore.Controllers
9{
10 public class VetCenterController : BaseController
11 {
12 public VetCenterController(UserManager<IdentityUser> userManager) : base(userManager)
13 {
14 }
15
[57fc402]16 public async Task<ActionResult> Create()
17 {
18 // Set if user is authenticated
19 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
20 ViewBag.isAuthenticated = await getCrrentUser();
21 if (customerClass == null)
22 {
23 return RedirectToAction("AccessDenied", "Error");
24 }
[118e414]25 // no access for standard user
26 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
[d6040ef]27
[57fc402]28 // Fetch cities for dropdown
29 var citiess = await db.CitiesObj.ToListAsync();
30 ViewBag.Citiess = new SelectList(citiess, "id", "name");
[d6040ef]31
[57fc402]32 return View();
33 }
[d6040ef]34
[72b1da2]35 /* public async Task<ActionResult> Index()
36 {
37
38 var vetCenters = await db.VetCentersObj.ToListAsync();
39 ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
40
41 // Check if the user is an admin
42 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
43 ViewBag.hasAccess = customerClass != null;
44
45 return View(vetCenters);
46 }*/
[57fc402]47 public async Task<ActionResult> Index()
[2aea0fd]48 {
[57fc402]49 var vetCenters = await db.VetCentersObj.ToListAsync();
[d6040ef]50 ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
[118e414]51 // no access for standard user
52 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
[d6040ef]53
[72b1da2]54 // Проверете дали корисникот е администратор или менаџер
[57fc402]55 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
[72b1da2]56 // ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
57
[57fc402]58 ViewBag.hasAccess = customerClass != null;
59
[d6040ef]60 return View(vetCenters);
[2aea0fd]61 }
62
[72b1da2]63
[57fc402]64 public async Task<ActionResult> Details(int? id)
[2aea0fd]65 {
66 if (id == null)
67 {
68 return RedirectToAction("NotExist", "Error");
69 }
[57fc402]70
71 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
[118e414]72 UsersClass customerClass = await getCrrentUser();
73 ViewBag.isAuthenticated = customerClass;
[2aea0fd]74 if (vetClass == null)
75 {
76 return RedirectToAction("NotExist", "Error");
77 }
[118e414]78 // no access for standard user
79 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
[57fc402]80
[2aea0fd]81 return View(vetClass);
82 }
83
84 [HttpPost]
85 [ValidateAntiForgeryToken]
[57fc402]86 public async Task<ActionResult> Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
[2aea0fd]87 {
88 if (ModelState.IsValid)
89 {
90 db.VetCentersObj.Add(vetClass);
[57fc402]91 await db.SaveChangesAsync();
[2aea0fd]92 return RedirectToAction("Index");
93 }
94
[57fc402]95 // If model is invalid, repopulate the cities for dropdown
96 var citiess = await db.CitiesObj.ToListAsync();
97 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
98
[2aea0fd]99 return View(vetClass);
100 }
101
[57fc402]102 public async Task<ActionResult> Edit(int? id)
[2aea0fd]103 {
104 if (id == null)
105 {
106 return RedirectToAction("NotExist", "Error");
107 }
[57fc402]108
109 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
[2aea0fd]110 if (vetClass == null)
111 {
112 return RedirectToAction("NotExist", "Error");
113 }
[57fc402]114
115 // Check for permission
[2aea0fd]116 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
[57fc402]117 //UsersClass customerClass = await checkAuthorizationAsync();
118 ViewBag.isAuthenticated = await getCrrentUser();
[2aea0fd]119 if (customerClass == null)
120 {
121 return RedirectToAction("AccessDenied", "Error");
122 }
[118e414]123 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
124
[57fc402]125
126 // Fetch cities for dropdown
[d6040ef]127 var citiess = await db.CitiesObj.ToListAsync();
128 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
[57fc402]129
[2aea0fd]130 return View(vetClass);
131 }
132
133 [HttpPost]
134 [ValidateAntiForgeryToken]
[d6040ef]135 public async Task<ActionResult> Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
[2aea0fd]136 {
137 if (ModelState.IsValid)
138 {
139 db.Entry(vetClass).State = EntityState.Modified;
[d6040ef]140 await db.SaveChangesAsync();
[2aea0fd]141 return RedirectToAction("Index");
142 }
[d6040ef]143
[57fc402]144 // If model is invalid, repopulate the cities for dropdown
[d6040ef]145 var citiess = await db.CitiesObj.ToListAsync();
146 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
147
[2aea0fd]148 return View(vetClass);
149 }
150
[72b1da2]151 public async Task<ActionResult> Delete(int? id) {
152 UsersClass customerClass = await checkAuthorizationAsync();
153
154 ViewBag.isAuthenticated = await getCrrentUser();
155
[2aea0fd]156 if (id == null)
157 {
158 return RedirectToAction("NotExist", "Error");
159 }
[57fc402]160
161 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
[2aea0fd]162 if (vetClass == null)
163 {
164 return RedirectToAction("NotExist", "Error");
165 }
[118e414]166 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
167
[57fc402]168
[2aea0fd]169 return View(vetClass);
170 }
171
172 [HttpPost, ActionName("Delete")]
173 [ValidateAntiForgeryToken]
[57fc402]174 public async Task<ActionResult> DeleteConfirmed(int id)
[2aea0fd]175 {
[57fc402]176 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
[2aea0fd]177 db.VetCentersObj.Remove(vetClass);
[57fc402]178 await db.SaveChangesAsync();
[2aea0fd]179 return RedirectToAction("Index");
180 }
181
182 protected override void Dispose(bool disposing)
183 {
184 if (disposing)
185 {
186 db.Dispose();
187 }
188 base.Dispose(disposing);
189 }
190
[57fc402]191 public async Task<ActionResult> IndexWithSearch(string searchTerm)
[2aea0fd]192 {
193 if (string.IsNullOrEmpty(searchTerm))
194 {
[57fc402]195 var vetCenters = await db.VetCentersObj.ToListAsync();
[2aea0fd]196 return View(vetCenters);
197 }
198 else
199 {
[57fc402]200 var searchResults = await db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToListAsync();
[2aea0fd]201 return View(searchResults);
202 }
203 }
204 }
[d6040ef]205}
Note: See TracBrowser for help on using the repository browser.