diff --git a/app/controllers/admin/admin_panel_controller.rb b/app/controllers/admin/admin_panel_controller.rb index 59c87a7..6c2b103 100644 --- a/app/controllers/admin/admin_panel_controller.rb +++ b/app/controllers/admin/admin_panel_controller.rb @@ -71,22 +71,4 @@ class AdminPanelController < AdminController end end end - - def manage_organizations_status - scope = environment.organizations - @filter = params[:filter] || 'any' - @title = "Organization profiles" - @title = @title+" - "+@filter if @filter != 'any' - - if @filter == 'enabled' - scope = scope.visible - elsif @filter == 'disabled' - scope = scope.disabled - end - - scope = scope.order('name ASC') - - @q = params[:q] - @collection = find_by_contents(:organizations, environment, scope, @q, {:per_page => 10, :page => params[:npage]})[:results] - end end diff --git a/app/controllers/admin/organizations_controller.rb b/app/controllers/admin/organizations_controller.rb new file mode 100644 index 0000000..25f413c --- /dev/null +++ b/app/controllers/admin/organizations_controller.rb @@ -0,0 +1,66 @@ +class OrganizationsController < AdminController + + protect 'manage_environment_organizations', :environment + + def index + @filter = params[:filter] || 'any' + @title = _('Organization profiles') + @type = params[:type] || "any" + @types_filter = [[_('All'), 'any'], [_('Community'), 'Community'], [_('Enterprise'), 'Enterprise']] + @types_filter = @types_filter | @plugins.dispatch(:organization_types_filter_options) + + scope = @plugins.dispatch_first(:filter_manage_organization_scope, @type) + if scope.blank? + scope = environment.organizations + scope = scope.where(:type => @type) if @type != 'any' + end + + if @filter == 'enabled' + scope = scope.visible + elsif @filter == 'disabled' + scope = scope.disabled + end + + scope = scope.order('name ASC') + + @q = params[:q] + @collection = find_by_contents(:organizations, environment, scope, @q, {:per_page => per_page, :page => params[:npage]})[:results] + end + + def activate + organization = environment.organizations.find(params[:id]) + if organization.enable + render :text => (_('%s enabled') % organization.name).to_json + else + render :text => (_('%s could not be enabled') % organization.name).to_json + end + end + + def deactivate + organization = environment.organizations.find(params[:id]) + if organization.disable + render :text => (_('%s disabled') % organization.name).to_json + else + render :text => (_('%s could not be disable') % organization.name).to_json + end + end + + def destroy + if request.post? + organization = environment.organizations.find(params[:id]) + if organization && organization.destroy + render :text => (_('%s removed') % organization.name).to_json + else + render :text => (_('%s could not be removed') % organization.name).to_json + end + else + render :nothing => true + end + end + + private + + def per_page + 10 + end +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index cf567d2..befcecc 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -14,7 +14,7 @@ module UsersHelper select_field = select_tag(:filter, options, :onchange => onchange) content_tag('div', content_tag('strong', _('Filter')) + ': ' + select_field, - :class => "environment-users-customize-search" + :class => "environment-profiles-customize-search" ) end diff --git a/app/models/environment.rb b/app/models/environment.rb index e101216..050708a 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -29,6 +29,7 @@ class Environment < ActiveRecord::Base 'manage_environment_roles' => N_('Manage environment roles'), 'manage_environment_validators' => N_('Manage environment validators'), 'manage_environment_users' => N_('Manage environment users'), + 'manage_environment_organizations' => N_('Manage environment organizations'), 'manage_environment_templates' => N_('Manage environment templates'), 'manage_environment_licenses' => N_('Manage environment licenses'), 'manage_environment_trusted_sites' => N_('Manage environment trusted sites'), diff --git a/app/models/profile.rb b/app/models/profile.rb index 9319918..3220fd2 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -957,11 +957,19 @@ private :generate_url, :url_options self.save end + def disabled? + !visible + end + def enable self.visible = true self.save end + def enabled? + visible + end + def control_panel_settings_button {:title => _('Edit Profile'), :icon => 'edit-profile'} end diff --git a/app/views/admin_panel/index.html.erb b/app/views/admin_panel/index.html.erb index f5980be..eab20d5 100644 --- a/app/views/admin_panel/index.html.erb +++ b/app/views/admin_panel/index.html.erb @@ -18,9 +18,9 @@ + -
<%= link_to _('User roles'), :controller => 'role' %>
<%= link_to _('Users'), :controller => 'users' %>
<%= link_to _('Organizations'), :controller => 'organizations' %>
<%= link_to _('Profile templates'), :controller => 'templates' %>
<%= link_to _('Fields'), :controller => 'features', :action => 'manage_fields' %>
<%= link_to _('Manage organizations status'), :action => 'manage_organizations_status' %>
diff --git a/app/views/admin_panel/manage_organizations_status.html.erb b/app/views/admin_panel/manage_organizations_status.html.erb deleted file mode 100644 index 3a708f1..0000000 --- a/app/views/admin_panel/manage_organizations_status.html.erb +++ /dev/null @@ -1,69 +0,0 @@ -

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

