From 0df7235cc80b31ad95faacfcf50fd60f5de14a43 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Tue, 23 May 2017 22:47:37 +0200 Subject: [PATCH 1/7] Add feature spec for new Administrators management in Admin area --- spec/features/admin/administrators_spec.rb | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 spec/features/admin/administrators_spec.rb diff --git a/spec/features/admin/administrators_spec.rb b/spec/features/admin/administrators_spec.rb new file mode 100644 index 000000000..5c1af0036 --- /dev/null +++ b/spec/features/admin/administrators_spec.rb @@ -0,0 +1,37 @@ +require 'rails_helper' + +feature 'Admin administrators' do + background do + @admin = create(:administrator) + @user = create(:user, username: 'Jose Luis Balbin') + @administrator = create(:administrator) + login_as(@admin.user) + visit admin_administrators_path + end + + scenario 'Index' do + expect(page).to have_content @administrator.name + expect(page).to have_content @administrator.email + expect(page).to_not have_content @user.name + end + + scenario 'Create Administrator', :js do + fill_in 'email', with: @user.email + click_button 'Search' + + expect(page).to have_content @user.name + click_link 'Add' + within("#administrators") do + expect(page).to have_content @user.name + end + end + + scenario 'Delete Administrator' do + find(:xpath, "//tr[contains(.,'#{@administrator.name}')]/td/a", text: 'Delete').click + + within("#administrators") do + expect(page).to_not have_content @administrator.name + end + end +end + From 9506855a448d391802dc76cf9a47e7a20affc27b Mon Sep 17 00:00:00 2001 From: Bertocq Date: Tue, 23 May 2017 22:49:44 +0200 Subject: [PATCH 2/7] Add Admin Administrator route, controller and manage ability for admin users --- .../admin/administrators_controller.rb | 32 +++++++++++++++++++ app/helpers/admin_helper.rb | 2 +- app/models/abilities/administrator.rb | 1 + config/routes.rb | 4 +++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 app/controllers/admin/administrators_controller.rb diff --git a/app/controllers/admin/administrators_controller.rb b/app/controllers/admin/administrators_controller.rb new file mode 100644 index 000000000..35325c5aa --- /dev/null +++ b/app/controllers/admin/administrators_controller.rb @@ -0,0 +1,32 @@ +class Admin::AdministratorsController < Admin::BaseController + load_and_authorize_resource + + def index + @administrators = @administrators.page(params[:page]) + end + + def search + @user = User.find_by(email: params[:email]) + + respond_to do |format| + if @user + @administrator = Administrator.find_or_initialize_by(user: @user) + format.js + else + format.js { render "user_not_found" } + end + end + end + + def create + @administrator.user_id = params[:user_id] + @administrator.save + + redirect_to admin_administrators_path + end + + def destroy + @administrator.destroy + redirect_to admin_administrators_path + end +end diff --git a/app/helpers/admin_helper.rb b/app/helpers/admin_helper.rb index 3f0d4db4b..8ca439a1c 100644 --- a/app/helpers/admin_helper.rb +++ b/app/helpers/admin_helper.rb @@ -29,7 +29,7 @@ module AdminHelper end def menu_profiles? - ["organizations", "officials", "moderators", "valuators", "managers"].include? controller_name + ["administrators", "organizations", "officials", "moderators", "valuators", "managers"].include? controller_name end def menu_banners? diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index 8df52e6eb..f2fe7aed9 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -34,6 +34,7 @@ module Abilities can :comment_as_administrator, [Debate, Comment, Proposal, Poll::Question, Budget::Investment] + can [:search, :create, :index, :destroy], ::Administrator can [:search, :create, :index, :destroy], ::Moderator can [:search, :create, :index, :summary], ::Valuator can [:search, :create, :index, :destroy], ::Manager diff --git a/config/routes.rb b/config/routes.rb index 4007595db..cdb1ccb2b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -207,6 +207,10 @@ Rails.application.routes.draw do get :search, on: :collection end + resources :administrators, only: [:index, :create, :destroy] do + get :search, on: :collection + end + scope module: :poll do resources :polls do get :search_questions, on: :member From 312e22956961614e392e245295b61c88327d1832 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Tue, 23 May 2017 22:50:17 +0200 Subject: [PATCH 3/7] Add Administrator management views and en/es translations --- app/views/admin/_menu.html.erb | 4 ++ .../administrators/_administrator.html.erb | 26 +++++++++++ app/views/admin/administrators/index.html.erb | 46 +++++++++++++++++++ app/views/admin/administrators/search.js.erb | 1 + .../administrators/user_not_found.js.erb | 1 + config/locales/admin.en.yml | 10 ++++ config/locales/admin.es.yml | 10 ++++ 7 files changed, 98 insertions(+) create mode 100644 app/views/admin/administrators/_administrator.html.erb create mode 100644 app/views/admin/administrators/index.html.erb create mode 100644 app/views/admin/administrators/search.js.erb create mode 100644 app/views/admin/administrators/user_not_found.js.erb diff --git a/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb index 0b9929f46..4e49cb7ed 100644 --- a/app/views/admin/_menu.html.erb +++ b/app/views/admin/_menu.html.erb @@ -95,6 +95,10 @@ <%= t("admin.menu.title_profiles") %>
    > +
  • > + <%= link_to t("admin.menu.administrators"), admin_administrators_path %> +
  • +
  • > <%= link_to t("admin.menu.organizations"), admin_organizations_path %>
  • diff --git a/app/views/admin/administrators/_administrator.html.erb b/app/views/admin/administrators/_administrator.html.erb new file mode 100644 index 000000000..d948e1eed --- /dev/null +++ b/app/views/admin/administrators/_administrator.html.erb @@ -0,0 +1,26 @@ +
    + + + + + + + + +
    + <%= administrator.name %> + + <%= administrator.email %> + + <% if administrator.persisted? %> + <%= link_to t('admin.administrators.administrator.delete'), + admin_administrator_path(administrator), + method: :delete, + class: "button hollow alert" %> + <% else %> + <%= link_to t('admin.administrators.administrator.add'),{ controller: "admin/administrators", action: :create, user_id: administrator.user_id }, + method: :post, + class: "button success" %> + <% end %> +
    +
    diff --git a/app/views/admin/administrators/index.html.erb b/app/views/admin/administrators/index.html.erb new file mode 100644 index 000000000..1cfbf5079 --- /dev/null +++ b/app/views/admin/administrators/index.html.erb @@ -0,0 +1,46 @@ +

    <%= t("admin.administrators.index.title") %>

    + +
    + <%= form_tag search_admin_administrators_path, method: :get, remote: true do %> +
    + <%= text_field_tag :email, '', placeholder: t('admin.administrators.search.email_placeholder') %> +
    +
    + <%= submit_tag t('admin.administrators.search.search'), class: 'button' %> +
    + <% end %> +
    + +
    + +

    <%= page_entries_info @administrators %>

    + + + <% @administrators.each do |administrator| %> + + + + + + <% end %> +
    + <%= administrator.name %> + + <%= administrator.email %> + + <% if administrator.persisted? %> + <%= link_to t('admin.administrators.administrator.delete'), + admin_administrator_path(administrator), + method: :delete, + class: "button hollow alert" + %> + <% else %> + <%= link_to t('admin.administrators.administrator.add'), + { controller: "admin/administrators", action: :create, + user_id: administrator.user_id }, + method: :post, + class: "button success" %> + <% end %> +
    + +<%= paginate @administrators %> diff --git a/app/views/admin/administrators/search.js.erb b/app/views/admin/administrators/search.js.erb new file mode 100644 index 000000000..1c30d9595 --- /dev/null +++ b/app/views/admin/administrators/search.js.erb @@ -0,0 +1 @@ +$("#search-result").html("<%= j render 'administrator', administrator: @administrator %>"); diff --git a/app/views/admin/administrators/user_not_found.js.erb b/app/views/admin/administrators/user_not_found.js.erb new file mode 100644 index 000000000..ba707fc9d --- /dev/null +++ b/app/views/admin/administrators/user_not_found.js.erb @@ -0,0 +1 @@ +$("#search-result").html("
    <%= j t('admin.administrators.search.user_not_found') %>
    "); diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 1d1bf9cb0..612b4db59 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -226,6 +226,16 @@ en: title_profiles: Profiles title_banners: Banners title_site_customization: Site customization + administrators: + index: + title: Administrators + moderator: + add: Add + delete: Delete + search: + email_placeholder: Search user by email + search: Search + user_not_found: User not found moderators: index: title: Moderators diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index b42843d70..da56e101c 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -181,6 +181,16 @@ es: with_confirmed_hide: Confirmados without_confirmed_hide: Pendientes title: Debates ocultos + administrators: + index: + title: Administradores + manager: + add: Añadir como Administrador + delete: Borrar + search: + email_placeholder: Buscar usuario por email + search: Buscar + user_not_found: Usuario no encontrado managers: index: title: Gestores From 115feb20aef78b347393766c0bb1ada3c17fb9e2 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Tue, 23 May 2017 23:33:50 +0200 Subject: [PATCH 4/7] Fix admin translations for administrators management panel actions --- config/locales/admin.en.yml | 2 +- config/locales/admin.es.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 612b4db59..be415ad7f 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -229,7 +229,7 @@ en: administrators: index: title: Administrators - moderator: + administrator: add: Add delete: Delete search: diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index da56e101c..79e55ccad 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -184,7 +184,7 @@ es: administrators: index: title: Administradores - manager: + administrator: add: Añadir como Administrador delete: Borrar search: From 7cc1745a29ada9460672d06f81db2dcae4db7cff Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 24 May 2017 00:00:44 +0200 Subject: [PATCH 5/7] Add missing Admin menu administrator translation --- config/locales/admin.en.yml | 1 + config/locales/admin.es.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index be415ad7f..2c39dc283 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -203,6 +203,7 @@ en: hidden_debates: Hidden debates hidden_proposals: Hidden proposals hidden_users: Hidden users + administrators: Administrators managers: Managers moderators: Moderators valuators: Valuators diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 79e55ccad..f569b84a0 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -214,6 +214,7 @@ es: hidden_proposals: Propuestas ocultas hidden_users: Usuarios bloqueados managers: Gestores + administrators: Administradores moderators: Moderadores valuators: Evaluadores poll_officers: Presidentes de mesa From e9ea72113b30968e46225315e5e42ac97caea46b Mon Sep 17 00:00:00 2001 From: Bertocq Date: Thu, 25 May 2017 11:28:36 +0200 Subject: [PATCH 6/7] Add spec scenario for current user trying to remove himself from administrators list --- spec/features/admin/administrators_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/features/admin/administrators_spec.rb b/spec/features/admin/administrators_spec.rb index 5c1af0036..bef57b75d 100644 --- a/spec/features/admin/administrators_spec.rb +++ b/spec/features/admin/administrators_spec.rb @@ -33,5 +33,13 @@ feature 'Admin administrators' do expect(page).to_not have_content @administrator.name end end + + scenario 'Delete Administrator when its the current user' do + find(:xpath, "//tr[contains(.,'#{@admin.name}')]/td/a", text: 'Delete').click + + within("#error") do + expect(page).to have_content I18n.t("admin.administrators.administrator.restricted_removal") + end + end end From fe5ddd019bc66fc95dffa8233b31b4552bcdf5a6 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Thu, 25 May 2017 11:29:39 +0200 Subject: [PATCH 7/7] Add logic to prevent the current user from removing himself from the administrator list, and a flash error notice for that scenario --- app/controllers/admin/administrators_controller.rb | 7 ++++++- config/locales/admin.en.yml | 1 + config/locales/admin.es.yml | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/administrators_controller.rb b/app/controllers/admin/administrators_controller.rb index 35325c5aa..938a7570d 100644 --- a/app/controllers/admin/administrators_controller.rb +++ b/app/controllers/admin/administrators_controller.rb @@ -26,7 +26,12 @@ class Admin::AdministratorsController < Admin::BaseController end def destroy - @administrator.destroy + if current_user.id == @administrator.user_id + flash[:error] = I18n.t("admin.administrators.administrator.restricted_removal") + else + @administrator.destroy + end + redirect_to admin_administrators_path end end diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 2c39dc283..aa6980510 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -233,6 +233,7 @@ en: administrator: add: Add delete: Delete + restricted_removal: "Sorry, you can't remove yourself from the administrators" search: email_placeholder: Search user by email search: Search diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index f569b84a0..9e30742bc 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -187,6 +187,7 @@ es: administrator: add: Añadir como Administrador delete: Borrar + restricted_removal: "Lo sentimos, no puedes te eliminar a ti mismo de la lista" search: email_placeholder: Buscar usuario por email search: Buscar