2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
class UsersController < ApplicationController
|
2019-11-26 17:03:43 -05:00
|
|
|
respond_to :html, :json
|
2018-04-02 13:51:26 -04:00
|
|
|
skip_before_action :api_check
|
2020-06-15 21:12:08 -04:00
|
|
|
before_action :logged_in_only, only: [:edit, :upload_limit, :update]
|
2019-12-29 05:37:50 -05:00
|
|
|
before_action :member_only, only: [:custom_style, :upload_limit]
|
2010-03-11 19:42:04 -05:00
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
def new
|
2019-09-19 18:21:04 -04:00
|
|
|
raise User::PrivilegeError.new("Already signed in") unless CurrentUser.is_anonymous?
|
2019-08-17 12:00:26 -04:00
|
|
|
return access_denied("Signups are disabled") unless Danbooru.config.enable_signups?
|
2010-03-11 19:42:04 -05:00
|
|
|
@user = User.new
|
2011-02-02 15:53:28 -05:00
|
|
|
respond_with(@user)
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
def edit
|
2020-06-15 21:12:08 -04:00
|
|
|
@user = User.find(CurrentUser.id)
|
2011-02-02 15:53:28 -05:00
|
|
|
check_privilege(@user)
|
|
|
|
respond_with(@user)
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
def index
|
2013-05-13 15:25:52 -04:00
|
|
|
if params[:name].present?
|
2022-10-13 09:36:56 -04:00
|
|
|
redirect_to user_path(id: params[:name])
|
2013-05-13 15:25:52 -04:00
|
|
|
else
|
2022-04-02 15:35:25 -04:00
|
|
|
@users = User.search(search_params).includes(:user_status).paginate(params[:page], limit: params[:limit], search_count: params[:search])
|
2013-05-13 15:25:52 -04:00
|
|
|
respond_with(@users) do |format|
|
2018-05-16 19:39:31 -04:00
|
|
|
format.json do
|
2018-05-22 17:57:46 -04:00
|
|
|
render json: @users.to_json
|
|
|
|
expires_in params[:expiry].to_i.days if params[:expiry]
|
2018-05-16 19:39:31 -04:00
|
|
|
end
|
2013-03-29 15:37:28 -04:00
|
|
|
end
|
|
|
|
end
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2019-08-17 12:00:26 -04:00
|
|
|
def home
|
2019-09-28 01:27:18 -04:00
|
|
|
@user = CurrentUser.user
|
2019-08-17 12:00:26 -04:00
|
|
|
end
|
|
|
|
|
2012-02-20 15:33:42 -05:00
|
|
|
def search
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2019-12-29 05:37:50 -05:00
|
|
|
def upload_limit
|
|
|
|
@presenter = UserPresenter.new(CurrentUser.user)
|
|
|
|
pieces = CurrentUser.upload_limit_pieces
|
|
|
|
@approved_count = pieces[:approved]
|
|
|
|
@deleted_count = pieces[:deleted]
|
|
|
|
@pending_count = pieces[:pending]
|
|
|
|
respond_with(CurrentUser.user)
|
|
|
|
end
|
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
def show
|
2020-03-26 05:55:41 -04:00
|
|
|
@user = User.find(User.name_or_id_to_id_forced(params[:id]))
|
2011-08-15 17:10:35 -04:00
|
|
|
@presenter = UserPresenter.new(@user)
|
2017-04-29 11:45:24 -04:00
|
|
|
respond_with(@user, methods: @user.full_attributes)
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
def create
|
2019-09-19 18:21:04 -04:00
|
|
|
raise User::PrivilegeError.new("Already signed in") unless CurrentUser.is_anonymous?
|
2019-08-31 19:08:16 -04:00
|
|
|
raise User::PrivilegeError.new("Signups are disabled") unless Danbooru.config.enable_signups?
|
2020-05-02 13:29:53 -04:00
|
|
|
User.transaction do
|
2021-12-20 08:47:05 -05:00
|
|
|
@user = User.new(user_params(:create).merge({last_ip_addr: request.remote_ip}))
|
2022-10-16 09:03:16 -04:00
|
|
|
@user.validate_email_format = true
|
2020-05-02 13:29:53 -04:00
|
|
|
@user.email_verification_key = '1' if Danbooru.config.enable_email_verification?
|
|
|
|
if !Danbooru.config.enable_recaptcha? || verify_recaptcha(model: @user)
|
|
|
|
@user.save
|
|
|
|
if @user.errors.empty?
|
|
|
|
session[:user_id] = @user.id
|
2021-12-20 08:47:05 -05:00
|
|
|
session[:ph] = @user.password_token
|
2020-05-02 13:29:53 -04:00
|
|
|
if Danbooru.config.enable_email_verification?
|
|
|
|
Maintenance::User::EmailConfirmationMailer.confirmation(@user).deliver_now
|
|
|
|
end
|
|
|
|
else
|
|
|
|
flash[:notice] = "Sign up failed: #{@user.errors.full_messages.join("; ")}"
|
2019-10-21 22:37:49 -04:00
|
|
|
end
|
2020-05-02 13:29:53 -04:00
|
|
|
set_current_user
|
|
|
|
respond_with(@user)
|
2017-12-21 22:57:37 -05:00
|
|
|
else
|
2020-05-02 13:29:53 -04:00
|
|
|
flash[:notice] = "Sign up failed"
|
|
|
|
respond_with(@user)
|
2017-09-16 15:44:17 -04:00
|
|
|
end
|
2011-09-15 18:02:00 -04:00
|
|
|
end
|
2020-05-02 13:29:53 -04:00
|
|
|
rescue ::Mailgun::CommunicationError
|
|
|
|
session[:user_id] = nil
|
|
|
|
@user.errors.add(:email, "There was a problem with your email that prevented sign up")
|
|
|
|
@user.id = nil
|
|
|
|
flash[:notice] = "There was a problem with your email that prevented sign up"
|
|
|
|
respond_with(@user)
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
def update
|
2020-06-15 21:12:08 -04:00
|
|
|
@user = User.find(CurrentUser.id)
|
2022-10-16 09:03:16 -04:00
|
|
|
@user.validate_email_format = true
|
2011-02-02 15:53:28 -05:00
|
|
|
check_privilege(@user)
|
2018-04-02 13:51:26 -04:00
|
|
|
@user.update(user_params(:update))
|
2013-11-20 15:48:51 -05:00
|
|
|
if @user.errors.any?
|
|
|
|
flash[:notice] = @user.errors.full_messages.join("; ")
|
2015-06-30 16:13:57 -04:00
|
|
|
else
|
|
|
|
flash[:notice] = "Settings updated"
|
2013-11-20 15:48:51 -05:00
|
|
|
end
|
2018-04-26 22:31:53 -04:00
|
|
|
respond_with(@user) do |format|
|
|
|
|
format.html { redirect_back fallback_location: edit_user_path(@user) }
|
|
|
|
end
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2019-08-01 01:06:18 -04:00
|
|
|
def custom_style
|
|
|
|
@css = CustomCss.parse(CurrentUser.user.custom_style)
|
|
|
|
expires_in 10.years
|
|
|
|
end
|
|
|
|
|
2018-04-02 13:51:26 -04:00
|
|
|
private
|
2015-10-26 17:27:03 -04:00
|
|
|
|
2011-02-02 15:53:28 -05:00
|
|
|
def check_privilege(user)
|
2023-12-03 09:36:37 -05:00
|
|
|
raise User::PrivilegeError unless user.id == CurrentUser.id || CurrentUser.is_admin?
|
2020-04-16 21:01:03 -04:00
|
|
|
raise User::PrivilegeError.new("Must verify account email") unless CurrentUser.is_verified?
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|
2018-04-02 13:51:26 -04:00
|
|
|
|
|
|
|
def user_params(context)
|
|
|
|
permitted_params = %i[
|
2019-04-28 09:59:29 -04:00
|
|
|
password old_password password_confirmation
|
2018-04-02 13:51:26 -04:00
|
|
|
comment_threshold default_image_size favorite_tags blacklisted_tags
|
2020-02-24 00:31:12 -05:00
|
|
|
time_zone per_page custom_style description_collapsed_initially hide_comments
|
2018-04-02 13:51:26 -04:00
|
|
|
|
2019-06-01 10:58:10 -04:00
|
|
|
receive_email_notifications enable_keyboard_navigation
|
2020-02-05 16:48:59 -05:00
|
|
|
enable_privacy_mode disable_user_dmails blacklist_users show_post_statistics
|
2020-11-21 18:12:32 -05:00
|
|
|
style_usernames show_hidden_comments
|
2019-06-01 11:33:26 -04:00
|
|
|
enable_auto_complete
|
2024-02-23 11:06:50 -05:00
|
|
|
disable_cropped_thumbnails
|
|
|
|
enable_safe_mode disable_responsive_mode
|
2018-04-02 13:51:26 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
permitted_params += [dmail_filter_attributes: %i[id words]]
|
2020-01-01 20:36:38 -05:00
|
|
|
permitted_params += [:profile_about, :profile_artinfo, :avatar_id] if CurrentUser.is_member? # Prevent editing when blocked
|
2020-01-06 14:58:22 -05:00
|
|
|
permitted_params += [:enable_compact_uploader] if context != :create && CurrentUser.post_upload_count >= 10
|
2019-04-28 09:59:29 -04:00
|
|
|
permitted_params += [:name, :email] if context == :create
|
2018-04-02 13:51:26 -04:00
|
|
|
|
|
|
|
params.require(:user).permit(permitted_params)
|
|
|
|
end
|
2020-03-06 14:37:13 -05:00
|
|
|
|
2022-02-06 09:19:08 -05:00
|
|
|
def search_params
|
2023-08-01 14:06:46 -04:00
|
|
|
permitted_params = %i[name_matches about_me avatar_id level min_level max_level can_upload_free can_approve_posts order]
|
2022-11-28 10:21:40 -05:00
|
|
|
permitted_params += %i[ip_addr email_matches] if CurrentUser.is_admin?
|
2022-02-06 09:19:08 -05:00
|
|
|
permit_search_params permitted_params
|
2020-03-06 14:37:13 -05:00
|
|
|
end
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|