Merge pull request #1631 from PopulateTools/1489-admin-section-for-users

Admin section for users
This commit is contained in:
Raimond Garcia
2017-06-09 20:28:43 +02:00
committed by GitHub
20 changed files with 316 additions and 182 deletions

View File

@@ -0,0 +1,33 @@
class Admin::HiddenUsersController < Admin::BaseController
has_filters %w{without_confirmed_hide all with_confirmed_hide}, only: :index
before_action :load_user, only: [:confirm_hide, :restore]
def index
@users = User.only_hidden.send(@current_filter).page(params[:page])
end
def show
@user = User.with_hidden.find(params[:id])
@debates = @user.debates.with_hidden.page(params[:page])
@comments = @user.comments.with_hidden.page(params[:page])
end
def confirm_hide
@user.confirm_hide
redirect_to request.query_parameters.merge(action: :index)
end
def restore
@user.restore
Activity.log(current_user, :restore, @user)
redirect_to request.query_parameters.merge(action: :index)
end
private
def load_user
@user = User.with_hidden.find(params[:id])
end
end

View File

@@ -1,33 +1,16 @@
class Admin::UsersController < Admin::BaseController
has_filters %w{without_confirmed_hide all with_confirmed_hide}, only: :index
before_action :load_user, only: [:confirm_hide, :restore]
load_and_authorize_resource
def index
@users = User.only_hidden.send(@current_filter).page(params[:page])
end
def show
@user = User.with_hidden.find(params[:id])
@debates = @user.debates.with_hidden.page(params[:page])
@comments = @user.comments.with_hidden.page(params[:page])
end
def confirm_hide
@user.confirm_hide
redirect_to request.query_parameters.merge(action: :index)
end
def restore
@user.restore
Activity.log(current_user, :restore, @user)
redirect_to request.query_parameters.merge(action: :index)
end
private
def load_user
@user = User.with_hidden.find(params[:id])
if params[:search]
s = params[:search]
@users = User.where("username ILIKE ? OR email ILIKE ? OR document_number ILIKE ?", "%#{s}%","%#{s}%","%#{s}%").page(params[:page])
else
@users = @users.page(params[:page])
end
end
respond_to do |format|
format.html
format.js
end
end
end

View File

@@ -17,7 +17,7 @@ module AdminHelper
end
def menu_moderated_content?
["proposals", "debates", "comments", "users"].include? controller_name
["proposals", "debates", "comments", "hidden_users"].include? controller_name
end
def menu_budget?
@@ -29,7 +29,7 @@ module AdminHelper
end
def menu_profiles?
["administrators", "organizations", "officials", "moderators", "valuators", "managers"].include? controller_name
["administrators", "organizations", "officials", "moderators", "valuators", "managers", "users"].include? controller_name
end
def menu_banners?
@@ -52,6 +52,22 @@ module AdminHelper
resource.persisted? ? "edit" : "new"
end
def user_roles(user)
roles = []
roles << :admin if user.administrator?
roles << :moderator if user.moderator?
roles << :valuator if user.valuator?
roles << :manager if user.manager?
roles << :poll_officer if user.poll_officer?
roles << :official if user.official?
roles << :organization if user.organization?
roles
end
def display_user_roles(user)
user_roles(user).join(", ")
end
private
def namespace

View File

@@ -38,6 +38,7 @@ module Abilities
can [:search, :create, :index, :destroy], ::Moderator
can [:search, :create, :index, :summary], ::Valuator
can [:search, :create, :index, :destroy], ::Manager
can [:search, :index], ::User
can :manage, Annotation

View File

