Class: UsersController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- UsersController
- Includes:
- Devise::Controllers::Rememberable
- Defined in:
- /var/apps/qpixel/app/controllers/users_controller.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Method Summary collapse
- #activity ⇒ Object
- #annotate ⇒ Object
- #annotations ⇒ Object
- #avatar ⇒ Object
- #cleaned_profile_websites(profile_params) ⇒ Object
- #confirm_disconnect_sso ⇒ Object
- #default_filter ⇒ Object
- #delete_filter ⇒ Object
- #do_qr_login ⇒ Object
-
#filter_json(filter) ⇒ Hash
Converts a given filter to JSON.
- #filters ⇒ Object
- #filters_json ⇒ Object
- #full_log ⇒ Object
- #index ⇒ Object
- #me ⇒ Object
- #mod_privilege_action ⇒ Object
- #mod_privileges ⇒ Object
- #my_activity ⇒ Object
- #my_network ⇒ Object
- #my_vote_summary ⇒ Object
- #network ⇒ Object
- #posts ⇒ Object
- #preferences ⇒ Object
- #qr_login_code ⇒ Object
- #role_toggle ⇒ Object
- #set_filter ⇒ Object
- #set_preference ⇒ Object
- #show ⇒ Object
- #soft_delete ⇒ Object
- #specific_avatar ⇒ Object
- #stack_redirect ⇒ Object
-
#system_filters_json ⇒ Hash{String => Hash}
Gets system filters as JSON.
- #transfer_se_content ⇒ Object
- #update_profile ⇒ Object
- #vote_summary ⇒ Object
Methods inherited from ApplicationController
#dashboard, #keyboard_tools, #upload
Instance Method Details
#activity ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 255 def activity @posts = Post.undeleted.by(@user).count @comments = Comment.by(@user).joins(:comment_thread, :post).undeleted.where(comment_threads: { deleted: false }, posts: { deleted: false }).count @suggested_edits = SuggestedEdit.by(@user).count @edits = PostHistory.by(@user).of_type('post_edited').on_undeleted.count @all_edits = @suggested_edits + @edits items = case params[:filter] when 'posts' Post.undeleted.by(@user) when 'comments' Comment.by(@user).joins(:comment_thread, :post).undeleted.where(comment_threads: { deleted: false }, posts: { deleted: false }) when 'edits' SuggestedEdit.by(@user) + PostHistory.by(@user).of_type('post_edited').on_undeleted else Post.undeleted.by(@user) + Comment.by(@user).joins(:comment_thread, :post).undeleted.where(comment_threads: { deleted: false }, posts: { deleted: false }) + SuggestedEdit.by(@user).all + PostHistory.by(@user).on_undeleted.all end @items = items.sort_by(&:created_at).reverse.paginate(page: params[:page], per_page: 50) end |
#annotate ⇒ Object
579 580 581 582 583 584 585 586 587 588 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 579 def annotate @log = AuditLog.user_annotation(event_type: 'annotation', user: current_user, related: @user, comment: params[:comment]) if @log.errors.none? redirect_to user_annotations_path(@user) else flash[:danger] = 'Failed to save your annotation.' render :annotations end end |
#annotations ⇒ Object
573 574 575 576 577 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 573 def annotations @logs = AuditLog.where(log_type: 'user_annotation', related: @user) .newest_first .paginate(page: params[:page], per_page: 20) end |
#avatar ⇒ Object
608 609 610 611 612 613 614 615 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 608 def avatar respond_to do |format| format.png do size = params[:size]&.to_i&.positive? ? [params[:size]&.to_i, 256].min : 64 send_data helpers.user_auto_avatar(size, user: @user).to_blob, type: 'image/png', disposition: 'inline' end end end |
#cleaned_profile_websites(profile_params) ⇒ Object
353 354 355 356 357 358 359 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 353 def cleaned_profile_websites(profile_params) sites = profile_params[:user_websites_attributes] sites.transform_values do |w| w.merge({ label: w[:label].presence, url: w[:url].presence }) end end |
#confirm_disconnect_sso ⇒ Object
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 627 def confirm_disconnect_sso if current_user.sso_profile.blank? || !helpers.devise_sign_in_enabled? || !SiteSetting['AllowSsoDisconnect'] flash[:danger] = 'You cannot disable Single Sign-On.' redirect_to edit_user_registration_path return end if current_user.sso_profile.destroy current_user.send_reset_password_instructions flash[:success] = 'Successfully disconnected from Single Sign-On. Please see your email to set your password.' redirect_to edit_user_registration_path else flash[:danger] = 'Failed to disconnect from Single Sign-On.' redirect_to user_disconnect_sso_path end end |
#default_filter ⇒ Object
198 199 200 201 202 203 204 205 206 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 198 def default_filter if user_signed_in? && params[:category].present? default_filter = helpers.default_filter(current_user.id, params[:category].to_i) render json: { status: 'success', success: true, name: default_filter&.name } else render json: { status: 'failed', success: false }, status: :bad_request end end |
#delete_filter ⇒ Object
166 167 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 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 166 def delete_filter unless params[:name].present? return render json: { status: 'failed', success: false, errors: ['Filter name is required'] }, status: 400 end as_user = current_user if params[:system] == 'true' if current_user&.global_admin? as_user = helpers.system_user else return render json: { status: 'failed', success: false, errors: ['You do not have permission to delete'] }, status: 400 end end filter = Filter.find_by(user: as_user, name: params[:name]) unless filter.present? return render json: { status: 'failed', success: false, errors: ['Filter not found'] }, status: :not_found end if filter.destroy render json: { status: 'success', success: true, filters: filters_json } else render json: { status: 'failed', success: false, errors: ['Failed to delete'] }, status: 400 end end |
#do_qr_login ⇒ Object
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 557 def do_qr_login user = User.find_by(login_token: params[:token]) if user&.login_token_expires_at&.present? && user.login_token_expires_at >= DateTime.now flash[:success] = 'You are now signed in.' user.update(login_token: nil, login_token_expires_at: nil) sign_in user remember_me user AuditLog.user_history(event_type: 'mobile_login', related: user) redirect_to after_sign_in_path_for(user) else flash[:danger] = "That login link isn't valid. Codes expire after 5 minutes - if it's been longer than that, " \ 'get a new code and try again.' not_found! end end |
#filter_json(filter) ⇒ Hash
Converts a given filter to JSON
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 109 def filter_json(filter) { min_score: filter.min_score, max_score: filter.max_score, min_answers: filter.min_answers, max_answers: filter.max_answers, include_tags: Tag.where(id: filter.).map { |tag| [tag.name, tag.id] }, exclude_tags: Tag.where(id: filter.).map { |tag| [tag.name, tag.id] }, source: filter.source, status: filter.status, system: filter.system? } end |
#filters ⇒ Object
139 140 141 142 143 144 145 146 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 139 def filters respond_to do |format| format.html format.json do render json: filters_json end end end |
#filters_json ⇒ Object
131 132 133 134 135 136 137 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 131 def filters_json if user_signed_in? current_user.filters.to_h { |filter| [filter.name, filter_json(filter)] }.merge(system_filters_json) else system_filters_json end end |
#full_log ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 283 def full_log @posts = Post.by(@user).count @comments = Comment.by(@user).count @flags = Flag.by(@user).count @suggested_edits = SuggestedEdit.by(@user).count @edits = PostHistory.by(@user).count @mod_warnings_received = ModWarning.to(@user).count @all_edits = @suggested_edits + @edits @interesting_comments = Comment.by(@user).deleted.count @interesting_flags = Flag.by(@user).declined.count @interesting_edits = SuggestedEdit.by(@user).rejected.count @interesting_posts = Post.by(@user).problematic.count @interesting = @interesting_comments + @interesting_flags + @mod_warnings_received + @interesting_edits + @interesting_posts @items = (case params[:filter] when 'posts' Post.by(@user).all when 'comments' Comment.by(@user).all when 'flags' Flag.by(@user).all when 'edits' SuggestedEdit.by(@user).all + PostHistory.by(@user).all when 'warnings' ModWarning.to(@user).all when 'interesting' Comment.by(@user).deleted.all + Flag.by(@user).declined.all + SuggestedEdit.by(@user).rejected.all + Post.by(@user).problematic.all else Post.by(@user).all + Comment.by(@user).all + Flag.by(@user).all + SuggestedEdit.by(@user).all + PostHistory.by(@user).all + ModWarning.to(@user).all end).sort_by(&:created_at).reverse.paginate(page: params[:page], per_page: 50) end |
#index ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 25 def index @sort_param = { reputation: :reputation, age: :created_at }[params[:sort]&.to_sym] || :reputation @users = if params[:search].present? user_scope.search(params[:search]) else user_scope end @users = @users.where.not(deleted: true) .where.not(community_users: { deleted: true }) .order(@sort_param => :desc) .paginate(page: params[:page], per_page: 48) @post_counts = Post.where(user_id: @users.pluck(:id).uniq).group(:user_id).count respond_to do |format| format.html do render layout: 'application' end format.json do render json: @users end end end |
#me ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 70 def me @user = current_user respond_to do |format| format.html do redirect_to user_path(@user) end format.json do data = [:id, :username, :trust_level, :se_acct_id].to_h { |a| [a, @user.send(a)] } data_with_ac = data.merge({ is_standard: @user.standard?, is_admin: @user.admin?, is_global_admin: @user.global_admin?, is_moderator: @user.at_least_moderator?, is_global_moderator: @user.at_least_global_moderator? }) render json: data_with_ac end end end |
#mod_privilege_action ⇒ Object
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 460 def mod_privilege_action ability = Ability.find_by internal_id: params[:ability] return not_found! if ability.internal_id == 'mod' ua = @user.community_user.privilege(ability.internal_id) case params[:do] when 'grant' if ua.nil? @user.community_user.grant_privilege!(ability.internal_id) AuditLog.admin_audit(event_type: 'ability_grant', related: @user, user: current_user, comment: ability.internal_id.to_s) elsif ua.is_suspended ua.update is_suspended: false AuditLog.admin_audit(event_type: 'ability_unsuspend', related: @user, user: current_user, comment: "#{ability.internal_id} ability unsuspended") end when 'suspend' return not_found! if ua.nil? duration = params[:duration]&.to_i duration = duration <= 0 ? nil : duration.days.from_now = params[:message] ua.update is_suspended: true, suspension_end: duration, suspension_message: @user.create_notification("Your #{ability.name} ability has been suspended. Click for more information.", ability_url(ability.internal_id)) AuditLog.admin_audit(event_type: 'ability_suspend', related: @user, user: current_user, comment: "#{ability.internal_id} ability suspended\n\n#{}") when 'delete' return not_found! if ua.nil? ua.destroy AuditLog.admin_audit(event_type: 'ability_remove', related: @user, user: current_user, comment: "#{ability.internal_id} ability removed") AuditLog.user_history(event_type: 'deleted_ability', related: nil, user: @user, comment: ability.internal_id) else return not_found! end render json: { status: 'success' } end |
#mod_privileges ⇒ Object
323 324 325 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 323 def mod_privileges @abilities = Ability.all end |
#my_activity ⇒ Object
251 252 253 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 251 def my_activity redirect_to user_activity_path(current_user) end |
#my_network ⇒ Object
243 244 245 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 243 def my_network redirect_to network_path(current_user) end |
#my_vote_summary ⇒ Object
590 591 592 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 590 def my_vote_summary redirect_to vote_summary_path(current_user) end |
#network ⇒ Object
247 248 249 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 247 def network @communities = Community.all end |
#posts ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 225 def posts @posts = set_posts.user_sort({ term: params[:sort], default: :score }, activity: :last_activity, age: :created_at, score: :score) .order(created_at: :desc) .paginate(page: params[:page], per_page: 25) respond_to do |format| format.html do render :posts, layout: 'application' end format.json do render json: @posts end end end |
#preferences ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 92 def preferences current_user.validate_prefs! respond_to do |format| format.html do prefs = current_user.preferences @preferences = prefs[:global] @community_prefs = prefs[:community] end format.json do render json: current_user.preferences end end end |
#qr_login_code ⇒ Object
551 552 553 554 555 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 551 def qr_login_code @token = SecureRandom.urlsafe_base64(64) @qr_code = RQRCode::QRCode.new(qr_login_url(@token)) current_user.update(login_token: @token, login_token_expires_at: 5.minutes.from_now) end |
#role_toggle ⇒ Object
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 406 def role_toggle role_map = { mod: :is_moderator, admin: :is_admin, mod_global: :is_global_moderator, admin_global: :is_global_admin, staff: :staff } # values must match methods on the User model = { mod: :admin?, admin: :global_admin?, mod_global: :global_admin?, admin_global: :global_admin?, staff: :staff } unless role_map.keys.include?(params[:role].underscore.to_sym) render json: { status: 'error', message: "Role not found: #{params[:role]}" }, status: :bad_request end key = params[:role].underscore.to_sym attrib = role_map[key] = [key] return not_found! unless current_user.send() case key when :mod new_value = !@user.community_user.send(attrib) # Set/update ability if new_value @user.community_user.grant_privilege!('mod') else @user.community_user.privilege('mod')&.destroy end @user.community_user.update(attrib => new_value) when :admin new_value = !@user.community_user.send(attrib) @user.community_user.update(attrib => new_value) else new_value = !@user.send(attrib) @user.update(attrib => new_value) end AuditLog.admin_audit(event_type: 'role_toggle', related: @user, user: current_user, comment: "#{attrib} to #{new_value}") AbilityQueue.add(@user, 'Role Change') render json: { status: 'success' } end |
#set_filter ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 148 def set_filter if user_signed_in? && params[:name] filter = Filter.find_or_create_by(user: current_user, name: params[:name]) filter.update(filter_params) unless params[:category].nil? || params[:is_default].nil? helpers.set_filter_default(current_user.id, filter.id, params[:category].to_i, params[:is_default]) end render json: { status: 'success', success: true, filters: filters_json }, status: 200 else render json: { status: 'failed', success: false, errors: ['Filter name is required'] }, status: 400 end end |
#set_preference ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 208 def set_preference if !params[:name].nil? && !params[:value].nil? global_key = "prefs.#{current_user.id}" community_key = "prefs.#{current_user.id}.community.#{RequestContext.community_id}" key = params[:community].present? && params[:community] ? community_key : global_key current_user.validate_prefs! render json: { status: 'success', count: RequestContext.redis.hset(key, params[:name], params[:value].to_s), preferences: current_user.preferences } else render json: { status: 'failed', message: 'Failed to save the preference', errors: ['Both name and value parameters are required'] }, status: :bad_request end end |
#show ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 51 def show @abilities = Ability.on_user(@user) @limit = params[:limit]&.to_i || 15 @posts = set_posts.user_sort({ term: params[:sort], default: :score }, age: :created_at, score: :score) @total_post_count = if @user == current_user || current_user&.at_least_moderator? Post.all.by(@user).count else Post.all.by(@user) .undeleted .joins(:category) .where('categories.min_view_trust_level <= ?', current_user&.trust_level || 0) .count end @posts = @posts.first(@limit) end |
#soft_delete ⇒ Object
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 327 def soft_delete if @user.at_least_moderator? render json: { status: 'failed', message: 'Admins and moderators cannot be deleted.' }, status: :unprocessable_entity return end case params[:type] when 'profile' @user.community_user.soft_delete(current_user) when 'user' unless current_user.at_least_global_moderator? render json: { status: 'failed', message: 'Non-global moderator cannot perform global deletion.' }, status: :forbidden return end @user.soft_delete(current_user) else render json: { status: 'failed', message: 'Unrecognised deletion type.' }, status: 400 return end render json: { status: 'success', user: @user.id } end |
#specific_avatar ⇒ Object
617 618 619 620 621 622 623 624 625 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 617 def specific_avatar respond_to do |format| format.png do size = params[:size]&.to_i&.positive? ? params[:size]&.to_i : 64 send_data helpers.user_auto_avatar(size, letter: params[:letter], color: params[:color]).to_blob, type: 'image/png', disposition: 'inline' end end end |
#stack_redirect ⇒ Object
506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 506 def stack_redirect response = Net::HTTP.post_form(URI('https://stackoverflow.com/oauth/access_token/json'), 'client_id' => SiteSetting['SEApiClientId'], 'client_secret' => SiteSetting['SEApiClientSecret'], 'code' => params[:code], 'redirect_uri' => stack_redirect_url) access_token = JSON.parse(response.body)['access_token'] uri = "https://api.stackexchange.com/2.2/me/associated?key=#{SiteSetting['SEApiKey']}" \ "&access_token=#{access_token}&filter=!-rH86dva" accounts = JSON.parse(Net::HTTP.get(URI(uri))) network_id = accounts['items'][0]['account_id'] current_user.update(se_acct_id: network_id) redirect_to edit_user_profile_path end |
#system_filters_json ⇒ Hash{String => Hash}
Gets system filters as JSON
125 126 127 128 129 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 125 def system_filters_json Rails.cache.fetch 'default_system_filters', expires_in: 1.day do system_user.filters.to_h { |filter| [filter.name, filter_json(filter)] } end end |
#transfer_se_content ⇒ Object
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 521 def transfer_se_content unless params[:agree_to_relicense].present? && params[:agree_to_relicense] == 'true' flash[:danger] = 'To claim your content, we need you to agree to relicense your posts to us.' redirect_to(edit_user_profile_path) && return end auto_user = User.where(se_acct_id: current_user.se_acct_id).where.not(id: current_user.id).first if auto_user.nil? flash[:warning] = "There doesn't appear to be any of your content here." redirect_to(edit_user_profile_path) && return end community = RequestContext.community Thread.new do RequestContext.community = community ApplicationRecord.transaction do auto_user.posts.each do |post| post.reassign_user(current_user) post.remove_attribution_notice! end auto_user.reload.destroy current_user.update(transferred_content: true) end end flash[:success] = 'Your content is being transferred to you.' redirect_to edit_user_profile_path end |
#update_profile ⇒ Object
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 361 def update_profile profile_params = params.require(:user).permit(:username, :profile_markdown, :website, :discord, :twitter, user_websites_attributes: [:id, :label, :url]) if profile_params[:user_websites_attributes].present? profile_params[:user_websites_attributes] = cleaned_profile_websites(profile_params) end @user = current_user if params[:user][:avatar].present? if helpers.valid_upload?(params[:user][:avatar]) @user.avatar.attach(params[:user][:avatar]) else @user.errors.add(:avatar, 'must be a valid image') flash[:danger] = "Couldn't update your profile." render :edit_profile return end end new_profile_markdown = params[:user][:profile_markdown]&.strip if new_profile_markdown.present? profile_rendered = helpers.rendered_post(:user, :profile_markdown) profile_params = profile_params.merge(profile: profile_rendered) elsif new_profile_markdown && new_profile_markdown.empty? profile_params = profile_params.merge(profile: '') end status = @user.update(profile_params) if status flash[:success] = 'Your profile details were updated.' redirect_to user_path(current_user) else flash[:danger] = "Couldn't update your profile." render :edit_profile end end |
#vote_summary ⇒ Object
594 595 596 597 598 599 600 601 602 603 604 605 606 |
# File '/var/apps/qpixel/app/controllers/users_controller.rb', line 594 def vote_summary @votes = Vote.for(@user).includes(:post).group(:date_of, :post_id, :vote_type) @votes = @votes.select(:post_id, :vote_type) .select('count(*) as vote_count') .select('date(votes.created_at) as date_of') @votes = @votes.order(date_of: :desc, post_id: :desc).all .group_by(&:date_of).map do |k, vl| [k, vl.group_by(&:post), vl.sum { |v| v.vote_type * v.vote_count }] end .paginate(page: params[:page], per_page: 15) end |