Class: UsersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/users_controller.rb

Overview

This is a user management controller.

This includes all the actions necessary to create, list, edit, disable, and destroy users.

Instance Method Summary collapse

Methods inherited from BarkestCore::ApplicationControllerBase

#authorize!, #show_denial_reason?

Methods included from BarkestCore::StatusHelper

#clear_system_status, #show_system_status, #status_button_label, #status_redirect_url

Methods included from BarkestCore::RecaptchaHelper

#add_recaptcha_challenge, #verify_recaptcha_challenge

Methods included from BarkestCore::SessionsHelper

#current_user, #current_user?, #forget, #log_in, #log_out, #logged_in?, #redirect_back_or, #remember, #store_location, #store_location_and_redirect_to, #system_admin?

Instance Method Details

#createObject

Creates a new user account after verifying the user is not a robot.



46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/users_controller.rb', line 46

def create
  @user = User.new(user_params)
  if @user.valid? && verify_recaptcha_challenge(@user)
    if @user.save
      @user.send_activation_email request.remote_ip
      flash[:safe_info] = 'Your account has been created, but needs to be activated before you can use it.<br/>Please check your email to activate your account.'
      redirect_to root_url and return
    end
  end
  render 'new'
end

#destroyObject

Destroys a user account that has been disabled for at least 15 days as long as the requesting user is an admin.



81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/users_controller.rb', line 81

def destroy
  if @user.enabled?
    flash[:danger] = 'Cannot delete an enabled user.'
  elsif @user.disabled_at.blank? || @user.disabled_at > 15.days.ago
    flash[:danger] = 'Cannot delete a user within 15 days of being disabled.'
  else
    @user.destroy
    flash[:success] = "User #{@user.name} has been deleted."
  end
  redirect_to users_path
end

#disableObject

Disables a user account as long as the requesting user is an administrator and provides a reason the account is being disabled.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/users_controller.rb', line 109

def disable
  load_disable_user

  if @disable.valid?
    if @disable.user.disable(current_user, @disable.reason)
      flash[:success] = "User #{@disable.user.name} has been disabled."
      redirect_to users_path and return
    else
      @disable.errors.add(:user, 'was unable to be updated')
    end
  end

  render 'disable_confirm'
end

#disable_confirmObject

Shows a form requesting a reason to disable a user and allowing the administrator a chance to cancel the action.



97
98
99
100
101
102
103
# File 'app/controllers/users_controller.rb', line 97

def disable_confirm
  load_disable_user
  unless @disable.user.enabled?
    flash[:warning] = "User #{@disable.user.name} is already disabled."
    redirect_to users_path
  end
end

#editObject

Shows a form to edit the user profile.



61
62
63
# File 'app/controllers/users_controller.rb', line 61

def edit

end

#enableObject

Enables a previosly disabled user as long as the requesting user is an administrator.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/controllers/users_controller.rb', line 128

def enable
  if @user.enabled?
    flash[:warning] = "User #{@user.name} is already enabled."
    redirect_to users_path and return
  end

  if @user.enable
    flash[:success] = "User #{@user.name} has been enabled."
  else
    flash[:danger] = "Failed to enable user #{@user.name}."
  end

  redirect_to users_path
end

#indexObject

Shows a list of all users.

Admin users are shown all users including disabled and inactive. Other users only see the enabled users.



25
26
27
# File 'app/controllers/users_controller.rb', line 25

def index
  @users = (current_user.system_admin? ? User.known.sorted : User.known.enabled.sorted).paginate(page: params[:page])
end

#newObject

Shows the signup form for a new user.



39
40
41
# File 'app/controllers/users_controller.rb', line 39

def new
  @user = User.new
end

#showObject

Shows a specific user profile.



32
33
34
# File 'app/controllers/users_controller.rb', line 32

def show

end

#updateObject

Updates a user profile.



68
69
70
71
72
73
74
75
# File 'app/controllers/users_controller.rb', line 68

def update
  if @user.update_attributes(user_params)
    flash[:success] = 'Your profile has been updated.'
    redirect_to @user
  else
    render 'edit'
  end
end