@@ -33,8 +33,8 @@
<%= link_to t("admin.menu.hidden_comments"), admin_comments_path %>
</li>
<li <%= "class=active" if controller_name == "users" %>>
<%= link_to t("admin.menu.hidden_users"), admin_users_path %>
<li <%= "class=active" if controller_name == "hidden_users" %>>
<%= link_to t("admin.menu.hidden_users"), admin_hidden_users_path %>
</li>
</ul>
</li>
@@ -118,6 +118,10 @@
<li <%= "class=active" if controller_name == "managers" %>>
<%= link_to t('admin.menu.managers'), admin_managers_path %>
</li>
<li <%= 'class=active' if controller_name == 'users' %>>
<%= link_to t('admin.menu.users'), admin_users_path %>
</li>
</ul>
</li>

View File

@@ -16,7 +16,7 @@
<div class="small-12 medium-4 column">
<p>
<strong><%= t("admin.budget_investments.show.by") %>:</strong>
<%= link_to @investment.author.name, admin_user_path(@investment.author) %>
<%= link_to @investment.author.name, admin_hidden_user_path(@investment.author) %>
</p>
</div>

View File

@@ -0,0 +1,33 @@
<h2><%= t("admin.hidden_users.index.title") %></h2>
<%= render 'shared/filter_subnav', i18n_namespace: "admin.hidden_users.index" %>
<h3><%= page_entries_info @users %></h3>
<table>
<% @users.each do |user| %>
<tr id="<%= dom_id(user) %>">
<td>
<p><%= link_to user.name, admin_hidden_user_path(user) %></p>
</td>
<td class="text-right">
<%= link_to t("admin.actions.restore"),
restore_admin_hidden_user_path(user, request.query_parameters),
method: :put,
data: { confirm: t("admin.actions.confirm") },
class: "button hollow on-hover" %>
<% unless user.confirmed_hide? %>
<%= link_to t("admin.actions.confirm_hide"),
confirm_hide_admin_hidden_user_path(user, request.query_parameters),
method: :put,
class: "button hollow warning on-hover" %>
<% end %>
</td>
</tr>
<% end %>
</table>
<%= paginate @users %>

View File

@@ -1,11 +1,11 @@
<%= render 'shared/back_link' %>
<h2><%= t("admin.users.show.title", user: @user.name) %></h2>
<h2><%= t("admin.hidden_users.show.title", user: @user.name) %></h2>
<p>
<strong><%= t("admin.users.show.email") %></strong> <%= @user.email %> |
<strong><%= t("admin.users.show.registered_at") %></strong> <%= @user.confirmed_at %> |
<strong><%= t("admin.users.show.hidden_at") %></strong> <%= @user.hidden_at %>
<strong><%= t("admin.hidden_users.show.email") %></strong> <%= @user.email %> |
<strong><%= t("admin.hidden_users.show.registered_at") %></strong> <%= @user.confirmed_at %> |
<strong><%= t("admin.hidden_users.show.hidden_at") %></strong> <%= @user.hidden_at %>
</p>
<% if @debates.present? %>

View File

@@ -16,7 +16,7 @@
<div class="small-12 medium-4 column">
<p>
<strong><%= t("admin.spending_proposals.show.by") %>:</strong>
<%= link_to @spending_proposal.author.name, admin_user_path(@spending_proposal.author) %>
<%= link_to @spending_proposal.author.name, admin_hidden_user_path(@spending_proposal.author) %>
</p>
</div>

View File

@@ -0,0 +1,24 @@
<h3><%= page_entries_info @users %></h3>
<table>
<thead>
<tr>
<th><%= t('admin.users.columns.name') %></th>
<th><%= t('admin.users.columns.email') %></th>
<th><%= t('admin.users.columns.document_number') %></th>
<th><%= t('admin.users.columns.roles') %></th>
<th><%= t('admin.users.columns.verification_level') %></th>
</tr>
</thead>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td><%= user.document_number %></td>
<td><%= display_user_roles(user) %></td>
<td><%= user.user_type %></td>
</tr>
<% end %>
</table>
<%= paginate @users %>

View File

