diff --git a/app/controllers/redmine_my_users_controller.rb b/app/controllers/redmine_my_users_controller.rb index 44ebe71..f160576 100644 --- a/app/controllers/redmine_my_users_controller.rb +++ b/app/controllers/redmine_my_users_controller.rb @@ -22,7 +22,14 @@ def index @status = params[:status] || 1 - scope = User.where("parent_id = ?", User.current.id).status(@status) + @admin_toggle = params[:admin_toggle_all] || false + + if User.current.admin? && @admin_toggle == "on" + scope = User.all.status(@status) + else + scope = User.where("parent_id = ?", User.current.id).status(@status) + end + scope = scope.like(params[:name]) if params[:name].present? scope = scope.in_group(params[:group_id]) if params[:group_id].present? @@ -45,4 +52,3 @@ def index end end - diff --git a/app/helpers/users_helper_patch.rb b/app/helpers/users_helper_patch.rb index db8165d..66f14e0 100644 --- a/app/helpers/users_helper_patch.rb +++ b/app/helpers/users_helper_patch.rb @@ -2,10 +2,7 @@ module UsersHelperPatch def self.included(base) base.send(:include, InstanceMethods) - # Wrap the methods we are extending - base.alias_method_chain :users_status_options_for_select, :parent_id - - # Exectue this code at the class level (not instance level) + # Execute this code at the class level (not instance level) base.class_eval do unloadable # Send unloadable so it will not be unloaded in development @@ -28,7 +25,7 @@ def users_select(form, id_field, users, opts = {}) end def valid_user_parents - User.all.select(&:active?) - [@user] - @user.descendants + (User.all.select(&:active?) + User.where("id=?",@user.parent_id) - [@user] - @user.descendants).uniq end end end diff --git a/app/views/redmine_my_users/index.html.erb b/app/views/redmine_my_users/index.html.erb index dbe6c21..baae0c7 100644 --- a/app/views/redmine_my_users/index.html.erb +++ b/app/views/redmine_my_users/index.html.erb @@ -6,7 +6,7 @@ <%= select_tag 'status', - users_status_options_for_select(@status), + users_status_options_for_select_with_parent_id(@status), :class => "small", :onchange => "this.form.submit(); return false;" %> @@ -22,6 +22,10 @@ <%= text_field_tag 'name', params[:name], :size => 30 %> <%= submit_tag l(:button_apply), :class => "small", :name => nil %> + <%if User.current.admin?%> + + <%= check_box_tag 'admin_toggle_all', params[:admin_toggle_all], @admin_toggle, onclick: "this.form.submit();" %> + <%end%> <%= link_to l(:button_clear), my_users_path, :class => 'icon icon-reload' %> <% end %> @@ -54,4 +58,3 @@
<%= pagination_links_full @user_pages, @user_count %>
<% html_title(l(:label_my_users)) -%> - diff --git a/config/locales/en.yml b/config/locales/en.yml index a876add..a6555bf 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2,3 +2,4 @@ en: field_user_parent: "Sponsor" label_my_users: "My users" + label_admin_toggle_box: "Show all users" diff --git a/init.rb b/init.rb index 186844b..0d836a4 100644 --- a/init.rb +++ b/init.rb @@ -12,6 +12,10 @@ version '0.1.4' requires_redmine :version_or_higher => '2.0.0' + settings :default => { + :admin_toggle_all => false, + :save_log => false + } end @@ -41,4 +45,3 @@ def load_patches(path = nil) end end load_patches - diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..9b4b4ef --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,26 @@ +require File.expand_path('../../test_helper', __FILE__) + +class UserTest < ActiveSupport::TestCase + include TestSetupMethods + include TestHelperMethods + def setup + @show_debug = false + end + + test "parent_id_required?" do + @alice = create_user('alice', 'alice@doe.com') + assert @alice.errors.any?, + "User should have failed creation - #{@alice.inspect}" + end + + test "cannot_set_parent_id_to_child_id" do + flunk('Functionality does not exist') # comment this out to test actual code + @alice = create_user('alice','alice@doe.com', 1) + @bob = create_user('bob','bob@doe.com', @alice.id) + + find_user(@alice).update({:parent_id => @bob.id.to_i}) + + refute find_user(@alice).parent_id.to_i == @bob.id.to_i, + "Should not allow update with child as parent - #{find_user(@alice).inspect}" + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 54685d3..978d038 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,2 +1,44 @@ # Load the Redmine helper require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper') + +module TestSetupMethods + def create_user(login, email, parent_id = nil) + pwd = repeat_str('123') + mock_user = User.create() do |u| + u.login = login + u.password = pwd + u.password_confirmation = pwd + u.firstname = login + u.lastname = 'doe' + u.mail = email + u.language = 'en' + u.mail_notification = 'only_my_events' + u.must_change_passwd = false + u.parent_id = parent_id + u.status = 1 + u.auth_source_id = nil + end + # block below prints out all errors if validation fails + if mock_user.errors.any? && @show_debug + mock_user.errors.each do |attribute, message| + puts "Error - #{attribute} : #{message}" + end + end + mock_user + end +end + +module TestHelperMethods + def repeat_str(input, length = Setting.password_min_length) + puts input if @show_debug + repeated_input = "" + while repeated_input.length < length.to_i + repeated_input << input + end + repeated_input + end + + def find_user(user) + User.find_by_login(user.login) + end +end