From 0764432abf0b9d534b479efefa1a467da574cd19 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Mon, 23 Dec 2024 05:55:14 +0300 Subject: [PATCH] Use resourceful route for user status --- app/abilities/ability.rb | 3 +- app/controllers/users/statuses_controller.rb | 42 ++++++++++++ app/controllers/users_controller.rb | 28 -------- app/views/users/show.html.erb | 20 +++--- config/routes.rb | 4 +- .../users/statuses_controller_test.rb | 68 +++++++++++++++++++ test/controllers/users_controller_test.rb | 59 ---------------- 7 files changed, 124 insertions(+), 100 deletions(-) create mode 100644 app/controllers/users/statuses_controller.rb create mode 100644 test/controllers/users/statuses_controller_test.rb diff --git a/app/abilities/ability.rb b/app/abilities/ability.rb index e4b9dcf6bf..f5201aed1f 100644 --- a/app/abilities/ability.rb +++ b/app/abilities/ability.rb @@ -66,7 +66,8 @@ def initialize(user) can [:hide, :unhide], [DiaryEntry, DiaryComment] can [:read, :resolve, :ignore, :reopen], Issue can :create, IssueComment - can [:set_status, :destroy], User + + can [:update, :destroy], :user_status can [:read, :update], :users_list can [:create, :destroy], UserRole end diff --git a/app/controllers/users/statuses_controller.rb b/app/controllers/users/statuses_controller.rb new file mode 100644 index 0000000000..4b2e636458 --- /dev/null +++ b/app/controllers/users/statuses_controller.rb @@ -0,0 +1,42 @@ +module Users + class StatusesController < ApplicationController + layout "site" + + before_action :authorize_web + before_action :set_locale + before_action :check_database_readable + + authorize_resource :class => :user_status + + before_action :lookup_user_by_name + + ## + # sets a user's status + def update + @user.activate! if params[:event] == "activate" + @user.confirm! if params[:event] == "confirm" + @user.unconfirm! if params[:event] == "unconfirm" + @user.hide! if params[:event] == "hide" + @user.unhide! if params[:event] == "unhide" + @user.unsuspend! if params[:event] == "unsuspend" + redirect_to user_path(params[:user_display_name]) + end + + ## + # destroy a user, marking them as deleted and removing personal data + def destroy + @user.soft_destroy! + redirect_to user_path(params[:user_display_name]) + end + + private + + ## + # ensure that there is a "user" instance variable + def lookup_user_by_name + @user = User.find_by!(:display_name => params[:user_display_name]) + rescue ActiveRecord::RecordNotFound + redirect_to user_path(params[:user_display_name]) unless @user + end + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 471215c922..e38f03698f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -15,7 +15,6 @@ class UsersController < ApplicationController before_action :check_database_writable, :only => [:new, :go_public] before_action :require_cookies, :only => [:new] - before_action :lookup_user_by_name, :only => [:set_status, :destroy] allow_thirdparty_images :only => :show allow_social_login :only => :new @@ -82,13 +81,6 @@ def create end end - ## - # destroy a user, marking them as deleted and removing personal data - def destroy - @user.soft_destroy! - redirect_to user_path(:display_name => params[:display_name]) - end - def terms @legale = params[:legale] || OSM.ip_to_country(request.remote_ip) || Settings.default_legale @text = OSM.legal_text_for_country(@legale) @@ -147,18 +139,6 @@ def go_public redirect_to edit_account_path end - ## - # sets a user's status - def set_status - @user.activate! if params[:event] == "activate" - @user.confirm! if params[:event] == "confirm" - @user.unconfirm! if params[:event] == "unconfirm" - @user.hide! if params[:event] == "hide" - @user.unhide! if params[:event] == "unhide" - @user.unsuspend! if params[:event] == "unsuspend" - redirect_to user_path(:display_name => params[:display_name]) - end - ## # omniauth success callback def auth_success @@ -289,14 +269,6 @@ def welcome_options(referer = nil) end end - ## - # ensure that there is a "user" instance variable - def lookup_user_by_name - @user = User.find_by(:display_name => params[:display_name]) - rescue ActiveRecord::RecordNotFound - redirect_to :action => "view", :display_name => params[:display_name] unless @user - end - ## # return permitted user parameters def user_params diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index f392245022..55dea1e08f 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -161,50 +161,50 @@ - <% if can?(:set_status, User) || can?(:destroy, User) %> + <% if can?(:update, :user_status) || can?(:destroy, User) %>