@@ -1,33 +1,16 @@
<h2><%= t("admin.users.index.title") %></h2>
<%= render 'shared/filter_subnav', i18n_namespace: "admin.users.index" %>
<h3><%= page_entries_info @users %></h3>
<table>
<% @users.each do |user| %>
<tr id="<%= dom_id(user) %>">
<td>
<p><%= link_to user.name, admin_user_path(user) %></p>
</td>
<td class="text-right">
<%= link_to t("admin.actions.restore"),
restore_admin_user_path(user, request.query_parameters),
method: :put,
data: { confirm: t("admin.actions.confirm") },
class: "button hollow on-hover" %>
<% unless user.confirmed_hide? %>
<%= link_to t("admin.actions.confirm_hide"),
confirm_hide_admin_user_path(user, request.query_parameters),
method: :put,
class: "button hollow warning on-hover" %>
<% end %>
</td>
</tr>
<% end %>
</table>
<%= paginate @users %>
<div class="row">
<%= form_tag admin_users_path, method: :get, remote: true do %>
<div class="small-12 medium-6 column">
<%= text_field_tag :search, '', placeholder: t('admin.users.search.placeholder') %>
</div>
<div class="small-12 medium-6 column">
<%= submit_tag t('admin.users.search.search'), class: 'button' %>
</div>
<% end %>
</div>
<div id="users">
<%= render "users" %>
</div>

View File

@@ -0,0 +1 @@
$("#users").html("<%= j render 'users' %>");

View File

@@ -8,7 +8,7 @@
<% @users.each do |user| %>
<tr id="<%= dom_id(user) %>">
<td>
<%= link_to user.name, admin_user_path(user) %>
<%= link_to user.name, admin_hidden_user_path(user) %>
</td>
<td>
<%= render "pending_step", user: user %>
@@ -17,4 +17,4 @@
<% end %>
</table>
<%= paginate @users %>
<%= paginate @users %>

View File

@@ -126,7 +126,7 @@ ignore_unused:
- 'admin.budget_investments.index.filter*'
- 'admin.spending_proposals.index.filter*'
- 'admin.organizations.index.filter*'
- 'admin.users.index.filter*'
- 'admin.hidden_users.index.filter*'
- 'admin.activity.show.filter*'
- 'admin.legislation.processes.index.filter*'
- 'admin.legislation.processes.*.submit_button'

View File

@@ -181,6 +181,19 @@ en:
with_confirmed_hide: Confirmed
without_confirmed_hide: Pending
title: Hidden debates
hidden_users:
index:
filter: Filter
filters:
all: All
with_confirmed_hide: Confirmed
without_confirmed_hide: Pending
title: Hidden users
show:
email: 'Email:'
hidden_at: 'Hidden at:'
registered_at: 'Registered at:'
title: Activity of user (%{user})
legislation:
processes:
create:
@@ -367,6 +380,7 @@ en:
title_banners: Banners
title_site_customization: Site customization
legislation: Collaborative Legislation
users: Users
administrators:
index:
title: Administrators
@@ -804,18 +818,17 @@ en:
placeholder: Type the name of the topic
update: Update Topic
users:
columns:
name: Name
email: Email
document_number: Document number
roles: Roles
verification_level: Verification level
index:
filter: Filter
filters:
all: All
with_confirmed_hide: Confirmed
without_confirmed_hide: Pending
title: Hidden users
show:
email: 'Email:'
hidden_at: 'Hidden at:'
registered_at: 'Registered at:'
title: Activity of user (%{user})
title: User
search:
placeholder: Search user by email, name or document number
search: Search
verifications:
index:
phone_not_given: Phone not given

View File

