Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View and controllers #7

Merged
merged 55 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
6201abd
improve the controller and add a table to the view
antopalidi Jan 16, 2023
a469bf4
logs list
antopalidi Jan 18, 2023
b01bac4
add PaperTrail instead ActionLog
antopalidi Jan 18, 2023
ca5b656
add removal date to table
antopalidi Jan 18, 2023
7eddd73
change datetime format
antopalidi Jan 18, 2023
bb9a015
move remove instance var from helper
antopalidi Jan 18, 2023
87dad02
add styles to table, add role
antopalidi Jan 19, 2023
25bef6b
add pagination
antopalidi Jan 19, 2023
a87cf47
fix pagination
antopalidi Jan 19, 2023
4fdc6e7
add rspec
antopalidi Jan 19, 2023
38b0066
change controller
antopalidi Jan 19, 2023
e31d309
change controller
antopalidi Jan 19, 2023
762dd11
fix lint
antopalidi Jan 19, 2023
c2f3cd8
add rspec
antopalidi Jan 19, 2023
160b3ba
add participatory_space_type to table
antopalidi Jan 20, 2023
1df11ed
fix lint
antopalidi Jan 20, 2023
62084d3
refactoring
antopalidi Jan 20, 2023
991b0fe
change method's name
antopalidi Jan 20, 2023
51aab07
Merge branch 'main' into admin_accountability_view_and_controllers
microstudi Jan 23, 2023
ce1c261
improve the controller and add a table to the view
antopalidi Jan 16, 2023
ac667ba
logs list
antopalidi Jan 18, 2023
11d0e6b
add PaperTrail instead ActionLog
antopalidi Jan 18, 2023
df2abaa
add removal date to table
antopalidi Jan 18, 2023
f6e3b26
change datetime format
antopalidi Jan 18, 2023
e1684a2
move remove instance var from helper
antopalidi Jan 18, 2023
d8be8b4
add styles to table, add role
antopalidi Jan 19, 2023
219a72f
add pagination
antopalidi Jan 19, 2023
fb7635d
fix pagination
antopalidi Jan 19, 2023
29267d5
add rspec
antopalidi Jan 19, 2023
031f5ce
change controller
antopalidi Jan 19, 2023
210088b
change controller
antopalidi Jan 19, 2023
876b761
fix lint
antopalidi Jan 19, 2023
22ee012
add rspec
antopalidi Jan 19, 2023
824835a
add participatory_space_type to table
antopalidi Jan 20, 2023
128580f
fix lint
antopalidi Jan 20, 2023
e719a8a
refactoring
antopalidi Jan 20, 2023
c034994
change method's name
antopalidi Jan 20, 2023
d2baf5f
Merge remote-tracking branch 'origin/admin_accountability_view_and_co…
antopalidi Jan 23, 2023
1bcaa96
add config for types_user_roles
antopalidi Jan 23, 2023
ff76cb5
change view and helper, move building html to view from helper
antopalidi Jan 23, 2023
c717960
fix lint
antopalidi Jan 23, 2023
339679c
fix lint
antopalidi Jan 23, 2023
9479634
add presenter, remove helper
antopalidi Jan 24, 2023
1fb431a
fix pagination
microstudi Jan 25, 2023
ced9a4d
add link to participatory space, add presenter spec
antopalidi Jan 27, 2023
3bbe202
Merge branch 'main' into admin_accountability_view_and_controllers
microstudi Jan 30, 2023
a69b990
Merge branch 'develop' into admin_accountability_view_and_controllers
microstudi Jan 30, 2023
9d5d3ae
extract the i18n version of the role
antopalidi Jan 30, 2023
66ac621
add rspec PaperTrailVersion
antopalidi Jan 30, 2023
ba5d83b
rspec PaperTrailRolePresenter
antopalidi Jan 31, 2023
308dfe0
Fix presenter
microstudi Jan 31, 2023
055a6d4
fix tests
microstudi Jan 31, 2023
71407aa
fix presenter
microstudi Jan 31, 2023
88653b7
handle deleted users
microstudi Jan 31, 2023
d92780b
fix checksums
microstudi Jan 31, 2023
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
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ GEM
net-smtp (0.3.3)
net-protocol
nio4r (2.5.8)
nokogiri (1.13.10-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.13.10-x86_64-linux)
racc (~> 1.4)
oauth (1.1.0)
Expand Down Expand Up @@ -796,6 +798,7 @@ GEM
zeitwerk (2.6.6)

