Class: AdminController

Inherits:
ApplicationController show all
Defined in:
/var/apps/qpixel/app/controllers/admin_controller.rb

Overview

Web controller. Provides authenticated actions for use by administrators.

Instance Method Summary collapse

Methods inherited from ApplicationController

#dashboard, #keyboard_tools, #upload

Instance Method Details

#admin_emailObject



66
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 66

def admin_email; end

#all_emailObject



84
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 84

def all_email; end

#audit_logsObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 106

def audit_logs
  @page = helpers.safe_page(params)
  @per_page = helpers.safe_per_page(params)

  @log_types = AuditLog.unscoped.select(:log_type).distinct.map(&:log_type) - ['user_annotation', 'user_history']
  @event_types = AuditLog.unscoped.select(:event_type).distinct.map(&:event_type)

  @logs = if current_user.global_admin?
            AuditLog.unscoped.where.not(log_type: ['user_annotation', 'user_history'])
          else
            AuditLog.where.not(log_type: ['block_log', 'user_annotation', 'user_history'])
          end

  [:log_type, :event_type].each do |key|
    if params[key].present?
      @logs = @logs.where(key => params[key])
    end
  end

  if params[:from].present?
    @logs = @logs.where('date(created_at) >= ?', params[:from])
  end

  if params[:to].present?
    @logs = @logs.where('date(created_at) <= ?', params[:to])
  end

  @logs = @logs.user_sort({ term: params[:sort], default: :created_at },
                          age: :created_at, type: :log_type, event: :event_type,
                          related: Arel.sql('related_type DESC, related_id DESC'), user: :user_id)
               .paginate(page: @page, per_page: @per_page)

  render layout: 'without_sidebar'
end

#change_backObject



238
239
240
241
242
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 238

def change_back
  return not_found! unless session[:impersonator_id].present?

  @impersonator = User.find session[:impersonator_id]
end

#change_usersObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 222

def change_users
  unless params[:comment].present? || Rails.env.development?
    flash[:danger] = 'Please explain why you are impersonating this user.'
    render :impersonate
    return
  end

  dev_id = current_user.id
  AuditLog.admin_audit(event_type: 'impersonation_start', related: @user, user: current_user,
                       comment: params[:comment])
   @user
  session[:impersonator_id] = dev_id
  flash[:success] = "You are now impersonating #{@user.username}."
  redirect_to root_path
end

#create_siteObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 145

def create_site
  @new_community = Community.create(name: params[:community][:name], host: params[:community][:host])

  # Run Seeds
  Rails.application.load_seed

  # Manage Site Settings
  settings = SiteSetting.for_community_id(@new_community.id)
  settings.find_by(name: 'SiteName').update(value: @new_community.name)

  # Audit Log
  AuditLog.admin_audit(event_type: 'new_site', related: @new_community, user: current_user,
                       comment: "<<Community #{@new_community.attributes_print}>>")

  # Clear cache
  Rails.cache.clear

  # Render template
  render
end

#do_email_queryObject



264
265
266
267
268
269
270
271
272
273
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 264

def do_email_query
  users = User.where(email: params[:email])
  if users.any?
    @user = users.first
    @profiles = @user.community_users.includes(:community).where(community: current_user.admin_communities)
  else
    flash[:danger] = helpers.i18ns('admin.errors.email_query_not_found')
  end
  render :email_query
end

#error_reportsObject



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
41
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 13

def error_reports
  base_scope = if current_user.global_admin?
                 ErrorLog.all
               else
                 ErrorLog.where(community: RequestContext.community)
               end

  @error_types = base_scope.select(:klass).distinct.to_a.map(&:klass)

  base_scope = base_scope.includes(:community, :user)

  if params[:type].present?
    base_scope = base_scope.where(klass: params[:type])
  end

  if params[:uuid].present?
    base_scope = base_scope.where(uuid: params[:uuid])
  end

  if params[:version].present? && params[:version] == 'current'
    sha, _date = helpers.current_commit
    base_scope = base_scope.where(version: sha)
  elsif params[:version].present?
    base_scope = base_scope.where(version: params[:version])
  end

  @reports = base_scope.newest_first.paginate(page: params[:page], per_page: 50)
  render layout: 'without_sidebar'
end

#failbanObject



208
209
210
211
212
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 208

def failban
  @user.block("user manually blocked by admin ##{current_user.id}")
  flash[:success] = t 'admin.user_fed_stat'
  redirect_back fallback_location: admin_path
end

#impersonateObject



214
215
216
217
218
219
220
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 214

def impersonate
  if Rails.env.development?
    change_users
  else
    render layout: 'without_sidebar'
  end
end

#indexObject



11
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 11

def index; end

#new_siteObject



141
142
143
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 141

def new_site
  @new_community = Community.new
end

#privilegesObject



43
44
45
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 43

def privileges
  @abilities = Ability.all
end

#send_admin_emailObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 68

