-
Notifications
You must be signed in to change notification settings - Fork 472
Implemented the "Disable users" feature #240
Changes from all commits
26a767e
36a013b
fddfe77
6ae62eb
0c5b0fc
bf68c0b
bc8bf56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ class Auth::RegistrationsController < Devise::RegistrationsController | |
|
||
before_action :check_admin, only: [:new, :create] | ||
before_action :configure_sign_up_params, only: [:create] | ||
before_action :authenticate_user!, only: [:disable] | ||
|
||
# Re-implemented so the template has the auxiliary variables regarding if | ||
# there are more users on the system or this is the first user to be created. | ||
|
@@ -25,6 +26,11 @@ def create | |
end | ||
end | ||
|
||
def edit | ||
@admin_count = User.admins.count | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, we should count only enabled ones There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ignore it, I just realized admins are already filtered by status |
||
super | ||
end | ||
|
||
def update | ||
success = | ||
if password_update? | ||
|
@@ -46,6 +52,19 @@ def update | |
end | ||
end | ||
|
||
# Disable a user. | ||
def disable | ||
user = User.find(params[:id]) | ||
|
||
if can_disable?(user) | ||
render nothing: true, status: 403 | ||
else | ||
user.update_attributes(enabled: false) | ||
sign_out user if current_user == user | ||
render template: 'auth/registrations/disabled', locals: { user: user, path: request.fullpath } | ||
end | ||
end | ||
|
||
# Devise does not allow to disable routes on purpose. Ideally, though we | ||
# could still be able to disable the `destroy` method through the | ||
# `routes.rb` file as described in the wiki (by disabling all the routes and | ||
|
@@ -76,4 +95,18 @@ def password_update? | |
!user[:current_password].blank? || !user[:password].blank? || | ||
!user[:password_confirmation].blank? | ||
end | ||
|
||
# Returns whether the given user can be disabled or not. The following rules | ||
# apply: | ||
# 1. A user can disable himself unless it's the last admin on the system. | ||
# 2. The admin user is the only one that can disable other users. | ||
def can_disable?(user) | ||
if current_user == user | ||
# An admin cannot disable himself if he's the only admin in the system. | ||
current_user.admin? && User.admins.count == 1 | ||
else | ||
# Only admin users can disable other users. | ||
!current_user.admin? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part of the branch is useless because we have the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that a regular user should be able to disable himself, therefore non-admin users should be able to perform this action. I'd say to just remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, ignore all my comments about this part of the code 👍 |
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
$('#user_<%= user.id %> td a i').addClass("fa-toggle-<%= user.admin? ? 'on' : 'off' %>"); | ||
$('#user_<%= user.id %> td a i').removeClass("fa-toggle-<%= user.admin? ? 'off' : 'on' %>"); | ||
$('#user_<%= user.id %> .admin-btn a i').addClass("fa-toggle-<%= user.admin? ? 'on' : 'off' %>"); | ||
$('#user_<%= user.id %> .admin-btn a i').removeClass("fa-toggle-<%= user.admin? ? 'off' : 'on' %>"); | ||
|
||
$('#notice p').html("User '<%= user.username %>' is <%= user.admin? ? 'now' : 'no longer' %> an admin"); | ||
$('#notice').fadeIn(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
if (window.location.pathname == '/admin/users') { | ||
// We are on the admin panel. | ||
$("#user_<%= user.id %>").fadeOut('normal', function() { | ||
$("#user_<%= user.id %>").remove(); | ||
}); | ||
|
||
$('#alert p').html("User '<%= user.username %>' has been disabled."); | ||
$('#alert').fadeIn(); | ||
} else { | ||
// The user profile page. | ||
window.location.pathname = '/'; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddEnabledToUsers < ActiveRecord::Migration | ||
def change | ||
add_column :users, :enabled, :bool, default: true | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
FactoryGirl.define do | ||
|
||
factory :team do | ||
sequence(:name) { |n| "team_name#{n}" } | ||
owners { |t| [t.association(:user)] } | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'rails_helper' | ||
|
||
feature 'Admin - Users panel' do | ||
let!(:registry) { create(:registry) } | ||
let!(:admin) { create(:admin) } | ||
let!(:user) { create(:user) } | ||
|
||
before do | ||
login_as admin, scope: :user | ||
visit admin_users_path | ||
end | ||
|
||
describe 'disable users' do | ||
scenario 'allows the admin to disable other users', js: true do | ||
expect(page).to have_css("#user_#{user.id}") | ||
find("#user_#{user.id} .enabled-btn").click | ||
|
||
wait_for_effect_on("#user_#{user.id}") | ||
|
||
expect(page).to_not have_css("#user_#{user.id}") | ||
wait_for_effect_on('#alert') | ||
expect(page).to have_content("User '#{user.username}' has been disabled.") | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should filter for enabled users too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ignore it, I just realized admins are already filtered by status