public async Task <IActionResult> AddBug(AddNewBug newbug) { if (ModelState.IsValid) { var currentUserId = userManager.GetUserId(HttpContext.User); var currentUser = await userManager.FindByIdAsync(currentUserId); var claims = await userManager.GetClaimsAsync(currentUser); GlobalVar.globalCurrentUserClaims = claims.ToList(); var UserIsMangerLevel = UserClaimsLevel.IsManager(claims.ToList(), newbug.NewBugAttributes.AssociatedProject); if (UserIsMangerLevel && newbug.NewBugAttributes.AssigneeUserId != null) { var assignedUser = await userManager.FindByIdAsync(newbug.NewBugAttributes.AssigneeUserId); newbug.NewBugAttributes.AssingeeUserName = assignedUser.UserName; } newbug.NewBugAttributes.ReporterID = userManager.GetUserId(HttpContext.User); newbug.NewBugAttributes.ReporterUserName = userManager.GetUserName(HttpContext.User); newbug.NewBugAttributes.EnteredDate = DateTime.Now; var bug = _bugRepository.Add(newbug.NewBugAttributes); if (bug.Title == null) { bug.Title = $"bug{bug.BugId}"; bug = _bugRepository.Update(bug); } var projectBug = new ProjectBugs { BugId = bug.BugId, ProjectId = bug.AssociatedProject }; _projectRepository.AddProjectBugs(projectBug); List <ScreenShots> uniqueFileNames = new List <ScreenShots>(); if (GlobalVar.InitialScreenShots == true) { uniqueFileNames = await UploadScreenShotsToStorage(bug.BugId); } GlobalVar.InitialScreenShots = false; _bugRepository.AddScreenShots(uniqueFileNames); return(RedirectToAction("BugDetails", new { bugId = bug.BugId })); } return(View()); }
public async Task <IActionResult> BugDetails(BugDetailsAndProjectNameAndId updatedBug) { var currentUserId = userManager.GetUserId(HttpContext.User); var currentUser = await userManager.FindByIdAsync(currentUserId); var claims = await userManager.GetClaimsAsync(currentUser); GlobalVar.globalCurrentUserClaims = claims.ToList(); var UserIsMangerLevel = UserClaimsLevel.IsManager(claims.ToList(), updatedBug.Bug.AssociatedProject); if (UserIsMangerLevel && updatedBug.Bug.AssigneeUserId != null) { var assignedUser = await userManager.FindByIdAsync(updatedBug.Bug.AssigneeUserId); updatedBug.Bug.AssingeeUserName = assignedUser.UserName; } List <ScreenShots> uniqueFileNames = new List <ScreenShots>(); if (GlobalVar.InitialScreenShots == true) { uniqueFileNames = await UploadScreenShotsToStorage(updatedBug.Bug.BugId); } GlobalVar.InitialScreenShots = false; var originalBug = _bugRepository.GetBug(updatedBug.Bug.BugId); if (updatedBug.Bug.Title == null) { updatedBug.Bug.Title = originalBug.Title; } var UserIsDeveloperLevel = UserClaimsLevel.IsDeveloper(claims.ToList(), updatedBug.Bug.AssociatedProject); if (UserIsDeveloperLevel) { foreach (var property in originalBug.GetType().GetProperties()) { if (property.Name == "AssigneeUserId") { continue; } var oldValue = ""; var newValue = ""; if (property.GetValue(updatedBug.Bug) != null) { newValue = property.GetValue(updatedBug.Bug).ToString(); } if (property.GetValue(originalBug) != null) { oldValue = property.GetValue(originalBug).ToString(); } if (oldValue != newValue) { var changes = new BugHistory { AssociatedBugId = originalBug.BugId, Property = property.Name, OldValue = oldValue, NewValue = newValue, DateChanged = DateTime.Now }; _bugRepository.AddHistoryEntry(changes); } } } var bug = new BugAttributes(); if (UserIsDeveloperLevel) { updatedBug.Bug.ScreenShots = uniqueFileNames; updatedBug.Bug.ScreenShots.AddRange(_bugRepository.ScreenShots(updatedBug.Bug.BugId)); bug = _bugRepository.Update(updatedBug.Bug); } else { //bug = _bugRepository.GetBug(updatedBug.Bug.BugId); bug = originalBug; bug.ScreenShots = uniqueFileNames; bug.ScreenShots.AddRange(_bugRepository.ScreenShots(updatedBug.Bug.BugId)); } var project = _projectRepository.GetProject(bug.AssociatedProject); var projectName = project.ProjectName; bug.Comments = _bugRepository.Comments(bug.BugId); var bugHistory = _bugRepository.GetBugHistories(bug.BugId); var users = new List <IdentityUser>(); var projectUsers = new List <string>(); projectUsers.Add(project.OwnerId); if (project.UsersAssigned != null) { projectUsers.AddRange(project.UsersAssigned.Split(" ").ToList()); } foreach (var userId in projectUsers) { var user = await userManager.FindByIdAsync(userId); if (user != null && !users.Contains(user)) { users.Add(user); } } var viewModel = new BugDetailsAndProjectNameAndId() { Bug = bug, ProjectName = projectName, ProjectId = bug.AssociatedProject, Updated = 1, //Src = new List<ScreenShots>(), Src = bug.ScreenShots, bugHistories = bugHistory, ProjectUsers = users }; return(View(viewModel)); }
//[Authorize(Policy = "ManagerPolicy")] public async Task <IActionResult> ManageUserClaims(string userId, int projectId) { GlobalVar.ProjectId = projectId; var project = projectRepository.GetProject(GlobalVar.ProjectId); GlobalVar.Project = project; var currentUserId = userManager.GetUserId(HttpContext.User); var currentUser = await userManager.FindByIdAsync(currentUserId); var currentUserClaims = await userManager.GetClaimsAsync(currentUser); GlobalVar.globalCurrentUserClaims = currentUserClaims.ToList(); var user = await userManager.FindByIdAsync(userId); var claims = await userManager.GetClaimsAsync(user); //var UserIsMangerLevel = UserClaimsLevel.IsManager(HttpContext.User.Claims.ToList(), projectId); var UserIsMangerLevel = UserClaimsLevel.IsManager(claims.ToList(), projectId); if (UserIsMangerLevel == false) { return(RedirectToAction("AccessDenied", "Account")); } else if (userId == userManager.GetUserId(HttpContext.User) || userId == project.OwnerId) { return(RedirectToAction("AccessDenied", "Account")); } if (user == null) { ViewBag.ErrorMessage = $"User with Id = {userId} cannot be found"; return(View("NotFound")); } // UserManager service GetClaimsAsync method gets all the current claims of the user var existingUserClaims = await userManager.GetClaimsAsync(user); var model = new UserClaimsViewModel { ProjectId = projectId, UserId = userId }; var projectIdString = projectId.ToString(); for (var i = 0; i < ClaimsStore.AllClaims.Count; i++) { UserClaim userClaim = new UserClaim { ClaimType = ClaimsStore.AllClaims[i].Type }; var projectList = new List <string>(); if (existingUserClaims.Count == 4) { projectList = existingUserClaims[i].Value.Split(" ").ToList(); } for (int j = 0; j < projectList.Count; j++) { if (projectList[j] == projectIdString) { userClaim.IsSelected = true; break; } } model.Claims.Add(userClaim); } return(View(model)); }
//[Authorize(Policy = "ManagerPolicy")] public async Task <IActionResult> EditUser(string id, int projectId) { GlobalVar.ProjectId = projectId; var project = projectRepository.GetProject(GlobalVar.ProjectId); GlobalVar.Project = project; var currentUserId = userManager.GetUserId(HttpContext.User); var currentUser = await userManager.FindByIdAsync(currentUserId); var claims = await userManager.GetClaimsAsync(currentUser); GlobalVar.globalCurrentUserClaims = claims.ToList(); var UserIsMangerLevel = UserClaimsLevel.IsManager(claims.ToList(), projectId); if (UserIsMangerLevel == false) { return(RedirectToAction("AccessDenied", "Account")); } var user = await userManager.FindByIdAsync(id); if (user == null) { ViewBag.ErrorMessage = $"User with Id = {id} cannot be found"; return(View("NotFound")); } var model = new EditUserViewModel { Id = user.Id, Email = user.Email, UserName = user.UserName, //Claims = projectUserClaims.Select(c => c.Type + " : ").ToList(), ProjectId = projectId }; // GetClaimsAsync returns the list of user Claims var allUserClaims = await userManager.GetClaimsAsync(user); //var projectUserClaims = new List<Claim>(); var projectIdString = projectId.ToString(); foreach (var claim in allUserClaims) { var projectList = claim.Value.Split(" "); var claimString = ""; for (int i = 0; i < projectList.Length; i++) { if (projectList[i] == projectIdString) { claimString = claim.Type + " : true"; model.Claims.Add(claimString); break; } } if (claimString == "") { model.Claims.Add(claim.Type + " : false"); } } // GetRolesAsync returns the list of user Roles //I'm not using roles //var userRoles = await userManager.GetRolesAsync(user); return(View(model)); }