public async Task <ActionResult> Edit(string id) { ApplicationUser user = await _userManager.FindByIdAsync(id); if (user == null) { return(View("Error")); } ViewBag.Id = id; ViewBag.FirstName = user.FirstName; ViewBag.LastName = user.LastName; ViewBag.Email = user.Email; #region GetUserClubRank string userClubRole = "Brak"; IList <Claim> claim = await _userManager.GetClaimsAsync(user); Claim[] userClaim = claim.Where(u => u.Type == "ClubRank").ToArray(); if (userClaim != null && userClaim.Length > 0 && userClaim[0].Value.Length > 0) { userClubRole = userClaim[0].Value; } #endregion ViewBag.UserRank = userClubRole; ViewBag.ClubRoles = ClubRolesFactory.GetAll(); return(View(new EditMemberViewModel() { Id = user.Id })); }
public async Task <ActionResult> Edit(string id, EditMemberViewModel model) { if (!ModelState.IsValid) { return(View("Error")); } // TODO: add security (user with lower rank cannot change user with higher rank) string clubRankName = ClubRolesFactory.GetName(ClubRolesFactory.GetId(model.ClubRank)); // verify if rank from form is "real" ApplicationUser user = await _userManager.FindByIdAsync(id); #region GetUserClubRank IList <Claim> claim = await _userManager.GetClaimsAsync(user); Claim[] userClaim = claim.Where(u => u.Type == "ClubRank").ToArray(); if (userClaim != null && userClaim.Length > 0 && userClaim[0].Value.Length > 0) { await _userManager.ReplaceClaimAsync(user, userClaim[0], new Claim("ClubRank", clubRankName)); } else { await _userManager.AddClaimAsync(user, new Claim("ClubRank", clubRankName)); } #endregion return(RedirectToAction("Index")); }
public async Task <ActionResult> Delete(string id, IFormCollection collection) { if (id == null || id.Length <= 0) { return(View("Error")); } try { ApplicationUser user = await _userManager.FindByIdAsync(id); if (user == null) { return(View("Error")); } bool canDelete = false; // stores information, if there is any user able to add new users except the one, that will be delete (prevents situation, when there is no user able to add or invite a new one) foreach (ApplicationUser u in _userManager.Users) { if (u.Id == user.Id) { continue; } else if (await _userManager.IsInRoleAsync(user, "Administrator")) { canDelete = true; break; } var claim = await _userManager.GetClaimsAsync(u); if (claim.Where(c => c.Type == "ClubRank" && c.Value == ClubRolesFactory.GetName(ClubRolesFactory.Role.PRESIDENT)).Count <Claim>() > 0) { canDelete = true; break; } } if (!canDelete) { return(View("Error")); } await _userManager.DeleteAsync(user); return(RedirectToAction("Index")); } catch { return(View()); } }
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, MinimumClubRankRequirement requirement) { if (context.User.IsInRole("Administrator")) { context.Succeed(requirement); } if (!context.User.HasClaim(c => c.Type == "ClubRank")) { return(Task.CompletedTask); } try { string rankName = context.User.FindFirst(c => c.Type == "ClubRank").Value; if (requirement.GetMinimumRank() == ClubRolesFactory.GetId(rankName) || (int)requirement.GetMinimumRank() >= (int)ClubRolesFactory.GetId(rankName)) { context.Succeed(requirement); } } catch { } return(Task.CompletedTask); }
// // GET: /Home/Index public IActionResult Index() { IndexViewModel model = new IndexViewModel(); try { model.ClubName = "Studenckie Koło Naukowe NEO"; // -------------------------- USERS INFO ------------------------------ var users = _dbContext.Users.Include(u => u.Claims); model.MembersCount = users.Where(u => u.Claims.Any(c => c.ClaimType == "ClubRank")).Count(); if (users.Any(u => u.Claims.Any(c => c.ClaimValue == ClubRolesFactory.GetName(ClubRolesFactory.Role.PRESIDENT)))) { var president = users.Where(u => u.Claims.Any(c => c.ClaimValue == ClubRolesFactory.GetName(ClubRolesFactory.Role.PRESIDENT))).First(); model.President = president.FirstName + " " + president.LastName; } if (users.Any(u => u.Claims.Any(c => c.ClaimValue == ClubRolesFactory.GetName(ClubRolesFactory.Role.VICE_PRESIDENT)))) { var vicepresident = users.Where(u => u.Claims.Any(c => c.ClaimValue == ClubRolesFactory.GetName(ClubRolesFactory.Role.VICE_PRESIDENT))).First(); model.VicePresident = vicepresident.FirstName + " " + vicepresident.LastName; } if (users.Any(u => u.Claims.Any(c => c.ClaimValue == ClubRolesFactory.GetName(ClubRolesFactory.Role.SECRETARY)))) { var secretary = users.Where(u => u.Claims.Any(c => c.ClaimValue == ClubRolesFactory.GetName(ClubRolesFactory.Role.SECRETARY))).First(); model.Secretary = secretary.FirstName + " " + secretary.LastName; } if (users.Any(u => u.Claims.Any(c => c.ClaimValue == ClubRolesFactory.GetName(ClubRolesFactory.Role.TREASURER)))) { var treasurer = users.Where(u => u.Claims.Any(c => c.ClaimValue == ClubRolesFactory.GetName(ClubRolesFactory.Role.TREASURER))).First(); model.VicePresident = treasurer.FirstName + " " + treasurer.LastName; } // -------------------------- STATISTICS ------------------------------ var projects = _dbContext.Project; model.ProjectCount = projects.Count(); model.ProjectInProgressCount = projects.Where(p => p.EndDate <= DateTime.Now).Count(); model.EquipmentCount = _dbContext.Equipment.Count(); model.EquipmentSetsCount = _dbContext.EquipmentSet.Count(); //model.EquipmentLoansInProgressCount = 0; var upcomingDelegation = _dbContext.Delegation.OrderByDescending(d => d.StartDate).Where(d => d.StartDate >= (DateTime.Now.AddDays(-1))).First(); if (upcomingDelegation != null) { model.UpcomingEvent = upcomingDelegation.Name + " ("; if (DateTime.Compare(upcomingDelegation.StartDate, upcomingDelegation.EndDate) != 0) { model.UpcomingEvent += ("" + upcomingDelegation.StartDate.ToString("dd-MM-yyyy") + " - " + upcomingDelegation.EndDate.ToString("dd-MM-yyyy")); } else { model.UpcomingEvent += upcomingDelegation.StartDate.ToString("dd-MM-yyyy"); } model.UpcomingEvent += ")"; } } catch { } return(View(model)); }