Changeset 72b1da2


Ignore:
Timestamp:
08/23/24 03:03:32 (5 weeks ago)
Author:
ElenaMoskova <elena.moskova99@…>
Branches:
main
Children:
118e414
Parents:
57fc402
Message:

Providing access

Providing access to various functionalities

Location:
PostgreSqlDotnetCore
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • PostgreSqlDotnetCore/Controllers/BlogController.cs

    r57fc402 r72b1da2  
    3030        */
    3131
    32         public async Task<ActionResult> Index()
    33         {
    34             // Проверка за автентикација
    35             bool isAuthenticated = User.Identity.IsAuthenticated;
    36 
    37             if (!isAuthenticated)
    38             {
    39                 return RedirectToAction("AccessDenied", "Error");
    40             }
    41 
    42             // Список на блог постови
    43             var blogPosts = await db.BlogPostControllerObj.ToListAsync();
    44 
    45             // Предавање на ViewBag за проверка на автентикација
    46             ViewBag.isAuthenticated = isAuthenticated;
    47 
    48             return View(blogPosts);
    49         }
     32        /* public async Task<ActionResult> Index()
     33         {
     34             // Проверка за автентикација
     35             bool isAuthenticated = User.Identity.IsAuthenticated;
     36
     37             if (!isAuthenticated)
     38             {
     39                 return RedirectToAction("AccessDenied", "Error");
     40             }
     41
     42             // Список на блог постови
     43             var blogPosts = await db.BlogPostControllerObj.ToListAsync();
     44
     45             // Предавање на ViewBag за проверка на автентикација
     46             ViewBag.isAuthenticated = isAuthenticated;
     47
     48             return View(blogPosts);
     49         }*/
     50
     51          public async Task<ActionResult> Index()
     52          {
     53              // Проверка за автентикација
     54              bool isAuthenticated = User.Identity.IsAuthenticated;
     55
     56              if (!isAuthenticated)
     57              {
     58                  return RedirectToAction("AccessDenied", "Error");
     59              }
     60
     61              // Список на блог постови
     62              var blogPosts = await db.BlogPostControllerObj.ToListAsync();
     63
     64              // Вземи тековниот корисник
     65              var currentUser = await _userManager.GetUserAsync(User);
     66              var customerClass = await db.CustomerObj.SingleOrDefaultAsync(x => x.email == currentUser.Email);
     67
     68              // Предавање на ViewBag за проверка на автентикација и корисничкиот ID
     69              ViewBag.isAuthenticated = isAuthenticated;
     70              ViewBag.CurrentUserId = customerClass?.id;
     71
     72              return View(blogPosts);
     73          }
     74       
     75
     76       
     77
     78
    5079        // GET: Customer/Details/5
    5180        public async Task<ActionResult> DetailsAsync(int? id)
     
    100129        [HttpPost]
    101130        [ValidateAntiForgeryToken]
    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;
     131        public async Task<ActionResult> CreateAsync([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
     132        {
     133            if (ModelState.IsValid)
     134            {
     135                bool isAuthenticated = User.Identity.IsAuthenticated;
     136                if (isAuthenticated)
     137                {
     138                    var user = await _userManager.GetUserAsync(User);
     139                    var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
     140                    if (customerClass != null)
     141                    {
     142                        // Поставете users_id на идентификаторот на корисникот
     143                        blogClass.users_id = customerClass.id;
    115144                        //blogClass.date_askes = DateOnly.FromDateTime(DateTime.UtcNow);
    116145                        blogClass.date_askes = DateOnly.FromDateTime(DateTime.Now); // Ова ќе стави локално време
    117146
    118147                        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        
     148                        await db.SaveChangesAsync();
     149                        return RedirectToAction("Index");
     150                    }
     151                }
     152                else
     153                {
     154                    return RedirectToAction("AccessDenied", "Error");
     155                }
     156            }
     157
     158            return View(blogClass);
     159        }
     160
     161
     162
     163
    134164
    135165
     
    151181            // check for permission
    152182            UsersClass customerClass = await checkAuthorizationAsync();
     183            //dodadeno na 23.08
     184            ViewBag.isAuthenticated = await getCrrentUser();
    153185            if (customerClass == null)
    154186            {
     
    174206        [HttpPost]
    175207        [ValidateAntiForgeryToken]
    176         public ActionResult Edit([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
     208        /* public ActionResult Edit([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
     209         {
     210             if (ModelState.IsValid)
     211             {
     212                 db.Entry(blogClass).State = EntityState.Modified;
     213                 db.SaveChanges();
     214                 return RedirectToAction("Index");
     215             }
     216             return View(blogClass);
     217         }*/
     218
     219       
     220        public async Task<ActionResult> EditAsync(int id, [Bind(include: "id,date_askes,title,description")] BlogPostConsultation blogClass)
    177221        {
    178222            if (ModelState.IsValid)
    179223            {
    180                 db.Entry(blogClass).State = EntityState.Modified;
    181                 db.SaveChanges();
    182                 return RedirectToAction("Index");
    183             }
    184             return View(blogClass);
    185         }
     224                var existingBlogClass = await db.BlogPostControllerObj.FindAsync(id);
     225                if (existingBlogClass != null)
     226                {
     227                    // Запамтете ја старата вредност на users_id
     228                    blogClass.users_id = existingBlogClass.users_id;
     229
     230                    db.Entry(existingBlogClass).CurrentValues.SetValues(blogClass);
     231                    await db.SaveChangesAsync();
     232                    return RedirectToAction("Index");
     233                }
     234            }
     235            return View(blogClass);
     236        }
     237
     238
    186239
    187240        // GET: Customer/Delete/5
    188         public async Task<ActionResult> DeleteAsync(int? id)
    189         {
     241        public async Task<ActionResult> DeleteAsync(int? id) {
     242          //  UsersClass customerClass = await checkAuthorizationAsync();
     243
     244        ViewBag.isAuthenticated = await getCrrentUser();
     245       
    190246            if (id == null)
    191247           {
  • PostgreSqlDotnetCore/Controllers/PetCaresController.cs

    r57fc402 r72b1da2  
    1717
    1818        [HttpGet]
    19         public async Task<ActionResult> Create()
    20         {
     19        /*  public async Task<ActionResult> Create()
     20          {
     21
     22              var vetCenters = await db.VetCentersObj.ToListAsync();
     23
     24              ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
     25
     26              return View();
     27          }*/
     28         public async Task<ActionResult> Create()
     29         {
     30             
     31             
     32            UsersClass customerClass = await getCrrentUser();
    2133           
     34            ViewBag.isAuthenticated = customerClass;
    2235            var vetCenters = await db.VetCentersObj.ToListAsync();
    23 
    24             ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
    25 
    26             return View();
    27         }
     36             ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
     37
     38             return View();
     39         }
     40
     41       
     42
    2843
    2944        // GET: Customer
     
    111126        [HttpPost]
    112127        [ValidateAntiForgeryToken]
    113         public async Task<ActionResult> CreateAsync([Bind(include: "id,title,description,dateending, start_date, usersid, vetcentersid")] Pet_CaresClass peClass)
    114         {
    115             bool isAuthenticated = User.Identity.IsAuthenticated;
    116             if (!isAuthenticated)
    117             {
    118                 return RedirectToAction("AccessDenied", "Error");
    119             }
     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            ViewBag.isAuthenticated = new UsersClass();
     136
    120137            if (ModelState.IsValid)
    121             {
     138              {
     139                ViewBag.isAuthenticated = new UsersClass();
    122140                peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
    123                 peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc);
    124                 var user = await _userManager.GetUserAsync(User);
    125                 var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
    126                 peClass.usersid = customerClass.id;
    127                 db.PetCaresObj.Add(peClass);
    128                 db.SaveChanges();
    129                 return RedirectToAction("Index");
    130             }
    131 
    132             return View(peClass);
    133         }
     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       
     159
     160
     161
     162
    134163
    135164        // GET: Customer/Edit/5
  • PostgreSqlDotnetCore/Controllers/VetCenterController.cs

    r57fc402 r72b1da2  
    3131        }
    3232
     33        /* public async Task<ActionResult> Index()
     34         {
     35
     36             var vetCenters = await db.VetCentersObj.ToListAsync();
     37             ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
     38
     39             // Check if the user is an admin
     40             UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
     41             ViewBag.hasAccess = customerClass != null;
     42
     43             return View(vetCenters);
     44         }*/
    3345        public async Task<ActionResult> Index()
    3446        {
     
    3648            ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
    3749
    38             // Check if the user is an admin
     50            // Проверете дали корисникот е администратор или менаџер
    3951            UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
     52               // ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
     53
    4054            ViewBag.hasAccess = customerClass != null;
    4155
    4256            return View(vetCenters);
    4357        }
     58
    4459
    4560        public async Task<ActionResult> Details(int? id)
     
    124139        }
    125140
    126         public async Task<ActionResult> Delete(int? id)
    127         {
     141        public async Task<ActionResult> Delete(int? id) {
     142             UsersClass customerClass = await checkAuthorizationAsync();
     143       
     144        ViewBag.isAuthenticated = await getCrrentUser();
     145       
    128146            if (id == null)
    129147            {
  • PostgreSqlDotnetCore/Views/Blog/Edit.cshtml

    r57fc402 r72b1da2  
    4141        </div>
    4242    </div>
    43     <div class="form-group">
     43    @*<div class="form-group">
    4444        @Html.LabelFor(model => model.users_id, htmlAttributes: new { @class = "control-label col-md-2" })
    4545        <div class="col-md-10">
     
    4848        </div>
    4949    </div>
    50 
     50    *@
    5151    <div class="form-group">
    5252        <div class="col-md-offset-2 col-md-10">
  • PostgreSqlDotnetCore/Views/Blog/Index.cshtml

    r57fc402 r72b1da2  
    4545        </th>
    4646
    47         <td>
     47       @*<td>
    4848            @Html.ActionLink("Edit", "Edit", new { id = item.id }) |
    4949            @Html.ActionLink("Answers", "Details", new { id = item.id }) |
    5050            @Html.ActionLink("Delete", "Delete", new { id = item.id })
    5151        </td>
     52        *@
     53           
     54
     55
     56
     57              @if (item.users_id == ViewBag.CurrentUserId)
     58              {
     59              <td>
     60            @Html.ActionLink("Edit", "Edit", new { id = item.id })
     61            @Html.ActionLink("Delete", "Delete", new { id = item.id })
     62             
     63        </td>
     64            }
     65
     66           
     67
     68            @if (item.users_id == ViewBag.CurrentUserId || item.users_id != ViewBag.CurrentUserId)
     69            {
     70                <td>
     71                   
     72                    @Html.ActionLink("Answers", "Details", new { id = item.id })
     73                   
     74
     75                </td>
     76            }
     77           
     78
     79
     80           
     81       
     82           
    5283    </tr>
    5384}
  • PostgreSqlDotnetCore/Views/Products/Index.cshtml

    r57fc402 r72b1da2  
    9898                        <div class="col-md-6 col-xs-6">
    9999                            <h3>@item.name</h3>
    100                             @if (SignInManager.IsSignedIn(User))
     100                            @if (SignInManager.IsSignedIn(User) && ViewBag.hasAccess != null)
     101                            // SignInManager.IsSignedIn(User) && ViewBag.hasAccess != null
     102                            //SignInManager.IsSignedIn(User) && ViewBag.hasAccess != null && ViewBag.hasAccess == true
     103                         
     104                               
    101105                            {
    102106                                <div class="edit-crud-products">
     
    107111                            }
    108112                        </div>
     113                       
     114                       
    109115                        <div class="col-md-6 col-xs-6 price">
    110116                            <h3>
  • PostgreSqlDotnetCore/Views/VetCenter/Index.cshtml

    r57fc402 r72b1da2  
    8585            </td>
    8686
    87             @if (SignInManager.IsSignedIn(User))
     87           
     88
     89         @*   @if ((SignInManager.IsSignedIn(User)))
    8890            {
    8991                <td>
     
    9597                </td>
    9698            }
     99           *@
     100
     101            @* Додадете условие за проверка на улогите *@
     102            @if (SignInManager.IsSignedIn(User) && (ViewBag.hasAccess != null && ViewBag.hasAccess == true))
     103            {
     104                <td>
     105                    @Html.ActionLink("Edit", "Edit", new { id = item.id }) |
     106                    @Html.ActionLink("Details", "Details", new { id = item.id }) |
     107                    @Html.ActionLink("Delete", "Delete", new { id = item.id })
     108                </td>
     109            }
    97110
    98111        </tr>
Note: See TracChangeset for help on using the changeset viewer.