@@ -181,6 +181,19 @@ es:
with_confirmed_hide: Confirmados
without_confirmed_hide: Pendientes
title: Debates ocultos
hidden_users:
index:
filter: Filtro
filters:
all: Todos
with_confirmed_hide: Confirmados
without_confirmed_hide: Pendientes
title: Usuarios bloqueados
show:
email: 'Email:'
hidden_at: 'Bloqueado:'
registered_at: 'Fecha de alta:'
title: Actividad del usuario (%{user})
legislation:
processes:
create:
@@ -378,6 +391,7 @@ es:
title_banners: Banners
title_site_customization: Personalizar sitio
legislation: Legislación colaborativa
users: Usuarios
moderators:
index:
title: Moderadores
@@ -804,18 +818,17 @@ es:
placeholder: Escribe el nombre del tema
update: Actualizar Tema
users:
columns:
name: Nombre
email: Correo electrónico
document_number: DNI/Pasaporte/Tarjeta de residencia
roles: Roles
verification_level: Nivel de verficación
index:
filter: Filtro
filters:
all: Todos
with_confirmed_hide: Confirmados
without_confirmed_hide: Pendientes
title: Usuarios bloqueados
show:
email: 'Email:'
hidden_at: 'Bloqueado:'
registered_at: 'Fecha de alta:'
title: Actividad del usuario (%{user})
title: Usuarios
search:
placeholder: Buscar usuario por email, nombre o DNI
search: Buscar
verifications:
index:
phone_not_given: No ha dado su teléfono

View File

@@ -161,7 +161,7 @@ Rails.application.routes.draw do
end
end
resources :users, only: [:index, :show] do
resources :hidden_users, only: [:index, :show] do
member do
put :restore
put :confirm_hide
@@ -238,6 +238,8 @@ Rails.application.routes.draw do
get :search, on: :collection
end
resources :users, only: [:index, :show]
scope module: :poll do
resources :polls do
get :search_questions, on: :member

View File

@@ -314,7 +314,7 @@ feature 'Admin activity' do
scenario "Shows admin restores" do
user = create(:user, :hidden)
visit admin_users_path
visit admin_hidden_users_path
within("#user_#{user.id}") do
click_on "Restore"
@@ -331,4 +331,4 @@ feature 'Admin activity' do
end
end
end
end

View File

@@ -0,0 +1,97 @@
require 'rails_helper'
feature 'Admin hidden users' do
background do
admin = create(:administrator)
login_as(admin.user)
end
scenario 'Show user activity' do
user = create(:user, :hidden)
debate1 = create(:debate, :hidden, author: user)
debate2 = create(:debate, author: user)
comment1 = create(:comment, :hidden, user: user, commentable: debate2, body: "You have the manners of a beggar")
comment2 = create(:comment, user: user, commentable: debate2, body: 'Not Spam')
visit admin_hidden_user_path(user)
expect(page).to have_content(debate1.title)
expect(page).to have_content(debate2.title)
expect(page).to have_content(comment1.body)
expect(page).to have_content(comment2.body)
end
scenario 'Restore' do
user = create(:user, :hidden)
visit admin_hidden_users_path
click_link 'Restore'
expect(page).to_not have_content(user.username)
expect(user.reload).to_not be_hidden
end
scenario 'Confirm hide' do
user = create(:user, :hidden)
visit admin_hidden_users_path
click_link 'Confirm'
expect(page).to_not have_content(user.username)
click_link('Confirmed')
expect(page).to have_content(user.username)
expect(user.reload).to be_confirmed_hide
end
scenario "Current filter is properly highlighted" do
visit admin_hidden_users_path
expect(page).to_not have_link('Pending')
expect(page).to have_link('All')
expect(page).to have_link('Confirmed')
visit admin_hidden_users_path(filter: 'Pending')
expect(page).to_not have_link('Pending')
expect(page).to have_link('All')
expect(page).to have_link('Confirmed')
visit admin_hidden_users_path(filter: 'all')
expect(page).to have_link('Pending')
expect(page).to_not have_link('All')
expect(page).to have_link('Confirmed')
visit admin_hidden_users_path(filter: 'with_confirmed_hide')
expect(page).to have_link('All')
expect(page).to have_link('Pending')
expect(page).to_not have_link('Confirmed')
end
scenario "Filtering users" do
create(:user, :hidden, username: "Unconfirmed")
create(:user, :hidden, :with_confirmed_hide, username: "Confirmed user")
visit admin_hidden_users_path(filter: 'all')
expect(page).to have_content('Unconfirmed')
expect(page).to have_content('Confirmed user')
visit admin_hidden_users_path(filter: 'with_confirmed_hide')
expect(page).to_not have_content('Unconfirmed')
expect(page).to have_content('Confirmed user')
end
scenario "Action links remember the pagination setting and the filter" do
per_page = Kaminari.config.default_per_page
(per_page + 2).times { create(:user, :hidden, :with_confirmed_hide) }
visit admin_hidden_users_path(filter: 'with_confirmed_hide', page: 2)
click_on('Restore', match: :first, exact: true)
expect(current_url).to include('filter=with_confirmed_hide')
expect(current_url).to include('page=2')
end
end

