Skip to content

Commit

Permalink
Admin: can add/remove editor for specific users
Browse files Browse the repository at this point in the history
  • Loading branch information
skelz0r committed Dec 9, 2024
1 parent b1d0a9e commit 3602355
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 9 deletions.
25 changes: 24 additions & 1 deletion app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
class Admin::UsersController < AdminController
def index
@q = User.ransack(params[:q])
@q = User.includes(:editor).ransack(params[:q])
@users = @q.result(distinct: true).page(params[:page])
end

def edit
@user = User.find(params[:id])
@editors = Editor.all
end

def update
@user = User.find(params[:id])

if @user.update(user_params)
success_message(title: "Utilisateur #{@user.email} a bien été modifié")

redirect_to admin_users_path
else
render :edit
end
end

def impersonate
user = User.find(params[:id])

Expand All @@ -17,4 +34,10 @@ def stop_impersonating

redirect_to admin_users_path
end

private

def user_params
params.require(:user).permit(:editor_id)
end
end
13 changes: 13 additions & 0 deletions app/views/admin/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%= form_for(@user, url: [:admin, @user]) do |f| %>
<div class="fr-input-group">
<%= f.label :email, class: %w[fr-label] %>
<%= f.text_field :email, disabled: true, class: %w[fr-input] %>
</div>

<div class="fr-select-group">
<%= f.label :editor, class: %w[fr-label] %>
<%= f.collection_select :editor_id, @editors, :id, :name, { include_blank: true }, { class: %w[fr-select] } %>
</div>

<%= f.button :submit, class: %[fr-btn], id: 'submit' %>
<% end %>
16 changes: 10 additions & 6 deletions app/views/admin/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<tr>
<%
[
'ID',
'Prénom',
'Nom',
'Email',
'Organizations',
'Éditeur associé',
'DataPass ID',
'Actions',
].each do |attr|
Expand All @@ -36,9 +36,6 @@
<tbody>
<% @users.each do |user| %>
<tr id="<%= dom_id(user) %>" class="user">
<td class="user-id">
<%= user.id %>
</td>
<td class="user-first_name">
<%= user.first_name %>
</td>
Expand All @@ -62,7 +59,14 @@
<% end %>
</td>

<td class="user-organizations">
<td class="user-editor">
<% if user.editor %>
<%= user.editor.name %>
<% end %>

<%= link_to "Modifier l'éditeur associé", edit_admin_user_path(user), id: dom_id(user, :edit) %>
</td>
<td class="user-datapass">
<% if user.authorization_requests.where(api: namespace).any? %>
<ul>
<% user.authorization_requests.where(api: namespace).each do |authorization_request| %>
Expand All @@ -76,7 +80,7 @@

<td class="user-actions">
<% if current_user != user %>
<%= button_to 'Se connecter en tant que cet utilisateur', impersonate_admin_user_path(user), data: { turbo: false }, class: 'fr-btn', id: dom_id(user, :impersonate) %>
<%= button_to 'Se connecter en tant que cet utilisateur', impersonate_admin_user_path(user), data: { turbo: false }, class: 'fr-btn fr-btn--sm', id: dom_id(user, :impersonate) %>
<% end %>
</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/admin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<body>
<%= render partial: 'shared/admin/header' %>

<div class="fr-container fr-mb-5w fr-mt-5w">
<div class="fr-container fr-mb-5w fr-my-5w">
<turbo-frame id="alerts">
<%= render partial: 'shared/alerts' %>
</turbo-frame>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
get '/admin', to: redirect('/admin/users')

namespace :admin do
resources :users, only: %i[index] do
resources :users, only: %i[index edit update] do
post :impersonate, on: :member
post :stop_impersonating, on: :collection
end
Expand Down
22 changes: 22 additions & 0 deletions spec/features/admin/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,26 @@
expect(page).to have_no_content(invalid_user.email)
end
end

describe 'adding an editor to a specific user' do
subject(:add_editor) do
visit admin_users_path

click_on dom_id(user, :edit)

select editor.name, from: 'user_editor_id'

click_on 'submit'
end

let!(:user) { create(:user, editor: nil) }
let!(:editor) { create(:editor) }

it 'adds the editor to the user' do
add_editor

expect(user.reload.editor).to eq(editor)
expect(page).to have_css('.fr-alert.fr-alert--success')
end
end
end

0 comments on commit 3602355

Please sign in to comment.