- -<%= form_tag( { :action => 'manage_organizations_status' }, :method => 'get', :class => 'users-search' ) do %> - -
- - <%= text_field_tag 'q', @q, :title => _("Find profiles"), :style=>"width:85%" %> - - - <%= submit_button(:search, _('Search')) %> -
- -
-
<%= @title %>
- -
- <%= _("Filter by: ") %> - - -
-
-
- - - - - - - - - - - - - <% @collection.each do |p| %> - - - - - - <% end %> -
<%= _('Member') %><%= _('Actions') %>
<%= link_to_profile p.short_name, p.identifier, :title => p.name %> -
- <% if p.visible %> - <%= button_without_text :'deactivate-user', _('Deactivate'), {:controller => "profile_editor", :action => 'deactivate_profile', :profile => p.identifier, :id => p.id}, :confirm => _("Do you want to deactivate this profile ?") %> - <% else %> - <%= button_without_text :'activate-user', _('Activate'), {:controller => "profile_editor", :action => 'activate_profile', :profile => p.identifier, :id => p.id}, :confirm => _("Do you want to activate this profile ?") %> - <% end %> - <%= button_without_text :'delete', _('Remove'), {:controller => "profile_editor", :action => 'destroy_profile', :profile => p.identifier, :id => p.id, :return_to => "/admin/admin_panel/manage_organizations_status"}, :method => :post, :confirm => _("Do you want to deactivate this profile ?") %> -
-
- -<% end %> - -<%= pagination_links @collection, {:param_name => 'npage', :page_links => true} %> - -<% button_bar do %> - <%= button :back, _('Back'), :controller => 'admin_panel' %> -<% end %> - - \ No newline at end of file diff --git a/app/views/organizations/_results.html.erb b/app/views/organizations/_results.html.erb new file mode 100644 index 0000000..03bd40c --- /dev/null +++ b/app/views/organizations/_results.html.erb @@ -0,0 +1,41 @@ +
+ + + + + + + + + + + + + + <% @collection.each do |p| %> + + + + + + + + <% end %> +
<%= _('Profile') %><%= _('Actions') %><%= _('Type') %> + + <%= select_tag(:type, options_for_select(@types_filter, @type)) %> +
<%= link_to_profile p.short_name, p.identifier, :title => p.name %> +
+ <% if p.visible %> + <%= button_without_text :'deactivate-user', _('Deactivate'), {:action => 'deactivate', :id => p.id}, :class => 'action', 'data-confirm' => _("Do you want to deactivate this organization?") %> + <% else %> + <%= button_without_text :'activate-user', _('Activate'), {:action => 'activate', :id => p.id}, :class => 'action', 'data-confirm' => _("Do you want to activate this organization?") %> + <% end %> + <%= button_without_text :'delete', _('Remove'), {:action => 'destroy', :id => p.id}, :class => 'action', 'data-method' => :post, 'data-confirm' => _("Do you want to destroy this organization?") %> +
+
<%= _("#{p.type}") %>
+ +
+ <%= pagination_links @collection, {:param_name => 'npage', :page_links => true} %> +
+
diff --git a/app/views/organizations/index.html.erb b/app/views/organizations/index.html.erb new file mode 100644 index 0000000..0370a3d --- /dev/null +++ b/app/views/organizations/index.html.erb @@ -0,0 +1,30 @@ +

<%= _('Organizations') %>

