Project

General

Profile

Patch #1568 ยป htauth_patch.patch

Robert Shell, 2008-07-03 14:32

View differences:

app/controllers/auth_sources_controller.rb (working copy)
end
def new
@auth_source = AuthSourceLdap.new
if params[:id]=='file'
@auth_source = AuthSourceFile.new
else
@auth_source = AuthSourceLdap.new
end
end
def create
@auth_source = AuthSourceLdap.new(params[:auth_source])
if params[:id]=='file'
@auth_source = AuthSourceFile.new(params[:auth_source])
else
@auth_source = AuthSourceLdap.new(params[:auth_source])
end
if @auth_source.save
flash[:notice] = l(:notice_successful_create)
redirect_to :action => 'list'
......
end
end
def edit
@auth_source = AuthSource.find(params[:id])
end
app/controllers/users_controller.rb (working copy)
@user = User.new(params[:user])
@user.admin = params[:user][:admin] || false
@user.login = params[:user][:login]
@user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id
if @user.passwd?
@user.password = params[:password]
@user.password_confirmation = params[:password_confirmation]
end
if @user.save
Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
flash[:notice] = l(:notice_successful_create)
app/models/auth_source.rb (working copy)
validates_presence_of :name
validates_uniqueness_of :name
validates_length_of :name, :host, :maximum => 60
validates_length_of :account_password, :maximum => 60, :allow_nil => true
validates_length_of :account, :base_dn, :maximum => 255
validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30
validates_length_of :name, :maximum => 60
validates_length_of :account, :maximum => 255
def authenticate(login, password)
end
def passwd?
false
end
def test_connection
end
app/models/auth_source_file.rb (revision 0)
# redMine - project management software
# Copyright (C) 2006 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'htauth'
class AuthSourceFile < AuthSource
validates_presence_of :account
def authenticate(login, password)
return nil if login.blank? || password.blank?
HTAuth::PasswdFile.open(self.account) do |pf|
dn = pf.fetch(login)
if dn and dn.authenticated?(password)
logger.debug "Authenticated #{login}: #{dn}" if logger && logger.debug?
return true
else
return false
end
end
end
def passwd?
true
end
def passwd(login,password)
return false if login.blank? || password.blank?
HTAuth::PasswdFile.open(self.account) do |pf|
logger.debug "add_or_update #{login}" if logger && logger.debug?
pf.add_or_update(login,password)
return true
end
end
# test the connection to the LDAP
def test_connection
con = HTAuth::PasswdFile.open(self.account)
return con.is_a?(HTAuth::PasswdFile)
end
def auth_method_name
"HTAUTH"
end
private
end
app/models/auth_source_ldap.rb (working copy)
class AuthSourceLdap < AuthSource
validates_presence_of :host, :port, :attr_login
validates_presence_of :attr_firstname, :attr_lastname, :attr_mail, :if => Proc.new { |a| a.onthefly_register? }
validates_length_of :host, :maximum => 60
validates_length_of :account_password, :maximum => 60, :allow_nil => true
validates_length_of :base_dn, :maximum => 255
validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30
def after_initialize
self.port = 389 if self.port == 0
app/models/user.rb (working copy)
def before_save
# update hashed_password if password was set
self.hashed_password = User.hash_password(self.password) if self.password
if self.password
self.hashed_password = User.hash_password(self.password)
if self.auth_source
self.auth_source.passwd(self.login,self.password)
end
end
end
def self.active
......
self.status == STATUS_ACTIVE
end
def passwd?
return true unless self.auth_source_id
self.auth_source.passwd?
end
def registered?
self.status == STATUS_REGISTERED
end
app/views/auth_sources/_file_form.rhtml (revision 0)
<%= error_messages_for 'auth_source' %>
<div class="box">
<!--[form:auth_source]-->
<p><label for="auth_source_name"><%=l(:field_name)%> <span class="required">*</span></label>
<%= text_field 'auth_source', 'name' %></p>
<p><label for="auth_source_filename"><%=l(:field_account)%></label>
<%= text_field 'auth_source', 'account' %></p>
</div>
<!--[eoform:auth_source]-->
app/views/auth_sources/list.rhtml (working copy)
<div class="contextual">
<%= link_to l(:label_auth_source_new), {:action => 'new'}, :class => 'icon icon-add' %>
<%= link_to l(:label_auth_source_ldap_new), {:action => 'new'}, :class => 'icon icon-add' %>
<%= link_to l(:label_auth_source_file_new), {:action => 'new',:id=>'file'}, :class => 'icon icon-add' %>
</div>
<h2><%=l(:label_auth_source_plural)%></h2>
app/views/auth_sources/new.rhtml (working copy)
<h2><%=l(:label_auth_source_new)%> (<%= @auth_source.auth_method_name %>)</h2>
<% form_tag({:action => 'create'}, :class => "tabular") do %>
<%= render :partial => 'form' %>
<%= submit_tag l(:button_create) %>
<% end %>
<% if @auth_source.is_a?(AuthSourceFile) %>
<% form_tag({:action => 'create', :id=>'file'}, :class => "tabular") do %>
<%= render :partial => 'file_form' %>
<%= submit_tag l(:button_create) %>
<% end %>
<%else%>
<% form_tag({:action => 'create'}, :class => "tabular") do %>
<%= render :partial => 'form' %>
<%= submit_tag l(:button_create) %>
<% end %>
<%end%>
app/views/users/_form.rhtml (working copy)
<div class="box">
<h3><%=l(:label_authentication)%></h3>
<% unless @auth_sources.empty? %>
<p><%= f.select :auth_source_id, ([[l(:label_internal), ""]] + @auth_sources.collect { |a| [a.name, a.id] }), {}, :onchange => "if (this.value=='') {Element.show('password_fields');} else {Element.hide('password_fields');}" %></p>
<p><%= f.select :auth_source_id, ([[l(:label_internal), ""]] + @auth_sources.collect { |a| [a.name, a.id] }), {} %></p>
<% end %>
<div id="password_fields" style="<%= 'display:none;' if @user.auth_source %>">
<div id="password_fields" >
<p><label for="password"><%=l(:field_password)%><span class="required"> *</span></label>
<%= password_field_tag 'password', nil, :size => 25 %><br />
<em><%= l(:text_caracters_minimum, 4) %></em></p>
lang/en.yml (working copy)
label_environment: Environment
label_authentication: Authentication
label_auth_source: Authentication mode
label_auth_source_ldap_new: New Ldap authentication mode
label_auth_source_file_new: New htpasswd authentication mode
label_auth_source_new: New authentication mode
label_auth_source_plural: Authentication modes
label_subproject_plural: Subprojects
test/unit/auth_source_file_test.rb (revision 0)
require File.dirname(__FILE__) + '/../test_helper'
class AuthSourceFileTest < Test::Unit::TestCase
fixtures :projects, :boards, :messages
def setup
system("htpasswd -c -b auth_file admin admin ")
system("htpasswd -b auth_file test test ")
@auth = AuthSourceFile.create(:account => 'auth_file')
end
def test_auth_name
assert_equal "HTAUTH",@auth.auth_method_name
end
def test_connection
assert @auth.test_connection
end
def test_auth_test_ok
assert @auth.authenticate('test','test')
end
def test_auth_test_bad_password
assert !@auth.authenticate('test','xxx')
end
def test_auth_test_bad_username
assert !@auth.authenticate('testx','xxx')
end
def test_passwd_add_user
assert !@auth.authenticate('testx','xxx')
assert @auth.passwd('testx','xxx')
assert @auth.authenticate('testx','xxx')
end
def test_passwd_update_user
assert !@auth.authenticate('test','xxx')
assert @auth.passwd('test','xxx')
assert @auth.authenticate('test','xxx')
end
end
    (1-1/1)