From 8db2ff289f2bd0927fdcdc5bcb6ec33126d27faa Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Mon, 23 Dec 2024 05:55:14 +0300 Subject: [PATCH 1/1] Use resourceful route for user status --- app/abilities/ability.rb | 3 +- app/controllers/users/statuses_controller.rb | 36 ++++++++ app/controllers/users_controller.rb | 28 ------- app/views/users/show.html.erb | 82 +++++++++---------- config/routes.rb | 4 +- .../users/statuses_controller_test.rb | 64 +++++++++++++++ test/controllers/users_controller_test.rb | 59 ------------- 7 files changed, 144 insertions(+), 132 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 9516a3012..651dc4973 100644 --- a/app/abilities/ability.rb +++ b/app/abilities/ability.rb @@ -67,7 +67,8 @@ class Ability can [:hide, :unhide], [DiaryEntry, DiaryComment] can [:read, :resolve, :ignore, :reopen], Issue can :create, IssueComment - can [:set_status, :destroy], User + + can [:update], :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 000000000..a66782edf --- /dev/null +++ b/app/controllers/users/statuses_controller.rb @@ -0,0 +1,36 @@ +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" + @user.soft_destroy! if params[:event] == "soft_destroy" # destroy a user, marking them as deleted and removing personal data + 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 904b960a2..a0be87bdc 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -14,7 +14,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 @@ -98,13 +97,6 @@ class UsersController < ApplicationController 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 go_public current_user.data_public = true current_user.save @@ -112,18 +104,6 @@ class UsersController < ApplicationController 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 @@ -237,14 +217,6 @@ class UsersController < ApplicationController 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 c168972aa..571976ee3 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -161,50 +161,48 @@ - <% if can?(:set_status, User) || can?(:destroy, User) %> + <% if can?(:update, :user_status) %>