| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | using OzonDomains.Models.AccessControl; |
| | | 5 | | using OzonRepositories.Context; |
| | | 6 | | |
| | | 7 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Admin; |
| | | 8 | | |
| | | 9 | | [Authorize(Roles = "Admin")] |
| | | 10 | | [Area("Studio2")] |
| | | 11 | | public class AccessProfilesController : Controller |
| | | 12 | | { |
| | | 13 | | private readonly ApplicationDbContext _context; |
| | | 14 | | |
| | 3 | 15 | | public AccessProfilesController(ApplicationDbContext context) |
| | | 16 | | { |
| | 3 | 17 | | _context = context; |
| | 3 | 18 | | } |
| | | 19 | | |
| | | 20 | | // GET: AccessProfiles |
| | | 21 | | public async Task<IActionResult> Index() |
| | | 22 | | { |
| | 0 | 23 | | var profiles = await _context.AccessProfiles |
| | 0 | 24 | | .Include(ap => ap.DataScopeSets) |
| | 0 | 25 | | .ToListAsync(); |
| | 0 | 26 | | return View(profiles); |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | // GET: AccessProfiles/Details/5 |
| | | 30 | | public async Task<IActionResult> Details(int? id) |
| | | 31 | | { |
| | 4 | 32 | | if (id == null) return NotFound(); |
| | | 33 | | |
| | 2 | 34 | | var profile = await _context.AccessProfiles |
| | 2 | 35 | | .Include(ap => ap.DataScopeSets) |
| | 2 | 36 | | .ThenInclude(s => s.Items) |
| | 2 | 37 | | .AsSplitQuery() |
| | 2 | 38 | | .FirstOrDefaultAsync(ap => ap.Id == id); |
| | | 39 | | |
| | 3 | 40 | | if (profile == null) return NotFound(); |
| | | 41 | | |
| | 1 | 42 | | return View(profile); |
| | 3 | 43 | | } |
| | | 44 | | |
| | | 45 | | // GET: AccessProfiles/Create |
| | | 46 | | public IActionResult Create() |
| | | 47 | | { |
| | 0 | 48 | | return View(new AccessProfile()); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | // POST: AccessProfiles/Create |
| | | 52 | | [HttpPost] |
| | | 53 | | [ValidateAntiForgeryToken] |
| | | 54 | | public async Task<IActionResult> Create([Bind("Name,AvailableOrderColumns")] AccessProfile profile) |
| | | 55 | | { |
| | 0 | 56 | | if (ModelState.IsValid) |
| | | 57 | | { |
| | 0 | 58 | | _context.AccessProfiles.Add(profile); |
| | 0 | 59 | | await _context.SaveChangesAsync(); |
| | 0 | 60 | | return RedirectToAction(nameof(Index)); |
| | | 61 | | } |
| | 0 | 62 | | return View(profile); |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | // GET: AccessProfiles/Edit/5 |
| | | 66 | | public async Task<IActionResult> Edit(int? id) |
| | | 67 | | { |
| | 0 | 68 | | if (id == null) return NotFound(); |
| | | 69 | | |
| | 0 | 70 | | var profile = await _context.AccessProfiles |
| | 0 | 71 | | .Include(ap => ap.DataScopeSets) |
| | 0 | 72 | | .FirstOrDefaultAsync(ap => ap.Id == id); |
| | | 73 | | |
| | 0 | 74 | | if (profile == null) return NotFound(); |
| | | 75 | | |
| | 0 | 76 | | ViewBag.AllDataScopeSets = await _context.DataScopeSets.ToListAsync(); |
| | 0 | 77 | | return View(profile); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | // POST: AccessProfiles/Edit/5 |
| | | 81 | | [HttpPost] |
| | | 82 | | [ValidateAntiForgeryToken] |
| | | 83 | | public async Task<IActionResult> Edit(int id, [Bind("Id,Name,AvailableOrderColumns")] AccessProfile profile, |
| | | 84 | | List<int>? selectedDataScopeSetIds) |
| | | 85 | | { |
| | 0 | 86 | | if (id != profile.Id) return NotFound(); |
| | | 87 | | |
| | 0 | 88 | | if (ModelState.IsValid) |
| | | 89 | | { |
| | 0 | 90 | | var existing = await _context.AccessProfiles |
| | 0 | 91 | | .Include(ap => ap.DataScopeSets) |
| | 0 | 92 | | .FirstOrDefaultAsync(ap => ap.Id == id); |
| | | 93 | | |
| | 0 | 94 | | if (existing == null) return NotFound(); |
| | | 95 | | |
| | 0 | 96 | | existing.Name = profile.Name; |
| | 0 | 97 | | existing.AvailableOrderColumns = profile.AvailableOrderColumns; |
| | | 98 | | |
| | | 99 | | // Update M:N relationship with DataScopeSets |
| | 0 | 100 | | existing.DataScopeSets.Clear(); |
| | 0 | 101 | | if (selectedDataScopeSetIds?.Any() == true) |
| | | 102 | | { |
| | 0 | 103 | | var sets = await _context.DataScopeSets |
| | 0 | 104 | | .Where(s => selectedDataScopeSetIds.Contains(s.Id)) |
| | 0 | 105 | | .ToListAsync(); |
| | 0 | 106 | | foreach (var set in sets) |
| | 0 | 107 | | existing.DataScopeSets.Add(set); |
| | | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | await _context.SaveChangesAsync(); |
| | 0 | 111 | | return RedirectToAction(nameof(Index)); |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | ViewBag.AllDataScopeSets = await _context.DataScopeSets.ToListAsync(); |
| | 0 | 115 | | return View(profile); |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | // GET: AccessProfiles/Delete/5 |
| | | 119 | | public async Task<IActionResult> Delete(int? id) |
| | | 120 | | { |
| | 0 | 121 | | if (id == null) return NotFound(); |
| | | 122 | | |
| | 0 | 123 | | var profile = await _context.AccessProfiles |
| | 0 | 124 | | .Include(ap => ap.DataScopeSets) |
| | 0 | 125 | | .FirstOrDefaultAsync(ap => ap.Id == id); |
| | | 126 | | |
| | 0 | 127 | | if (profile == null) return NotFound(); |
| | | 128 | | |
| | 0 | 129 | | return View(profile); |
| | 0 | 130 | | } |
| | | 131 | | |
| | | 132 | | // POST: AccessProfiles/Delete/5 |
| | | 133 | | [HttpPost, ActionName("Delete")] |
| | | 134 | | [ValidateAntiForgeryToken] |
| | | 135 | | public async Task<IActionResult> DeleteConfirmed(int id) |
| | | 136 | | { |
| | 0 | 137 | | var profile = await _context.AccessProfiles.FindAsync(id); |
| | 0 | 138 | | if (profile != null) |
| | | 139 | | { |
| | 0 | 140 | | _context.AccessProfiles.Remove(profile); |
| | 0 | 141 | | await _context.SaveChangesAsync(); |
| | | 142 | | } |
| | 0 | 143 | | return RedirectToAction(nameof(Index)); |
| | 0 | 144 | | } |
| | | 145 | | } |