public ActionResult UserIndex(string userId, string CurrentRole, List <int> CurrentProjects) { foreach (var role in roleHelper.ListUserRoles(userId)) { roleHelper.RemoveUserFromRole(userId, role); } if (!string.IsNullOrEmpty(CurrentRole)) { roleHelper.AddUserToRole(userId, CurrentRole); } foreach (var proj in projectHelper.ListUserProjects(userId)) { projectHelper.RemoveUserFromProject(userId, proj.Id); } if (CurrentProjects != null) { foreach (var projectId in CurrentProjects) { projectHelper.AddUserToProject(userId, projectId); } } ////return RedirectToAction("EditProfile","Members", new { userId = userId }); return(RedirectToAction("Manage", "Members", new { userId = userId })); }
public ActionResult ManageUserRoles(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 ManageUserRole(string userId, string userRole) { 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 AdminAddToRole(ListViewModel selectedUser, string RoleName) { var nHelper = new UserNotificationsHelper(); ViewBag.Notifications = nHelper.filterNotifications(User.Identity.GetUserId()); var Helper = new UserRolesHelper(); foreach (var id in selectedUser.SelectednonUsers) { if (id != null) { Helper.AddUserToRole(id, RoleName); Helper.AddUserToRole(id, "Project Manager"); Helper.AddUserToRole(id, "Developer"); } } List <SelectListItem> listadminUsers = new List <SelectListItem>(); List <SelectListItem> listnonadminUsers = new List <SelectListItem>(); var result1 = Helper.UsersInRole(RoleName); foreach (var user in result1) { SelectListItem selectList = new SelectListItem() { Text = user.DispalyName, Value = user.Id }; listadminUsers.Add(selectList); } var result2 = Helper.UsersNotInRole(RoleName); foreach (var user in result2) { SelectListItem selectList = new SelectListItem() { Text = user.DispalyName, Value = user.Id }; listnonadminUsers.Add(selectList); } ListViewModel adminLViewModel = new ListViewModel() { zUsers = listadminUsers, otherUsers = listnonadminUsers }; return(View("AdminAccount", adminLViewModel)); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //when a register is registered, automatically assign them the role "Submitter" so they can view a submitted ticket var helper = new UserRolesHelper(); helper.AddUserToRole(user.Id, "Submitter"); 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 ActionResult ManageRoles(string role, string[] assignedUsers) //postback method will receive the role name given to the multiselect //list in the GET action method and it will receive a string array of the users that were selected in the multiselect list. { 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. }
public ActionResult Details([Bind(Include = "SelectedRoles,RolesList")] UserRolesViewModel model) { if (ModelState.IsValid) { var user = (ApplicationUser)TempData["CurrentUser"]; var userRoles = helper.ListUserRoles(user.Id); //DELETE current Roles) foreach (var roleName in userRoles) { helper.RemoveUserFromRole(user.Id, roleName); } //ADD the whole list of new Roles) if (model.SelectedRoles != null) { foreach (var roleName in model.SelectedRoles) { helper.AddUserToRole(user.Id, roleName); } } db.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult ManageUserRole(string userId, string UserRole) { //user UserRolesHelper class //remove user from all roles foreach (var role in roleHelper.ListUserRoles(userId)) { roleHelper.RemoveUserFromRole(userId, role); } //if incoming role is not null, assign to role if (!string.IsNullOrEmpty(UserRole)) { roleHelper.AddUserToRole(userId, UserRole); } return(RedirectToAction("UserIndex")); }
public ActionResult Edit([Bind(Include = "UserId,RolesToSelect")] AdminUserViewModel admModel) { var user = db.Users.Find(admModel.UserId); var id = admModel.UserId; var allRoles = new List <string>(); UserRolesHelper helper = new UserRolesHelper(); allRoles.Add("Submitter"); allRoles.Add("Developer"); allRoles.Add("Project Manager"); allRoles.Add("Admin"); //build a list of selected roles based on the array in the model that was returned var selectedRoles = new List <string>(); for (int i = 0; i < admModel.RolesToSelect.Length; i++) { if (admModel.RolesToSelect[i].Checked == true) { selectedRoles.Add(admModel.RolesToSelect[i].RoleName); } } //if no roles have been selected, remove user from all roles if (selectedRoles == null) { foreach (var rRole in allRoles) { if (helper.IsUserInRole(admModel.UserId, rRole)) { helper.RemoveUserFromRole(admModel.UserId, rRole); } } return(RedirectToAction("Index")); } else { foreach (var sRole in selectedRoles) { if (!helper.IsUserInRole(admModel.UserId, sRole)) { helper.AddUserToRole(admModel.UserId, sRole); } } var rolesToRemove = allRoles.Except(selectedRoles); foreach (var rRole in rolesToRemove) { if (helper.IsUserInRole(admModel.UserId, rRole)) { helper.RemoveUserFromRole(admModel.UserId, rRole); } } return(RedirectToAction("Index")); } }
public ActionResult ManageRoles(List <string> users, string roleName) { if (users != null) { //Lets iterate over the incoming list of users that were selected from the form // and remove each of them from whatever role they occupy foreach (var userId in users) { //Remove them from any role they occupy foreach (var role in roleHelper.ListUserRoles(userId)) { roleHelper.RemoveUserFromRole(userId, role); } if (!string.IsNullOrEmpty(roleName)) { roleHelper.AddUserToRole(userId, roleName); //call the 'AddUserToRole' method from the 'helper' object and pass it the userid //and the specified 'role'. } } } return(RedirectToAction("ManageRoles")); }
public ActionResult Details([Bind(Include = "SelectedProjects,ProjectsList,SelectedRoles,RolesList")] UserAssignmentsViewModel model) // Work on Both Assignments { if (ModelState.IsValid) { var user = (ApplicationUser)TempData["CurrentUser"]; // Work on Project Assignments var userProjects = projecthelper.ListProjectsForUser(user.Id); //DELETE current Projects) foreach (var project in userProjects) { projecthelper.RemoveUserFromProject(user.Id, project.Id); } //ADD the whole list of new Projects) if (model.SelectedProjects != null) { foreach (var projectid in model.SelectedProjects) { int projectId = Convert.ToInt32(projectid); projecthelper.AddUserToProject(user.Id, projectId); } } // Work on Role Assignments // Only Admin Role can edit Role Assignments if (User.IsInRole("Admin")) { var userRoles = rolehelper.ListUserRoles(user.Id); //DELETE current Roles) foreach (var roleName in userRoles) { rolehelper.RemoveUserFromRole(user.Id, roleName); } //ADD the whole list of new Roles) if (model.SelectedRoles != null) { foreach (var roleName in model.SelectedRoles) { rolehelper.AddUserToRole(user.Id, roleName); } } } db.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "Id,FirstName, LastName, Email, DisplayName,AvatarPath,EmailConfirmed,PhoneNumber,PhoneNumberConfirmed")] ApplicationUser applicationUser, string roles) { if (ModelState.IsValid) { //1st get list of roles user occupies var currentRoles = roleHelper.ListUserRoles(applicationUser.Id); foreach (var role in currentRoles) { //Removes users from all and any role they occupy roleHelper.RemoveUserFromRole(applicationUser.Id, role); } if (!string.IsNullOrEmpty(roles)) { roleHelper.AddUserToRole(applicationUser.Id, roles); } //db.Entry(applicationUser).State = EntityState.Modified; db.Users.Attach(applicationUser); applicationUser.UserName = applicationUser.Email; // times 7 for the properties that are subject to change, this allows for only for the ones to be changed instead of losing the information db.Entry(applicationUser).Property(x => x.FirstName).IsModified = true; db.Entry(applicationUser).Property(x => x.LastName).IsModified = true; db.Entry(applicationUser).Property(x => x.AvatarPath).IsModified = true; db.Entry(applicationUser).Property(x => x.Email).IsModified = true; db.Entry(applicationUser).Property(x => x.PhoneNumber).IsModified = true; db.Entry(applicationUser).Property(x => x.UserName).IsModified = true; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(applicationUser)); }
public ActionResult Edit([Bind(Include = "Id,FirstName,LastName,DisplayName,Email")] ApplicationUser applicationUser, string Roles, List <int> Projects) { if (ModelState.IsValid) { applicationUser.UserName = applicationUser.Email; //Assign the user to the selected role //Determine if the user currently occupies a role and if so remove them from it if (User.IsInRole("Admin")) { foreach (var role in rolesHelper.ListUserRoles(applicationUser.Id)) { rolesHelper.RemoveUserFromRole(applicationUser.Id, role); } //Add them to the selected role if (Roles != null) { rolesHelper.AddUserToRole(applicationUser.Id, Roles); } } //Remove user from all projects foreach (var project in projHelper.ListUserProjects(applicationUser.Id)) { projHelper.RemoveUserFromProject(applicationUser.Id, project.Id); } //Add them to the selected projects if (Projects != null) { foreach (var projectId in Projects) { projHelper.AddUserToProject(applicationUser.Id, projectId); } } //Because we have removed several fields from the User Edit Form, we have to tell the SQL that only a few specific properties are eligible for modification //db.Entry(applicationUser).State = EntityState.Modified; <-- This tells SQL that ALL properties are eligible for modification db.Users.Attach(applicationUser); db.Entry(applicationUser).Property(x => x.FirstName).IsModified = true; db.Entry(applicationUser).Property(x => x.LastName).IsModified = true; db.Entry(applicationUser).Property(x => x.DisplayName).IsModified = true; db.Entry(applicationUser).Property(x => x.Email).IsModified = true; db.Entry(applicationUser).Property(x => x.UserName).IsModified = true; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(applicationUser)); }
public ActionResult ManageRoles(AdminRoleModel model) { var y = db.Users.Find(model.Id); foreach (var item in db.Roles.Select(r => r.Name).ToList()) { roleHelper.RemoveUserFromRole(y.Id, item); } foreach (var item in model.selectedRoles) { roleHelper.AddUserToRole(y.Id, item); } return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "Id,FirstName,LastName,Email,PhoneNumber,Picture,DisplayName")] ApplicationUser applicationUser, HttpPostedFileBase image, string Roles, List <int> Projects) { if (ModelState.IsValid) { if (ImageUploadValidator.IsWebFriendlyImage(image)) { var fileName = Path.GetFileName(image.FileName); image.SaveAs(Path.Combine(Server.MapPath("~/Images/"), fileName)); applicationUser.Picture = "/Images/" + fileName; } applicationUser.UserName = applicationUser.Email; //Assign the user to the selected role //Determine if the user currently occupies a role, and if so remove them form it foreach (var role in rolesHelper.ListUserRoles(applicationUser.Id)) { rolesHelper.RemoveUserFromRole(applicationUser.Id, role); } if (Roles != null) { rolesHelper.AddUserToRole(applicationUser.Id, Roles); } //Add user to the new selected role foreach (var proj in projectHelper.ListUserProjects(applicationUser.Id)) { projectHelper.RemoveUserFromProject(applicationUser.Id, proj.Id); } if (Projects != null) { foreach (var projectId in Projects) { projectHelper.AddUserToProject(applicationUser.Id, projectId); } } //db.Entry(applicationUser).State = EntityState.Unchanged; db.Users.Attach(applicationUser); db.Entry(applicationUser).Property(p => p.FirstName).IsModified = true; db.Entry(applicationUser).Property(p => p.LastName).IsModified = true; db.Entry(applicationUser).Property(p => p.Email).IsModified = true; db.Entry(applicationUser).Property(p => p.PhoneNumber).IsModified = true; db.Entry(applicationUser).Property(p => p.DisplayName).IsModified = true; db.Entry(applicationUser).Property(p => p.UserName).IsModified = true; db.Entry(applicationUser).Property(p => p.Picture).IsModified = true; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(applicationUser)); }
public ActionResult RolesAssigned(string userId, List <string> Roles) { var roleHelp = new UserRolesHelper(); foreach (var role in db.Roles) { var name = role.Name; roleHelp.RemoveUserFromRole(userId, name); } foreach (var role in Roles) { var list = roleHelp.AddUserToRole(userId, role); } return(RedirectToAction("ListRoles", "Admin")); }
public ActionResult ManageRoles(UserRolesViewModel model) { var user = db.Users.Find(model.Id); foreach (var rolemv in db.Roles.Select(r => r.Name).ToList()) { helper.RemoveUserFromRole(user.Id, rolemv); } foreach (var rolemv in model.SelectedRoles) { helper.AddUserToRole(user.Id, rolemv); } ViewBag.confirm = "User's role has been modified"; return(RedirectToAction("Index")); }
public ActionResult Edit(AdminViewRolesModel model) { var user = db.Users.Find(model.User.Id); UserRolesHelper helper = new UserRolesHelper(); foreach (var rolermv in db.Roles.Select(r => r.Name).ToList()) { helper.RemoveUserFromRole(user.Id, rolermv); } foreach (var roleadd in model.SelectedRoles) { helper.AddUserToRole(user.Id, roleadd); } return(RedirectToAction("Index")); }
public async Task <ActionResult> Register(RegisterViewModel model) { //Create DisplayName StringBuilder dn = new StringBuilder(); dn.Append(model.FirstName + " " + model.LastName); model.DisplayName = dn.ToString(); if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, DisplayName = model.DisplayName }; 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>"); //Add new use to Registered User Role to limit access UserRolesHelper helper = new UserRolesHelper(db); var resultadd = helper.AddUserToRole(user.Id, "Registered User"); return(RedirectToAction("RegisterSucess", "Account")); } AddErrors(result); } LoginViewModel LoginViewModel = new LoginViewModel(); ForgotPasswordViewModel ForgotPasswordViewModel = new ForgotPasswordViewModel(); RegisterViewModel RegisterViewModel = new RegisterViewModel(); LoginGroupModel LoginGroupModel = new LoginGroupModel(); LoginGroupModel.LoginViewModel = LoginViewModel; LoginGroupModel.ForgotPasswordViewModel = ForgotPasswordViewModel; LoginGroupModel.RegisterViewModel = model; ViewBag.ErrorMsg = "Invalid Register Attempt, Please try again."; // If we got this far, something failed, redisplay form return(RedirectToAction("Login", "Account", LoginGroupModel)); }
public ActionResult AddRole(string AddId, List <string> SelectedAbsentRoles) { if (ModelState.IsValid) { UserRolesHelper helper = new UserRolesHelper(db); var user = db.Users.Find(AddId); foreach (var role in SelectedAbsentRoles) { helper.AddUserToRole(AddId, role); } db.Entry(user).State = EntityState.Modified; db.Users.Attach(user); db.SaveChanges(); return(RedirectToAction("ListUsers")); } return(View(AddId)); }
public ActionResult AssignRole(string users, string roles) { if (ModelState.IsValid) { var currentUser = db.Users.Find(users).Id; var currentRoles = roleHelper.ListUserRoles(users); foreach (var role in currentRoles) { //Removes users from all and any role they occupy roleHelper.RemoveUserFromRole(currentUser, role); } if (!string.IsNullOrEmpty(roles)) { roleHelper.AddUserToRole(currentUser, roles); } } return(RedirectToAction("Index", "Users")); }
public ActionResult AssignRoles(string id, List <string> userRoles) { //Unassign the user from all roles var user = db.Users.Find(id); if (user.Protected == true) { ViewBag.AdminError = "Can't change protected user."; return(View(user)); } else { foreach (var role in userHelper.ListUserRoles(id)) { if (role != "Admin") { userHelper.RemoveUserFromRole(id, role); } else if ((role == "Admin" && userHelper.UsersInRole("Admin").Count > 1) || (userRoles != null && userRoles.Contains("Admin"))) { userHelper.RemoveUserFromRole(id, role); } else { ViewBag.AdminError = "Can't remove Admin without adding another first."; return(View(user)); } } if (userRoles != null) { //Assign user to selected roles foreach (var role in userRoles) { userHelper.AddUserToRole(id, role); } } } return(RedirectToAction("Index")); }
public ActionResult EditUserRoles(AdminUserViewModels model) { var user = db.Users.Find(model.User.Id); UserRolesHelper helper = new UserRolesHelper(); foreach (var role in db.Roles.Select(r => r.Name).ToList()) { helper.RemoveUserFromRole(user.Id, role); } if (model.AssignedRoles != null) { foreach (var role in model.AssignedRoles) { helper.AddUserToRole(user.Id, role); } return(RedirectToAction("Index")); } return(RedirectToAction("Index")); }
public ActionResult EditUser([Bind(Include = "User,Roles,SelectedRoles")] AdminUserViewModel model) { var user = db.Users.Find(model.User.Id); UserRolesHelper helper = new UserRolesHelper(); foreach (var role in db.Roles.Select(r => r.Name).ToList()) { if (helper.IsUserInRole(user.Id, role)) { helper.RemoveUserFromRole(user.Id, role); } } foreach (var roleadd in model.SelectedRoles) { if (!helper.IsUserInRole(user.Id, roleadd)) { helper.AddUserToRole(user.Id, roleadd); } } return(RedirectToAction("AdminIndex")); }
public ActionResult EditUser(AdminUserViewModel model, string id) { UserRolesHelper helper = new UserRolesHelper(db); foreach (var role in model.SelectedRoles) { if (!helper.IsUserInRole(model.Id, role)) { helper.AddUserToRole(model.Id, role); } } foreach (var role in db.Roles.ToList()) { if (!model.SelectedRoles.Contains(role.Name)) { helper.RemoveUserFromRole(model.Id, role.Name); } } db.SaveChanges(); return(RedirectToAction("UserIndex", "Admin", model)); }
public ActionResult ManageRoles(List <string> usersIds, string role) { if (usersIds != null) { foreach (var userId in usersIds) { var userRole = roleHelper.ListUserRoles(userId).FirstOrDefault(); if (userRole != null) { roleHelper.RemoveUserFromRole(userId, userRole); } } if (!string.IsNullOrEmpty(role)) { foreach (var userId in usersIds) { roleHelper.AddUserToRole(userId, role); } } } return(RedirectToAction("ManageRoles", "Admin")); }
public ActionResult ManageRoles(List <string> userIds, string roleName) { //Useing roleHElper to assign their role if (userIds != null) { //remove role from users foreach (var userId in userIds) { var userRole = rolesHelper.ListUserRoles(userId).FirstOrDefault(); if (!string.IsNullOrEmpty(userRole)) { rolesHelper.RemoveUserFromRole(userId, userRole); } //add new role to user if (!string.IsNullOrEmpty(roleName)) { rolesHelper.AddUserToRole(userId, roleName); } } } return(RedirectToAction("ManageRoles")); }