+ +<%= form_tag( { :action => 'index' }, :method => 'get', :id => 'manage-profiles' ) do %> + +
+ + <%= text_field_tag 'q', @q, :title => _('Find organizations'), :style=>"width:85%" %> + + + <%= submit_button(:search, _('Search')) %> +
+ +
+
<%= @title %>
+ +
+ <%= _("Filter by: ") %> + <%= select_tag(:filter, options_for_select([[_('Any'), 'any'],[_('Disabled'), "disabled"], [_('Enabled') , "enabled"]], @filter)) %> +
+
+
+ + <%= render :partial => 'results' %> + + <% button_bar do %> + <%= button :back, _('Back'), :controller => 'admin_panel' %> + <% end %> +<% end %> + +<%= javascript_include_tag 'manage-organizations' %> diff --git a/app/views/organizations/index.js.erb b/app/views/organizations/index.js.erb new file mode 120000 index 0000000..be47f81 --- /dev/null +++ b/app/views/organizations/index.js.erb @@ -0,0 +1 @@ +../../views/shared/admin/profiles/index.js.rb \ No newline at end of file diff --git a/app/views/shared/admin/profiles/index.js.rb b/app/views/shared/admin/profiles/index.js.rb new file mode 100644 index 0000000..c7e295f --- /dev/null +++ b/app/views/shared/admin/profiles/index.js.rb @@ -0,0 +1 @@ +jQuery('#manage-profiles .results').replaceWith('<%= escape_javascript(render 'results') %>'); diff --git a/app/views/users/_users_list.html.erb b/app/views/users/_users_list.html.erb index 97bf13f..8b219aa 100644 --- a/app/views/users/_users_list.html.erb +++ b/app/views/users/_users_list.html.erb @@ -1,5 +1,5 @@ -
-
<%= users_filter_title(@filter) %>
+
+
<%= users_filter_title(@filter) %>
<%= filter_selector(@filter) %>
diff --git a/lib/noosfero/plugin.rb b/lib/noosfero/plugin.rb index b6d51a9..8625909 100644 --- a/lib/noosfero/plugin.rb +++ b/lib/noosfero/plugin.rb @@ -299,6 +299,18 @@ class Noosfero::Plugin nil end + # -> Filters the types of organizations that are shown on manage organizations + # returns a scope filtered by the specified type + def filter_manage_organization_scope type + nil + end + + # -> Add new options for manage organization filters + # returns an array of new options + # i.e [[_('Type'), 'type'], [_('Type2'), 'type2']] + def organization_types_filter_options + nil + end # -> Adds content to profile editor info and settings # returns = lambda block that creates html code or raw rhtml/html.erb def profile_editor_extras diff --git a/public/javascripts/manage-organizations.js b/public/javascripts/manage-organizations.js new file mode 100644 index 0000000..18882eb --- /dev/null +++ b/public/javascripts/manage-organizations.js @@ -0,0 +1,49 @@ +(function($) { + // Pagination + $('#manage-profiles').on('click', '.pagination a', function () { + $.ajax({ + url: this.href, + beforeSend: function(){$('#manage-profiles .results').addClass('fetching')}, + complete: function() {$('#manage-profiles .results').removeClass('fetching')}, + dataType: 'script' + }) + return false; + }); + + // Actions + $('#manage-profiles').on('click', '.action', function () { + if(confirm($(this).data('confirm'))) { + $.ajax({ + url: this.href, + method: $(this).data('method') || 'get', + dataType: 'script', + success: function(data){ + if(data) + display_notice(JSON.parse(data)); + }, + error: function(xhr, textStatus, message){ + display_notice(message); + } + }); + $('#manage-profiles').submit(); + } + return false; + }); + + // Sorting and Views + $('#manage-profiles select').live('change', function(){ + $('#manage-profiles').submit(); + }); + + // Form Ajax submission + $('#manage-profiles').submit(function () { + $.ajax({ + url: this.action, + data: $(this).serialize(), + beforeSend: function(){$('#manage-profiles .results').addClass('fetching')}, + complete: function() {$('#manage-profiles .results').removeClass('fetching')}, + dataType: 'script' + }) + return false; + }); +})(jQuery); diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index ee90807..ad00fbf 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -4763,7 +4763,7 @@ h1#agenda-title { float: right; } -#environment-users-search form { +#environment-profiles-search form { padding: 10px; margin-bottom: 15px; background-color: #E6E6E6; @@ -4771,14 +4771,14 @@ h1#agenda-title { -webkit-border-radius: 5px; } -.environment-users-results-header { +.environment-profiles-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 { +#environment-profiles-filter-title { font-weight: bold; font-size: 130%; line-height: 35px; diff --git a/test/factories.rb b/test/factories.rb index dade9a8..a347ed1 100644 --- a/test/factories.rb +++ b/test/factories.rb @@ -65,7 +65,7 @@ module Noosfero::Factory ###### old stuff to be rearranged def create_admin_user(env) admin_user = User.find_by_login('adminuser') || create_user('adminuser', :email => 'adminuser@noosfero.org', :password => 'adminuser', :password_confirmation => 'adminuser', :environment => env) - admin_role = Role.find_by_name('admin_role') || Role.create!(:name => 'admin_role', :permissions => ['view_environment_admin_panel','edit_environment_features', 'edit_environment_design', 'manage_environment_categories', 'manage_environment_roles', 'manage_environment_trusted_sites', 'manage_environment_validators', 'manage_environment_users', 'manage_environment_templates', 'manage_environment_licenses', 'edit_appearance']) + admin_role = Role.find_by_name('admin_role') || Role.create!(:name => 'admin_role', :permissions => ['view_environment_admin_panel','edit_environment_features', 'edit_environment_design', 'manage_environment_categories', 'manage_environment_roles', 'manage_environment_trusted_sites', 'manage_environment_validators', 'manage_environment_users', 'manage_environment_organizations', 'manage_environment_templates', 'manage_environment_licenses', 'edit_appearance']) create(RoleAssignment, :accessor => admin_user.person, :role => admin_role, :resource => env) unless admin_user.person.role_assignments.map{|ra|[ra.role, ra.accessor, ra.resource]}.include?([admin_role, admin_user, env]) admin_user.login end diff --git a/test/fixtures/roles.yml b/test/fixtures/roles.yml index f3caedd..90bc9f4 100644 --- a/test/fixtures/roles.yml +++ b/test/fixtures/roles.yml @@ -35,6 +35,7 @@ four: - moderate_comments - perform_task - manage_environment_users + - manage_environment_organizations - manage_environment_templates - manage_environment_licenses profile_admin: @@ -94,6 +95,7 @@ environment_administrator: - manage_environment_validators - moderate_comments - manage_environment_users + - manage_environment_organizations - edit_profile - destroy_profile - manage_environment_templates diff --git a/test/functional/admin_panel_controller_test.rb b/test/functional/admin_panel_controller_test.rb index a451e48..d25dca7 100644 --- a/test/functional/admin_panel_controller_test.rb +++ b/test/functional/admin_panel_controller_test.rb @@ -17,12 +17,12 @@ class AdminPanelControllerTest < ActionController::TestCase should 'manage the correct environment' do current = fast_create(Environment, :name => 'test environment', :is_default => false) current.domains.create!(:name => 'example.com') - + @request.expects(:host).returns('example.com').at_least_once get :index assert_equal current, assigns(:environment) end - + should 'link to site_info editing page' do get :index assert_tag :tag => 'a', :attributes => { :href => '/admin/admin_panel/site_info' } @@ -379,36 +379,4 @@ class AdminPanelControllerTest < ActionController::TestCase assert_equal body, Environment.default.signup_welcome_screen_body assert !Environment.default.signup_welcome_screen_body.blank? end - - should 'show list to deactivate organizations' do - enabled_community = fast_create(Community, :environment_id => Environment.default, :name=>"enabled community") - disabled_community = fast_create(Community, :environment_id => Environment.default, :name=>"disabled community") - user = create_user('user') - - disabled_community.disable - - Environment.default.add_admin user.person - login_as('user') - - get :manage_organizations_status, :filter=>"enabled" - assert_match(/Organization profiles - enabled/, @response.body) - assert_match(/enabled community/, @response.body) - assert_not_match(/disabled community/, @response.body) - end - - should 'show list to activate organizations' do - enabled_community = fast_create(Community, :environment_id => Environment.default, :name=>"enabled community") - disabled_community = fast_create(Community, :environment_id => Environment.default, :name=>"disabled community") - user = create_user('user') - - disabled_community.disable - - Environment.default.add_admin user.person - login_as('user') - - get :manage_organizations_status, :filter=>"disabled" - assert_match(/Organization profiles - disabled/, @response.body) - assert_not_match(/enabled community/, @response.body) - assert_match(/disabled community/, @response.body) - end end diff --git a/test/functional/organizations_controller_test.rb b/test/functional/organizations_controller_test.rb new file mode 100644 index 0000000..928fe8b --- /dev/null +++ b/test/functional/organizations_controller_test.rb @@ -0,0 +1,116 @@ +require_relative "../test_helper" +require 'organizations_controller' + +# Re-raise errors caught by the controller. +class OrganizationsController; def rescue_action(e) raise e end; end + +class OrganizationsControllerTest < ActionController::TestCase + + def setup + @controller = OrganizationsController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + Environment.destroy_all + @environment = fast_create(Environment, :is_default => true) + + admin_user = create_user_with_permission('adminuser', 'manage_environment_organizations', environment) + login_as('adminuser') + end + + attr_accessor :environment + + should 'not access without right permission' do + 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 'show list to deactivate organizations' do + enabled_community = fast_create(Community, :environment_id => Environment.default, :name=>"enabled community") + disabled_community = fast_create(Community, :environment_id => Environment.default, :name=>"disabled community") + disabled_community.disable + + get :index, :filter => 'enabled' + + assert_match(/enabled community/, @response.body) + assert_not_match(/disabled community/, @response.body) + end + + should 'show list to activate organizations' do + enabled_community = fast_create(Community, :environment_id => Environment.default, :name=>"enabled community") + disabled_community = fast_create(Community, :environment_id => Environment.default, :name=>"disabled community") + disabled_community.disable + + get :index, :filter => 'disabled' + + assert_not_match(/enabled community/, @response.body) + assert_match(/disabled community/, @response.body) + end + + should 'show list only of enterprises' do + community = fast_create(Community, :environment_id => Environment.default, :name=>"Community Test") + enterprise = fast_create(Enterprise, :environment_id => Environment.default, :name=>"Enterprise Test") + + get :index, :type => 'Enterprise' + + assert_match(/Enterprise Test/, @response.body) + assert_not_match(/Community Test/, @response.body) + end + + should 'show list only of communities' do + community = fast_create(Community, :environment_id => Environment.default, :name=>"Community Test") + enterprise = fast_create(Enterprise, :environment_id => Environment.default, :name=>"Enterprise Test") + + get :index, :type => 'Community' + + assert_not_match(/Enterprise Test/, @response.body) + assert_match(/Community Test/, @response.body) + end + + should 'show list all organizations' do + community = fast_create(Community, :environment_id => Environment.default, :name=>"Community Test") + enterprise = fast_create(Enterprise, :environment_id => Environment.default, :name=>"Enterprise Test") + + get :index, :type => 'any' + + assert_match(/Enterprise Test/, @response.body) + assert_match(/Community Test/, @response.body) + end + + should 'activate organization profile' do + organization = fast_create(Organization, :visible => false, :environment_id => environment.id) + assert organization.disabled? + + get :activate, {:id => organization.id} + organization.reload + + assert organization.enabled? + end + + should 'deactivate organization profile' do + organization = fast_create(Organization, :visible => true, :environment_id => environment.id) + assert organization.enabled? + + get :deactivate, {:id => organization.id} + organization.reload + + assert organization.disabled? + end + + should 'destroy organization profile' do + organization = fast_create(Organization, :environment_id => environment.id) + + post :destroy, {:id => organization.id} + + assert_raise ActiveRecord::RecordNotFound do + organization.reload + end + end +end diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb index 69cc7f8..3ba7b76 100644 --- a/test/functional/profile_editor_controller_test.rb +++ b/test/functional/profile_editor_controller_test.rb @@ -1152,57 +1152,4 @@ class ProfileEditorControllerTest < ActionController::TestCase get :index, :profile => user.identifier assert_tag :tag => 'div', :descendant => { :tag => 'a', :content => 'Edit Header and Footer' } end - - should 'deactivate organization profile' do - @request.env['HTTP_REFERER'] = 'http://localhost:3000/admin/admin_panel/manage_organizations_status' - user = create_user('user').person - Environment.default.add_admin user - login_as('user') - - community = fast_create(Community) - assert_equal true, community.enable - - get :index, :profile => community.identifier - get :deactivate_profile, {:profile => community.identifier, :id => community.id} - assert_equal @request.session[:notice], "The profile '#{community.name}' was deactivated." - end - - should 'activate organization profile' do - @request.env['HTTP_REFERER'] = 'http://localhost:3000/admin/admin_panel/manage_organizations_status' - user = create_user('user').person - Environment.default.add_admin user - login_as('user') - - community = fast_create(Community) - assert_equal true, community.disable - - get :index, :profile => community.identifier - get :activate_profile, {:profile => community.identifier, :id => community.id} - assert_equal @request.session[:notice], "The profile '#{community.name}' was activated." - end - - should 'not deactivate organization profile if user is not an admin' do - @request.env['HTTP_REFERER'] = 'http://localhost:3000/admin/admin_panel/manage_organizations_status' - user = create_user('user').person - login_as('user') - - community = fast_create(Community) - get :index, :profile => community.identifier - get :deactivate_profile, {:profile => community.identifier, :id => community.id} - assert_not_equal @request.session[:notice], "The profile '#{community.name}' was disabled." - end - - should 'destroy organization profile' do - @request.env['HTTP_REFERER'] = 'http://localhost:3000/admin/admin_panel/manage_organizations_status' - user = create_user('user').person - Environment.default.add_admin user - login_as('user') - - community = fast_create(Community) - assert_equal true, community.enable - - get :index, :profile => community.identifier - post :destroy_profile, {:profile => community.identifier, :id => community.id} - assert_equal @request.session[:notice], "The profile was deleted." - end end -- libgit2 0.21.2