public void CheckIfUserIsDifferentThanLogged(UserToLoginViewModel loggedUser, User friend, ProjectDbContext db) { var isUserSameAsLogged = friend.Id == loggedUser.Id; if (isUserSameAsLogged) { Status = ActionStatus.UserIsSameAsLogged; throw new Exception("Nie możesz manipulować kontem na które jesteś zalogowany"); } }
public void CheckIfNotFriend(UserToLoginViewModel loggedUser, User friend, ProjectDbContext db) { var dbLoggedUser = db.Users.Include(u => u.AddedToFriends).Include(u => u.AddedAsFriendBy).Single(u => u.Id == loggedUser.Id); var isFriend = dbLoggedUser.IsFriendWith(friend); if (!isFriend) { Status = ActionStatus.IsAlreadyFriend; throw new Exception("Użytkownik nie znajduje się w znajomych"); } }
public void CheckIfNotAddedToFriends(UserToLoginViewModel loggedUser, User friend, ProjectDbContext db) { var dbLoggedUser = db.Users.Include(u => u.AddedToFriends).Single(u => u.Id == loggedUser.Id); var isAlreadyAddedToFriends = dbLoggedUser.HasAddedToFriends(friend); if (!isAlreadyAddedToFriends) { Status = ActionStatus.IsAlreadyFriend; throw new Exception("Użytkownik nie jest dodany do znajomych"); } }
public UserToLoginViewModel CheckIfLogged() { var loggedUser = UserToLoginViewModel.GetAuthenticated(); if (loggedUser != null) { return(loggedUser); } Status = ActionStatus.NotLogged; throw new Exception("Nie jesteś zalogowany"); }
public UserToLoginViewModel GetAuthenticatedUser() { var userCookie = Request.Cookies["LoggedUser"]; var userSession = (UserToLoginViewModel)Session["LoggedUser"]; var user = new User(); UserToLoginViewModel userToLogin = null; if (userSession != null) { userToLogin = userSession; } else if (userCookie != null) { userToLogin = JsonConvert.DeserializeObject <UserToLoginViewModel>(userCookie.Value); } Mapper.Map(userToLogin, user); return(user.Authenticate(true) == ActionStatus.Success ? userToLogin : null); }
public string LoginUser([Bind(Include = "UserName,Password,RememberMe")] UserToLoginViewModel userToLogin) { if (!Request.IsAjaxRequest()) { throw new Exception("Zapytanie nie zostało wywołane jako zapytanie AJAX"); } //Thread.Sleep(5000); var user = new User(); Mapper.Map(userToLogin, user); var isAuthenticated = user.Authenticate(); Mapper.Map(user, userToLogin); switch (isAuthenticated) { case ActionStatus.Success: { userToLogin.Id = user.Id; // Zapisz w Sesji Session["LoggedUser"] = userToLogin; // Zapisz w Cookies if (user.RememberMe) { Response.SetCookie(new HttpCookie("LoggedUser") { Value = JsonConvert.SerializeObject(userToLogin), Expires = DateTime.Now.AddDays(30) }); } return(JsonConvert.SerializeObject(new { LoginMessage = "", PartialView = RenderPartialView("_LoginPanelLogged", userToLogin) })); } case ActionStatus.Failure: { return(JsonConvert.SerializeObject(new { LoginMessage = $"Niepoprawne Dane. Prób: {4 - user.RetryAttempts}", PartialView = RenderPartialView("_LoginPanel", userToLogin) })); } case ActionStatus.UserDoesNotExist: { return(JsonConvert.SerializeObject(new { LoginMessage = "Użytkownik nie istnieje", PartialView = RenderPartialView("_LoginPanel", userToLogin) })); } case ActionStatus.AccountNotActivated: { return(JsonConvert.SerializeObject(new { LoginMessage = "Konto Nieaktywne", PartialView = RenderPartialView("_LoginPanel", userToLogin) })); } case ActionStatus.AccountLocked: { int?secondsToUnlock = null; if (user.LockedDateTime != null) { secondsToUnlock = (int)(15 * 60 - DateTime.Now.Subtract((DateTime)user.LockedDateTime).TotalSeconds); } if (secondsToUnlock < 0) { secondsToUnlock = 0; } var timeToUnlock = secondsToUnlock != null ? $"{secondsToUnlock / 60:00}" + ":" + $"{secondsToUnlock % 60:00}" // string.Format("{0:00}", secondsToUnlock % 60) : "błąd"; return(JsonConvert.SerializeObject(new { LoginMessage = $"Zablokowano. Spróbuj za: {timeToUnlock}", PartialView = RenderPartialView("_LoginPanel", userToLogin) })); } case ActionStatus.DatabaseError: { return(JsonConvert.SerializeObject(new { LoginMessage = "Baza Danych nie odpowiada", PartialView = RenderPartialView("_LoginPanel", userToLogin) })); } default: throw new ArgumentOutOfRangeException(); } }