From cb2c64f5c624a158e5400da2cd6841f374272d10 Mon Sep 17 00:00:00 2001
From: Vivian Zhang <vivian.zhang@intel.com>
Date: Fri, 6 Aug 2010 00:26:33 -0400
Subject: [PATCH] Add change password link(optional) to home page

---
 src/webui/app/controllers/user_controller.rb      |   36 ++++++++++++++++++++-
 src/webui/app/views/home/index.rhtml              |    3 ++
 src/webui/app/views/user/change_my_password.rhtml |   20 +++++++++++
 src/webui/config/environments/production.rb       |    3 ++
 4 files changed, 61 insertions(+), 1 deletions(-)
 create mode 100644 src/webui/app/views/user/change_my_password.rhtml

diff --git a/src/webui/app/controllers/user_controller.rb b/src/webui/app/controllers/user_controller.rb
index 4b848e4..d2965e7 100644
--- a/src/webui/app/controllers/user_controller.rb
+++ b/src/webui/app/controllers/user_controller.rb
@@ -1,7 +1,7 @@
 class UserController < ApplicationController
 
   before_filter :require_login, :only => [:edit, :save, :register]
-  before_filter :check_user, :only => [:edit, :save]
+  before_filter :check_user, :only => [:edit, :save, :change_password]
 
   def logout
     logger.info "Logging out: #{session[:login]}"
@@ -75,4 +75,38 @@ class UserController < ApplicationController
     redirect_to :controller => :project, :action => :show, :project => "home:#{session[:login]}"
   end
 
+  def change_password
+    # check the valid of the params  
+    if not params[:current_password] == session[:passwd]
+      errmsg = "The value of current password does not match your current password. Please enter the password and try again."
+    end
+    if not params[:new_password] == params[:password_confirmation]
+      errmsg = "The new passwords do not match. Please enter the password and try again."
+    end    
+    if params[:current_password] == params[:new_password]
+      errmsg = "The new password is the same as your current password. Please enter the new password again."
+    end
+    if errmsg:
+      flash[:error] = errmsg
+      redirect_to :controller => :user, :action => :change_my_password
+      return
+    end
+
+    login = session[:login]
+    require 'base64'     
+    new_password = Base64.encode64(params[:new_password])
+    begin
+      path = "/person/#{login}/passwd/#{new_password}"
+      result = frontend.transport.direct_http( URI(path) )
+    rescue ActiveXML::Transport::Error => e
+      message, code, api_exception = ActiveXML::Transport.extract_error_message e
+      flash[:error] = message
+      redirect_to :controller => :user, :action => :change_my_password
+      return
+    end
+    session[:passwd] = params[:new_password]
+    flash[:success] = "Your password has been changed successfully."
+    redirect_to :controller => :home, :action => :index
+  end 
+
 end
diff --git a/src/webui/app/views/home/index.rhtml b/src/webui/app/views/home/index.rhtml
index 09529d7..58069e7 100644
--- a/src/webui/app/views/home/index.rhtml
+++ b/src/webui/app/views/home/index.rhtml
@@ -19,5 +19,8 @@
     </p>
     
     <p><%= link_to "Edit User Data", :controller => "user", :action => "edit" %></p>
+    <%if defined?( CHANGE_PASSWD ) && CHANGE_PASSWD == 'on' %>
+      <p><%= link_to "Change Password", :controller => "user", :action => "change_my_password" %></p>
+    <% end %>
 </div>
 
diff --git a/src/webui/app/views/user/change_my_password.rhtml b/src/webui/app/views/user/change_my_password.rhtml
new file mode 100644
index 0000000..8ec2f36
--- /dev/null
+++ b/src/webui/app/views/user/change_my_password.rhtml
@@ -0,0 +1,20 @@
+<% @pagetitle = "Change Password" %>
+
+<h2>Change Your Password</h2>
+
+<% form_tag :action => :change_password do -%>
+<table border='0' cellspacing='0' cellpadding='5'>
+  <tr>
+    <td>Current Password:</td>
+    <td><input type="password" name="current_password" id="current_password" size="20"/></td>
+  </tr><tr>
+    <td>New Password:</td>
+    <td><input type="password" name="new_password" id="new_password" size="20"/></td>
+  </tr><tr>
+    <td>Repeat New Password:</td>
+    <td><input type="password" name="password_confirmation" id="password_confirmation" size="20"/></td>
+  </tr>
+</table>
+<p><%= submit_tag %></p>
+<% end %>
+
diff --git a/src/webui/config/environments/production.rb b/src/webui/config/environments/production.rb
index db170d4..d63836c 100644
--- a/src/webui/config/environments/production.rb
+++ b/src/webui/config/environments/production.rb
@@ -39,3 +39,6 @@ FRONTEND_PROTOCOL = 'https'
 # ICHAIN_MODE can be one of  'on', 'off' or 'simulate'
 ICHAIN_MODE = "off"
 
+# Enable the interface to change user's password, it can be one of 'on', 'off'
+CHANGE_PASSWD = "on"
+
-- 
1.6.1
