Exemplo n.º 1
0
        public ActionResult Login(UserViewModel userView, string returnUrl)
        {
            User         user;
            MailAccount  mailAccount;
            MailAccount  existingMailAccount;
            ISession     session        = NHibernateManager.OpenSession();
            ITransaction tran           = session.BeginTransaction();
            Boolean      workingOffline = false;

            try
            {
                this.UpdateModel(userView);
                String cipherPassword = CryptoHelper.EncryptDefaultKey(userView);

                if (Glimpse.Models.User.IsEmail(userView.Username)) //si es un email
                {
                    mailAccount = new MailAccount(userView.Username, cipherPassword);
                    try
                    {
                        mailAccount.ConnectLight(); //si pasa este punto es que los datos ingresados son correctos
                    }
                    catch (SocketException)
                    {
                        workingOffline = true;
                        mailAccount.ValidateCredentials();
                    }
                    user = Glimpse.Models.User.FindByUsername(userView.Username, session);
                    existingMailAccount = MailAccount.FindByAddress(userView.Username, session, false);

                    if (user == null)
                    {
                        user = new User(userView.Username, cipherPassword);
                        user.SaveOrUpdate(session);
                    }
                    else if (!CryptoHelper.PasswordsMatch(user.Entity.Password, cipherPassword))
                    {
                        user.Entity.Password = cipherPassword;
                        user.SaveOrUpdate(session);
                    }

                    if (existingMailAccount == null)
                    {
                        mailAccount.SetUser(user);
                        mailAccount.SetOldestMailDate();
                        mailAccount.Deactivate(session); //llama a saveOrUpdate adentro
                    }
                    else
                    {
                        if (existingMailAccount.Entity.Password != mailAccount.Entity.Password)
                        {
                            existingMailAccount.Entity.Password = mailAccount.Entity.Password;
                            existingMailAccount.SaveOrUpdate(session);
                        }
                        mailAccount.Entity = existingMailAccount.Entity;
                    }

                    if (!workingOffline)
                    {
                        mailAccount.UpdateLabels(session);
                    }

                    user.AddAccount(mailAccount);
                }
                else //si es un usuario glimpse
                {
                    user = Glimpse.Models.User.FindByUsername(userView.Username, session);
                    if (user == null)
                    {
                        this.ModelState.AddModelError("User", "Usuario inexistente.");
                        tran.Rollback();
                        return(View(userView));
                    }
                    else if (!CryptoHelper.PasswordsMatch(user.Entity.Password, cipherPassword))
                    {
                        this.ModelState.AddModelError("User", "Contraseña incorrecta.");
                        tran.Rollback();
                        return(View(userView));
                    }
                    user.UpdateAccounts(session);
                    //try
                    //{
                    //    user.ConnectLight();
                    //    user.UpdateLabels(session);
                    //}
                    //catch (SocketException) { }
                }

                new CookieHelper().AddUsernameCookie(user.Entity.Username);
                FormsAuthentication.SetAuthCookie(userView.Username, true);
                tran.Commit();
                Session[AccountController.USER_NAME] = user;

                return(RedirectToLocal(returnUrl));
            }
            catch (InvalidOperationException) //model state invalido
            {
                tran.Rollback();
                return(View(userView));
            }
            catch (InvalidAuthenticationException)
            {
                tran.Rollback();
                ModelState.AddModelError("", "La dirección de correo o la contraseña no son correctos.");
                return(View(userView));
            }
            catch (Exception)
            {
                tran.Rollback();
                ModelState.AddModelError("", "Existen problemas para iniciar sesión, intentalo de nuevo más tarde.");
                return(View(userView));
            }
            finally
            {
                session.Close();
            }
        }