diff --git a/app/controllers/admin/environment_users_controller.rb b/app/controllers/admin/environment_users_controller.rb deleted file mode 100644 index 1302cf1..0000000 --- a/app/controllers/admin/environment_users_controller.rb +++ /dev/null @@ -1,44 +0,0 @@ -class EnvironmentUsersController < AdminController - - protect 'manage_environment_users', :environment - - def per_page - 10 - end - - def index - @q = params[:q] - if @q.blank? - @collection = environment.people.no_templates(environment).paginate( - :per_page => per_page, - :page => params[:npage] - ) - else - @collection = find_by_contents(:people, environment.people.no_templates(environment), @q, {:per_page => per_page, :page => params[:npage]})[:results] - end - end - - def set_admin_role - @person = environment.people.find(params[:id]) - environment.add_admin(@person) - redirect_to :action => :index, :q => params[:q] - end - - def reset_admin_role - @person = environment.people.find(params[:id]) - environment.remove_admin(@person) - redirect_to :action => :index, :q => params[:q] - end - - def activate - @person = environment.people.find(params[:id]) - @person.user.activate - redirect_to :action => :index, :q => params[:q] - end - - def deactivate - @person = environment.people.find(params[:id]) - @person.user.deactivate - redirect_to :action => :index, :q => params[:q] - end -end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index e71c17b..b7611aa 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -4,7 +4,57 @@ class UsersController < AdminController protect 'manage_environment_users', :environment + include UsersHelper + + def per_page + 10 + end + def index + @filter = params[:filter] + if @filter.blank? || @filter == 'all_users' + @filter = 'all_users' + scope = environment.people.no_templates(environment) + elsif @filter == 'admin_users' + scope = environment.people.no_templates(environment).admins + elsif @filter == 'activated_users' + scope = environment.people.no_templates(environment).activated + elsif @filter == 'deactivated_users' + scope = environment.people.no_templates(environment).deactivated + end + @q = params[:q] + if @q.blank? + @collection = scope.paginate(:per_page => per_page, :page => params[:npage]) + else + @collection = find_by_contents(:people, scope, @q, {:per_page => per_page, :page => params[:npage]})[:results] + end + end + + def set_admin_role + @person = environment.people.find(params[:id]) + environment.add_admin(@person) + redirect_to :action => :index, :q => params[:q], :filter => params[:filter] + end + + def reset_admin_role + @person = environment.people.find(params[:id]) + environment.remove_admin(@person) + redirect_to :action => :index, :q => params[:q], :filter => params[:filter] + end + + def activate + @person = environment.people.find(params[:id]) + @person.user.activate + redirect_to :action => :index, :q => params[:q], :filter => params[:filter] + end + + def deactivate + @person = environment.people.find(params[:id]) + @person.user.deactivate + redirect_to :action => :index, :q => params[:q], :filter => params[:filter] + end + + def download respond_to do |format| format.html format.xml do diff --git a/app/controllers/admin/xxx-environment_users_controller.rb b/app/controllers/admin/xxx-environment_users_controller.rb new file mode 100644 index 0000000..1302cf1 --- /dev/null +++ b/app/controllers/admin/xxx-environment_users_controller.rb @@ -0,0 +1,44 @@ +class EnvironmentUsersController < AdminController + + protect 'manage_environment_users', :environment + + def per_page + 10 + end + + def index + @q = params[:q] + if @q.blank? + @collection = environment.people.no_templates(environment).paginate( + :per_page => per_page, + :page => params[:npage] + ) + else + @collection = find_by_contents(:people, environment.people.no_templates(environment), @q, {:per_page => per_page, :page => params[:npage]})[:results] + end + end + + def set_admin_role + @person = environment.people.find(params[:id]) + environment.add_admin(@person) + redirect_to :action => :index, :q => params[:q] + end + + def reset_admin_role + @person = environment.people.find(params[:id]) + environment.remove_admin(@person) + redirect_to :action => :index, :q => params[:q] + end + + def activate + @person = environment.people.find(params[:id]) + @person.user.activate + redirect_to :action => :index, :q => params[:q] + end + + def deactivate + @person = environment.people.find(params[:id]) + @person.user.deactivate + redirect_to :action => :index, :q => params[:q] + end +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a72504f..94febd3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -79,7 +79,7 @@ class ApplicationController < ActionController::Base helper_method :current_person, :current_person - protected + # protected def setup_multitenancy Noosfero::MultiTenancy.setup!(request.host) diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 0000000..d5c0a89 --- /dev/null +++ b/app/helpers/users_helper.rb @@ -0,0 +1,25 @@ +module UsersHelper + + FILTER_TRANSLATION = { + 'all_users' => _('All users'), + 'admin_users' => _('Admin users'), + 'activated_users' => _('Activated users'), + 'deactivated_users' => _('Deativated users'), + } + + def filter_selector(filter, float = 'right') + options = options_for_select(FILTER_TRANSLATION.map {|key, name| [name, key]}, :selected => filter) + url_params = url_for(params.merge(:filter => 'FILTER')) + onchange = "document.location.href = '#{url_params}'.replace('FILTER', this.value)" + select_field = select_tag(:filter, options, :onchange => onchange) + content_tag('div', + content_tag('strong', _('Filter')) + ': ' + select_field, + :class => "environment-users-customize-search" + ) + end + + def filter_title(filter) + FILTER_TRANSLATION[filter] + end + +end diff --git a/app/models/person.rb b/app/models/person.rb index 8729394..7ee77df 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -75,6 +75,10 @@ class Person < Profile named_scope :abusers, :joins => :abuse_complaints, :conditions => ['tasks.status = 3'], :select => 'DISTINCT profiles.*' named_scope :non_abusers, :joins => "LEFT JOIN tasks ON profiles.id = tasks.requestor_id AND tasks.type='AbuseComplaint'", :conditions => ["tasks.status != 3 OR tasks.id is NULL"], :select => "DISTINCT profiles.*" + named_scope :admins, :joins => [:role_assignments => :role], :conditions => ['roles.key = ?', 'environment_administrator' ] + named_scope :activated, :joins => :user, :conditions => ['users.activation_code IS NULL AND users.activated_at IS NOT NULL'] + named_scope :deactivated, :joins => :user, :conditions => ['NOT (users.activation_code IS NULL AND users.activated_at IS NOT NULL)'] + after_destroy do |person| Friendship.find(:all, :conditions => { :friend_id => person.id}).each { |friendship| friendship.destroy } end diff --git a/app/views/environment_users/_environment_users_search_form.rhtml b/app/views/environment_users/_environment_users_search_form.rhtml deleted file mode 100644 index 79747a0..0000000 --- a/app/views/environment_users/_environment_users_search_form.rhtml +++ /dev/null @@ -1,8 +0,0 @@ -<% form_tag( { :controller => 'environment_users', :action => 'index' }, :method => 'get', :class => 'environment-users-search' ) do %> -
- - <%= text_field_tag 'q', @q, :title => _("Find users") %> - - <%= submit_button(:search, _('Search')) %> -
-<% end %> diff --git a/app/views/environment_users/_index_buttons.rhtml b/app/views/environment_users/_index_buttons.rhtml deleted file mode 100644 index fe3d97e..0000000 --- a/app/views/environment_users/_index_buttons.rhtml +++ /dev/null @@ -1,3 +0,0 @@ -<% button_bar do %> - <%= button :back, _('Back'), :controller => 'users' %> -<% end %> diff --git a/app/views/environment_users/_users_list.rhtml b/app/views/environment_users/_users_list.rhtml deleted file mode 100644 index f31f553..0000000 --- a/app/views/environment_users/_users_list.rhtml +++ /dev/null @@ -1,30 +0,0 @@ -<% title = _('All Users') %> - -

