User's blocked field refactored to use state machine
This commit is contained in:
parent
9a06dd4aa1
commit
0d9a6fe7b1
12 changed files with 40 additions and 33 deletions
|
@ -45,7 +45,7 @@ class Admin::UsersController < Admin::ApplicationController
|
|||
end
|
||||
|
||||
def unblock
|
||||
if admin_user.update_attribute(:blocked, false)
|
||||
if admin_user.activate
|
||||
redirect_to :back, alert: "Successfully unblocked"
|
||||
else
|
||||
redirect_to :back, alert: "Error occured. User was not unblocked"
|
||||
|
|
|
@ -30,7 +30,7 @@ class ApplicationController < ActionController::Base
|
|||
end
|
||||
|
||||
def reject_blocked!
|
||||
if current_user && current_user.blocked
|
||||
if current_user && current_user.blocked?
|
||||
sign_out current_user
|
||||
flash[:alert] = "Your account is blocked. Retry when an admin unblock it."
|
||||
redirect_to new_user_session_path
|
||||
|
@ -38,7 +38,7 @@ class ApplicationController < ActionController::Base
|
|||
end
|
||||
|
||||
def after_sign_in_path_for resource
|
||||
if resource.is_a?(User) && resource.respond_to?(:blocked) && resource.blocked
|
||||
if resource.is_a?(User) && resource.respond_to?(:blocked?) && resource.blocked?
|
||||
sign_out resource
|
||||
flash[:alert] = "Your account is blocked. Retry when an admin unblock it."
|
||||
new_user_session_path
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
# dark_scheme :boolean default(FALSE), not null
|
||||
# theme_id :integer default(1), not null
|
||||
# bio :string(255)
|
||||
# blocked :boolean default(FALSE), not null
|
||||
# state :string(255)
|
||||
# failed_attempts :integer default(0)
|
||||
# locked_at :datetime
|
||||
# extern_uid :string(255)
|
||||
|
@ -87,10 +87,27 @@ class User < ActiveRecord::Base
|
|||
|
||||
delegate :path, to: :namespace, allow_nil: true, prefix: true
|
||||
|
||||
state_machine :state, initial: :active do
|
||||
after_transition any => :blocked do |user, transition|
|
||||
# Remove user from all projects and
|
||||
user.users_projects.find_each do |membership|
|
||||
return false unless membership.destroy
|
||||
end
|
||||
end
|
||||
|
||||
event :block do
|
||||
transition active: :blocked
|
||||
end
|
||||
|
||||
event :activate do
|
||||
transition blocked: :active
|
||||
end
|
||||
end
|
||||
|
||||
# Scopes
|
||||
scope :admins, -> { where(admin: true) }
|
||||
scope :blocked, -> { where(blocked: true) }
|
||||
scope :active, -> { where(blocked: false) }
|
||||
scope :blocked, -> { with_state(:blocked) }
|
||||
scope :active, -> { with_state(:active) }
|
||||
scope :alphabetically, -> { order('name ASC') }
|
||||
scope :in_team, ->(team){ where(id: team.member_ids) }
|
||||
scope :not_in_team, ->(team){ where('users.id NOT IN (:ids)', ids: team.member_ids) }
|
||||
|
@ -260,17 +277,6 @@ class User < ActiveRecord::Base
|
|||
MergeRequest.cared(self)
|
||||
end
|
||||
|
||||
# Remove user from all projects and
|
||||
# set blocked attribute to true
|
||||
def block
|
||||
users_projects.find_each do |membership|
|
||||
return false unless membership.destroy
|
||||
end
|
||||
|
||||
self.blocked = true
|
||||
save
|
||||
end
|
||||
|
||||
def projects_limit_percent
|
||||
return 100 if projects_limit.zero?
|
||||
(personal_projects.count.to_f / projects_limit) * 100
|
||||
|
|
|
@ -61,7 +61,7 @@
|
|||
.span4
|
||||
- unless @admin_user.new_record?
|
||||
.alert.alert-error
|
||||
- if @admin_user.blocked
|
||||
- if @admin_user.blocked?
|
||||
%p This user is blocked and is not able to login to GitLab
|
||||
= link_to 'Unblock User', unblock_admin_user_path(@admin_user), method: :put, class: "btn btn-small"
|
||||
- else
|
||||
|
|
|
@ -53,7 +53,7 @@
|
|||
|
||||
= link_to 'Edit', edit_admin_user_path(user), id: "edit_#{dom_id(user)}", class: "btn btn-small"
|
||||
- unless user == current_user
|
||||
- if user.blocked
|
||||
- if user.blocked?
|
||||
= link_to 'Unblock', unblock_admin_user_path(user), method: :put, class: "btn btn-small success"
|
||||
- else
|
||||
= link_to 'Block', block_admin_user_path(user), confirm: 'USER WILL BE BLOCKED! Are you sure?', method: :put, class: "btn btn-small btn-remove"
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
%h3.page_title
|
||||
= image_tag gravatar_icon(@admin_user.email, 90), class: "avatar s90"
|
||||
= @admin_user.name
|
||||
- if @admin_user.blocked
|
||||
- if @admin_user.blocked?
|
||||
%span.cred (Blocked)
|
||||
- if @admin_user.admin
|
||||
%span.cred (Admin)
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
%span.label This is you!
|
||||
- if @project.namespace_owner == user
|
||||
%span.label Owner
|
||||
- elsif user.blocked
|
||||
- elsif user.blocked?
|
||||
%span.label Blocked
|
||||
- elsif allow_admin
|
||||
= link_to project_team_member_path(@project, user), confirm: remove_from_project_team_message(@project, user), method: :delete, class: "btn-tiny btn btn-remove" do
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
%span.btn.disabled This is you!
|
||||
- if @team.owner == user
|
||||
%span.btn.disabled Owner
|
||||
- elsif user.blocked
|
||||
- elsif user.blocked?
|
||||
%span.btn.disabled.blocked Blocked
|
||||
- elsif allow_admin
|
||||
= link_to team_member_path(@team, user), confirm: remove_from_user_team_message(@team, user), method: :delete, class: "btn-tiny btn btn-remove", title: "Remove from team" do
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue