]> source.dussan.org Git - redmine.git/commitdiff
Allow AuthSources to control if they allow password changes.
authorEric Davis <edavis@littlestreamsoftware.com>
Sun, 23 May 2010 03:16:37 +0000 (03:16 +0000)
committerEric Davis <edavis@littlestreamsoftware.com>
Sun, 23 May 2010 03:16:37 +0000 (03:16 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3745 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/my_controller.rb
app/models/auth_source.rb
app/models/user.rb
app/views/my/account.rhtml
test/unit/user_test.rb

index f686759918a945e981af8e2bd2a59ee242ddcd56..f637b49b63a1c59e572a0176d70c0f95b75b1a65 100644 (file)
@@ -77,7 +77,7 @@ class MyController < ApplicationController
   # Manage user's password
   def password
     @user = User.current
-    if @user.auth_source_id
+    unless @user.change_password_allowed?
       flash[:error] = l(:notice_can_t_change_password)
       redirect_to :action => 'account'
       return
index 537ed2d43f438781e5ca79b89bf859eeef0c3a66..84f17b1bcde8e75bda5c0add6c0858956186e92b 100644 (file)
@@ -32,6 +32,15 @@ class AuthSource < ActiveRecord::Base
     "Abstract"
   end
 
+  def allow_password_changes?
+    self.class.allow_password_changes?
+  end
+
+  # Does this auth source backend allow password changes?
+  def self.allow_password_changes?
+    false
+  end
+
   # Try to authenticate a user not yet registered against available sources
   def self.authenticate(login, password)
     AuthSource.find(:all, :conditions => ["onthefly_register=?", true]).each do |source|
index 2dad3bb18aca7850defc30eddc621fd75724bcb1..a38a091701e2a0ec576639f22ae4b7f7386c30c0 100644 (file)
@@ -71,7 +71,7 @@ class User < Principal
   
   def before_save
     # update hashed_password if password was set
-    self.hashed_password = User.hash_password(self.password) if self.password
+    self.hashed_password = User.hash_password(self.password) if self.password && self.auth_source_id.blank?
   end
   
   def reload(*args)
@@ -116,7 +116,7 @@ class User < Principal
         user.language = Setting.default_language
         if user.save
           user.reload
-          logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger
+          logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source
         end
       end
     end    
@@ -161,7 +161,17 @@ class User < Principal
   end
 
   def check_password?(clear_password)
-    User.hash_password(clear_password) == self.hashed_password
+    if auth_source_id.present?
+      auth_source.authenticate(self.login, clear_password)
+    else
+      User.hash_password(clear_password) == self.hashed_password
+    end
+  end
+
+  # Does the backend storage allow this user to change their password?
+  def change_password_allowed?
+    return true if auth_source_id.blank?
+    return auth_source.allow_password_changes?
   end
 
   # Generate and set a random password.  Useful for automated user creation
index 9bf45b33e4dfed04ee6f18c0e6f98d7d2260e75d..befe6be5a1aac1b989396068aecd180fe1224620 100644 (file)
@@ -1,5 +1,5 @@
 <div class="contextual">
-<%= link_to(l(:button_change_password), :action => 'password') unless @user.auth_source_id %>
+<%= link_to(l(:button_change_password), :action => 'password') if @user.change_password_allowed? %>
 <%= call_hook(:view_my_account_contextual, :user => @user)%>
 </div>
 <h2><%=l(:label_my_account)%></h2>
index f63716501c38f55fe3bbbac99c8273cbd7413fa0..77a9ee984763c5006ab4bd10d1e105a89648d005 100644 (file)
@@ -273,6 +273,32 @@ class UserTest < ActiveSupport::TestCase
     assert !u.password.blank?
     assert !u.password_confirmation.blank?
   end
+
+  context "#change_password_allowed?" do
+    should "be allowed if no auth source is set" do
+      user = User.generate_with_protected!
+      assert user.change_password_allowed?
+    end
+
+    should "delegate to the auth source" do
+      user = User.generate_with_protected!
+      
+      allowed_auth_source = AuthSource.generate!
+      def allowed_auth_source.allow_password_changes?; true; end
+
+      denied_auth_source = AuthSource.generate!
+      def denied_auth_source.allow_password_changes?; false; end
+
+      assert user.change_password_allowed?
+
+      user.auth_source = allowed_auth_source
+      assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
+
+      user.auth_source = denied_auth_source
+      assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
+    end
+
+  end
   
   if Object.const_defined?(:OpenID)