##// END OF EJS Templates
Code cleanup in AuthSource controller and views....
Jean-Philippe Lang -
r10767:7775f86a69f1
parent child
Show More
@@ -1,76 +1,82
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class AuthSourcesController < ApplicationController
18 class AuthSourcesController < ApplicationController
19 layout 'admin'
19 layout 'admin'
20 menu_item :ldap_authentication
20 menu_item :ldap_authentication
21
21
22 before_filter :require_admin
22 before_filter :require_admin
23 before_filter :find_auth_source, :only => [:edit, :update, :test_connection, :destroy]
23
24
24 def index
25 def index
25 @auth_source_pages, @auth_sources = paginate AuthSource, :per_page => 10
26 @auth_source_pages, @auth_sources = paginate AuthSource, :per_page => 10
26 end
27 end
27
28
28 def new
29 def new
29 klass_name = params[:type] || 'AuthSourceLdap'
30 klass_name = params[:type] || 'AuthSourceLdap'
30 @auth_source = AuthSource.new_subclass_instance(klass_name, params[:auth_source])
31 @auth_source = AuthSource.new_subclass_instance(klass_name, params[:auth_source])
32 render_404 unless @auth_source
31 end
33 end
32
34
33 def create
35 def create
34 @auth_source = AuthSource.new_subclass_instance(params[:type], params[:auth_source])
36 @auth_source = AuthSource.new_subclass_instance(params[:type], params[:auth_source])
35 if @auth_source.save
37 if @auth_source.save
36 flash[:notice] = l(:notice_successful_create)
38 flash[:notice] = l(:notice_successful_create)
37 redirect_to auth_sources_path
39 redirect_to auth_sources_path
38 else
40 else
39 render :action => 'new'
41 render :action => 'new'
40 end
42 end
41 end
43 end
42
44
43 def edit
45 def edit
44 @auth_source = AuthSource.find(params[:id])
45 end
46 end
46
47
47 def update
48 def update
48 @auth_source = AuthSource.find(params[:id])
49 if @auth_source.update_attributes(params[:auth_source])
49 if @auth_source.update_attributes(params[:auth_source])
50 flash[:notice] = l(:notice_successful_update)
50 flash[:notice] = l(:notice_successful_update)
51 redirect_to auth_sources_path
51 redirect_to auth_sources_path
52 else
52 else
53 render :action => 'edit'
53 render :action => 'edit'
54 end
54 end
55 end
55 end
56
56
57 def test_connection
57 def test_connection
58 @auth_source = AuthSource.find(params[:id])
59 begin
58 begin
60 @auth_source.test_connection
59 @auth_source.test_connection
61 flash[:notice] = l(:notice_successful_connection)
60 flash[:notice] = l(:notice_successful_connection)
62 rescue Exception => e
61 rescue Exception => e
63 flash[:error] = l(:error_unable_to_connect, e.message)
62 flash[:error] = l(:error_unable_to_connect, e.message)
64 end
63 end
65 redirect_to auth_sources_path
64 redirect_to auth_sources_path
66 end
65 end
67
66
68 def destroy
67 def destroy
69 @auth_source = AuthSource.find(params[:id])
70 unless @auth_source.users.exists?
68 unless @auth_source.users.exists?
71 @auth_source.destroy
69 @auth_source.destroy
72 flash[:notice] = l(:notice_successful_delete)
70 flash[:notice] = l(:notice_successful_delete)
73 end
71 end
74 redirect_to auth_sources_path
72 redirect_to auth_sources_path
75 end
73 end
74
75 private
76
77 def find_auth_source
78 @auth_source = AuthSource.find(params[:id])
79 rescue ActiveRecord::RecordNotFound
80 render_404
81 end
76 end
82 end
@@ -1,13 +1,6
1 <%= error_messages_for 'auth_source' %>
1 <%= error_messages_for 'auth_source' %>
2
2
3 <div class="box">
3 <div class="box tabular">
4 <!--[form:auth_source]-->
4 <p><%= f.text_field :name, :required => true %></p>
5 <p><label for="auth_source_name"><%=l(:field_name)%> <span class="required">*</span></label>
5 <p><%= f.check_box :onthefly_register, :label => :field_onthefly %></p>
6 <%= text_field 'auth_source', 'name' %></p>
7
8 <p><label for="auth_source_onthefly_register"><%=l(:field_onthefly)%></label>
9 <%= check_box 'auth_source', 'onthefly_register' %></p>
10 </div>
6 </div>
11
12 <!--[eoform:auth_source]-->
13
@@ -1,50 +1,24
1 <%= error_messages_for 'auth_source' %>
1 <%= error_messages_for 'auth_source' %>
2
2
3 <div class="box">
3 <div class="box tabular">
4 <!--[form:auth_source]-->
4 <p><%= f.text_field :name, :required => true %></p>
5 <p><label for="auth_source_name"><%=l(:field_name)%> <span class="required">*</span></label>
5 <p><%= f.text_field :host, :required => true %></p>
6 <%= text_field 'auth_source', 'name' %></p>
6 <p><%= f.text_field :port, :required => true, :size => 6 %> <%= f.check_box :tls, :no_label => true %> LDAPS</p>
7
7 <p><%= f.text_field :account %></p>
8 <p><label for="auth_source_host"><%=l(:field_host)%> <span class="required">*</span></label>
8 <p><%= f.password_field :account_password, :label => :field_password,
9 <%= text_field 'auth_source', 'host' %></p>
9 :name => 'dummy_password',
10
10 :value => ((@auth_source.new_record? || @auth_source.account_password.blank?) ? '' : ('x'*15)),
11 <p><label for="auth_source_port"><%=l(:field_port)%> <span class="required">*</span></label>
11 :onfocus => "this.value=''; this.name='auth_source[account_password]';",
12 <%= text_field 'auth_source', 'port', :size => 6 %> <%= check_box 'auth_source', 'tls' %> LDAPS</p>
12 :onchange => "this.name='auth_source[account_password]';" %></p>
13
13 <p><%= f.text_field :base_dn, :required => true, :size => 60 %></p>
14 <p><label for="auth_source_account"><%=l(:field_account)%></label>
14 <p><%= f.text_field :filter, :size => 60, :label => :field_auth_source_ldap_filter %></p>
15 <%= text_field 'auth_source', 'account' %></p>
15 <p><%= f.text_field :timeout, :size => 4 %></p>
16
16 <p><%= f.check_box :onthefly_register, :label => :field_onthefly %></p>
17 <p><label for="auth_source_account_password"><%=l(:field_password)%></label>
18 <%= password_field 'auth_source', 'account_password', :name => 'ignore',
19 :value => ((@auth_source.new_record? || @auth_source.account_password.blank?) ? '' : ('x'*15)),
20 :onfocus => "this.value=''; this.name='auth_source[account_password]';",
21 :onchange => "this.name='auth_source[account_password]';" %></p>
22
23 <p><label for="auth_source_base_dn"><%=l(:field_base_dn)%> <span class="required">*</span></label>
24 <%= text_field 'auth_source', 'base_dn', :size => 60 %></p>
25
26 <p><label for="auth_source_custom_filter"><%=l(:field_auth_source_ldap_filter)%></label>
27 <%= text_field 'auth_source', 'filter', :size => 60 %></p>
28
29 <p><label for="auth_source_timeout"><%=l(:field_timeout)%></label>
30 <%= text_field 'auth_source', 'timeout', :size => 4 %></p>
31
32 <p><label for="auth_source_onthefly_register"><%=l(:field_onthefly)%></label>
33 <%= check_box 'auth_source', 'onthefly_register' %></p>
34 </div>
17 </div>
35
18
36 <fieldset class="box"><legend><%=l(:label_attribute_plural)%></legend>
19 <fieldset class="box tabular"><legend><%=l(:label_attribute_plural)%></legend>
37 <p><label for="auth_source_attr_login"><%=l(:field_login)%> <span class="required">*</span></label>
20 <p><%= f.text_field :attr_login, :required => true, :size => 20 %></p>
38 <%= text_field 'auth_source', 'attr_login', :size => 20 %></p>
21 <p><%= f.text_field :attr_firstname, :size => 20 %></p>
39
22 <p><%= f.text_field :attr_lastname, :size => 20 %></p>
40 <p><label for="auth_source_attr_firstname"><%=l(:field_firstname)%></label>
23 <p><%= f.text_field :attr_mail, :size => 20 %></p>
41 <%= text_field 'auth_source', 'attr_firstname', :size => 20 %></p>
42
43 <p><label for="auth_source_attr_lastname"><%=l(:field_lastname)%></label>
44 <%= text_field 'auth_source', 'attr_lastname', :size => 20 %></p>
45
46 <p><label for="auth_source_attr_mail"><%=l(:field_mail)%></label>
47 <%= text_field 'auth_source', 'attr_mail', :size => 20 %></p>
48 </fieldset>
24 </fieldset>
49 <!--[eoform:auth_source]-->
50
@@ -1,6 +1,6
1 <h2><%=l(:label_auth_source)%> (<%= h(@auth_source.auth_method_name) %>)</h2>
1 <h2><%=l(:label_auth_source)%> (<%= h(@auth_source.auth_method_name) %>)</h2>
2
2
3 <%= form_tag({:action => 'update', :id => @auth_source}, :method => :put, :class => "tabular") do %>
3 <%= form_for @auth_source, :as => :auth_source, :url => auth_source_path(@auth_source), :html => {:id => 'auth_source_form'} do |f| %>
4 <%= render :partial => auth_source_partial_name(@auth_source) %>
4 <%= render :partial => auth_source_partial_name(@auth_source), :locals => { :f => f } %>
5 <%= submit_tag l(:button_save) %>
5 <%= submit_tag l(:button_save) %>
6 <% end %>
6 <% end %>
@@ -1,31 +1,31
1 <div class="contextual">
1 <div class="contextual">
2 <%= link_to l(:label_auth_source_new), {:action => 'new'}, :class => 'icon icon-add' %>
2 <%= link_to l(:label_auth_source_new), {:action => 'new'}, :class => 'icon icon-add' %>
3 </div>
3 </div>
4
4
5 <h2><%=l(:label_auth_source_plural)%></h2>
5 <h2><%=l(:label_auth_source_plural)%></h2>
6
6
7 <table class="list">
7 <table class="list">
8 <thead><tr>
8 <thead><tr>
9 <th><%=l(:field_name)%></th>
9 <th><%=l(:field_name)%></th>
10 <th><%=l(:field_type)%></th>
10 <th><%=l(:field_type)%></th>
11 <th><%=l(:field_host)%></th>
11 <th><%=l(:field_host)%></th>
12 <th><%=l(:label_user_plural)%></th>
12 <th><%=l(:label_user_plural)%></th>
13 <th></th>
13 <th></th>
14 </tr></thead>
14 </tr></thead>
15 <tbody>
15 <tbody>
16 <% for source in @auth_sources %>
16 <% for source in @auth_sources %>
17 <tr id="auth-source-<%= source.id %>" class="<%= cycle("odd", "even") %>">
17 <tr id="auth-source-<%= source.id %>" class="<%= cycle("odd", "even") %>">
18 <td><%= link_to(h(source.name), :action => 'edit', :id => source)%></td>
18 <td><%= link_to(h(source.name), :action => 'edit', :id => source)%></td>
19 <td align="center"><%= h source.auth_method_name %></td>
19 <td align="center"><%= h source.auth_method_name %></td>
20 <td align="center"><%= h source.host %></td>
20 <td align="center"><%= h source.host %></td>
21 <td align="center"><%= h source.users.count %></td>
21 <td align="center"><%= h source.users.count %></td>
22 <td class="buttons">
22 <td class="buttons">
23 <%= link_to l(:button_test), {:action => 'test_connection', :id => source}, :class => 'icon icon-test' %>
23 <%= link_to l(:button_test), try_connection_auth_source_path(source), :class => 'icon icon-test' %>
24 <%= delete_link auth_source_path(source) %>
24 <%= delete_link auth_source_path(source) %>
25 </td>
25 </td>
26 </tr>
26 </tr>
27 <% end %>
27 <% end %>
28 </tbody>
28 </tbody>
29 </table>
29 </table>
30
30
31 <p class="pagination"><%= pagination_links_full @auth_source_pages %></p>
31 <p class="pagination"><%= pagination_links_full @auth_source_pages %></p>
@@ -1,7 +1,7
1 <h2><%=l(:label_auth_source_new)%> (<%= h(@auth_source.auth_method_name) %>)</h2>
1 <h2><%=l(:label_auth_source_new)%> (<%= h(@auth_source.auth_method_name) %>)</h2>
2
2
3 <%= form_tag({:action => 'create'}, :class => "tabular") do %>
3 <%= labelled_form_for @auth_source, :as => :auth_source, :url => auth_sources_path, :html => {:id => 'auth_source_form'} do |f| %>
4 <%= hidden_field_tag 'type', @auth_source.type %>
4 <%= hidden_field_tag 'type', @auth_source.type %>
5 <%= render :partial => auth_source_partial_name(@auth_source) %>
5 <%= render :partial => auth_source_partial_name(@auth_source), :locals => { :f => f } %>
6 <%= submit_tag l(:button_create) %>
6 <%= submit_tag l(:button_create) %>
7 <% end %>
7 <% end %>
@@ -1,127 +1,152
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class AuthSourcesControllerTest < ActionController::TestCase
20 class AuthSourcesControllerTest < ActionController::TestCase
21 fixtures :users, :auth_sources
21 fixtures :users, :auth_sources
22
22
23 def setup
23 def setup
24 @request.session[:user_id] = 1
24 @request.session[:user_id] = 1
25 end
25 end
26
26
27 def test_index
27 def test_index
28 get :index
28 get :index
29
29
30 assert_response :success
30 assert_response :success
31 assert_template 'index'
31 assert_template 'index'
32 assert_not_nil assigns(:auth_sources)
32 assert_not_nil assigns(:auth_sources)
33 end
33 end
34
34
35 def test_new
35 def test_new
36 get :new
36 get :new
37
37
38 assert_response :success
38 assert_response :success
39 assert_template 'new'
39 assert_template 'new'
40
40
41 source = assigns(:auth_source)
41 source = assigns(:auth_source)
42 assert_equal AuthSourceLdap, source.class
42 assert_equal AuthSourceLdap, source.class
43 assert source.new_record?
43 assert source.new_record?
44
44
45 assert_tag 'input', :attributes => {:name => 'type', :value => 'AuthSourceLdap'}
45 assert_select 'form#auth_source_form' do
46 assert_tag 'input', :attributes => {:name => 'auth_source[host]'}
46 assert_select 'input[name=type][value=AuthSourceLdap]'
47 assert_select 'input[name=?]', 'auth_source[host]'
48 end
49 end
50
51 def test_new_with_invalid_type_should_respond_with_404
52 get :new, :type => 'foo'
53 assert_response 404
47 end
54 end
48
55
49 def test_create
56 def test_create
50 assert_difference 'AuthSourceLdap.count' do
57 assert_difference 'AuthSourceLdap.count' do
51 post :create, :type => 'AuthSourceLdap', :auth_source => {:name => 'Test', :host => '127.0.0.1', :port => '389', :attr_login => 'cn'}
58 post :create, :type => 'AuthSourceLdap', :auth_source => {:name => 'Test', :host => '127.0.0.1', :port => '389', :attr_login => 'cn'}
52 assert_redirected_to '/auth_sources'
59 assert_redirected_to '/auth_sources'
53 end
60 end
54
61
55 source = AuthSourceLdap.first(:order => 'id DESC')
62 source = AuthSourceLdap.order('id DESC').first
56 assert_equal 'Test', source.name
63 assert_equal 'Test', source.name
57 assert_equal '127.0.0.1', source.host
64 assert_equal '127.0.0.1', source.host
58 assert_equal 389, source.port
65 assert_equal 389, source.port
59 assert_equal 'cn', source.attr_login
66 assert_equal 'cn', source.attr_login
60 end
67 end
61
68
62 def test_create_with_failure
69 def test_create_with_failure
63 assert_no_difference 'AuthSourceLdap.count' do
70 assert_no_difference 'AuthSourceLdap.count' do
64 post :create, :type => 'AuthSourceLdap', :auth_source => {:name => 'Test', :host => '', :port => '389', :attr_login => 'cn'}
71 post :create, :type => 'AuthSourceLdap', :auth_source => {:name => 'Test', :host => '', :port => '389', :attr_login => 'cn'}
65 assert_response :success
72 assert_response :success
66 assert_template 'new'
73 assert_template 'new'
67 end
74 end
68 assert_error_tag :content => /host can&#x27;t be blank/i
75 assert_error_tag :content => /host can&#x27;t be blank/i
69 end
76 end
70
77
71 def test_edit
78 def test_edit
72 get :edit, :id => 1
79 get :edit, :id => 1
73
80
74 assert_response :success
81 assert_response :success
75 assert_template 'edit'
82 assert_template 'edit'
76
83
77 assert_tag 'input', :attributes => {:name => 'auth_source[host]'}
84 assert_select 'form#auth_source_form' do
85 assert_select 'input[name=?]', 'auth_source[host]'
86 end
87 end
88
89 def test_edit_should_not_contain_password
90 AuthSource.find(1).update_column :account_password, 'secret'
91
92 get :edit, :id => 1
93 assert_response :success
94 assert_select 'input[value=secret]', 0
95 assert_select 'input[name=dummy_password][value=?]', /x+/
96 end
97
98 def test_edit_invalid_should_respond_with_404
99 get :edit, :id => 99
100 assert_response 404
78 end
101 end
79
102
80 def test_update
103 def test_update
81 put :update, :id => 1, :auth_source => {:name => 'Renamed', :host => '192.168.0.10', :port => '389', :attr_login => 'uid'}
104 put :update, :id => 1, :auth_source => {:name => 'Renamed', :host => '192.168.0.10', :port => '389', :attr_login => 'uid'}
82 assert_redirected_to '/auth_sources'
105 assert_redirected_to '/auth_sources'
83
106
84 source = AuthSourceLdap.find(1)
107 source = AuthSourceLdap.find(1)
85 assert_equal 'Renamed', source.name
108 assert_equal 'Renamed', source.name
86 assert_equal '192.168.0.10', source.host
109 assert_equal '192.168.0.10', source.host
87 end
110 end
88
111
89 def test_update_with_failure
112 def test_update_with_failure
90 put :update, :id => 1, :auth_source => {:name => 'Renamed', :host => '', :port => '389', :attr_login => 'uid'}
113 put :update, :id => 1, :auth_source => {:name => 'Renamed', :host => '', :port => '389', :attr_login => 'uid'}
91 assert_response :success
114 assert_response :success
92 assert_template 'edit'
115 assert_template 'edit'
93 assert_error_tag :content => /host can&#x27;t be blank/i
116 assert_error_tag :content => /host can&#x27;t be blank/i
94 end
117 end
95
118
96 def test_destroy
119 def test_destroy
97 assert_difference 'AuthSourceLdap.count', -1 do
120 assert_difference 'AuthSourceLdap.count', -1 do
98 delete :destroy, :id => 1
121 delete :destroy, :id => 1
122 assert_redirected_to '/auth_sources'
99 end
123 end
100 end
124 end
101
125
102 def test_destroy_auth_source_in_use
126 def test_destroy_auth_source_in_use
103 User.find(2).update_attribute :auth_source_id, 1
127 User.find(2).update_attribute :auth_source_id, 1
104
128
105 assert_no_difference 'AuthSourceLdap.count' do
129 assert_no_difference 'AuthSourceLdap.count' do
106 delete :destroy, :id => 1
130 delete :destroy, :id => 1
131 assert_redirected_to '/auth_sources'
107 end
132 end
108 end
133 end
109
134
110 def test_test_connection
135 def test_test_connection
111 AuthSourceLdap.any_instance.stubs(:test_connection).returns(true)
136 AuthSourceLdap.any_instance.stubs(:test_connection).returns(true)
112
137
113 get :test_connection, :id => 1
138 get :test_connection, :id => 1
114 assert_redirected_to '/auth_sources'
139 assert_redirected_to '/auth_sources'
115 assert_not_nil flash[:notice]
140 assert_not_nil flash[:notice]
116 assert_match /successful/i, flash[:notice]
141 assert_match /successful/i, flash[:notice]
117 end
142 end
118
143
119 def test_test_connection_with_failure
144 def test_test_connection_with_failure
120 AuthSourceLdap.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError.new("Something went wrong"))
145 AuthSourceLdap.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError.new("Something went wrong"))
121
146
122 get :test_connection, :id => 1
147 get :test_connection, :id => 1
123 assert_redirected_to '/auth_sources'
148 assert_redirected_to '/auth_sources'
124 assert_not_nil flash[:error]
149 assert_not_nil flash[:error]
125 assert_include 'Something went wrong', flash[:error]
150 assert_include 'Something went wrong', flash[:error]
126 end
151 end
127 end
152 end
General Comments 0
You need to be logged in to leave comments. Login now