ScientificReport
ReportThesisController.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 ReportThesisController : Controller
16  {
17  private readonly IReportThesisService _reportThesisService;
18  private readonly IUserProfileService _userProfileService;
19  private readonly IDepartmentService _departmentService;
20  private readonly IConferenceService _conferenceService;
21 
23  IReportThesisService reportThesisService,
24  IUserProfileService userProfileService,
25  IDepartmentService departmentService,
26  IConferenceService conferenceService
27  )
28  {
29  _reportThesisService = reportThesisService;
30  _userProfileService = userProfileService;
31  _departmentService = departmentService;
32  _conferenceService = conferenceService;
33  }
34 
35  // GET: ReportThesis/Details/{id}
36  public IActionResult Details(Guid? id)
37  {
38  if (id == null)
39  {
40  return NotFound();
41  }
42 
43  var reportThesis = _reportThesisService.GetById(id.Value);
44 
45  if (reportThesis == null)
46  {
47  return NotFound();
48  }
49 
50  if (!UserHasPermission(reportThesis))
51  {
52  return Forbid();
53  }
54 
55  var reportThesisDetails = new ReportThesisDetails
56  {
57  ReportThesis = reportThesis,
58  Authors = _reportThesisService.GetAuthors(reportThesis.Id).ToList()
59  };
60 
61  return View(reportThesisDetails);
62  }
63 
64  // GET: ReportThesis/Edit/{id}
65  public IActionResult Edit(Guid? id)
66  {
67  if (id == null)
68  {
69  return NotFound();
70  }
71 
72  var reportThesis = _reportThesisService.GetById(id.Value);
73  if (reportThesis == null)
74  {
75  return NotFound();
76  }
77 
78  if (!UserHasPermission(reportThesis))
79  {
80  return Forbid();
81  }
82 
83  return View(new ReportThesisEdit(reportThesis)
84  {
85  Authors = _reportThesisService.GetAuthors(reportThesis.Id),
86  Users = _userProfileService.GetAll()
87  });
88  }
89 
90  // POST: ReportThesis/Edit/{id}
91  [HttpPost]
92  [ValidateAntiForgeryToken]
93  public IActionResult Edit(Guid? id, ReportThesisEdit model)
94  {
95  var reportThesis = _reportThesisService.GetById(model.Id);
96  if (id != reportThesis.Id)
97  {
98  return NotFound();
99  }
100 
101  if (!UserHasPermission(reportThesis))
102  {
103  return Forbid();
104  }
105 
106  if (!ModelState.IsValid)
107  {
108  model.Authors = _reportThesisService.GetAuthors(reportThesis.Id);
109  model.Users = _userProfileService.GetAll();
110  return View(model);
111  }
112 
113  model.Conference = _conferenceService.GetById(model.ConferenceId);
114  _reportThesisService.UpdateItem(model);
115 
116  return RedirectToAction("Index", "Publication");
117  }
118 
119  // GET: ReportThesis/Delete/{id}
120  public IActionResult Delete(Guid? id)
121  {
122  if (id == null)
123  {
124  return NotFound();
125  }
126 
127  var reportThesis = _reportThesisService.GetById(id.Value);
128  if (reportThesis == null)
129  {
130  return NotFound();
131  }
132 
133  if (!UserHasPermission(reportThesis))
134  {
135  return Forbid();
136  }
137 
138  return View(reportThesis);
139  }
140 
141  // POST: ReportThesis/Delete/{id}
142  [HttpPost, ActionName("Delete")]
143  [ValidateAntiForgeryToken]
144  public IActionResult DeleteConfirmed(Guid id)
145  {
146  if (!_reportThesisService.Exists(id))
147  {
148  return NotFound();
149  }
150 
151  if (!UserHasPermission(_reportThesisService.GetById(id)))
152  {
153  return Forbid();
154  }
155 
156  _reportThesisService.DeleteById(id);
157  return RedirectToAction("Index", "Publication");
158  }
159 
160  // POST: ReportThesis/AddAuthor/{id}
161  [HttpPost]
162  public IActionResult AddAuthor(Guid id, [FromBody] UpdateUserRequest request)
163  {
164  if (!_reportThesisService.Exists(id))
165  {
166  return NotFound();
167  }
168 
169  if (!UserHasPermission(_reportThesisService.GetById(id)))
170  {
171  return Forbid();
172  }
173 
174  _reportThesisService.AddAuthor(id, request.UserId);
175  return Json(ApiResponse.Ok);
176  }
177 
178  // POST: ReportThesis/DeleteAuthor/{id}
179  [HttpPost]
180  public IActionResult DeleteAuthor(Guid id, [FromBody] UpdateUserRequest request)
181  {
182  if (!_reportThesisService.Exists(id))
183  {
184  return NotFound();
185  }
186 
187  if (!UserHasPermission(_reportThesisService.GetById(id)))
188  {
189  return Forbid();
190  }
191 
192  _reportThesisService.RemoveAuthor(id, request.UserId);
193  return Json(ApiResponse.Ok);
194  }
195 
196  private bool UserHasPermission(ReportThesis reportThesis)
197  {
198  var user = _userProfileService.Get(User);
199  var department = _departmentService.Get(d => d.Staff.Contains(user));
200  return PageHelpers.IsAdmin(User) ||
201  PageHelpers.IsHeadOfDepartment(User) &&
202  _reportThesisService.GetAuthors(reportThesis.Id).Any(p => department.Staff.Contains(p)) ||
203  _reportThesisService.GetAuthors(reportThesis.Id).Contains(user);
204  }
205  }
206 }
IEnumerable< DAL.Entities.UserProfile.UserProfile > Users
IActionResult DeleteAuthor(Guid id, [FromBody] UpdateUserRequest request)
IEnumerable< DAL.Entities.UserProfile.UserProfile > Authors
IActionResult Edit(Guid?id, ReportThesisEdit model)
ReportThesisController(IReportThesisService reportThesisService, IUserProfileService userProfileService, IDepartmentService departmentService, IConferenceService conferenceService)
IActionResult AddAuthor(Guid id, [FromBody] UpdateUserRequest request)