Ignore:
Timestamp:
08/23/24 15:40:14 (5 weeks ago)
Author:
ElenaMoskova <elena.moskova99@…>
Branches:
main
Children:
e9bb9d1
Parents:
72b1da2
Message:

fix access

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • PostgreSqlDotnetCore/Controllers/PetCaresController.cs

    r72b1da2 r118e414  
    2626              return View();
    2727          }*/
    28          public async Task<ActionResult> Create()
    29          {
    30              
    31              
     28        public async Task<ActionResult> Create()
     29        {
     30
     31
    3232            UsersClass customerClass = await getCrrentUser();
    33            
     33
    3434            ViewBag.isAuthenticated = customerClass;
    3535            var vetCenters = await db.VetCentersObj.ToListAsync();
    36              ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
    37 
    38              return View();
    39          }
    40 
    41        
     36            ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
     37
     38            return View();
     39        }
     40
     41
    4242
    4343
     
    5353                return RedirectToAction("AccessDenied", "Error");
    5454            }
     55            // no access for standard user
     56            ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
     57
    5558            if (customerClass.role_id == RoleConstants.Standard)
    5659            {
    5760                // query
    58                             var query = from st in db.PetCaresObj
     61                var query = from st in db.PetCaresObj
    5962                            where st.usersid == customerClass.id
    6063                            select st;
     
    7275                // query
    7376                var queryVetCenters = from kk in db.VetCentersObj
    74                             select kk;
     77                                      select kk;
    7578
    7679                // query
     
    8588
    8689                return View(petCareAllData);
    87             } else
     90            }
     91            else
    8892            {
    8993                return View(db.PetCaresObj.ToList());
     
    99103                return RedirectToAction("NotExist", "Error");
    100104            }
    101             UsersClass customerClass = await getCrrentUser(); 
     105            UsersClass customerClass = await getCrrentUser();
    102106            ViewBag.isAuthenticated = customerClass;
    103107            Pet_CaresClass peClass = db.PetCaresObj.Find(id);
     
    106110                return RedirectToAction("NotExist", "Error");
    107111            }
     112            // no access for standard user
     113            ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
     114
    108115            return View(peClass);
    109116        }
     
    126133        [HttpPost]
    127134        [ValidateAntiForgeryToken]
    128          public async Task<ActionResult> CreateAsync([Bind(include: "id,title,description,dateending, start_date, usersid, vetcentersid")] Pet_CaresClass peClass)
    129           {
    130               bool isAuthenticated = User.Identity.IsAuthenticated;
    131               if (!isAuthenticated)
    132               {
    133                   return RedirectToAction("AccessDenied", "Error");
    134               }
     135        public async Task<ActionResult> CreateAsync([Bind(include: "id,title,description,dateending, start_date, usersid, vetcentersid")] Pet_CaresClass peClass)
     136        {
     137            bool isAuthenticated = User.Identity.IsAuthenticated;
     138            if (!isAuthenticated)
     139            {
     140                return RedirectToAction("AccessDenied", "Error");
     141            }
    135142            ViewBag.isAuthenticated = new UsersClass();
     143            // no access for standard user
     144            ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
    136145
    137146            if (ModelState.IsValid)
    138               {
     147            {
    139148                ViewBag.isAuthenticated = new UsersClass();
    140149                peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
    141                   peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc);
    142                  var user = await _userManager.GetUserAsync(User);
    143                   var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
    144                   peClass.usersid = customerClass.id;
    145                   db.PetCaresObj.Add(peClass);
    146                   db.SaveChanges();
    147                   return RedirectToAction("Index");
    148               }
    149               var vetCenters = await db.VetCentersObj.ToListAsync();
    150               ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
    151 
    152               return View(peClass);
    153           }
    154 
    155 
    156 
    157 
    158        
     150                peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc);
     151                var user = await _userManager.GetUserAsync(User);
     152                var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
     153                peClass.usersid = customerClass.id;
     154                db.PetCaresObj.Add(peClass);
     155                db.SaveChanges();
     156                return RedirectToAction("Index");
     157            }
     158            var vetCenters = await db.VetCentersObj.ToListAsync();
     159            ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
     160
     161            return View(peClass);
     162        }
     163
     164
     165
     166
     167
    159168
    160169
     
    229238            }
    230239            ViewBag.isAuthenticated = await getCrrentUser();
     240            // no access for standard user
     241            ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
    231242
    232243            if (ModelState.IsValid)
     
    260271                return RedirectToAction("NotExist", "Error");
    261272            }
     273            // no access for standard user
     274            ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
     275
    262276            return View(peClass);
    263277        }
Note: See TracChangeset for help on using the changeset viewer.