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

main
Last change on this file since 118e414 was 118e414, checked in by ElenaMoskova <elena.moskova99@…>, 3 months 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
Line 
1using Microsoft.AspNetCore.Identity;
2using Microsoft.AspNetCore.Mvc;
3using Microsoft.EntityFrameworkCore;
4using PostgreSqlDotnetCore.Models;
5using Microsoft.AspNetCore.Mvc.Rendering;
6using System.Threading.Tasks;
7
8namespace PostgreSqlDotnetCore.Controllers
9{
10 public class VetCenterController : BaseController
11 {
12 public VetCenterController(UserManager<IdentityUser> userManager) : base(userManager)
13 {
14 }
15
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 }
25 // no access for standard user
26 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
27
28 // Fetch cities for dropdown
29 var citiess = await db.CitiesObj.ToListAsync();
30 ViewBag.Citiess = new SelectList(citiess, "id", "name");
31
32 return View();
33 }
34
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 }*/
47 public async Task<ActionResult> Index()
48 {
49 var vetCenters = await db.VetCentersObj.ToListAsync();
50 ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
51 // no access for standard user
52 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
53
54 // Проверете дали корисникот е администратор или менаџер
55 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
56 // ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
57
58 ViewBag.hasAccess = customerClass != null;
59
60 return View(vetCenters);
61 }
62
63
64 public async Task<ActionResult> Details(int? id)
65 {
66 if (id == null)
67 {
68 return RedirectToAction("NotExist", "Error");
69 }
70
71 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
72 UsersClass customerClass = await getCrrentUser();
73 ViewBag.isAuthenticated = customerClass;
74 if (vetClass == null)
75 {
76 return RedirectToAction("NotExist", "Error");
77 }
78 // no access for standard user
79 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
80
81 return View(vetClass);
82 }
83
84 [HttpPost]
85 [ValidateAntiForgeryToken]
86 public async Task<ActionResult> Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
87 {
88 if (ModelState.IsValid)
89 {
90 db.VetCentersObj.Add(vetClass);
91 await db.SaveChangesAsync();
92 return RedirectToAction("Index");
93 }
94
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
99 return View(vetClass);
100 }
101
102 public async Task<ActionResult> Edit(int? id)
103 {
104 if (id == null)
105 {
106 return RedirectToAction("NotExist", "Error");
107 }
108
109 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
110 if (vetClass == null)
111 {
112 return RedirectToAction("NotExist", "Error");
113 }
114
115 // Check for permission
116 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
117 //UsersClass customerClass = await checkAuthorizationAsync();
118 ViewBag.isAuthenticated = await getCrrentUser();
119 if (customerClass == null)
120 {
121 return RedirectToAction("AccessDenied", "Error");
122 }
123 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
124
125
126 // Fetch cities for dropdown
127 var citiess = await db.CitiesObj.ToListAsync();
128 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
129
130 return View(vetClass);
131 }
132
133 [HttpPost]
134 [ValidateAntiForgeryToken]
135 public async Task<ActionResult> Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
136 {
137 if (ModelState.IsValid)
138 {
139 db.Entry(vetClass).State = EntityState.Modified;
140 await db.SaveChangesAsync();
141 return RedirectToAction("Index");
142 }
143
144 // If model is invalid, repopulate the cities for dropdown
145 var citiess = await db.CitiesObj.ToListAsync();
146 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
147
148 return View(vetClass);
149 }
150
151 public async Task<ActionResult> Delete(int? id) {
152 UsersClass customerClass = await checkAuthorizationAsync();
153
154 ViewBag.isAuthenticated = await getCrrentUser();
155
156 if (id == null)
157 {
158 return RedirectToAction("NotExist", "Error");
159 }
160
161 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
162 if (vetClass == null)
163 {
164 return RedirectToAction("NotExist", "Error");
165 }
166 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
167
168
169 return View(vetClass);
170 }
171
172 [HttpPost, ActionName("Delete")]
173 [ValidateAntiForgeryToken]
174 public async Task<ActionResult> DeleteConfirmed(int id)
175 {
176 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
177 db.VetCentersObj.Remove(vetClass);
178 await db.SaveChangesAsync();
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
191 public async Task<ActionResult> IndexWithSearch(string searchTerm)
192 {
193 if (string.IsNullOrEmpty(searchTerm))
194 {
195 var vetCenters = await db.VetCentersObj.ToListAsync();
196 return View(vetCenters);
197 }
198 else
199 {
200 var searchResults = await db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToListAsync();
201 return View(searchResults);
202 }
203 }
204 }
205}
Note: See TracBrowser for help on using the repository browser.