def send_admin_email
  community = RequestContext.community

  AdminMailer.with(body_markdown: params[:body_markdown],
                   subject: params[:subject],
                   community: community)
             .to_moderators
             .deliver_later

  AuditLog.admin_audit(event_type: 'send_admin_email', user: current_user,
                       comment: "Subject: #{params[:subject]}")

  flash[:success] = t('admin.email_being_sent')
  redirect_to admin_path
end

#send_all_emailObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 86

def send_all_email
  community = RequestContext.community

  emails = User.where.not(confirmed_at: nil).where('email NOT LIKE ?', '%localhost').select(:email).map(&:email)
  emails.each_slice(49) do |slice|
    AdminMailer.with(body_markdown: params[:body_markdown],
                     subject: params[:subject],
                     emails: slice,
                     community: community)
               .to_all_users
               .deliver_later
  end

  AuditLog.admin_audit(event_type: 'send_all_email', user: current_user,
                       comment: "Subject: #{params[:subject]}")

  flash[:success] = t 'admin.email_being_sent'
  redirect_to admin_path
end

#setupObject



166
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 166

def setup; end

#setup_saveObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 168

def setup_save
  settings = SiteSetting.for_community_id(@community.id)
  default_settings = SiteSetting.for_community_id(Community.first.id)

  # Set settings from config page
  { primary_color: 'SiteCategoryHeaderDefaultColor', logo_url: 'SiteLogoPath', ad_slogan: 'SiteAdSlogan',
    mathjax: 'MathJaxEnabled', syntax_highlighting: 'SyntaxHighlightingEnabled', chat_link: 'ChatLink',
    analytics_url: 'AnalyticsURL', analytics_id: 'AnalyticsSiteId', content_transfer: 'AllowContentTransfer' }
    .each do |key, setting|
    settings.find_by(name: setting).update(value: params[key])
  end

  # Auto-load settings
  ['AdminBadgeCharacter', 'ModBadgeCharacter', 'SEApiClientId', 'SEApiClientSecret', 'SEApiKey',
   'AdministratorContactEmail'].each do |setting|
    settings.find_by(name: setting)
            .update(value: default_settings.find_by(name: setting).value)
  end

  # Generate meta tags
  required_tags = ['discussion', 'support', 'feature-request', 'bug']
  status_tags = ['status-completed', 'status-declined', 'status-review', 'status-planned', 'status-deferred']
  tags = required_tags + status_tags
  Tag.create(tags.map { |t| { name: t, community_id: @community.id, tag_set: TagSet.meta } })

  Category.where(name: 'Q&A').last.update tag_set: TagSet.main
  Category.where(name: 'Meta').last.update tag_set: TagSet.meta

  # Set Meta tags as required/mod-only
  meta_category = Category.where(name: 'Meta').last
  meta_category.required_tags << Tag.unscoped.where(community: @community, name: required_tags)
  meta_category.moderator_tags << Tag.unscoped.where(community: @community, name: status_tags)

  Rails.cache.clear
  AuditLog.admin_audit(event_type: 'setup_site', related: @new_community, user: current_user,
                       comment: 'Site Settings updated via /admin/setup')

  render
end

#show_privilegeObject



47
48
49
50
51
52
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 47

def show_privilege
  @ability = Ability.find_by internal_id: params[:name]
  respond_to do |format|
    format.json { render json: @ability }
  end
end

#update_privilegeObject



54
55
56
57
58
59
60
61
62
63
64
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 54

def update_privilege
  @ability = Ability.find_by internal_id: params[:name]
  type = ['post', 'edit', 'flag'].include?(params[:type]) ? params[:type] : nil
  return not_found! if type.nil?

  pre = @ability.send(:"#{type}_score_threshold")
  @ability.update("#{type}_score_threshold" => params[:threshold])
  AuditLog.admin_audit(event_type: 'ability_threshold_update', related: @ability, user: current_user,
                       comment: "#{params[:type]} score\nfrom <<#{pre}>>\nto <<#{params[:threshold]}>>")
  render json: { status: 'OK', privilege: @ability }, status: :accepted
end

#verify_elevationObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File '/var/apps/qpixel/app/controllers/admin_controller.rb', line 244

def verify_elevation
  return not_found! unless session[:impersonator_id].present?

  @impersonator = User.find session[:impersonator_id]
  if @impersonator&.sso_profile.present?
    session.delete :impersonator_id
    AuditLog.admin_audit(event_type: 'impersonation_end', related: current_user, user: @impersonator)
    sign_out @impersonator
    redirect_to new_saml_user_session_path
  elsif @impersonator&.valid_password? params[:password]
    session.delete :impersonator_id
    AuditLog.admin_audit(event_type: 'impersonation_end', related: current_user, user: @impersonator)
     @impersonator
    redirect_to root_path
  else
    flash[:danger] = 'Incorrect password.'
    render :change_back
  end
end