Ignore:
Timestamp:
08/22/24 01:46:17 (5 weeks ago)
Author:
ElenaMoskova <elena.moskova99@…>
Branches:
main
Children:
72b1da2
Parents:
d6040ef
Message:

Аsync, access permission, and other fixes.

Regulation of access permissions. Which fields can be accessed by different users.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • PostgreSqlDotnetCore/Controllers/VetCenterController.cs

    rd6040ef r57fc402  
    44using PostgreSqlDotnetCore.Models;
    55using Microsoft.AspNetCore.Mvc.Rendering;
    6 using System.Data;
    7 using System.Net;
     6using System.Threading.Tasks;
    87
    98namespace PostgreSqlDotnetCore.Controllers
     
    1514        }
    1615
    17          public async Task<ActionResult> Create()
    18          {
    19              UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
    20              if (customerClass == null)
    21              {
    22                  return RedirectToAction("AccessDenied", "Error");
    23              }
    24              var citiess = await db.CitiesObj.ToListAsync();
     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            }
    2525
    26              ViewBag.Citiess = new SelectList(citiess, "id", "name");
     26            // Fetch cities for dropdown
     27            var citiess = await db.CitiesObj.ToListAsync();
     28            ViewBag.Citiess = new SelectList(citiess, "id", "name");
    2729
    28              return View();
    29          }
     30            return View();
     31        }
    3032
    31         // GET: Customer
    32         /* public ActionResult Index()
    33          {
    34              return View(db.VetCentersObj.ToList());
    35          }*/
     33        public async Task<ActionResult> Index()
     34        {
     35            var vetCenters = await db.VetCentersObj.ToListAsync();
     36            ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
    3637
    37         public ActionResult Index()
    38         {
    39             var vetCenters = db.VetCentersObj.ToList();
    40 
    41             // Составување на списокот на ветеринарни центри и проверка на автентикацијата
    42             ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
     38            // Check if the user is an admin
     39            UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
     40            ViewBag.hasAccess = customerClass != null;
    4341
    4442            return View(vetCenters);
    4543        }
    4644
    47 
    48         // GET: Customer/Details/5
    49         public ActionResult Details(int? id)
     45        public async Task<ActionResult> Details(int? id)
    5046        {
    5147            if (id == null)
     
    5349                return RedirectToAction("NotExist", "Error");
    5450            }
    55             VetCenter vetClass = db.VetCentersObj.Find(id);
     51
     52            VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
    5653            if (vetClass == null)
    5754            {
    5855                return RedirectToAction("NotExist", "Error");
    59             }
    60             return View(vetClass);
    61         }
    62 
    63         // GET: Customer/Create
    64         /*  public async Task<ActionResult> CreateAsync()
    65           {
    66               // check for permission
    67               UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
    68               if (customerClass == null)
    69               {
    70                   return RedirectToAction("AccessDenied", "Error");
    71               }
    72               return View();
    73           }*/
    74 
    75         // POST: Customer/Create
    76         // To protect from overposting attacks, enable the specific properties you want to bind to, for
    77         // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
    78         [HttpPost]
    79         [ValidateAntiForgeryToken]
    80  
    81 
    82         public ActionResult Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
    83         {
    84             if (ModelState.IsValid)
    85             {
    86                 db.VetCentersObj.Add(vetClass);
    87                 db.SaveChanges();
    88                 return RedirectToAction("Index");
    8956            }
    9057
     
    9259        }
    9360
    94         // GET: Customer/Edit/5
    95         public async Task<ActionResult> EditAsync(int? id)
     61        [HttpPost]
     62        [ValidateAntiForgeryToken]
     63        public async Task<ActionResult> Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
     64        {
     65            if (ModelState.IsValid)
     66            {
     67                db.VetCentersObj.Add(vetClass);
     68                await db.SaveChangesAsync();
     69                return RedirectToAction("Index");
     70            }
     71
     72            // If model is invalid, repopulate the cities for dropdown
     73            var citiess = await db.CitiesObj.ToListAsync();
     74            ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
     75
     76            return View(vetClass);
     77        }
     78
     79        public async Task<ActionResult> Edit(int? id)
    9680        {
    9781            if (id == null)
     
    9983                return RedirectToAction("NotExist", "Error");
    10084            }
    101             VetCenter vetClass = db.VetCentersObj.Find(id);
     85
     86            VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
    10287            if (vetClass == null)
    10388            {
    10489                return RedirectToAction("NotExist", "Error");
    10590            }
    106             // check for permission
     91
     92            // Check for permission
    10793            UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
     94            //UsersClass customerClass = await checkAuthorizationAsync();
     95            ViewBag.isAuthenticated = await getCrrentUser();
    10896            if (customerClass == null)
    10997            {
    11098                return RedirectToAction("AccessDenied", "Error");
    11199            }
     100
     101            // Fetch cities for dropdown
    112102            var citiess = await db.CitiesObj.ToListAsync();
    113103            ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
     104
    114105            return View(vetClass);
    115106        }
    116107
    117         // POST: Customer/Edit/5
    118         // To protect from overposting attacks, enable the specific properties you want to bind to, for
    119         // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
    120108        [HttpPost]
    121109        [ValidateAntiForgeryToken]
    122         /*
    123          public  ActionResult Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
    124          {
    125              if (ModelState.IsValid)
    126              {
    127                  db.Entry(vetClass).State = EntityState.Modified;
    128                  db.SaveChanges();
    129                  return RedirectToAction("Index");
    130              }
    131              return View(vetClass);
    132          }*/
    133 
    134         // POST: VetCenter/Edit/5
    135 
    136110        public async Task<ActionResult> Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
    137111        {
     
    143117            }
    144118
     119            // If model is invalid, repopulate the cities for dropdown
    145120            var citiess = await db.CitiesObj.ToListAsync();
    146121            ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
     
    149124        }
    150125
    151 
    152 
    153         // GET: Customer/Delete/5
    154         public ActionResult Delete(int? id)
     126        public async Task<ActionResult> Delete(int? id)
    155127        {
    156128            if (id == null)
     
    158130                return RedirectToAction("NotExist", "Error");
    159131            }
    160             VetCenter vetClass = db.VetCentersObj.Find(id);
     132
     133            VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
    161134            if (vetClass == null)
    162135            {
    163136                return RedirectToAction("NotExist", "Error");
    164137            }
     138
    165139            return View(vetClass);
    166140        }
    167141
    168         // POST: Customer/Delete/5
    169142        [HttpPost, ActionName("Delete")]
    170143        [ValidateAntiForgeryToken]
    171         public ActionResult DeleteConfirmed(int id)
     144        public async Task<ActionResult> DeleteConfirmed(int id)
    172145        {
    173             VetCenter vetClass = db.VetCentersObj.Find(id);
     146            VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
    174147            db.VetCentersObj.Remove(vetClass);
    175             db.SaveChanges();
     148            await db.SaveChangesAsync();
    176149            return RedirectToAction("Index");
    177150        }
     
    186159        }
    187160
    188 
    189         // GET: VetCenter/Search
    190         public ActionResult IndexWithSearch(string searchTerm)
     161        public async Task<ActionResult> IndexWithSearch(string searchTerm)
    191162        {
    192163            if (string.IsNullOrEmpty(searchTerm))
    193164            {
    194                 var vetCenters = db.VetCentersObj.ToList();
     165                var vetCenters = await db.VetCentersObj.ToListAsync();
    195166                return View(vetCenters);
    196167            }
    197168            else
    198169            {
    199                 var searchResults = db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToList();
     170                var searchResults = await db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToListAsync();
    200171                return View(searchResults);
    201172            }
    202173        }
    203 
    204 
    205 
    206174    }
    207175}
Note: See TracChangeset for help on using the changeset viewer.