<%= title %>

- - - - - - <% @collection.each do |p| %> - - - - - <% end %> -
<%= _('Member') %><%= _('Actions') %>
<%= link_to_profile p.short_name, p.identifier, :title => p.name %> -
- <% if p.is_admin? %> - <%= button_without_text :'reset-admin-role', _('Reset admin role'), :action => 'reset_admin_role', :id => p, :q => @q %> - <% else %> - <%= button_without_text :'set-admin-role', _('Set admin role'), :action => 'set_admin_role', :id => p, :q => @q %> - <% end %> - <% if !p.user.activated? %> - <%= button_without_text :'activate-user', _('Activate user'), :action => 'activate', :id => p, :q => @q %> - <% else %> - <%= button_without_text :'deactivate-user', _('Deactivate user'), :action => 'deactivate', :id => p, :q => @q %> - <% end %> -
-
- -<%= pagination_links @collection, {:param_name => 'npage', :page_links => true} %> \ No newline at end of file diff --git a/app/views/environment_users/index.rhtml b/app/views/environment_users/index.rhtml deleted file mode 100644 index b6d15f1..0000000 --- a/app/views/environment_users/index.rhtml +++ /dev/null @@ -1,12 +0,0 @@ -

<%= _('Edit Users')%>

- -<%= render :partial => 'index_buttons' %> - -
- <%= render :partial => 'environment_users_search_form' %> -
-
- <%= render :partial => 'users_list' %> -
- -<%= render :partial => 'index_buttons' %> diff --git a/app/views/users/_index_buttons.rhtml b/app/views/users/_index_buttons.rhtml new file mode 100644 index 0000000..63add14 --- /dev/null +++ b/app/views/users/_index_buttons.rhtml @@ -0,0 +1,6 @@ +<% button_bar do %> + <%= button :'text-plain', _('User list as [CSV]'), :action => :download, :format => 'csv' %> + <%= button :'text-html', _('User list as [XML]'), :action => :download, :format => 'xml' %> + <%= button :send, _('Send e-mail to all users'), :action => 'send_mail' %> + <%= button :back, _('Back'), :controller => 'admin_panel' %> +<% end %> diff --git a/app/views/users/_users_list.rhtml b/app/views/users/_users_list.rhtml new file mode 100644 index 0000000..92ce21c --- /dev/null +++ b/app/views/users/_users_list.rhtml @@ -0,0 +1,33 @@ +
+
<%= filter_title(@filter) %>
+ <%= filter_selector(@filter) %> +
+
+ + + + + + + <% @collection.each do |p| %> + + + + + <% end %> +
<%= _('Member') %><%= _('Actions') %>
<%= link_to_profile p.short_name, p.identifier, :title => p.name %> +
+ <% if p.is_admin? %> + <%= button_without_text :'reset-admin-role', _('Reset admin role'), :action => 'reset_admin_role', :id => p, :q => @q, :filter => @filter %> + <% else %> + <%= button_without_text :'set-admin-role', _('Set admin role'), :action => 'set_admin_role', :id => p, :q => @q, :filter => @filter %> + <% end %> + <% if !p.user.activated? %> + <%= button_without_text :'activate-user', _('Activate user'), :action => 'activate', :id => p, :q => @q, :filter => @filter %> + <% else %> + <%= button_without_text :'deactivate-user', _('Deactivate user'), :action => 'deactivate', :id => p, :q => @q, :filter => @filter %> + <% end %> +
+
+ +<%= pagination_links @collection, {:param_name => 'npage', :page_links => true} %> \ No newline at end of file diff --git a/app/views/users/_users_search_form.rhtml b/app/views/users/_users_search_form.rhtml new file mode 100644 index 0000000..b4f6281 --- /dev/null +++ b/app/views/users/_users_search_form.rhtml @@ -0,0 +1,6 @@ +
+ + <%= text_field_tag 'q', @q, :title => _("Find users") %> + + <%= submit_button(:search, _('Search')) %> +
\ No newline at end of file diff --git a/app/views/users/index.rhtml b/app/views/users/index.rhtml index f41c280..d2f6406 100644 --- a/app/views/users/index.rhtml +++ b/app/views/users/index.rhtml @@ -1,19 +1,12 @@

