ScientificReport
ArticleController.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 ArticleController : Controller
15  {
16  private readonly IArticleService _articleService;
17  private readonly IUserProfileService _userProfileService;
18  private readonly IDepartmentService _departmentService;
19 
21  IArticleService articleService,
22  IUserProfileService userProfileService,
23  IDepartmentService departmentService
24  )
25  {
26  _articleService = articleService;
27  _userProfileService = userProfileService;
28  _departmentService = departmentService;
29  }
30 
31  // GET: Article/Details/{id}
32  public IActionResult Details(Guid? id)
33  {
34  if (id == null)
35  {
36  return NotFound();
37  }
38 
39  var article = _articleService.GetById(id.Value);
40  if (article == null)
41  {
42  return NotFound();
43  }
44 
45  return View(new ArticleDetailsModel
46  {
47  Article = article,
48  Authors = _articleService.GetAuthors(article.Id)
49  });
50  }
51 
52  // GET: Article/Edit/{id}
53  public IActionResult Edit(Guid? id)
54  {
55  if (id == null)
56  {
57  return NotFound();
58  }
59 
60  var article = _articleService.GetById(id.Value);
61  if (article == null)
62  {
63  return NotFound();
64  }
65 
66  if (!AllowUserToEditArticle(article))
67  {
68  return Forbid();
69  }
70 
71  var model = new ArticleEditModel(article)
72  {
73  Users = _userProfileService.GetAll(),
74  Authors = _articleService.GetAuthors(article.Id)
75  };
76 
77  return View(model);
78  }
79 
80  // POST: Article/Edit/{id}
81  [HttpPost]
82  [ValidateAntiForgeryToken]
83  public IActionResult Edit(Guid? id, ArticleEditModel model)
84  {
85  if (id == null || id != model.Id)
86  {
87  return NotFound();
88  }
89 
90  var article = _articleService.GetById(id.Value);
91 
92  if (!AllowUserToEditArticle(article))
93  {
94  return Forbid();
95  }
96 
97  if (!ModelState.IsValid)
98  {
99  return View(model);
100  }
101 
102  _articleService.UpdateItem(model.Modify(article));
103  return RedirectToAction("Index", "Publication");
104  }
105 
106  // POST: Article/AddUserToAuthors/{articleId}
107  [HttpPost]
108  [Authorize(Roles = UserProfileRole.HeadOfDepartmentOrAdmin)]
109  public IActionResult AddUserToAuthors(Guid? id, [FromBody] ArticleUpdateAuthorsRequest request)
110  {
111  if (id == null)
112  {
113  return NotFound();
114  }
115 
116  var user = _userProfileService.GetById(request.UserId);
117  if (user == null)
118  {
119  return Json(ApiResponse.Fail);
120  }
121 
122  var article = _articleService.GetById(id.Value);
123  if (article == null)
124  {
125  return NotFound();
126  }
127 
128  if (!AllowUserToEditArticle(article))
129  {
130  return Json(ApiResponse.Fail);
131  }
132 
133  if (!_articleService.GetAuthors(article.Id).Contains(user))
134  {
135  _articleService.AddAuthor(article, user);
136  }
137 
138  return Json(ApiResponse.Ok);
139  }
140 
141  // POST: Article/RemoveUserFromAuthors/{articleId}
142  [HttpPost]
143  [Authorize(Roles = UserProfileRole.HeadOfDepartmentOrAdmin)]
144  public IActionResult RemoveUserFromAuthors(Guid? id, [FromBody] ArticleUpdateAuthorsRequest request)
145  {
146  if (id == null)
147  {
148  return NotFound();
149  }
150 
151  var user = _userProfileService.GetById(request.UserId);
152  if (user == null)
153  {
154  return Json(ApiResponse.Fail);
155  }
156 
157  var article = _articleService.GetById(id.Value);
158  if (article == null)
159  {
160  return NotFound();
161  }
162 
163  if (!AllowUserToEditArticle(article))
164  {
165  return Json(ApiResponse.Fail);
166  }
167 
168  if (_articleService.GetAuthors(article.Id).Contains(user))
169  {
170  _articleService.RemoveAuthor(article, user);
171  }
172 
173  return Json(ApiResponse.Ok);
174  }
175 
176  // GET: Article/Delete/{id}
177  public IActionResult Delete(Guid? id)
178  {
179  if (id == null)
180  {
181  return NotFound();
182  }
183 
184  var article = _articleService.GetById(id.Value);
185  if (article == null)
186  {
187  return NotFound();
188  }
189 
190  if (!AllowToDeleteArticle())
191  {
192  return Forbid();
193  }
194 
195  return View(article);
196  }
197 
198  // POST: Article/Delete/{id}
199  [HttpPost, ActionName("Delete")]
200  [ValidateAntiForgeryToken]
201  public IActionResult DeleteConfirmed(Guid id)
202  {
203  var article = _articleService.GetById(id);
204  if (article == null)
205  {
206  return NotFound();
207  }
208 
209  if (!AllowToDeleteArticle())
210  {
211  return Forbid();
212  }
213 
214  _articleService.DeleteById(id);
215 
216  return RedirectToAction("Index", "Publication");
217  }
218 
219  private bool AllowUserToEditArticle(Article article)
220  {
221  var user = _userProfileService.Get(User);
222  var department = _departmentService.Get(d => d.Staff.Contains(user));
223  var isHeadOfDepartment = PageHelpers.IsHeadOfDepartment(User) && article.UserProfilesArticles.Any(p => department.Staff.Contains(p.Author));
224  return PageHelpers.IsAdmin(User) || isHeadOfDepartment ||
225  article.UserProfilesArticles.Any(p => p.Author.UserName == User.Identity.Name) &&
226  article.PublishingYear == DateTime.Now.Year;
227  }
228 
229  private bool AllowToDeleteArticle()
230  {
231  return PageHelpers.IsAdmin(User);
232  }
233  }
234 }
IActionResult Edit(Guid?id, ArticleEditModel model)
ArticleController(IArticleService articleService, IUserProfileService userProfileService, IDepartmentService departmentService)
IActionResult RemoveUserFromAuthors(Guid?id, [FromBody] ArticleUpdateAuthorsRequest request)
IActionResult AddUserToAuthors(Guid?id, [FromBody] ArticleUpdateAuthorsRequest request)
DAL.Entities.Publications.Article Modify(DAL.Entities.Publications.Article article)
virtual ICollection< UserProfilesArticles > UserProfilesArticles
Definition: Article.cs:42