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/BlogController.cs

    rd6040ef r57fc402  
    7979        //}
    8080
    81         public ActionResult Create()
    82         {
    83             var model = new BlogPostConsultation();
    84             return View(model);
     81        /* public ActionResult Create()
     82         {
     83             var model = new BlogPostConsultation();
     84             return View(model);
     85         }*/
     86
     87        public async Task<ActionResult> CreateAsync()
     88        {
     89
     90            // check for permission
     91            UsersClass customerClass = await getCrrentUser();
     92            // set if is authenticated
     93            ViewBag.isAuthenticated = customerClass;
     94            return View();
    8595        }
    8696
     
    90100        [HttpPost]
    91101        [ValidateAntiForgeryToken]
    92         public async Task<ActionResult> CreateAsync([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
    93         {
    94             if (ModelState.IsValid)
    95             {
    96                 bool isAuthenticated = User.Identity.IsAuthenticated;
    97                 if (isAuthenticated)
    98                 {
    99                     var user = await _userManager.GetUserAsync(User);
    100                     var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
    101                     blogClass.users_id = customerClass.id;
    102                     db.BlogPostControllerObj.Add(blogClass);
    103                     db.SaveChanges();
    104                 return RedirectToAction("Index");
    105                 }
    106                 else
    107                 {
    108                     return RedirectToAction("AccessDenied", "Error");
    109                 }
    110             }
    111 
    112             return View(blogClass);
    113         }
     102         public async Task<ActionResult> CreateAsync([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
     103         {
     104             if (ModelState.IsValid)
     105             {
     106                 bool isAuthenticated = User.Identity.IsAuthenticated;
     107                 if (isAuthenticated)
     108                 {
     109                     var user = await _userManager.GetUserAsync(User);
     110                     var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
     111                     if (customerClass != null)
     112                     {
     113                         // Поставете users_id на идентификаторот на корисникот
     114                         blogClass.users_id = customerClass.id;
     115                        //blogClass.date_askes = DateOnly.FromDateTime(DateTime.UtcNow);
     116                        blogClass.date_askes = DateOnly.FromDateTime(DateTime.Now); // Ова ќе стави локално време
     117
     118                        db.BlogPostControllerObj.Add(blogClass);
     119                         await db.SaveChangesAsync();
     120                         return RedirectToAction("Index");
     121                     }
     122                 }
     123                 else
     124                 {
     125                     return RedirectToAction("AccessDenied", "Error");
     126                 }
     127             }
     128
     129             return View(blogClass);
     130         }
     131
     132       
     133       
     134
     135
    114136
    115137        // GET: Customer/Edit/5
Note: See TracChangeset for help on using the changeset viewer.