ScientificReport
PatentLicenseActivityController.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;
10 
11 namespace ScientificReport.Controllers
12 {
13  [Authorize(Roles = UserProfileRole.Any)]
14  public class PatentLicenseActivityController : Controller
15  {
16  private readonly IPatentLicenseActivityService _patentLicenseActivityService;
17  private readonly IDepartmentService _departmentService;
18  private readonly IUserProfileService _userProfileService;
19 
21  IPatentLicenseActivityService patentLicenseActivityService,
22  IDepartmentService departmentService,
23  IUserProfileService userProfileService
24  )
25  {
26  _patentLicenseActivityService = patentLicenseActivityService;
27  _departmentService = departmentService;
28  _userProfileService = userProfileService;
29  }
30 
31  // GET: PatentLicenseActivity
32  public IActionResult Index(PatentLicenseActivityIndexModel model)
33  {
34  model.PatentLicenseActivities = _patentLicenseActivityService.GetPageByRole(model.CurrentPage, model.PageSize, User);
35  model.Count = _patentLicenseActivityService.GetCountByRole(User);
36  return View(model);
37  }
38 
39  // GET: PatentLicenseActivity/Details/{id}
40  public IActionResult Details(Guid? id)
41  {
42  if (id == null)
43  {
44  return NotFound();
45  }
46 
47  var patentLicenseActivity = _patentLicenseActivityService.GetById(id.Value);
48  if (patentLicenseActivity == null)
49  {
50  return NotFound();
51  }
52 
53  if (!UserHasPermission(patentLicenseActivity))
54  {
55  return Forbid();
56  }
57 
58  return View(patentLicenseActivity);
59  }
60 
61  // GET: PatentLicenseActivity/Create
62  public IActionResult Create() => View();
63 
64  // POST: PatentLicenseActivity/Create
65  [HttpPost]
66  [ValidateAntiForgeryToken]
67  public IActionResult Create(PatentLicenseActivityModel model)
68  {
69  if (!ModelState.IsValid)
70  {
71  return View(model);
72  }
73 
74  _patentLicenseActivityService.CreateItem(model);
75  var patentLicenseActivity = _patentLicenseActivityService.Get(pla => pla.Name == model.Name);
76  var user = _userProfileService.Get(User);
77  if (model.Type == PatentLicenseActivity.Types.Patent)
78  {
79  _patentLicenseActivityService.AddAuthor(patentLicenseActivity, user);
80  }
81  else
82  {
83  _patentLicenseActivityService.AddApplicant(patentLicenseActivity, user);
84  }
85 
86  return RedirectToAction(nameof(Index));
87  }
88 
89  // GET: PatentLicenseActivity/Edit/{id}
90  public IActionResult Edit(Guid? id)
91  {
92  if (id == null)
93  {
94  return NotFound();
95  }
96 
97  var patentLicenseActivity = _patentLicenseActivityService.GetById(id.Value);
98  if (patentLicenseActivity == null)
99  {
100  return NotFound();
101  }
102 
103  if (!UserHasPermission(patentLicenseActivity))
104  {
105  return Forbid();
106  }
107 
108  return View(new PatentLicenseActivityEditModel(patentLicenseActivity)
109  {
110  Users = _userProfileService.GetAll(),
111  AuthorsOrApplicants = patentLicenseActivity.Type == PatentLicenseActivity.Types.Patent
112  ? _patentLicenseActivityService.GetAuthors(patentLicenseActivity.Id)
113  : _patentLicenseActivityService.GetApplicants(patentLicenseActivity.Id)
114  });
115  }
116 
117  // POST: PatentLicenseActivity/Edit/{id}
118  [HttpPost]
119  [ValidateAntiForgeryToken]
120  public IActionResult Edit(Guid id, PatentLicenseActivityEditModel model)
121  {
122  if (id != model.Id || !_patentLicenseActivityService.Exists(id))
123  {
124  return NotFound();
125  }
126 
127  var patentLicenseActivity = _patentLicenseActivityService.GetById(id);
128  if (!UserHasPermission(patentLicenseActivity))
129  {
130  return Forbid();
131  }
132 
133  if (!ModelState.IsValid)
134  {
135  model.Users = _userProfileService.GetAll();
136  model.AuthorsOrApplicants = patentLicenseActivity.Type == PatentLicenseActivity.Types.Patent
137  ? _patentLicenseActivityService.GetAuthors(patentLicenseActivity.Id)
138  : _patentLicenseActivityService.GetApplicants(patentLicenseActivity.Id);
139  return View(model);
140  }
141 
142  _patentLicenseActivityService.UpdateItem(model);
143  return RedirectToAction(nameof(Index));
144  }
145 
146  // GET: PatentLicenseActivity/Delete/{id}
147  public IActionResult Delete(Guid? id)
148  {
149  if (id == null)
150  {
151  return NotFound();
152  }
153 
154  var patentLicenseActivity = _patentLicenseActivityService.GetById(id.Value);
155  if (patentLicenseActivity == null)
156  {
157  return NotFound();
158  }
159 
160  if (!UserHasPermission(patentLicenseActivity))
161  {
162  return Forbid();
163  }
164 
165  return View(patentLicenseActivity);
166  }
167 
168  // POST: PatentLicenseActivity/Delete/{id}
169  [HttpPost, ActionName("Delete")]
170  [ValidateAntiForgeryToken]
171  public IActionResult DeleteConfirmed(Guid id)
172  {
173  if (!_patentLicenseActivityService.Exists(id))
174  {
175  return NotFound();
176  }
177 
178  if (!UserHasPermission(_patentLicenseActivityService.GetById(id)))
179  {
180  return Forbid();
181  }
182 
183  _patentLicenseActivityService.DeleteById(id);
184  return RedirectToAction(nameof(Index));
185  }
186 
187  // POST: PatentLicenseActivity/AddUser/{patentLicenseActivityId}
188  [HttpPost]
189  [Authorize(Roles = UserProfileRole.HeadOfDepartmentOrAdmin)]
190  public IActionResult AddUser(Guid? id, [FromBody] PatentLicenseActivityUpdUserRequest request)
191  {
192  if (id == null)
193  {
194  return NotFound();
195  }
196 
197  var user = _userProfileService.GetById(request.UserId);
198  if (user == null)
199  {
200  return Json(ApiResponse.Fail);
201  }
202 
203  var patentLicenseActivity = _patentLicenseActivityService.GetById(id.Value);
204  if (patentLicenseActivity == null)
205  {
206  return NotFound();
207  }
208 
209  if (!UserHasPermission(patentLicenseActivity))
210  {
211  return Json(ApiResponse.Fail);
212  }
213 
214  if (request.ActivityType == PatentLicenseActivity.Types.Patent)
215  {
216  if (!_patentLicenseActivityService.GetAuthors(patentLicenseActivity.Id).Contains(user))
217  {
218  _patentLicenseActivityService.AddAuthor(patentLicenseActivity, user);
219  }
220  }
221  else
222  {
223  if (!_patentLicenseActivityService.GetApplicants(patentLicenseActivity.Id).Contains(user))
224  {
225  _patentLicenseActivityService.AddApplicant(patentLicenseActivity, user);
226  }
227  }
228 
229  return Json(ApiResponse.Ok);
230  }
231 
232  // POST: PatentLicenseActivity/RemoveUser/{patentLicenseActivityId}
233  [HttpPost]
234  [Authorize(Roles = UserProfileRole.HeadOfDepartmentOrAdmin)]
235  public IActionResult RemoveUser(Guid? id, [FromBody] PatentLicenseActivityUpdUserRequest request)
236  {
237  if (id == null)
238  {
239  return NotFound();
240  }
241 
242  var user = _userProfileService.GetById(request.UserId);
243  if (user == null)
244  {
245  return Json(ApiResponse.Fail);
246  }
247 
248  var patentLicenseActivity = _patentLicenseActivityService.GetById(id.Value);
249  if (patentLicenseActivity == null)
250  {
251  return NotFound();
252  }
253 
254  if (!UserHasPermission(patentLicenseActivity))
255  {
256  return Json(ApiResponse.Fail);
257  }
258 
259  if (request.ActivityType == PatentLicenseActivity.Types.Patent)
260  {
261  if (_patentLicenseActivityService.GetAuthors(patentLicenseActivity.Id).Contains(user))
262  {
263  _patentLicenseActivityService.RemoveAuthor(patentLicenseActivity.Id, user);
264  }
265  }
266  else
267  {
268  if (_patentLicenseActivityService.GetApplicants(patentLicenseActivity.Id).Contains(user))
269  {
270  _patentLicenseActivityService.RemoveApplicant(patentLicenseActivity.Id, user);
271  }
272  }
273 
274  return Json(ApiResponse.Ok);
275  }
276 
277  private bool UserHasPermission(PatentLicenseActivity patentLicenseActivity)
278  {
279  var user = _userProfileService.Get(User);
280  var department = _departmentService.Get(d => d.Staff.Contains(user));
281  return PageHelpers.IsAdmin(User) ||
282  PageHelpers.IsHeadOfDepartment(User) &&
283  (patentLicenseActivity.AuthorsPatentLicenseActivities.Any(p => department.Staff.Contains(p.Author)) ||
284  patentLicenseActivity.ApplicantsPatentLicenseActivities.Any(p => department.Staff.Contains(p.Applicant))) ||
285  patentLicenseActivity.AuthorsPatentLicenseActivities.Any(a => a.Author.Id == user.Id) ||
286  patentLicenseActivity.ApplicantsPatentLicenseActivities.Any(a => a.Applicant.Id == user.Id);
287  }
288  }
289 }
IActionResult Edit(Guid id, PatentLicenseActivityEditModel model)
IActionResult RemoveUser(Guid?id, [FromBody] PatentLicenseActivityUpdUserRequest request)
IActionResult Index(PatentLicenseActivityIndexModel model)
PatentLicenseActivityController(IPatentLicenseActivityService patentLicenseActivityService, IDepartmentService departmentService, IUserProfileService userProfileService)
virtual ICollection< ApplicantsPatentLicenseActivities > ApplicantsPatentLicenseActivities
virtual ICollection< AuthorsPatentLicenseActivities > AuthorsPatentLicenseActivities
IActionResult AddUser(Guid?id, [FromBody] PatentLicenseActivityUpdUserRequest request)