public ActionResult Index() { List <UsersRolesViewModel> userList = new List <UsersRolesViewModel>(); //foreach (var user in db.Users.ToList()) foreach (var user in (helper.UsersInRole("BugtrackerUser"))) { var currentRoles = helper.ListUserRoles(user.Id); if (!currentRoles.Contains("Submitter")) { helper.AddUserToRole(user.Id, "Submitter"); db.SaveChanges(); } UsersRolesViewModel uservm = new UsersRolesViewModel(); uservm.User = user; uservm.Roles = helper.ListUserRoles(user.Id); uservm.Projectsb = pHelper.ListProjectsForAUser(user.Id); uservm.Tickets = new List <Ticket>(); foreach (var project in uservm.Projectsb) { foreach (var ticket in project.Tickets) { if (ticket.AssignedToUserId == user.Id) { uservm.Tickets.Add(ticket); } } } userList.Add(uservm); } return(View(userList)); }
public ActionResult Edit([Bind(Include = "Id,FirstName,LastName,DisplayName,AvatarPath,Email,SecurityStamp,PhoneNumber,PasswordHash,UserName")] ApplicationUser applicationUser, string roles) { if (User.Identity.GetUserId() == "db9a774b-807c-4b9b-9b22-34c191872996" || User.Identity.GetUserId() == "3eaa1491-7553-40fa-b7e1-b994e05d05e0" || User.Identity.GetUserId() == "5f84068f-4213-4d02-81a4-21936ae10cdc" || User.Identity.GetUserId() == "60f316c5-536c-4f06-83d3-38a555febc29") { return(RedirectToAction("InvalidAttempt", "Home")); } if (ModelState.IsValid) { //since i might be changing the users role, i want to first remove from all roles var currentRoles = roleHelper.ListUserRoles(applicationUser.Id); foreach (var role in currentRoles) { roleHelper.RemoveUserFromRole(applicationUser.Id, role); } if (!string.IsNullOrEmpty(roles)) { roleHelper.AddUserToRole(applicationUser.Id, roles); } applicationUser.UserName = applicationUser.Email; db.Entry(applicationUser).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(applicationUser)); }
public ActionResult UserIndex(string userId, string CurrentRole, List <int> CurrentProjects) { // Remove user from whatever role they currently occupy foreach (var role in roleHelper.ListUserRoles(userId)) { roleHelper.RemoveUserFromRole(userId, role); } if (!string.IsNullOrEmpty(CurrentRole)) { // Then add them back to the selected role roleHelper.AddUserToRole(userId, CurrentRole); } if (CurrentProjects != null) { // Remove user from whatever projects they currently occupy foreach (var project in projectHelper.ListUserProjects(userId)) { projectHelper.RemoveUserFromProject(userId, project.Id); } // Then add them back to the selected role foreach (var project in CurrentProjects) { projectHelper.AddUserToProject(userId, project); } } return(RedirectToAction("UserIndex")); }
//POST : Admin public ActionResult ManageRoles(List <string> userIds, string role) { if (userIds == null) { TempData["Failure"] = "Please select a user."; return(RedirectToAction("ManageRoles", "Admin")); } //step 1 : Unenroll all the selected users from any roles //the may currently occupy foreach (var userId in userIds) { //what is the role of this person var userRole = roleHelper.ListUserRoles(userId).FirstOrDefault(); if (userRole != null) { roleHelper.RemoveUserFromRole(userId, userRole); } } //Step 2 : Add them back to the selected roles if (!string.IsNullOrEmpty(role)) { foreach (var userId in userIds) { roleHelper.AddUserToRole(userId, role); } } //Redirect to dashboard return(RedirectToAction("ManageRoles", "Admin")); }
public ActionResult ManageUserRoles(string users, string roles) { foreach (var role in roleHelper.ListUserRoles(users)) { roleHelper.RemoveUserFromRole(users, role); } roleHelper.AddUserToRole(users, roles); return(RedirectToAction("UserIndex")); }
public ActionResult AssignUserRole(string UserId, string RoleName) { if (helper.AddUserToRole(UserId, RoleName)) { return(RedirectToAction("AssignUserRole", "Roles")); } else { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } }
public ActionResult ManageUserRole(string userId, string userRole) { foreach (var role in roleHelper.ListUserRoles(userId)) { roleHelper.RemoveUserFromRole(userId, role); } if (!string.IsNullOrEmpty(userRole)) { roleHelper.AddUserToRole(userId, userRole); } return(RedirectToAction("UserIndex")); }
public ActionResult ManageRoles(List <string> usersIds, string role) { //Uneneroll all the selected Users from ANY //roles they may current occupy foreach (var userId in usersIds) { var userRole = rolesHelper.ListUserRoles(userId).FirstOrDefault(); if (userRole != null) { rolesHelper.RemoveUserFromRole(userId, userRole); } } //Step 2:Add them back to the selected Role if (!string.IsNullOrEmpty(role)) { foreach (var userId in usersIds) { rolesHelper.AddUserToRole(userId, role); } } return(RedirectToAction("ManageRoles", "Admin")); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FName, LastName = model.LName, DisplayName = model.DName }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); UserRolesHelper helper = new UserRolesHelper(db); helper.AddUserToRole(user.Id, "Submitter"); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> AcceptInvitation(AcceptInvitationViewModel invitationvm) { if (ModelState.IsValid) { UserRolesHelper helper = new UserRolesHelper(); var user = new ApplicationUser { FirstName = invitationvm.FirstName, LastName = invitationvm.LastName, UserName = invitationvm.Email, Email = invitationvm.Email, HouseholdId = invitationvm.HouseholdId }; var result = await UserManager.CreateAsync(user, invitationvm.Password); if (result.Succeeded) { InvitationExtensions.MarkAsInvalid(invitationvm.Id); helper.RemoveUserFromRole(user.Id, "Guest"); helper.AddUserToRole(user.Id, "Household Member"); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToAction("Index", "Home")); } AddErrors(result); } return(View(invitationvm)); }
//[Authorize(Roles = "Admin")] public ActionResult ManageRoles(List <string> userIds, string role) { if (!userRolesHelper.IsDemoUser(User.Identity.GetUserId())) { // Unenroll selected users from any roles foreach (var userId in userIds) { var userRole = userRolesHelper.ListUserRoles(userId).FirstOrDefault(); if (userRole != null) { userRolesHelper.RemoveUserFromRole(userId, userRole); } } // Add user back to role if (!string.IsNullOrEmpty(role)) { foreach (var userId in userIds) { userRolesHelper.AddUserToRole(userId, role); } } } return(RedirectToAction("ManageRoles", "Admin")); }
public ActionResult ManageRoles(List <string> userIds, string roleName) { // Step 1: If anyone was selected, remove them from all of their roles if (userIds == null) { return(RedirectToAction("NoRoleSelected")); } else { foreach (var userId in userIds) { // Grab all user roles and assign to userRoles var userRoles = roleHelper.ListUserRoles(userId).ToList(); // Determine if user currently occupies a role foreach (var role in userRoles) { roleHelper.RemoveUserFromRole(userId, role); } // If a role was chosen, add each person to that role if (!string.IsNullOrEmpty(roleName)) { roleHelper.AddUserToRole(userId, roleName); } } } // Step 2: If a role was chosen, add each person to that role return(RedirectToAction("ManageRoles")); }
public ActionResult RoleAssignment(string UserId, string RoleId, bool Delete) { if (string.IsNullOrEmpty(UserId) || string.IsNullOrEmpty(RoleId)) { return(View()); } if (Delete) { foreach (var role in UserRolesHelper.ListUserRoles(UserId)) { UserRolesHelper.RemoveUserFromRole(UserId, role); } return(RedirectToAction("RoleAssignment")); } if (UserRolesHelper.IsUserInRole(UserId, RoleId)) { return(RedirectToAction("RoleAssignment")); } UserRolesHelper.AddUserToRole(UserId, RoleId); return(RedirectToAction("RoleAssignment")); }
public async Task <ActionResult> ManageRoles(List <string> userIds, string role) { if (userIds == null) { Session.Add("Message", "Please select one or more users to apply roles to."); return(RedirectToAction("ManageRoles", "Admin")); } if (User.IsInRole("Demo_Admin")) { Session.Add("Message", "For security purposes demo roles cannot save changes to the database."); return(RedirectToAction("ManageRoles", "Admin")); } else { foreach (var userId in userIds) { var userRole = RoleHelper.ListUserRoles(userId).FirstOrDefault(); if (userRole != null) { RoleHelper.RemoveUserFromRole(userId, userRole); } } if (!string.IsNullOrEmpty(role)) { foreach (var userId in userIds) { RoleHelper.AddUserToRole(userId, role); } } var user = db.Users.Find(User.Identity.GetUserId()); await HttpContext.RefreshAuthentication(user); return(RedirectToAction("ManageRoles", "Admin")); } }
public ActionResult ManageUserRole(string userId, string roles) { //This is where I will be using the UserRolesHelper class //The first thing I want to do is make sure I remove user from all roles they may occupy. foreach (var role in roleHelper.ListUserRoles(userId)) { roleHelper.RemoveUserFromRole(userId, role); } //If the incoming role selection Is not null i want to assign the user to selected role if (!string.IsNullOrEmpty(roles)) { roleHelper.AddUserToRole(userId, roles); } return(RedirectToAction("UserIndex")); }
public ActionResult Edit([Bind(Include = "Id,FirstName,LastName,DisplayName,AvatarPath,Email,UserName,PhoneNumber")] ApplicationUser applicationUser, string roles) { if (ModelState.IsValid) { //first get a list of roles the User occupies var currentRoles = roleHelper.ListUserRoles(applicationUser.Id); //Since I might be changing a User's role I want to first remove them from all Roles foreach (var role in currentRoles) { //remove the user from any they already occupy. roleHelper.RemoveUserFromRole(applicationUser.Id, role); } if (!string.IsNullOrEmpty(roles)) { roleHelper.AddUserToRole(applicationUser.Id, roles); } applicationUser.UserName = applicationUser.Email; db.Entry(applicationUser).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(applicationUser)); }
public ActionResult ManageUserRole(string userId, string userRole) { //This is where I will be using the UseRoleHelper class to make suer my user occupies //The first thing I want to do is to make sure I remove the user from ALL ROLES they may occupy foreach (var role in roleHelper.ListUserRoles(userId)) { roleHelper.RemoveUserFromRole(userId, role); } //If the incoming role selection IS NOT NULL I want to assign the user to the selected role if (!string.IsNullOrEmpty(userRole)) { roleHelper.AddUserToRole(userId, userRole); } return(RedirectToAction("UserIndex")); }
public ActionResult ManageUserRole(AssignUserRoleViewModel model) { //My Code & not Jason's //This is where I will be using the UserRolesHelper class to make sure my user occupies the proper role //The first thing I want to do is make sure I remove the user from ALL ROLES they may occupy foreach (var role in roleHelper.ListUserRoles(model.UserId)) { roleHelper.RemoveUserFromRole(model.UserId, role); } //If the incoming role selection is NOT NULL I want to assign the user to the selected role if (!string.IsNullOrEmpty(model.SelectedRole)) { roleHelper.AddUserToRole(model.UserId, model.SelectedRole); } return(RedirectToAction("UserIndex")); }
public async Task <ActionResult> Register(RegisterViewModel model, HttpPostedFileBase image, int hourConversion, int serverTime) { ViewBag.ServerHourRegister = System.DateTime.Now.Hour; var now = System.DateTime.Now.Hour; var diff = Math.Abs(now - serverTime); if (ModelState.IsValid) { var joinChapter = db.Chapters.First(c => c.CurrentChapter == true).Id; var pPic = "/assets/images/DefaultProfilePic.png"; if (ImageUploadValidator.IsWebFriendlyImage(image)) { //Counter var num = 0; //Gets Filename without the extension var fileName = Path.GetFileNameWithoutExtension(image.FileName); pPic = Path.Combine("/ProfilePics/", fileName + Path.GetExtension(image.FileName)); //Checks if pPic matches any of the current attachments, //if so it will loop and add a (number) to the end of the filename while (db.Users.Any(u => u.ProfilePic == pPic)) { //Sets "filename" back to the default value fileName = Path.GetFileNameWithoutExtension(image.FileName); //Add's parentheses after the name with a number ex. filename(4) fileName = string.Format(fileName + "(" + ++num + ")"); //Makes sure pPic gets updated with the new filename so it could check pPic = Path.Combine("/ProfilePics/", fileName + Path.GetExtension(image.FileName)); } image.SaveAs(Path.Combine(Server.MapPath("~/ProfilePics/"), fileName + Path.GetExtension(image.FileName))); } var user = new ApplicationUser { UserName = model.Email, Email = model.Email, PhoneNumber = model.PhoneNumber, ProfilePic = pPic, JoinChapterId = joinChapter, FirstName = model.FirstName, LastName = model.LastName, DisplayName = model.FirstName + ' ' + model.LastName, Company = model.Company, JobTitle = model.JobTitle, HourConversion = hourConversion - diff }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { var helper = new UserRolesHelper(db); helper.AddUserToRole(user.Id, "User"); db.SaveChanges(); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); //// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 //// Send an email with this link //string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public bool RemoveHouseholdMember(string userId, int householdId) { if (IsMemberOfHousehold(userId, householdId)) { UserRolesHelper.RemoveUserFromRole(userId, HouseholdRoleName.MEMBER); UserRolesHelper.AddUserToRole(userId, HouseholdRoleName.NONE); db.Households.Find(householdId).Members.Remove(db.Users.Find(userId)); db.SaveChanges(); } return(!IsMemberOfHousehold(userId, householdId)); }
public ActionResult ManageRoles(string userId, string roleName) { var user = db.Users.Find(userId); var currentRole = roleHelper.ListUserRoles(userId).FirstOrDefault(); roleHelper.RemoveUserFromRole(userId, currentRole); roleHelper.AddUserToRole(userId, roleName); TempData["roleMessage"] = user.FullName + "'s role has been updated to " + roleName + "."; return(RedirectToAction("ManageRoles")); }
public ActionResult ManageRoles(string userId, string roles) { //I want to ensure theat the person I selected occupies one and only 1 role //Therefore the first thing I am going to do is remove the user from any role //they currently occupy //Step 1: Remove any roles currently occupied by the user. We can do this by // looping over the roles currently occupied by the user using the roleHelper foreach (var role in roleHelper.ListUserRoles(userId)) { roleHelper.RemoveUserFromRole(userId, role); } //Step 2: Add the newly selected role to the user roleHelper.AddUserToRole(userId, roles); return(RedirectToAction("UserIndex", "Admin")); }
public async Task <ActionResult> Invite(RegisterViewModel model) { //if (ModelState.IsValid) //{ var _db = new ApplicationDbContext(); var helper = new UserRolesHelper(_db); var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, DisplayName = model.FirstName + " " + model.LastName }; if (model.PhoneNumber != null) { user.PhoneNumber = model.PhoneNumber; } IdentityResult result = new IdentityResult(); try { result = await UserManager.CreateAsync(user, model.Password); } catch (Exception ex) { Console.WriteLine(ex.Message); } if (result.Succeeded) { //await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); await UserManager.SendEmailAsync(user.Id, "An Invitation From Char-Em United Way", "Hello " + user.DisplayName + "! <br> You have been invited to join Char Em United Way's Service Guide. <br> Your account has been created just click <a href=\"" + callbackUrl + "\">here</a> to claim your account.<br> Your temporary password is:" + model.Password); helper.AddUserToRole(user.Id, model.SelectedRole); return(RedirectToAction("Index", "Home")); } AddErrors(result); // } // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult AddUserToRole(List <string> userIds, string roleName) { //go through each user selected and add to the selected role foreach (var userId in userIds) { //check if the user is currently in a role. if so, remove it and add new role. var currentRole = userRoleHelper.ListUserRoles(userId); if (currentRole.Count > 0) { userRoleHelper.RemoveUserFromRole(userId, currentRole.FirstOrDefault()); userRoleHelper.AddUserToRole(userId, roleName); } else { userRoleHelper.AddUserToRole(userId, roleName); } } return(RedirectToAction("ManageRoles")); }
public ActionResult EditUserRoles(/*[Bind(Include = "User, Roles, SelectedRoles, Id, Name")]*/ AdminUserViewModel AdminUserViewModel) { UserRolesHelper helper = new UserRolesHelper(db); string userId = AdminUserViewModel.Id; var editedUserAccount = db.Users.Find(userId); var currentUser = db.Users.Find(User.Identity.GetUserId()); if (User.IsInRole("DemoAcc")) { string errcode = User.Identity.Name + " Permission not granted, EditUserRoles, Edited User: "******"Err403", "BT", new { errcode = errcode })); } if (ModelState.IsValid) { var selectedRoleList = AdminUserViewModel.SelectedRoles; string[] currentRoleList = helper.ListUserRoles(userId).ToArray(); int counter = 0; //Counter for currentRoleList. //Spins through currentRoleList and removes roles that are nolonger selected foreach (var item in currentRoleList) { bool present = false; int counter2 = 0; //Counter for SelectedRolesList while (present == false && counter2 < selectedRoleList.Length) { if (currentRoleList[counter] == selectedRoleList[counter2]) { present = true; } counter2++; if (present == false && counter2 == selectedRoleList.Length) { helper.RemoveUserFromRole(userId, currentRoleList[counter]); } } counter++; } counter = 0; //Adds only roles that were selected. foreach (var item in selectedRoleList) { string toBeAdded = selectedRoleList[counter]; if (!helper.IsUserInRole(userId, toBeAdded)) { var result = helper.AddUserToRole(userId, toBeAdded); } counter += 1; } return(RedirectToAction("EditUserRoles", "Admin", userId)); } return(RedirectToAction("EditUserRoles", "Admin", userId)); }
public ActionResult Create([Bind(Include = "Id,Name,Greating,Created")] Households households) { if (ModelState.IsValid) { households.Created = DateTime.Now; db.Households.Add(households); //Add User to Household var userId = User.Identity.GetUserId(); var user = db.Users.Find(userId); user.HouseholdId = households.Id; //Add User Role roleHelper.AddUserToRole(userId, "HouseholdHead"); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(households)); }
public async Task <ActionResult> Create([Bind(Include = "Id,Name,Greeting")] Household household) { if (ModelState.IsValid) { var userId = User.Identity.GetUserId(); foreach (var userRole in rolesHelper.ListUserRoles(userId)) { rolesHelper.RemoveUserFromRole(userId, userRole); } rolesHelper.AddUserToRole(userId, "Household Owner"); household.Created = DateTime.Now; db.Households.Add(household); db.Users.Find(userId).HouseholdId = household.Id; db.SaveChanges(); await HttpContext.RefreshAuthentication(db.Users.Find(userId)); return(RedirectToAction("Index", "Home")); } return(View(household)); }
public ActionResult AssignRoles(RolesViewModel model) { if (ModelState.IsValid) { if (model.SelectedUsers != null) { foreach (string id in model.SelectedUsers) { helper.AddUserToRole(id, model.RoleName); } } return(RedirectToAction("Index", "RolesManager")); } return(View(model)); }
public bool AddHouseholdMember(string userId, int householdId, bool headOfHousehold = false) { if (!IsMemberOfHousehold(userId, householdId)) { UserRolesHelper.RemoveUserFromRole(userId, HouseholdRoleName.NONE); UserRolesHelper.AddUserToRole(userId, HouseholdRoleName.MEMBER); if (headOfHousehold) { UserRolesHelper.AddUserToRole(userId, HouseholdRoleName.HEAD_OF_HOUSEHOLD); } db.Households.Find(householdId).Members.Add(db.Users.Find(userId)); db.SaveChanges(); } return(IsMemberOfHousehold(userId, householdId)); }
public ActionResult AssignRoles(AssignRolesViewModel model) { if (ModelState.IsValid) { if (model.SelectedUsers != null) { foreach (string userId in model.SelectedUsers) { helper.AddUserToRole(userId, model.RoleName); } } return(RedirectToAction("RoleAssignations", "Roles", new { id = model.RoleId })); } return(View(model)); }
public ActionResult EditUser(string id, AdminUserViewModel model) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var user = db.Users.Find(id); UserRolesHelper helper = new UserRolesHelper(); foreach (var role in db.Roles.ToList()) { //If selected but user doesn't have, add if (model.SelectedRoles.Contains(role.Id)) { helper.AddUserToRole(id, role.Name); } //If not selected but user *does* have, remove else if (!model.SelectedRoles.Contains(role.Id)) { helper.RemoveUserFromRole(id, role.Name); } } return RedirectToAction("Index", new { id }); }
public ActionResult ManageRoles(string role, string[] assignedUsers) { var helper = new UserRolesHelper();//a new 'UserRolesHelper' object is instantiated and named 'helper'. It has all of the methods //defined in the 'UserRolesHelper' class. var dbRole = db.Roles.FirstOrDefault(r => r.Name == role);//go to the 'Roles' table in the database and find the first role where //it's 'Name' property has the same value as the 'role' parameter that was passed to this action method and assign that role //(it's 'Name' and 'Id' properties and their values) to the variable called 'dbRole'. if (dbRole == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); dbRole.Users.Clear();//remove all of the users from the 'Users' property of 'dbRole' db.SaveChanges();//save any changes made to the database. foreach (var user in assignedUsers ?? new string[0])//loop over the string array 'assignedUser', which was passed as a parameter //to this acton method....LOOK INTO THE SECOND PART OF THIS LOOP STATEMENT...NOT REALLY SURE WHAT THAT IS ABOUT... helper.AddUserToRole(user, role);//call the 'AddUserToRole' method from the 'helper' object and pass it the current user from //the 'assignedUsers' array and the specified 'role'. return RedirectToAction("ManageRoles", new { role });//redirect to the 'ManageRoles' action method with a new object of the //'role' that was passed to this action method. }