PLATFORMS
x86_64-darwin-22
x86_64-linux

DEPENDENCIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ module DecidimAwesome
module Admin
class AdminActionsController < DecidimAwesome::Admin::ApplicationController
include NeedsAwesomeConfig
include Decidim::Admin::Filterable

helper_method :admin_actions, :admin_actions_destroy, :types_user_roles

layout "decidim/admin/users"

before_action do
enforce_permission_to :edit_config, :allow_admin_accountability
end
Expand All @@ -16,6 +20,22 @@ def index; end
def export_xls
# TODO: export to xls
end

private

def admin_actions
@admin_actions ||= PaperTrail::Version.where(item_type: types_user_roles, event: "create")
.page(params[:page])
.per(params[:per_page])
end

def admin_actions_destroy
@admin_actions_destroy ||= PaperTrail::Version.where(item_type: types_user_roles, event: "destroy")
end
antopalidi marked this conversation as resolved.
Show resolved Hide resolved

def types_user_roles
@types_user_roles ||= Decidim::DecidimAwesome.user_roles
end
end
end
end
Expand Down
33 changes: 33 additions & 0 deletions app/helpers/decidim/decidim_awesome/admin/admin_actions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module Decidim
module DecidimAwesome
module Admin
module AdminActionsHelper
def role_from_papertrail(admin_action)
destroy_action = PaperTrail::Version.find_by(item_type: admin_action.item_type, event: "destroy", item_id: admin_action.item_id)
role = destroy_action&.reify&.role || admin_action.item&.role
role == "admin" ? "administrator" : role
end

def participatory_space_type(admin_action)
Decidim::ActionLog.find_by(resource_id: admin_action.changeset["decidim_user_id"].last).try(:participatory_space_type)
end

def admin_action_user(admin_action)
Decidim::User.find_by(id: admin_action.changeset["decidim_user_id"].last)
end

def removal_date(admin_action)
removal_date = admin_actions_destroy.find { |date| date.item_id == admin_action.item_id }.try(:created_at)

removal_date ? I18n.l(removal_date, format: :short) : currently_active
end

def currently_active
t("decidim.decidim_awesome.admin.admin_accountability.currently_active")
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,40 @@
<div class="card-divider">
<h2 class="card-title"><%= t("menu.admin_accountability", scope: "decidim.admin", default: "Admin accountability") %></h2>
</div>
<div class="card-section fcell grid-x">
<div class="cell auto text-right">
<%= admin_filters_pagination %>
</div>
</div>
<div class="card-section">
<p>List of admin actions</p>
<div class="table-scroll">
<table class="table-list">
<thead>
<tr>
<th><%= t("role", scope: "decidim.decidim_awesome.admin.admin_accountability") %></th>
<th><%= t("name", scope: "decidim.decidim_awesome.admin.admin_accountability") %></th>
<th><%= t("email", scope: "decidim.decidim_awesome.admin.admin_accountability") %></th>
<th><%= t("participatory_space", scope: "decidim.decidim_awesome.admin.admin_accountability") %></th>
<th><%= t("last_login", scope: "decidim.decidim_awesome.admin.admin_accountability") %></th>
<th><%= t("created_at", scope: "decidim.decidim_awesome.admin.admin_accountability") %></th>
<th><%= t("removal_date", scope: "decidim.decidim_awesome.admin.admin_accountability") %></th>
</tr>
</thead>
<tbody>
<% admin_actions.each do |admin_action| %>
<tr>
<td><%= role_from_papertrail(admin_action) %></td>
<td><%= admin_action_user(admin_action).name %></td>
<td><%= admin_action_user(admin_action).email %></td>
<td><%= participatory_space_type(admin_action) %></td>
<td><%= admin_action_user(admin_action).last_sign_in_at ? I18n.l(admin_action_user(admin_action).last_sign_in_at, format: :short) : "" %></td>
<td><%= I18n.l(admin_action.changeset["created_at"].last, format: :short) %></td>
<td class="<%= currently_active == removal_date(admin_action) ? "text-success" : nil %>"><%= removal_date(admin_action) %></td>
antopalidi marked this conversation as resolved.
Show resolved Hide resolved
</tr>
<% end %>
</tbody>
</table>
<%= paginate admin_actions, theme: "decidim" %>
</div>
</div>
</div>
10 changes: 10 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ en:
show_withdrawn: Show withdrawn proposals
decidim_awesome:
admin:
admin_accountability:
created_at: Creation date
currently_active: Currently active
email: Email
last_login: Last sign in date
missing_info: "(missing information)"
name: Name
participatory_space: Participatory space
removal_date: Removal date
role: Role
checks:
index:
admin_head_tags: Awesome tags included in the admin application header
Expand Down
5 changes: 5 additions & 0 deletions lib/decidim/decidim_awesome/awesome.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ module DecidimAwesome
true
end

