Class: Panda::Core::Admin::UsersController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/panda/core/admin/users_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#add_breadcrumb, #authenticate_admin_user!, #authenticate_user!, #breadcrumbs, #current_user, #set_current_request_details, #user_signed_in?

Methods included from Panda::Core::Authorizable

#authorize!, #authorized_for?, #authorized_for_admin_access?, #can?

Instance Method Details

#activityObject



127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/controllers/panda/core/admin/users_controller.rb', line 127

def activity
  add_breadcrumb @user.name, admin_user_path(@user)
  add_breadcrumb "Activity", activity_admin_user_path(@user)
  @activities = @user.user_activities.recent
  @page = [params.fetch(:page, 1).to_i, 1].max
  @per_page = 25
  @total_count = Rails.cache.fetch(["user_activities_count", @user.id], expires_in: 5.minutes) do
    @activities.count
  end
  @total_pages = (@total_count.to_f / @per_page).ceil
  @activities = @activities.offset((@page - 1) * @per_page).limit(@per_page)
end

#bulk_actionObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/controllers/panda/core/admin/users_controller.rb', line 108

def bulk_action
  user_ids = Array(params[:user_ids]).map(&:to_s)
  user_ids = user_ids.select { |id| id.match?(/\A[0-9a-f-]{36}\z/i) }

  return redirect_to admin_users_path, alert: "No valid users selected." if user_ids.blank?

  case params[:bulk_action]
  when "enable"
    enabled_count = User.where(id: user_ids).update_all(enabled: true)
    flash[:success] = "#{enabled_count} user(s) enabled."
  when "disable"
    disabled_count = User.where(id: user_ids).where.not(id: current_user.id).update_all(enabled: false)
    flash[:success] = "#{disabled_count} user(s) disabled (excluding yourself)."
  else
    flash[:error] = "Unknown action."
  end
  redirect_to admin_users_path
end

#disableObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/panda/core/admin/users_controller.rb', line 95

def disable
  if @user == current_user
    flash[:error] = "You cannot disable your own account."
    redirect_to admin_user_path(@user)
    return
  end

  @user.disable!
  UserActivity.log!(user: current_user, action: "disabled_user", resource: @user, request: request)
  flash[:success] = "#{@user.name} has been disabled."
  redirect_to admin_user_path(@user)
end

#editObject



49
50
51
52
# File 'app/controllers/panda/core/admin/users_controller.rb', line 49

def edit
  add_breadcrumb @user.name, admin_user_path(@user)
  add_breadcrumb "Edit", edit_admin_user_path(@user)
end

#enableObject



88
89
90
91
92
93
# File 'app/controllers/panda/core/admin/users_controller.rb', line 88

def enable
  @user.enable!
  UserActivity.log!(user: current_user, action: "enabled_user", resource: @user, request: request)
  flash[:success] = "#{@user.name} has been enabled."
  redirect_to admin_user_path(@user)
end

#indexObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/panda/core/admin/users_controller.rb', line 10

def index
  @users = User.with_attached_avatar
  @users = @users.search(params[:search]) if params[:search].present?

  case params[:status]
  when "enabled" then @users = @users.enabled
  when "disabled" then @users = @users.disabled
  when "invited" then @users = @users.invited
  end

  case params[:role]
  when "admin" then @users = @users.admins
  when "user" then @users = @users.where(User.admin_column => false)
  end

  @users = User.(@users, params)

  @users = case params[:sort]
  when "name" then @users.order(name: sort_direction)
  when "email" then @users.order(email: sort_direction)
  when "last_login" then @users.order(last_login_at: sort_direction)
  when "created" then @users.order(created_at: sort_direction)
  else @users.order(name: :asc)
  end

  @page = [params.fetch(:page, 1).to_i, 1].max
  @per_page = 25
  @total_count = @users.count
  @total_pages = (@total_count.to_f / @per_page).ceil
  @users = @users.offset((@page - 1) * @per_page).limit(@per_page)
end

#inviteObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/panda/core/admin/users_controller.rb', line 69

def invite
  result = InviteUserService.call(
    email: params[:email],
    name: params[:name],
    invited_by: current_user,
    admin: params[:admin] == "true"
  )

  if result.success?
    if (hook = Panda::Core.config.after_user_invited)
      hook.call(result.payload[:user], params, current_user)
    end
    flash[:success] = "Invitation sent to #{params[:email]}."
  else
    flash[:error] = result.errors.join(", ")
  end
  redirect_to admin_users_path
end

#revoke_sessionObject



153
154
155
156
157
158
159
160
161
162
# File 'app/controllers/panda/core/admin/users_controller.rb', line 153

def revoke_session
  session_record = @user.user_sessions.find(params[:session_id])
  session_record.revoke!(admin: current_user)
  UserActivity.log!(
    user: current_user, action: "revoked_session", resource: @user,
    request: request, metadata: {session_id: session_record.id}
  )
  flash[:success] = "Session revoked."
  redirect_back fallback_location: admin_user_path(@user)
end

#sessionsObject



140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/controllers/panda/core/admin/users_controller.rb', line 140

def sessions
  add_breadcrumb @user.name, admin_user_path(@user)
  add_breadcrumb "Sessions", sessions_admin_user_path(@user)
  @sessions = @user.user_sessions.recent
  @page = [params.fetch(:page, 1).to_i, 1].max
  @per_page = 25
  @total_count = @sessions.count
  @total_pages = (@total_count.to_f / @per_page).ceil
  @sessions = @sessions.offset((@page - 1) * @per_page).limit(@per_page)
  @active_sessions = @user.user_sessions.active_sessions.recent
  @revoked_sessions = @user.user_sessions.revoked_sessions.recent
end

#showObject



42
43
44
45
46
47
# File 'app/controllers/panda/core/admin/users_controller.rb', line 42

def show
  add_breadcrumb @user.name, admin_user_path(@user)
  @recent_activity = @user.user_activities.recent.limit(5)
  @active_sessions = @user.user_sessions.active_sessions.recent
  @active_sessions_count = @active_sessions.size
end

#updateObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/panda/core/admin/users_controller.rb', line 54

def update
  if @user.update(user_params)
    if (hook = Panda::Core.config.admin_user_after_update)
      hook.call(@user, params, current_user)
    end
    UserActivity.log!(user: current_user, action: "updated_user", resource: @user, request: request)
    flash[:success] = "User has been updated successfully."
    redirect_to admin_users_path
  else
    add_breadcrumb @user.name, admin_user_path(@user)
    add_breadcrumb "Edit", edit_admin_user_path(@user)
    render :edit, status: :unprocessable_content
  end
end