public static MvcHtmlString ShowMessage(Alert alert) { if (alert != null) { string type = alert.Class ?? "Info"; string title = String.IsNullOrEmpty(alert.Title) ? "" : string.Format("<div class=\"Title\">{0}</div>", CultureInfo.CurrentCulture.TextInfo.ToTitleCase(alert.Title)); string message = alert.Description; string htmlMessage = string.Format("<div class=\"Alert {0}\">{1}<div class=\"Body\">{2}</div></div>", type, title, message); return MvcHtmlString.Create(htmlMessage); } return MvcHtmlString.Create(""); }
public ActionResult Create(string roleName) { // Creates a new Role Set with the provided roleName // And assign a set of default rules taken from the // "Users Role Set" RoleSet userRoleSetTemplate = repository.RoleSets.FirstOrDefault(r => r.RoleSetId == 3); // Users RoleSet RoleSet roleSet = new RoleSet(); roleSet.Name = roleName; roleSet.Roles = new List<RoleSetting>(); foreach (RoleSetting roleSetting in userRoleSetTemplate.Roles) { roleSet.Roles.Add(new RoleSetting() { Role = roleSetting.Role, IsActive = roleSetting.IsActive }); } try { repository.SaveRole(roleSet); TempData["message"] = new Alert() { Class = "Success", Title = "Role added.", Description = roleName + " has been added" }; return RedirectToAction("Settings", new { id = roleSet.RoleSetId }); } catch (Exception e) { TempData["message"] = new Alert(e); return RedirectToAction("Index"); } }
public ActionResult Settings(RoleSettingsViewModel model) { if (ModelState.IsValid) { try { RoleSet roleSet = repository.RoleSets.FirstOrDefault(r => r.RoleSetId == model.RoleSetId); RoleSetting[] roles = roleSet.Roles.ToArray(); RoleSetting[] roleSettings = model.Roles.ToArray(); for (int i = 0; i < roleSet.Roles.Count(); i++) { roles[i].IsActive = roleSettings[i].IsActive; } roleSet.Roles = roles.ToList(); repository.SaveRole(roleSet); TempData["message"] = new Alert() { Class = "Success", Title = "Role Settings Updated.", Description = "Role settings for " + model.RoleSetName + " has been updated." }; return RedirectToAction("Index"); } catch (Exception e) { TempData["message"] = new Alert(e); return RedirectToAction("Index"); } } else { return View(); } }
public ActionResult Create(MemberViewModel model) { if (ModelState.IsValid) { Member member; if (UserExists(model.UserName, model.Email)) { ModelState.AddModelError("UserAlreadyExist", "Username or Email already in use."); ShowUserAlreadyExistAlert(); return View(); } // Check if passwords matches if (model.Password == model.PasswordConfirmation) { string salt = PasswordHelper.CreateSalt(); string hashedPassword = PasswordHelper.CreatePasswordHash(model.Password, salt); member = new Member() { MemberId = 0, MemberGuid = Guid.NewGuid(), UserName = model.UserName, Email = model.Email, Profile = new Profile() { FirstName = model.FirstName, LastName = model.LastName, Description = model.Description, Phone = model.Phone, Address = model.Address, Building = model.Building, Floor = model.Floor, Office = model.Office }, Password = new Password() { Salt = salt, Hash = hashedPassword } }; } else { ModelState.AddModelError("PasswordConfirmation", "Confirmation Password doesn't match your Password."); return View(); } try { repository.SaveMember(member); TempData["message"] = new Alert() { Class = "Success", Title = "New member created.", Description = string.Format("{0} {1}", model.LastName.ToUpper(), model.FirstName) }; } catch (Exception e) { TempData["message"] = new Alert(e); } return RedirectToAction("Index"); } else { return View("Create", model); } }
private void ShowUserAlreadyExistAlert() { TempData["message"] = new Alert() { Class = "Warning", Title = "User already exist.", Description = "Username or Email are already assigned to an another user." }; }
public ActionResult Edit(MemberViewModel model) { if (ModelState.IsValid) { try { Member member = repository.Members.FirstOrDefault(m => m.MemberId == model.MemberId); // If UserName or Email are changed // verify that we are not creating a duplicate member if (member.UserName != model.UserName || member.Email != model.Email) { if (UserExists(model.UserName, model.Email)) { ModelState.AddModelError("", "Username or Email already in use."); ShowUserAlreadyExistAlert(); return View(model); } } member.UserName = model.UserName; member.Email = model.Email; member.Profile.FirstName = model.FirstName; member.Profile.LastName = model.LastName; member.Profile.Phone = model.Phone; member.Profile.Description = model.Description; member.Profile.Address = model.Address; member.Profile.Building = model.Building; member.Profile.Floor = model.Floor; member.Profile.Office = model.Office; repository.SaveMember(member); TempData["message"] = new Alert() { Class = "Success", Title = "User successfully edited.", Description = string.Format("{0} {1} has been edited.", model.LastName.ToUpper(), model.FirstName) }; } catch (Exception e) { TempData["message"] = new Alert(e); return View(model); } } else { return View("Edit", model); } return RedirectToAction("Index"); }
public ActionResult DeleteMember(int id, string returnUrl) { try { Member member = repository.Members.FirstOrDefault(m => m.MemberId == id); // Check if we are not deleting ourself. Guid MemberGuid = (Guid)Session["MemberGuid"]; if (MemberGuid == member.MemberGuid) { TempData["message"] = new Alert() { Class = "Warning", Title = "Cannot delete user.", Description = "You can't delete yourself." }; return Redirect(returnUrl ?? Url.Action("Index", "Member")); } string memberName = string.Format("{0} {1}", member.Profile.LastName.ToUpper(), member.Profile.FirstName); repository.DeleteMember(member); TempData["message"] = new Alert() { Class = "Success", Title = "Member deleted.", Description = string.Format("{0} has been deleted", memberName) }; return RedirectToAction("Index"); } catch (Exception e) { TempData["message"] = new Alert(e); return Redirect(returnUrl ?? Url.Action("Index", "Member")); } }