<%= _('Manage users') %>

- - -<% button_bar do %> - <%= button :back, _('Back to admin panel'), :controller => 'admin_panel' %> +<% form_tag( { :action => 'index' }, :method => 'get', :class => 'users-search' ) do %> +
+ <%= render :partial => 'users_search_form' %> +
+
+ <%= render :partial => 'users_list' %> +
<% end %> + +<%= render :partial => 'index_buttons' %> \ No newline at end of file diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index f29b69f..66e41c7 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -4150,6 +4150,19 @@ h1#agenda-title { -webkit-border-radius: 5px; } +.environment-users-results-header { + font-size: 0.9em; + padding: 6px 0px 0px 0px; + margin:0 0 5px 0; + border-bottom: 2px dotted #999; + text-align: right; +} +#environment-users-filter-title { + font-weight: bold; + font-size: 130%; + line-height: 35px; + float: left; +} /* * * Profile search * * * * * * * */ diff --git a/test/functional/environment_users_controller_test.rb b/test/functional/environment_users_controller_test.rb deleted file mode 100644 index 0bb6c36..0000000 --- a/test/functional/environment_users_controller_test.rb +++ /dev/null @@ -1,103 +0,0 @@ -require File.dirname(__FILE__) + '/../test_helper' -require 'environment_users_controller' - -# Re-raise errors caught by the controller. -class EnvironmentUsersController; def rescue_action(e) raise e end; end - -class EnvironmentUsersControllerTest < ActionController::TestCase - - # all_fixtures - def setup - @controller = EnvironmentUsersController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - - admin_user = create_user_with_permission('adminuser', 'manage_environment_users', Environment.default) - login_as('adminuser') - end - - should 'not access without right permission' do - guest = create_user('guest') - login_as 'guest' - - get :index - assert_response 403 # forbidden - end - - should 'grant access with right permission' do - get :index - assert_response :success - end - - should 'blank search results include activated and deactivated users' do - deactivated = create_user('deactivated') - deactivated.activated_at = nil - deactivated.person.visible = false - deactivated.save! - get :index, :q => '' - assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /adminuser/}} - assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /deactivated/}} - end - - should 'blank search include all users' do - (1..5).each {|i| - u = create_user('user'+i.to_s) - } - get :index, :q => '' # blank search - assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /adminuser/}} - (1..5).each {|i| - u = 'user'+i.to_s - assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => u}} - } - end - - should 'search not include all users' do - (1..5).each {|i| - u = create_user('user'+i.to_s) - } - get :index, :q => 'er5' # search - assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /user5/}} - (1..4).each {|i| - u = 'user'+i.to_s - assert_no_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => u}} - } - end - - should 'set admin role' do - u = create_user() - assert_equal false, u.person.is_admin? - post :set_admin_role, :id => u.person.id, :q => '' - u.reload - assert u.person.is_admin? - end - - should 'reset admin role' do - u = create_user() - e = Environment.default - e.add_admin(u.person) - u.reload - assert u.person.is_admin? - post :reset_admin_role, :id => u.person.id, :q => '' - u.reload - assert_equal false, u.person.is_admin? - end - - should 'activate user' do - u = create_user() - assert_equal false, u.activated? - post :activate, :id => u.person.id, :q => '' - u.reload - assert u.activated? - end - - should 'deactivate user' do - u = create_user() - u.activated_at = Time.now.utc - u.activation_code = nil - u.person.visible = true - assert u.activated? - post :deactivate, :id => u.person.id, :q => '' - u.reload - assert_equal false, u.activated? - end -end diff --git a/test/functional/users_controller_test.rb b/test/functional/users_controller_test.rb index 4f08625..295151f 100644 --- a/test/functional/users_controller_test.rb +++ b/test/functional/users_controller_test.rb @@ -6,11 +6,13 @@ class UsersController; def rescue_action(e) raise e end; end class UsersControllerTest < ActionController::TestCase - all_fixtures def setup @controller = UsersController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new + + admin_user = create_user_with_permission('adminuser', 'manage_environment_users', Environment.default) + login_as('adminuser') end should 'not access without right permission' do @@ -28,11 +30,83 @@ class UsersControllerTest < ActionController::TestCase assert_response :success end + should 'blank search results include activated and deactivated users' do + deactivated = create_user('deactivated') + deactivated.activated_at = nil + deactivated.person.visible = false + deactivated.save! + get :index, :q => '' + assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /adminuser/}} + assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /deactivated/}} + end + + should 'blank search include all users' do + (1..5).each {|i| + u = create_user('user'+i.to_s) + } + get :index, :q => '' # blank search + assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /adminuser/}} + (1..5).each {|i| + u = 'user'+i.to_s + assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => u}} + } + end + + should 'search not include all users' do + (1..5).each {|i| + u = create_user('user'+i.to_s) + } + get :index, :q => 'er5' # search + assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /user5/}} + (1..4).each {|i| + u = 'user'+i.to_s + assert_no_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => u}} + } + end + + should 'set admin role' do + u = create_user() + assert_equal false, u.person.is_admin? + post :set_admin_role, :id => u.person.id, :q => '' + u.reload + assert u.person.is_admin? + end + + should 'reset admin role' do + u = create_user() + e = Environment.default + e.add_admin(u.person) + u.reload + assert u.person.is_admin? + post :reset_admin_role, :id => u.person.id, :q => '' + u.reload + assert_equal false, u.person.is_admin? + end + + should 'activate user' do + u = create_user() + assert_equal false, u.activated? + post :activate, :id => u.person.id, :q => '' + u.reload + assert u.activated? + end + + should 'deactivate user' do + u = create_user() + u.activated_at = Time.now.utc + u.activation_code = nil + u.person.visible = true + assert u.activated? + post :deactivate, :id => u.person.id, :q => '' + u.reload + assert_equal false, u.activated? + end + should 'response as XML to export users' do admin_user = create_user_with_permission('admin_user', 'manage_environment_users', Environment.default) login_as('admin_user') - get :index, :format => 'xml' + get :download, :format => 'xml' assert_equal 'text/xml', @response.content_type end @@ -40,7 +114,7 @@ class UsersControllerTest < ActionController::TestCase admin_user = create_user_with_permission('admin_user', 'manage_environment_users', Environment.default) login_as('admin_user') - get :index, :format => 'csv' + get :download, :format => 'csv' assert_equal 'text/csv', @response.content_type assert_equal 'name;email', @response.body.split("\n")[0] end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 610a81f..0592658 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -1335,4 +1335,56 @@ class PersonTest < ActiveSupport::TestCase assert_includes non_abusers, not_abuser end + should 'admins named_scope return persons who are admin users' do + Person.delete_all + e = Environment.default + admins = [] + (1..5).each {|i| + u = create_user('user'+i.to_s) + e.add_admin(u.person) + admins << u.person + } + (6..10).each {|i| + u = create_user('user'+i.to_s) + } + assert_equal admins, Person.admins + end + + should 'activated named_scope return persons who are activated users' do + Person.delete_all + e = Environment.default + activated = [] + (1..5).each {|i| + u = create_user('user'+i.to_s) + u.activated_at = Time.now.utc + u.activation_code = nil + u.save! + activated << u.person + } + (6..10).each {|i| + u = create_user('user'+i.to_s) + u.activated_at = nil + u.save! + } + assert_equal activated, Person.activated + end + + should 'deactivated named_scope return persons who are deactivated users' do + Person.delete_all + e = Environment.default + deactivated = [] + (1..5).each {|i| + u = create_user('user'+i.to_s) + u.activated_at = nil + u.save! + deactivated << u.person + } + (6..10).each {|i| + u = create_user('user'+i.to_s) + u.activated_at = Time.now.utc + u.activation_code = nil + u.save! + } + assert_equal deactivated, Person.deactivated + end end -- libgit2 0.21.2