# Roles for which it is necessary to show admin_accountability
config_accessor :user_roles do
antopalidi marked this conversation as resolved.
Show resolved Hide resolved
%w(Decidim::AssemblyUserRole Decidim::ParticipatoryProcessUserRole Decidim::ConferencesUserRole)
end

#
# HELPERS
#
Expand Down
4 changes: 2 additions & 2 deletions spec/permissions/admin/permissions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ module Decidim::DecidimAwesome::Admin
allow(Decidim::DecidimAwesome.config).to receive(feature).and_return(status)
end

it { is_expected.to eq true }
it { is_expected.to be true }

context "when admin_accountability is disabled" do
let(:status) { :disabled }

it { is_expected.to eq false }
it { is_expected.to be false }
end
end
end
Expand Down
68 changes: 66 additions & 2 deletions spec/system/admin/admin_accountability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@

describe "Admin accountability", type: :system do
let(:organization) { create :organization }
let!(:user) { create :user, :admin, :confirmed, organization: organization }
let!(:admin) { create :user, :admin, :confirmed, organization: organization }

let(:administrator) { create(:user, organization: organization, last_sign_in_at: 1.day.ago) }
let(:valuator) { create(:user, organization: organization) }
let(:collaborator) { create(:user, organization: organization) }
let(:moderator) { create(:user, organization: organization) }
let(:participatory_process) { create(:participatory_process, organization: organization) }

let(:status) { true }

before do
allow(Decidim::DecidimAwesome.config).to receive(:allow_admin_accountability).and_return(status)
switch_to_host(organization.host)
login_as user, scope: :user
login_as admin, scope: :user

visit decidim_admin.root_path
end
Expand All @@ -32,4 +39,61 @@
expect(page).not_to have_content("Admin accountability")
end
end

describe "admin action list" do
context "when there are admin actions" do
datetime = (1.day.ago).strftime("%d/%m/%Y %H:%M")

before do
create(:participatory_process_user_role, user: administrator, participatory_process: participatory_process, role: "admin", created_at: 1.day.ago)
create(:participatory_process_user_role, user: valuator, participatory_process: participatory_process, role: "valuator", created_at: 1.day.ago)
create(:participatory_process_user_role, user: collaborator, participatory_process: participatory_process, role: "collaborator", created_at: 1.day.ago)
create(:participatory_process_user_role, user: moderator, participatory_process: participatory_process, role: "moderator", created_at: 1.day.ago)

user_to_delete = Decidim::ParticipatoryProcessUserRole.find_by(user: collaborator)
user_to_delete.destroy

click_link "Participants"
click_link "Admin accountability"
end

it "shows the correct roles for each user", versioning: true do
expect(page).to have_content("administrator", count: 1)
expect(page).to have_content("valuator", count: 1)
expect(page).to have_content("collaborator", count: 1)
expect(page).to have_content("moderator", count: 1)
end

it "shows tha user name", versioning: true do
expect(page).to have_content(administrator.name, count: 1)
expect(page).to have_content(valuator.name, count: 1)
expect(page).to have_content(collaborator.name, count: 1)
expect(page).to have_content(moderator.name, count: 1)
end

it "shows the user email", versioning: true do
expect(page).to have_content(administrator.email, count: 1)
expect(page).to have_content(valuator.email, count: 1)
expect(page).to have_content(collaborator.email, count: 1)
expect(page).to have_content(moderator.email, count: 1)
end

it "shows the creation date", versioning: true do
expect(page).to have_css("table tr td:nth-child(6)", text: datetime, count: 4)
end

context "when the user was logged in" do
it "shows the last login date", versioning: true do
expect(page).to have_css("table tr td:nth-child(5)", text: datetime, count: 1)
end
end

context "when the user was deleted" do
it "shows the user as deleted", versioning: true do
expect(page).to have_content("Currently active", count: 3)
expect(page).to have_css("table tr td:nth-child(7)", text: Time.current.strftime("%d/%m/%Y %H:%M"), count: 1)
end
end
end
end
end