ScientificReport
GrantController.cs
Go to the documentation of this file.
1 using System;
2 using System.Linq;
3 using Microsoft.AspNetCore.Authorization;
4 using Microsoft.AspNetCore.Mvc;
11 
12 namespace ScientificReport.Controllers
13 {
14  [Authorize(Roles = UserProfileRole.Any)]
15  public class GrantController : Controller
16  {
17  private readonly IGrantService _grantService;
18  private readonly IUserProfileService _userProfileService;
19  private readonly IDepartmentService _departmentService;
20 
22  IGrantService grantService,
23  IUserProfileService userProfileService,
24  IDepartmentService departmentService
25  )
26  {
27  _grantService = grantService;
28  _userProfileService = userProfileService;
29  _departmentService = departmentService;
30  }
31 
32  // GET: Grant
33  public IActionResult Index(GrantIndexModel model)
34  {
35  model.Grants = _grantService.GetPageByRole(model.CurrentPage, model.PageSize, User);
36  model.Count = _grantService.GetCountByRole(User);
37  return View(model);
38  }
39 
40  // GET: Grant/Details/{id}
41  public IActionResult Details(Guid? id)
42  {
43  if (id == null)
44  {
45  return NotFound();
46  }
47 
48  var grant = _grantService.GetById(id.Value);
49  if (grant == null)
50  {
51  return NotFound();
52  }
53 
54  if (!UserHasPermission(grant))
55  {
56  return Forbid();
57  }
58 
59  return View(grant);
60  }
61 
62  // GET: Grant/Create
63  public IActionResult Create() => View();
64 
65  // POST: Grant/Create
66  [HttpPost]
67  [ValidateAntiForgeryToken]
68  public IActionResult Create(GrantModel model)
69  {
70  if (!ModelState.IsValid)
71  {
72  return View(model);
73  }
74 
75  _grantService.CreateItem(model);
76  _grantService.AddUser(_grantService.Get(g => g.Info == model.Info), _userProfileService.Get(User));
77 
78  return RedirectToAction(nameof(Index));
79  }
80 
81  // GET: Grant/Edit/{id}
82  public IActionResult Edit(Guid? id)
83  {
84  if (id == null)
85  {
86  return NotFound();
87  }
88 
89  var grant = _grantService.GetById(id.Value);
90  if (grant == null)
91  {
92  return NotFound();
93  }
94 
95  if (!UserHasPermission(grant))
96  {
97  return Forbid();
98  }
99 
100  return View(new GrantEditModel(grant)
101  {
102  Users = _userProfileService.GetAll(),
103  Authors = _grantService.GetUsers(grant.Id)
104  });
105  }
106 
107  // POST: Grant/Edit/{id}
108  [HttpPost]
109  [ValidateAntiForgeryToken]
110  public IActionResult Edit(Guid id, GrantEditModel model)
111  {
112  if (id != model.Id || !_grantService.Exists(id))
113  {
114  return NotFound();
115  }
116 
117  var grant = _grantService.GetById(id);
118  if (!UserHasPermission(grant))
119  {
120  return Forbid();
121  }
122 
123  if (!ModelState.IsValid)
124  {
125  return View(model);
126  }
127 
128  _grantService.UpdateItem(model);
129  return RedirectToAction(nameof(Index));
130  }
131 
132  // GET: Grant/Delete/{id}
133  public IActionResult Delete(Guid? id)
134  {
135  if (id == null)
136  {
137  return NotFound();
138  }
139 
140  var grant = _grantService.GetById(id.Value);
141  if (grant == null)
142  {
143  return NotFound();
144  }
145 
146  if (!UserHasPermission(grant))
147  {
148  return Forbid();
149  }
150 
151  return View(grant);
152  }
153 
154  // POST: Grant/Delete/{id}
155  [HttpPost, ActionName("Delete")]
156  [ValidateAntiForgeryToken]
157  public IActionResult DeleteConfirmed(Guid id)
158  {
159  if (!_grantService.Exists(id))
160  {
161  return NotFound();
162  }
163 
164  if (!UserHasPermission(_grantService.GetById(id)))
165  {
166  return Forbid();
167  }
168 
169  _grantService.DeleteById(id);
170  return RedirectToAction(nameof(Index));
171  }
172 
173  // POST: Grant/AddAuthor/{id}
174  [HttpPost]
175  public IActionResult AddAuthor(Guid id, [FromBody] UpdateUserRequest request)
176  {
177  if (!_grantService.Exists(id))
178  {
179  return NotFound();
180  }
181 
182  var grant = _grantService.GetById(id);
183  if (!UserHasPermission(grant))
184  {
185  return Forbid();
186  }
187 
188  _grantService.AddUser(grant, _userProfileService.GetById(request.UserId));
189  return Json(ApiResponse.Ok);
190  }
191 
192  // POST: Grant/DeleteAuthor/{id}
193  [HttpPost]
194  public IActionResult DeleteAuthor(Guid id, [FromBody] UpdateUserRequest request)
195  {
196  if (!_grantService.Exists(id))
197  {
198  return NotFound();
199  }
200 
201  var grant = _grantService.GetById(id);
202  if (!UserHasPermission(grant))
203  {
204  return Forbid();
205  }
206 
207  _grantService.RemoveUser(grant, _userProfileService.GetById(request.UserId));
208  return Json(ApiResponse.Ok);
209  }
210 
211  private bool UserHasPermission(Grant grant)
212  {
213  var user = _userProfileService.Get(User);
214  var department = _departmentService.Get(d => d.Staff.Contains(user));
215  return PageHelpers.IsAdmin(User) ||
216  PageHelpers.IsHeadOfDepartment(User) &&
217  _grantService.GetUsers(grant.Id).Any(p => department.Staff.Contains(p)) ||
218  _grantService.GetUsers(grant.Id).Contains(user);
219  }
220  }
221 }
IActionResult Create(GrantModel model)
IEnumerable< DAL.Entities.Grant > Grants
IActionResult DeleteAuthor(Guid id, [FromBody] UpdateUserRequest request)
IActionResult AddAuthor(Guid id, [FromBody] UpdateUserRequest request)
IActionResult Index(GrantIndexModel model)
GrantController(IGrantService grantService, IUserProfileService userProfileService, IDepartmentService departmentService)
IActionResult Edit(Guid id, GrantEditModel model)