package org.zanata.action;

import java.io.Serializable;

import javax.persistence.EntityManager;

import org.hibernate.validator.Length;
import org.hibernate.validator.NotEmpty;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Identity;
import org.jboss.seam.security.RunAsOperation;
import org.jboss.seam.security.management.IdentityManager;
import org.zanata.model.HAccountResetPasswordKey;
import org.zanata.security.KeyNotFoundException;

@Name("passwordReset")
@Scope(ScopeType.CONVERSATION)
public class PasswordResetAction implements Serializable
{

   private static final long serialVersionUID = -3966625589007754411L;

   @Logger
   Log log;

   @In
   private EntityManager entityManager;

   @In
   private IdentityManager identityManager;

   @In
   private Identity identity;

   private String activationKey;

   private String password;
   private String passwordConfirm;

   private HAccountResetPasswordKey key;

   public void setPassword(String password)
   {
      this.password = password;
   }

   @NotEmpty
   @Length(min = 6, max = 20)
   // @Pattern(regex="(?=^.{6,}$)((?=.*\\d)|(?=.*\\W+))(?![.\\n])(?=.*[A-Z])(?=.*[a-z]).*$",
   // message="Password is not secure enough!")
   public String getPassword()
   {
      return password;
   }

   public void setPasswordConfirm(String passwordConfirm)
   {
      this.passwordConfirm = passwordConfirm;
      validatePasswordsMatch();
   }

   public String getPasswordConfirm()
   {
      return passwordConfirm;
   }

   public boolean validatePasswordsMatch()
   {
      if (password == null || !password.equals(passwordConfirm))
      {
         FacesMessages.instance().addToControl("passwordConfirm", "Passwords do not match");
         return false;
      }
      return true;
   }

   public String getActivationKey()
   {
      return activationKey;
   }

   public void setActivationKey(String activationKey)
   {
      this.activationKey = activationKey;
      key = entityManager.find(HAccountResetPasswordKey.class, getActivationKey());
   }

   private HAccountResetPasswordKey getKey()
   {
      return key;
   }

   @Begin(join = true)
   public void validateActivationKey()
   {

      if (getActivationKey() == null)
         throw new KeyNotFoundException();

      key = entityManager.find(HAccountResetPasswordKey.class, getActivationKey());

      if (key == null)
         throw new KeyNotFoundException();
   }

   @End
   public String changePassword()
   {

      if (!validatePasswordsMatch())
         return null;

      new RunAsOperation()
      {
         public void execute()
         {
            identityManager.changePassword(getKey().getAccount().getUsername(), getPassword());
         }
      }.addRole("admin").run();

      entityManager.remove(getKey());

      FacesMessages.instance().add("Your password has been successfully changed.");

      // Login the user
      identity.getCredentials().setUsername(getKey().getAccount().getUsername());
      identity.getCredentials().setPassword(getPassword());
      identity.login();

      return "/home.xhtml";
   }

}

@@ -551,24 +551,132 @@
 ecurity.
-Identity
+AuthorizationException;%0Aimport org.jboss.seam.security.Identity;%0Aimport org.jboss.seam.security.NotLoggedInException
 ;%0Aimport
@@ -2838,24 +2838,61 @@
 on();%0A   %7D%0A%0A
+   private boolean passwordChanged;%0A%0A
    @End%0A   p
@@ -3071,24 +3071,75 @@
 %0A           
+ try%0A            %7B%0A               passwordChanged =
  identityMan
@@ -3207,24 +3207,446 @@
 assword());%0A
+            %7D%0A            catch (AuthorizationException e)%0A            %7B%0A               passwordChanged = false;%0A               FacesMessages.instance().add(%22Error changing password: %22 + e.getMessage());%0A            %7D%0A            catch (NotLoggedInException ex)%0A            %7B%0A               passwordChanged = false;%0A               FacesMessages.instance().add(%22Error changing password: %22 + ex.getMessage());%0A            %7D%0A
          %7D%0A 
@@ -3712,24 +3712,62 @@
 getKey());%0A%0A
+      if (passwordChanged)%0A      %7B%0A   
       FacesM
@@ -3838,13 +3838,55 @@
 ged.
+ Please sign in with your new password.
 %22);%0A%0A
+   
     
@@ -3906,24 +3906,30 @@
 e user%0A     
+    //
  identity.ge
@@ -3991,32 +3991,38 @@
 rname());%0A      
+ 
+  // 
 identity.getCred
@@ -4064,16 +4064,22 @@
 );%0A     
+    //
  identit
@@ -4092,24 +4092,30 @@
 n();%0A%0A      
+ 
+  // 
 return %22/hom
@@ -4124,16 +4124,217 @@
 xhtml%22;%0A
+         return %22/account/login.xhtml%22;%0A      %7D%0A      else%0A      %7B%0A         FacesMessages.instance().add(%22There was a problem changing the password. Please try again.%22);%0A         return null;%0A      %7D%0A%0A
    %7D%0A%0A%7D%0A

fix password reset for internal authentication