3 using System.Threading.Tasks;
4 using Microsoft.AspNetCore.Authorization;
5 using Microsoft.AspNetCore.Identity;
6 using Microsoft.AspNetCore.Mvc;
7 using Microsoft.Extensions.Localization;
19 private readonly UserManager<UserProfile> _userManager;
20 private readonly SignInManager<UserProfile> _signInManager;
21 private readonly RoleManager<UserProfileRole> _roleManager;
25 private readonly IStringLocalizer<UserProfileController> _localizer;
28 UserManager<UserProfile> usrMgr,
29 SignInManager<UserProfile> signInManager,
30 RoleManager<UserProfileRole> roleManager,
33 IStringLocalizer<UserProfileController> localizer
36 _userManager = usrMgr;
37 _signInManager = signInManager;
38 _roleManager = roleManager;
39 _userProfileService = userProfileService;
40 _departmentService = departmentService;
41 _localizer = localizer;
49 model.
UserProfiles = _userProfileService.Filter(model, User, PageHelpers.IsAdmin(User));
51 model.
Count = _userProfileService.GetCount();
57 public async Task<IActionResult>
Details(Guid?
id)
64 var userProfile = _userProfileService.GetById(
id.Value);
65 if (userProfile == null)
70 var department = _departmentService.Get(d => d.Staff.Contains(userProfile));
72 var fullPositionTitle =
"";
75 fullPositionTitle = _localizer[
"Administrator"] +
", ";
77 fullPositionTitle += _localizer[userProfile.Position] + (department != null
78 ?
" " + _localizer[
"of department"] +
" \"" + department.Title +
"\"" 84 FullPositionTitle = fullPositionTitle
87 if (!PageHelpers.IsAdmin(User))
89 var currentUser = _userProfileService.Get(User);
90 if (PageHelpers.IsHeadOfDepartment(User))
92 if (department == null || !_departmentService.UserWorksInDepartment(currentUser, userProfile.Id))
99 return View(detailsModel);
104 public async Task<IActionResult>
Edit(Guid?
id) {
109 var user = _userProfileService.GetById(
id.Value);
112 var currentUser = _userProfileService.Get(User);
113 if (!PageHelpers.IsAdmin(User))
115 if (PageHelpers.IsHeadOfDepartment(User))
117 if (!_departmentService.UserWorksInDepartment(currentUser, user.Id))
122 else if (PageHelpers.IsTeacher(User) && currentUser.Id != user.Id)
132 MiddleName = user.MiddleName,
133 LastName = user.LastName,
134 BirthYear = user.BirthYear,
135 GraduationYear = user.GraduationYear,
136 ScientificDegree = user.ScientificDegree,
137 YearDegreeGained = user.YearDegreeGained,
138 AcademicStatus = user.AcademicStatus,
139 YearDegreeAssigned = user.YearDegreeAssigned,
140 PhoneNumber = user.PhoneNumber,
141 IsApproved = user.IsApproved,
142 IsActive = user.IsActive,
143 UserName = user.UserName,
146 IsSelfEditing = currentUser.Id == user.Id,
147 AllRoles = _roleManager.Roles.ToList(),
148 UserRoles = await _userManager.GetRolesAsync(user),
153 return RedirectToAction(
"Index");
160 if (!ModelState.IsValid)
170 if (_userProfileService.UserExists(
id.Value))
172 var user = _userProfileService.GetById(
id.Value);
173 var currentUser = _userProfileService.Get(User);
174 if (PageHelpers.IsAdmin(User) || PageHelpers.IsTeacher(User) && currentUser.Id == user.Id || PageHelpers.IsHeadOfDepartment(User) && _departmentService.UserWorksInDepartment(currentUser, user.Id))
202 user.Sex = model.
Sex;
205 if (PageHelpers.IsAdminOrHead(User) && currentUser.Id !=
id.Value)
212 user.Email = model.
Email;
214 _userProfileService.UpdateItem(user);
226 return PageHelpers.IsAdminOrHead(User) ? RedirectToAction(
"Index") : RedirectToAction(
"Details",
"UserProfile",
new {
id =
id.Value});
242 return Json(
new {Success =
false});
245 var userExists = _userProfileService.UserExists(
id.Value);
248 var user = _userProfileService.GetById(
id.Value);
254 if (!await _userProfileService.IsInRoleAsync(user, request.RoleName, _userManager))
256 await _userProfileService.AddToRoleAsync(user, request.RoleName, _userManager);
260 return Json(
new {Success = userExists});
273 var userExists = _userProfileService.UserExists(
id.Value);
276 var user = _userProfileService.GetById(
id.Value);
277 if (await _userProfileService.IsInRoleAsync(user, request.RoleName, _userManager))
279 if (user.UserName != User.Identity.Name)
281 await _userProfileService.RemoveFromRoleAsync(user, request.RoleName, _userManager);
284 _userProfileService.UpdateItem(user);
314 if (!_userProfileService.UserExists(
id.Value))
319 var currentUser = _userProfileService.Get(User);
320 if (currentUser.Id !=
id.Value)
322 if (!PageHelpers.IsAdmin(User) && PageHelpers.IsHeadOfDepartment(User))
324 if (!_departmentService.UserWorksInDepartment(currentUser,
id.Value))
330 _userProfileService.DeleteById(
id.Value);
332 return RedirectToAction(
"Index");
345 if (!_userProfileService.UserExists(
id.Value))
350 var currentUser = _userProfileService.Get(User);
351 if (currentUser.Id !=
id.Value)
353 if (!PageHelpers.IsAdmin(User) && PageHelpers.IsHeadOfDepartment(User))
355 if (!_departmentService.UserWorksInDepartment(currentUser,
id.Value))
361 _userProfileService.SetActiveById(
id.Value, isActive);
364 return RedirectToAction(
"Index");
373 var departments = _departmentService.GetAll();
376 Departments = departments
383 [ValidateAntiForgeryToken]
385 if (!ModelState.IsValid)
391 if (_userProfileService.Get(usr => usr.UserName == model.
UserName) != null)
393 ModelState.AddModelError(
string.Empty, _localizer[
"User already exists"]);
411 var result = await _userManager.CreateAsync(user, model.
Password);
412 if (result.Succeeded)
414 var createdUser = _userProfileService.Get(u => u.UserName == user.UserName);
415 var addUserToRoleResult = await _userProfileService.AddToRoleAsync(
418 if (addUserToRoleResult.Succeeded)
421 if (department != null)
423 department.Staff.Add(createdUser);
424 _departmentService.UpdateItem(department);
425 return RedirectToAction(
"Index");
429 AddErrorsFromResult(addUserToRoleResult);
432 AddErrorsFromResult(result);
436 ModelState.AddModelError(
string.Empty, _localizer[
"Password confirmation failed"]);
448 if (User.Identity.IsAuthenticated)
450 return Redirect(
"/");
459 [ValidateAntiForgeryToken]
462 if (ModelState.IsValid)
464 var user = _userProfileService.Get(usr => usr.UserName == model.
UserName);
467 if (!user.IsApproved)
469 ModelState.AddModelError(
string.Empty, _localizer[
"Account is not approved yet"]);
475 var result = await _signInManager.PasswordSignInAsync(
478 if (result.Succeeded)
486 ModelState.AddModelError(
string.Empty, _localizer[
"Incorrect login or password"]);
493 public async Task<IActionResult>
Logout()
495 await _signInManager.SignOutAsync();
496 return Redirect(
"/");
503 var currentUser = _userManager.GetUserAsync(HttpContext.User);
504 if (currentUser == null)
511 Id = currentUser.Result.
Id 519 if (model.
Id == null)
524 var user = _userProfileService.GetById(model.
Id.Value);
530 if (!ModelState.IsValid)
535 var error = await _userProfileService.ChangePassword(
541 return Redirect(
"/");
544 ModelState.AddModelError(
string.Empty, error);
548 private void AddErrorsFromResult(IdentityResult result)
550 foreach (var error
in result.Errors)
552 ModelState.AddModelError(
string.Empty, error.Description);
IActionResult Edit(Guid?id, UserProfileEditModel model)
IActionResult ChangePassword()
async Task< IActionResult > Logout()
const string Administrator
async Task< IActionResult > Edit(Guid?id)
IActionResult SetActive(Guid?id, bool isActive)
IActionResult Delete(Guid?id)
async Task< IActionResult > Login(LoginModel model)
IActionResult Index(UserProfileIndexModel model)
const string HeadOfDepartment
async Task< IActionResult > RemoveUserFromAdministration(Guid?id, [FromBody] UserProfileUpdateRolesRequest request)
IEnumerable< DAL.Entities.Department > Departments
async Task< IActionResult > Register(RegisterModel model)
IEnumerable< DAL.Entities.Department > Departments
Guid SelectedDepartmentId
async Task< IActionResult > AddUserToAdministration(Guid?id, [FromBody] UserProfileUpdateRolesRequest request)
async Task< IActionResult > ChangePassword(ChangePasswordModel model)
UserProfileController(UserManager< UserProfile > usrMgr, SignInManager< UserProfile > signInManager, RoleManager< UserProfileRole > roleManager, IUserProfileService userProfileService, IDepartmentService departmentService, IStringLocalizer< UserProfileController > localizer)
const string HeadOfDepartmentOrAdmin
IEnumerable< DAL.Entities.UserProfile.UserProfile > UserProfiles
async Task< IActionResult > Details(Guid?id)
DAL.Entities.UserProfile.UserProfile.SexValue Sex