Skip to content

Commit

Permalink
ban or unban users
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Jun 20, 2021
1 parent e72151d commit ce1bd0e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
10 changes: 10 additions & 0 deletions app/controllers/users_controller.rb
Expand Up @@ -2,4 +2,14 @@ class UsersController < ApplicationController
def index
@users = User.all
end

def ban
@user = User.find(params[:id])
if @user.access_locked?
@user.unlock_access!
else
@user.lock_access!
end
redirect_to users_path, notice: "User access locked: #{@user.access_locked?}"
end
end
9 changes: 9 additions & 0 deletions app/helpers/users_helper.rb
@@ -0,0 +1,9 @@
module UsersHelper
def ban_status(user)
if user.access_locked?
'Unban'
else
'Ban'
end
end
end
3 changes: 3 additions & 0 deletions app/views/users/index.html.erb
Expand Up @@ -5,5 +5,8 @@
<%= user.current_sign_in_ip %>
<strong>confirmed?</strong>
<%= user.confirmed? %>
<strong>access locked?</strong>
<%= user.access_locked? %>
<%= link_to ban_status(user), ban_user_path(user), method: :patch %>
<br>
<% end %>
12 changes: 7 additions & 5 deletions config/initializers/devise.rb
Expand Up @@ -194,7 +194,8 @@
# Defines which strategy will be used to lock an account.
# :failed_attempts = Locks an account after a number of failed attempts to sign in.
# :none = No lock strategy. You should handle locking by yourself.
config.lock_strategy = :failed_attempts
# config.lock_strategy = :failed_attempts
config.lock_strategy = :none

# Defines which key will be used when locking and unlocking an account
# config.unlock_keys = [:email]
Expand All @@ -204,17 +205,18 @@
# :time = Re-enables login after a certain amount of time (see :unlock_in below)
# :both = Enables both strategies
# :none = No unlock strategy. You should handle unlocking by yourself.
config.unlock_strategy = :both
# config.unlock_strategy = :both
config.unlock_strategy = :none

# Number of authentication tries before locking an account if lock_strategy
# is failed attempts.
config.maximum_attempts = 3
# config.maximum_attempts = 20

# Time interval to unlock the account if :time is enabled as unlock_strategy.
config.unlock_in = 1.hour
# config.unlock_in = 1.hour

# Warn on the last attempt before the account is locked.
config.last_attempt_warning = true
# config.last_attempt_warning = true

# ==> Configuration for :recoverable
#
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
@@ -1,6 +1,10 @@
Rails.application.routes.draw do
resources :posts
resources :users, only: [:index]
resources :users, only: [:index] do
member do
patch :ban
end
end
devise_for :users, controllers: { confirmations: 'users/confirmations' }
root "dashboard#index"
get 'dashboard/index'
Expand Down

0 comments on commit ce1bd0e

Please sign in to comment.