Changeset 57fc402


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.

Location:
PostgreSqlDotnetCore
Files:
11 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
  • PostgreSqlDotnetCore/Controllers/CityController.cs

    rd6040ef r57fc402  
    1717        public async Task<ActionResult> IndexAsync()
    1818         {
     19           // check for permission
     20          UsersClass customerClass = await checkAuthorizationAsync();
     21
     22
     23           if (customerClass == null)
     24             {
     25                 return RedirectToAction("AccessDenied", "Error");
     26             }
     27
     28            var citiess = await db.CitiesObj.ToListAsync();
     29
     30            return View(citiess);
     31        }
     32       
     33
     34
     35
     36        /* public async Task<ActionResult> IndexAsync()
     37         {
    1938             // check for permission
    2039             UsersClass customerClass = await checkAuthorizationAsync();
     
    2645             return View(db.CitiesObj.ToList());
    2746         }
    28        
     47        */
     48
    2949
    3050
     
    138158            base.Dispose(disposing);
    139159        }
     160
     161
     162
     163       
    140164    }
    141165}
  • PostgreSqlDotnetCore/Controllers/PetCaresController.cs

    rd6040ef r57fc402  
    7878
    7979        // GET: Customer/Details/5
    80         public ActionResult Details(int? id)
     80        public async Task<ActionResult> Details(int? id)
    8181        {
    8282            if (id == null)
     
    8484                return RedirectToAction("NotExist", "Error");
    8585            }
     86            UsersClass customerClass = await getCrrentUser();
     87            ViewBag.isAuthenticated = customerClass;
    8688            Pet_CaresClass peClass = db.PetCaresObj.Find(id);
    8789            if (peClass == null)
     
    178180            var vetCenters = await db.VetCentersObj.ToListAsync();
    179181            ViewBag.VetCenters = new SelectList(vetCenters, "id", "name", peClass.vetcentersid);
    180 
     182            // dodadeno na 22.08
     183            ViewBag.isAuthenticated = await getCrrentUser();
    181184            return View(peClass);
    182185        }
     
    196199                return RedirectToAction("AccessDenied", "Error");
    197200            }
     201            ViewBag.isAuthenticated = await getCrrentUser();
    198202
    199203            if (ModelState.IsValid)
     
    214218
    215219        // GET: Customer/Delete/5
    216         public ActionResult Delete(int? id)
     220        public async Task<ActionResult> Delete(int? id)
    217221        {
    218222            if (id == null)
     
    220224                return RedirectToAction("NotExist", "Error");
    221225            }
     226            UsersClass customerClass = await getCrrentUser(); // Добијте ја тековната улога на корисникот
     227            ViewBag.isAuthenticated = customerClass;
    222228            Pet_CaresClass peClass = db.PetCaresObj.Find(id);
    223229            if (peClass == null)
  • PostgreSqlDotnetCore/Controllers/PetsController.cs

    rd6040ef r57fc402  
    4545
    4646        // GET: Customer/Details/5
    47         public ActionResult Details(int? id)
     47        /* public ActionResult Details(int? id)
     48         {
     49             if (id == null)
     50             {
     51                 return RedirectToAction("NotExist", "Error");
     52             }
     53             PetsClass peClass = db.PetsObj.Find(id);
     54             if (peClass == null)
     55             {
     56                 return RedirectToAction("NotExist", "Error");
     57             }
     58             return View(peClass);
     59         }*/
     60
     61        public async Task<ActionResult> Details(int? id)
    4862        {
    4963            if (id == null)
     
    5165                return RedirectToAction("NotExist", "Error");
    5266            }
    53             PetsClass peClass = db.PetsObj.Find(id);
     67
     68            UsersClass customerClass = await getCrrentUser(); // Добијте ја тековната улога на корисникот
     69            ViewBag.isAuthenticated = customerClass;
     70
     71            PetsClass peClass = await db.PetsObj.FindAsync(id);
    5472            if (peClass == null)
    5573            {
    5674                return RedirectToAction("NotExist", "Error");
    5775            }
     76
    5877            return View(peClass);
    5978        }
     
    110129
    111130        // GET: Customer/Edit/5
    112         public ActionResult Edit(int? id)
     131        // public ActionResult Edit(int? id)
     132        public async Task<ActionResult> Edit(int? id)
    113133        {
    114134            if (id == null)
     
    121141                return RedirectToAction("NotExist", "Error");
    122142            }
     143            // додадено на 21.08
     144            ViewBag.isAuthenticated = await getCrrentUser();
    123145            return View(peClass);
    124146        }
     
    132154        {
    133155            bool isAuthenticated = User.Identity.IsAuthenticated;
     156            ViewBag.isAuthenticated = await getCrrentUser();
     157
    134158            if (!isAuthenticated)
    135159            {
     
    140164
    141165            // set if is authenticated
    142             ViewBag.isAuthenticated = new UsersClass();
     166            // додадено и избришено
     167            ViewBag.isAuthenticated = await getCrrentUser();
     168            //ViewBag.isAuthenticated = new UsersClass();
    143169           
    144170             
     
    158184
    159185        // GET: Customer/Delete/5
    160         public ActionResult Delete(int? id)
     186        /* public ActionResult Delete(int? id)
     187         {
     188             if (id == null)
     189             {
     190                 return RedirectToAction("NotExist", "Error");
     191             }
     192             PetsClass peClass = db.PetsObj.Find(id);
     193             if (peClass == null)
     194             {
     195                 return RedirectToAction("NotExist", "Error");
     196             }
     197             return View(peClass);
     198         }*/
     199
     200        public async Task<ActionResult> Delete(int? id)
    161201        {
    162202            if (id == null)
     
    164204                return RedirectToAction("NotExist", "Error");
    165205            }
    166             PetsClass peClass = db.PetsObj.Find(id);
     206
     207            UsersClass customerClass = await getCrrentUser(); // Добијте ја тековната улога на корисникот
     208            ViewBag.isAuthenticated = customerClass;
     209
     210            PetsClass peClass = await db.PetsObj.FindAsync(id);
    167211            if (peClass == null)
    168212            {
    169213                return RedirectToAction("NotExist", "Error");
    170214            }
     215
    171216            return View(peClass);
    172217        }
  • PostgreSqlDotnetCore/Controllers/ProductsController.cs

    rd6040ef r57fc402  
    4646
    4747        // GET: Customer/Details/5
    48         public ActionResult Details(int? id)
     48        public async Task<ActionResult> Details(int? id)
    4949        {
    5050            if (id == null)
     
    5252                return RedirectToAction("NotExist", "Error");
    5353            }
     54            UsersClass customerClass = await getCrrentUser();
     55            ViewBag.isAuthenticated = customerClass;
    5456            ProductsClass prodClass = db.ProductObj.Find(id);
    5557            if (prodClass == null)
  • 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}
  • PostgreSqlDotnetCore/Models/BlogPostConsultation.cs

    rd6040ef r57fc402  
    1515        //public DateTime date_askes { get; set; }
    1616        //public DateTime date_askes { get; set; } = DateTime.UtcNow.Date;
    17         public DateOnly date_askes { get; set; } = DateOnly.FromDateTime(DateTime.UtcNow);
    18 
     17        public DateOnly date_askes { get; set; } = DateOnly.FromDateTime(DateTime.Now);
     18       
    1919        public string title { get; set; }
    2020       
  • PostgreSqlDotnetCore/Views/Blog/Create.cshtml

    rd6040ef r57fc402  
    1717        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    1818
    19         <div class="form-group">
     19       @* <div class="form-group">
    2020            @Html.LabelFor(model => model.date_askes, htmlAttributes: new { @class = "control-label col-md-2" })
    2121            <div class="col-md-10">
     
    2424            </div>
    2525        </div>
     26        *@
     27      @*  <div class="form-group">
     28            @Html.LabelFor(model => model.date_askes, htmlAttributes: new { @class = "control-label col-md-2" })
     29            <div class="col-md-10">
     30                @Html.EditorFor(model => model.date_askes, new { htmlAttributes = new { @class = "form-control", @type = "date", value = DateTime.UtcNow.ToString("yyyy-MM-dd") } })
     31                @Html.ValidationMessageFor(model => model.date_askes, "", new { @class = "text-danger" })
     32            </div>
     33        </div>
     34        *@
     35
     36       
     37
     38
     39       
    2640
    2741        <div class="form-group">
  • PostgreSqlDotnetCore/Views/City/Index.cshtml

    rd6040ef r57fc402  
    1616            @Html.DisplayNameFor(model => model.name)
    1717        </th>
    18    
     18
    1919        <th></th>
    2020
    2121    </tr>
    2222
    23 @foreach (var item in Model) {
    24     <tr>
    25         <td>
    26             @Html.DisplayFor(modelItem => item.name)
    27         </td>
    28      
     23    @foreach (var item in Model)
     24    {
     25        <tr>
     26            <td>
     27                @Html.DisplayFor(modelItem => item.name)
     28            </td>
    2929
    30         <td>
    31             @Html.ActionLink("Edit", "Edit", new { id = item.id }) |
    32             @Html.ActionLink("Details", "Details", new { id = item.id }) |
    33             @Html.ActionLink("Delete", "Delete", new { id = item.id })
    34         </td>
    35     </tr>
    36 }
     30
     31            <td>
     32                @Html.ActionLink("Edit", "Edit", new { id = item.id }) |
     33                @Html.ActionLink("Details", "Details", new { id = item.id }) |
     34                @Html.ActionLink("Delete", "Delete", new { id = item.id })
     35            </td>
     36        </tr>
     37    }
    3738
    3839</table>
  • PostgreSqlDotnetCore/Views/Shared/_Layout.cshtml

    rd6040ef r57fc402  
    5050                                    <a class="nav-link text-dark" asp-area="" asp-controller="City" asp-action="Index">Manage Cities</a>
    5151                                </li>
     52                               
    5253                                <li class="nav-item">
    5354                                    <a class="nav-link text-dark" asp-area="" asp-controller="Blog" asp-action="Index">BlogPost</a>
    5455                                </li>
    5556                            }
     57
     58                           
    5659                        }
     60
     61                       
     62                       
     63
     64
     65                       
    5766                        <li class="nav-item">
    5867                            <a class="nav-link text-dark" asp-area="" asp-controller="VetCenter" asp-action="Index">VetCenters</a>
  • PostgreSqlDotnetCore/Views/VetCenter/Index.cshtml

    rd6040ef r57fc402  
    99<h2>Vet Center</h2>
    1010
    11 <p>
     11@*<p>
    1212    @Html.ActionLink("Create New", "Create")
    1313</p>
     14*@
     15@if (ViewBag.hasAccess != null && ViewBag.hasAccess == true)
     16{
     17    <p>
     18        @Html.ActionLink("Create New", "Create")
     19    </p>
     20}
     21
    1422<table class="table">
    1523    <tr>
Note: See TracChangeset for help on using the changeset viewer.