public override string[] FindUsersInRole(string roleName, string usernameToMatch) { using (var usersContext = new UsersContext()) { return usersContext.GetUsers().Where(m => m.type == roleName && m.login.Contains(usernameToMatch)).Select(m => m.login).ToArray(); } }
public ActionResult EditUser(ProfileViewModel user) { if (ModelState.IsValid) { using (UsersContext context = new UsersContext()) { var user2 = context.GetUser(User.Identity.Name); if (context.GetUsers().Any(m => m.login == user.login && m.id != user.id)) { ViewBag.ErrorMessage = "Taki login już istnieje"; } else { user.id = user2.id; profileModel.EditProfile(user); } } } UserModel model = new UserModel() { PasswordModel = new LocalPasswordModel(), ProfileModel = user }; return View("Manage", model); }
public int GetUserId() { var userName = User.Identity.Name; using (UsersContext context = new UsersContext()) { var user = context.GetUsers().First(m => m.login == userName); return user.id; } }
public override void AddUsersToRoles(string[] usernames, string[] roleNames) { if (roleNames.Length > 1) { throw new NotSupportedException(); } using (var usersContext = new UsersContext()) { foreach (var user in usernames) { var uzytkownik = usersContext.GetUser(user); var role = roleNames.FirstOrDefault(); if (role != null) { uzytkownik.type = role; } else { uzytkownik.type = string.Empty; } usersContext.SaveUser(uzytkownik); } } }
private ProfileViewModel CreateProfileModel() { using (UsersContext context = new UsersContext()) { var user = context.GetUser(User.Identity.Name); var uzytkownik = profileModel.GetProfile(user.id); return uzytkownik; } }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user try { using (UsersContext context = new UsersContext()) { if (!context.GetUsers().Any(m => m.login == model.Login)) { context.AddUser(new Profile() { firstname = model.Firstname, surname = model.Surname, password = model.Password, type = "user", login = model.Login }); return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", "Istnieje już użytkownik o takim loginie!"); } } } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult Manage(LocalPasswordModel model) { if (ModelState.IsValid) { using (UsersContext context = new UsersContext()) { var user = context.GetUser(User.Identity.Name); if (user.password == model.OldPassword) { if (model.NewPassword == model.ConfirmPassword) { user.password = model.NewPassword; context.SaveUser(user); ViewBag.StatusMessage = "Zmiana hasła powiodła się!"; return RedirectToAction("Manage"); } else { ViewBag.ErrorMessage = "Nowe hasło i powtórzone nie są identyczne!"; } } else { ViewBag.ErrorMessage = "Stare hasło jest niepoprawne"; } } } UserModel uzytkownikModel = new UserModel() { PasswordModel = model, ProfileModel = CreateProfileModel() }; var uzytkownik = this.profileModel.GetProfile(User.Identity.Name); ViewBag.ReturnUrl = Url.Action("Manage"); return View(uzytkownikModel); }
public ActionResult Login(LoginModel model, string returnUrl) { if (ModelState.IsValid) { using (UsersContext context = new UsersContext()) { var user = context.GetUsers().SingleOrDefault(m => m.login == model.Login && m.password == model.Password); if (user != null) { // FormsAuthentication.SetAuthCookie(user.UzytkownikID, model.RememberMe); FormsAuthentication.SetAuthCookie(model.Login, model.RememberMe); return RedirectToLocal(returnUrl); } } } // If we got this far, something failed, redisplay form ModelState.AddModelError("", "Login lub hasło jest niepoprawne!"); return View(model); }
public override string[] GetUsersInRole(string roleName) { using (var usersContext = new UsersContext()) { return usersContext.GetUsers().Where(m => m.type == roleName).Select(m => m.login).ToArray(); } }
public override string[] GetRolesForUser(string username) { using (var usersContext = new UsersContext()) { var user = usersContext.GetUser(username); if (user == null) return new string[] { }; return user.type == null ? new string[] { } : new string[] { user.type }; } }
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { using (var usersContext = new UsersContext()) { foreach (var user in usernames) { var uzytkownik = usersContext.GetUser(user); if (roleNames.Contains(uzytkownik.type)) { uzytkownik.type = string.Empty; } usersContext.SaveUser(uzytkownik); } } }
public override bool IsUserInRole(string username, string roleName) { using (var usersContext = new UsersContext()) { var user = usersContext.GetUser(username); if (user == null) return false; return user.type != null && user.type == roleName; } }