ScientificReport
UserProfileService.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Security.Claims;
5 using System.Threading.Tasks;
6 using Microsoft.AspNetCore.Identity;
15 
16 namespace ScientificReport.BLL.Services
17 {
19  {
20  private readonly UserProfileRepository _userProfileRepository;
21  private readonly DepartmentRepository _departmentRepository;
22  private readonly ReviewRepository _reviewRepository;
23  private readonly ScientificConsultationRepository _scientificConsultationRepository;
24 
26  {
27  _userProfileRepository = new UserProfileRepository(context);
28  _departmentRepository = new DepartmentRepository(context);
29  _reviewRepository = new ReviewRepository(context);
30  _scientificConsultationRepository = new ScientificConsultationRepository(context);
31  }
32 
33  public virtual int GetCount()
34  {
35  return _userProfileRepository.All().Count();
36  }
37 
38  public virtual IEnumerable<UserProfile> GetAll()
39  {
40  return _userProfileRepository.All();
41  }
42 
43  public virtual IEnumerable<UserProfile> Filter(UserProfileIndexModel model, ClaimsPrincipal userPrincipal, bool userIsAdmin)
44  {
45  IEnumerable<UserProfile> users;
46  if (userIsAdmin)
47  {
48  if (model.DepartmentId != null)
49  {
50  var department = _departmentRepository.Get(d => d.Id == model.DepartmentId.Value);
51  users = department != null
52  ? GetPage(department.Staff, model.CurrentPage, model.PageSize)
53  : GetPage(model.CurrentPage, model.PageSize);
54  }
55  else
56  {
57  users = GetPage(model.CurrentPage, model.PageSize);
58  }
59  }
60  else
61  {
62  var currentUser = Get(userPrincipal);
63  var department = _departmentRepository.Get(u => u.Head.Id == currentUser.Id);
64  users = GetPage(department.Staff, model.CurrentPage, model.PageSize);
65  }
66 
67  if (model.IsApproved != null)
68  {
69  switch (model.IsApproved.Value)
70  {
72  break;
74  users = users.Where(u => u.IsApproved);
75  break;
77  users = users.Where(u => !u.IsApproved);
78  break;
79  }
80  }
81 
82  if (model.FirstName != null)
83  {
84  users = users.Where(u => u.FirstName.ToLower().Contains(model.FirstName.Trim().ToLower()));
85  }
86 
87  if (model.LastName != null)
88  {
89  users = users.Where(u => u.LastName.ToLower().Contains(model.LastName.Trim().ToLower()));
90  }
91 
92  return users;
93  }
94 
95  public virtual IEnumerable<UserProfile> GetAllWhere(Func<UserProfile, bool> predicate)
96  {
97  return _userProfileRepository.AllWhere(predicate);
98  }
99 
100  public virtual IEnumerable<UserProfile> GetPage(int page, int count)
101  {
102  return _userProfileRepository.All().Skip((page - 1) * count).Take(count).ToList();
103  }
104 
105  public virtual IEnumerable<UserProfile> GetPage(IEnumerable<UserProfile> userProfiles, int page, int count)
106  {
107  return userProfiles.Skip((page - 1) * count).Take(count).ToList();
108  }
109 
110  public virtual UserProfile Get(ClaimsPrincipal claimsPrincipal)
111  {
112  return _userProfileRepository.Get(u => u.UserName == claimsPrincipal.Identity.Name);
113  }
114 
115  public virtual UserProfile GetById(Guid id)
116  {
117  return _userProfileRepository.Get(id);
118  }
119 
120  public virtual UserProfile Get(Func<UserProfile, bool> predicate)
121  {
122  return _userProfileRepository.Get(predicate);
123  }
124 
125  public virtual void CreateItem(UserProfile item)
126  {
127  if (item != null)
128  {
129  _userProfileRepository.Create(item);
130  }
131  }
132 
133  public virtual void UpdateItem(UserProfile item)
134  {
135  if (item != null)
136  {
137  _userProfileRepository.Update(item);
138  }
139  }
140 
141  public virtual void DeleteById(Guid id)
142  {
143  _userProfileRepository.Delete(id);
144  }
145 
146  public virtual void SetActiveById(Guid id, bool isActive)
147  {
148  var user = _userProfileRepository.Get(id);
149  user.IsActive = isActive;
150  _userProfileRepository.Update(user);
151  }
152 
153  public virtual void SetApproved(Guid id, bool isApproved)
154  {
155  var user = _userProfileRepository.Get(id);
156  if (user == null) return;
157  user.IsApproved = isApproved;
158  _userProfileRepository.Update(user);
159  }
160 
161  public virtual bool UserExists(Guid id)
162  {
163  return _userProfileRepository.Get(id) != null;
164  }
165 
166  public virtual async Task<IdentityResult> AddToRoleAsync(UserProfile userProfile, string roleName, UserManager<UserProfile> userManager)
167  {
168  return await userManager.AddToRoleAsync(userProfile, roleName);
169  }
170 
171  public virtual async Task<IdentityResult> RemoveFromRoleAsync(UserProfile userProfile, string roleName, UserManager<UserProfile> userManager)
172  {
173  return await userManager.RemoveFromRoleAsync(userProfile, roleName);
174  }
175 
176  public virtual async Task<bool> IsInRoleAsync(UserProfile user, string roleName, UserManager<UserProfile> userManager)
177  {
178  return await userManager.IsInRoleAsync(user, roleName);
179  }
180 
181  public virtual async Task<string> ChangePassword(UserProfile user, string oldPassword, string newPassword, string newPasswordRepeat, UserManager<UserProfile> userManager)
182  {
183  string result = null;
184 
185  // Checks if entered password is equal to current user password
186  if (await userManager.CheckPasswordAsync(user, oldPassword))
187  {
188  // Checks if new password and repeated password are equal
189  if (newPassword.Equals(newPasswordRepeat))
190  {
191  // Checks if new password is equal to current user password
192  if (!oldPassword.Equals(newPassword))
193  {
194  var passwordValidator = new PasswordValidator<UserProfile>();
195 
196  // Validates new password
197  var validationResult = await passwordValidator.ValidateAsync(userManager, user, newPassword);
198  if (validationResult.Succeeded)
199  {
200  await userManager.ChangePasswordAsync(user, oldPassword, newPassword);
201  }
202  else
203  {
204  result = validationResult.ToString();
205  }
206  }
207  else
208  {
209  result = "New password must differ from an old one";
210  }
211  }
212  else
213  {
214  result = "Repeated password does not mach a new one";
215  }
216  }
217  else
218  {
219  result = "Old password is incorrect";
220  }
221 
222  return result;
223  }
224 
225  public virtual async Task<bool>IsTeacherOnlyAsync(UserProfile user, UserManager<UserProfile> userManager)
226  {
227  var isTeacher = await userManager.IsInRoleAsync(user, UserProfileRole.Teacher);
228  var isHead = await userManager.IsInRoleAsync(user, UserProfileRole.HeadOfDepartment);
229  var isAdmin = await userManager.IsInRoleAsync(user, UserProfileRole.Administrator);
230  return isTeacher && !(isHead || isAdmin);
231  }
232 
233  public virtual ICollection<Publication> GetUserPublications(Guid id)
234  {
235  var user = _userProfileRepository.Get(id);
236  ICollection<Publication> result = null;
237  if (user != null)
238  {
239  result = user.UserProfilesPublications.Select(item => item.Publication).ToList();
240  }
241 
242  return result;
243  }
244 
245  public virtual ICollection<Grant> GetUserGrants(Guid id)
246  {
247  var user = _userProfileRepository.Get(id);
248  ICollection<Grant> result = null;
249  if (user != null)
250  {
251  result = user.UserProfilesGrants.Select(item => item.Grant).ToList();
252  }
253 
254  return result;
255  }
256 
257  public virtual ICollection<ScientificWork> GetUserScientificWorks(Guid id)
258  {
259  var user = _userProfileRepository.Get(id);
260  ICollection<ScientificWork> result = null;
261  if (user != null)
262  {
263  result = user.UserProfilesScientificWorks.Select(item => item.ScientificWork).ToList();
264  }
265 
266  return result;
267  }
268 
269  public virtual ICollection<Article> GetUserArticles(Guid id)
270  {
271  var user = _userProfileRepository.Get(id);
272  ICollection<Article> result = null;
273  if (user != null)
274  {
275  result = user.UserProfilesArticles.Select(item => item.Article).ToList();
276  }
277 
278  return result;
279  }
280 
281  public virtual ICollection<ReportThesis> GetUserReportTheses(Guid id)
282  {
283  var user = _userProfileRepository.Get(id);
284  ICollection<ReportThesis> result = null;
285  if (user != null)
286  {
287  result = user.UserProfilesReportTheses.Select(item => item.ReportThesis).ToList();
288  }
289 
290  return result;
291  }
292 
293  public virtual ICollection<ScientificInternship> GetUserScientificInternships(Guid id)
294  {
295  var user = _userProfileRepository.Get(id);
296  ICollection<ScientificInternship> result = null;
297  if (user != null)
298  {
299  result = user.UserProfilesScientificInternships.Select(item => item.ScientificInternship).ToList();
300  }
301 
302  return result;
303  }
304 
305  public virtual ICollection<Review> GetUserReviews(Guid id)
306  {
307  var user = _userProfileRepository.Get(id);
308  IEnumerable<Review> result = null;
309  if (user != null)
310  {
311  result = _reviewRepository.AllWhere(r => r.Reviewer.Id == user.Id);
312  }
313 
314  return result?.ToList();
315  }
316 
317  public virtual ICollection<PatentLicenseActivity> GetUserPatentLicenseActivitiesAsAuthor(Guid id)
318  {
319  var user = _userProfileRepository.Get(id);
320  ICollection<PatentLicenseActivity> result = null;
321  if (user != null)
322  {
323  result = user.AuthorsPatentLicenseActivities.Select(item => item.PatentLicenseActivity).ToList();
324  }
325 
326  return result;
327  }
328 
329  public virtual ICollection<PatentLicenseActivity> GetUserPatentLicenseActivitiesAsApplicant(Guid id)
330  {
331  var user = _userProfileRepository.Get(id);
332  ICollection<PatentLicenseActivity> result = null;
333  if (user != null)
334  {
335  result = user.ApplicantsPatentLicenseActivities.Select(item => item.PatentLicenseActivity).ToList();
336  }
337 
338  return result;
339  }
340 
341  public ICollection<ScientificConsultation> GetUserScientificConsultations(Guid id)
342  {
343  var user = _userProfileRepository.Get(id);
344  IEnumerable<ScientificConsultation> result = null;
345  if (user != null)
346  {
347  result = _scientificConsultationRepository.AllWhere(sc => sc.Guide.Id == user.Id);
348  }
349 
350  return result?.ToList();
351  }
352  }
353 }
virtual ICollection< PatentLicenseActivity > GetUserPatentLicenseActivitiesAsApplicant(Guid id)
virtual UserProfile Get(Func< UserProfile, bool > predicate)
virtual IEnumerable< UserProfile > GetPage(IEnumerable< UserProfile > userProfiles, int page, int count)
virtual void SetActiveById(Guid id, bool isActive)
UserProfileService(ScientificReportDbContext context)
virtual ICollection< ReportThesis > GetUserReportTheses(Guid id)
virtual ICollection< Article > GetUserArticles(Guid id)
virtual ICollection< ScientificWork > GetUserScientificWorks(Guid id)
virtual ICollection< Review > GetUserReviews(Guid id)
virtual IEnumerable< UserProfile > GetAll()
virtual async Task< IdentityResult > RemoveFromRoleAsync(UserProfile userProfile, string roleName, UserManager< UserProfile > userManager)
virtual ICollection< ScientificInternship > GetUserScientificInternships(Guid id)
virtual ICollection< Grant > GetUserGrants(Guid id)
virtual async Task< string > ChangePassword(UserProfile user, string oldPassword, string newPassword, string newPasswordRepeat, UserManager< UserProfile > userManager)
virtual UserProfile Get(ClaimsPrincipal claimsPrincipal)
ICollection< ScientificConsultation > GetUserScientificConsultations(Guid id)
virtual IEnumerable< UserProfile > GetAllWhere(Func< UserProfile, bool > predicate)
virtual void SetApproved(Guid id, bool isApproved)
virtual ICollection< PatentLicenseActivity > GetUserPatentLicenseActivitiesAsAuthor(Guid id)
virtual async Task< bool > IsTeacherOnlyAsync(UserProfile user, UserManager< UserProfile > userManager)
virtual async Task< IdentityResult > AddToRoleAsync(UserProfile userProfile, string roleName, UserManager< UserProfile > userManager)
virtual ICollection< Publication > GetUserPublications(Guid id)
virtual IEnumerable< UserProfile > Filter(UserProfileIndexModel model, ClaimsPrincipal userPrincipal, bool userIsAdmin)
virtual async Task< bool > IsInRoleAsync(UserProfile user, string roleName, UserManager< UserProfile > userManager)
virtual IEnumerable< UserProfile > GetPage(int page, int count)