Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions app/controllers/redmine_my_users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand All @@ -45,4 +52,3 @@ def index
end

end

7 changes: 2 additions & 5 deletions app/helpers/users_helper_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions app/views/redmine_my_users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<label for='status'><%= l(:field_status) %>:</label>
<%= 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;" %>

Expand All @@ -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?%>
<label for='name'><%= l(:label_admin_toggle_box) %>:</label>
<%= 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' %>
</fieldset>
<% end %>
Expand Down Expand Up @@ -54,4 +58,3 @@
<p class="pagination"><%= pagination_links_full @user_pages, @user_count %></p>

<% html_title(l(:label_my_users)) -%>

1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
en:
field_user_parent: "Sponsor"
label_my_users: "My users"
label_admin_toggle_box: "Show all users"
5 changes: 4 additions & 1 deletion init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -41,4 +45,3 @@ def load_patches(path = nil)
end
end
load_patches

26 changes: 26 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
@@ -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', '[email protected]')
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','[email protected]', 1)
@bob = create_user('bob','[email protected]', @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
42 changes: 42 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -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