View File

@@ -1,97 +1,28 @@
require 'rails_helper'
feature 'Admin users' do
background do
admin = create(:administrator)
login_as(admin.user)
end
scenario 'Show user activity' do
user = create(:user, :hidden)
debate1 = create(:debate, :hidden, author: user)
debate2 = create(:debate, author: user)
comment1 = create(:comment, :hidden, user: user, commentable: debate2, body: "You have the manners of a beggar")
comment2 = create(:comment, user: user, commentable: debate2, body: 'Not Spam')
visit admin_user_path(user)
expect(page).to have_content(debate1.title)
expect(page).to have_content(debate2.title)
expect(page).to have_content(comment1.body)
expect(page).to have_content(comment2.body)
end
scenario 'Restore' do
user = create(:user, :hidden)
@admin = create(:administrator)
@user = create(:user, username: 'Jose Luis Balbin')
login_as(@admin.user)
visit admin_users_path
click_link 'Restore'
expect(page).to_not have_content(user.username)
expect(user.reload).to_not be_hidden
end
scenario 'Confirm hide' do
user = create(:user, :hidden)
visit admin_users_path
click_link 'Confirm'
expect(page).to_not have_content(user.username)
click_link('Confirmed')
expect(page).to have_content(user.username)
expect(user.reload).to be_confirmed_hide
scenario 'Index' do
expect(page).to have_content @user.name
expect(page).to have_content @user.email
expect(page).to have_content @admin.name
expect(page).to have_content @admin.email
end
scenario "Current filter is properly highlighted" do
visit admin_users_path
expect(page).to_not have_link('Pending')
expect(page).to have_link('All')
expect(page).to have_link('Confirmed')
scenario 'Search' do
fill_in :search, with: "Luis"
click_button 'Search'
visit admin_users_path(filter: 'Pending')
expect(page).to_not have_link('Pending')
expect(page).to have_link('All')
expect(page).to have_link('Confirmed')
visit admin_users_path(filter: 'all')
expect(page).to have_link('Pending')
expect(page).to_not have_link('All')
expect(page).to have_link('Confirmed')
visit admin_users_path(filter: 'with_confirmed_hide')
expect(page).to have_link('All')
expect(page).to have_link('Pending')
expect(page).to_not have_link('Confirmed')
expect(page).to have_content @user.name
expect(page).to have_content @user.email
expect(page).to_not have_content @admin.name
expect(page).to_not have_content @admin.email
end
scenario "Filtering users" do
create(:user, :hidden, username: "Unconfirmed")
create(:user, :hidden, :with_confirmed_hide, username: "Confirmed user")
visit admin_users_path(filter: 'all')
expect(page).to have_content('Unconfirmed')
expect(page).to have_content('Confirmed user')
visit admin_users_path(filter: 'with_confirmed_hide')
expect(page).to_not have_content('Unconfirmed')
expect(page).to have_content('Confirmed user')
end
scenario "Action links remember the pagination setting and the filter" do
per_page = Kaminari.config.default_per_page
(per_page + 2).times { create(:user, :hidden, :with_confirmed_hide) }
visit admin_users_path(filter: 'with_confirmed_hide', page: 2)
click_on('Restore', match: :first, exact: true)
expect(current_url).to include('filter=with_confirmed_hide')
expect(current_url).to include('page=2')
end
end