Fail loudly if we forget to whitelist a param instead of silently ignoring it. misc models: convert to strong params. artist commentaries: convert to strong params. * Disallow changing or setting post_id to a nonexistent post. artists: convert to strong params. * Disallow setting `is_banned` in create/update actions. Changing it this way instead of with the ban/unban actions would leave the artist in a partially banned state. bans: convert to strong params. * Disallow changing the user_id after the ban has been created. comments: convert to strong params. favorite groups: convert to strong params. news updates: convert to strong params. post appeals: convert to strong params. post flags: convert to strong params. * Disallow users from setting the `is_deleted` / `is_resolved` flags. ip bans: convert to strong params. user feedbacks: convert to strong params. * Disallow users from setting `disable_dmail_notification` when creating feedbacks. * Disallow changing the user_id after the feedback has been created. notes: convert to strong params. wiki pages: convert to strong params. * Also fix non-Builders being able to delete wiki pages. saved searches: convert to strong params. pools: convert to strong params. * Disallow setting `post_count` or `is_deleted` in create/update actions. janitor trials: convert to strong params. post disapprovals: convert to strong params. * Factor out quick-mod bar to shared partial. * Fix quick-mod bar to use `Post#is_approvable?` to determine visibility of Approve button. dmail filters: convert to strong params. password resets: convert to strong params. user name change requests: convert to strong params. posts: convert to strong params. users: convert to strong params. * Disallow setting password_hash, last_logged_in_at, last_forum_read_at, has_mail, and dmail_filter_attributes[user_id]. * Remove initialize_default_image_size (dead code). uploads: convert to strong params. * Remove `initialize_status` because status already defaults to pending in the database. tag aliases/implications: convert to strong params. tags: convert to strong params. forum posts: convert to strong params. * Disallow changing the topic_id after creating the post. * Disallow setting is_deleted (destroy/undelete actions should be used instead). * Remove is_sticky / is_locked (nonexistent attributes). forum topics: convert to strong params. * merges https://github.com/evazion/danbooru/tree/wip-rails-5.1 * lock pg gem to 0.21 (1.0.0 is incompatible with rails 5.1.4) * switch to factorybot and change all references Co-authored-by: r888888888 <r888888888@gmail.com> Co-authored-by: evazion <noizave@gmail.com> add diffs
119 lines
3.4 KiB
Ruby
119 lines
3.4 KiB
Ruby
class SessionLoader
|
|
class AuthenticationFailure < Exception ; end
|
|
|
|
attr_reader :session, :cookies, :request, :params
|
|
|
|
def initialize(session, cookies, request, params)
|
|
@session = session
|
|
@cookies = cookies
|
|
@request = request
|
|
@params = params
|
|
end
|
|
|
|
def load
|
|
CurrentUser.user = AnonymousUser.new
|
|
CurrentUser.ip_addr = request.remote_ip
|
|
|
|
if Rails.env.test? && Thread.current[:test_user_id]
|
|
load_for_test(Thread.current[:test_user_id])
|
|
elsif session[:user_id]
|
|
load_session_user
|
|
elsif cookie_password_hash_valid?
|
|
load_cookie_user
|
|
else
|
|
load_session_for_api
|
|
end
|
|
|
|
set_statement_timeout
|
|
update_last_logged_in_at
|
|
update_last_ip_addr
|
|
set_time_zone
|
|
store_favorite_tags_in_cookies
|
|
CurrentUser.user.unban! if CurrentUser.user.ban_expired?
|
|
end
|
|
|
|
private
|
|
|
|
def load_for_test(user_id)
|
|
CurrentUser.user = User.find(user_id)
|
|
CurrentUser.ip_addr = "127.0.0.1"
|
|
end
|
|
|
|
def set_statement_timeout
|
|
timeout = CurrentUser.user.statement_timeout
|
|
ActiveRecord::Base.connection.execute("set statement_timeout = #{timeout}")
|
|
end
|
|
|
|
def load_session_for_api
|
|
if request.authorization
|
|
authenticate_basic_auth
|
|
|
|
elsif params[:login].present? && params[:api_key].present?
|
|
authenticate_api_key(params[:login], params[:api_key])
|
|
|
|
elsif params[:login].present? && params[:password_hash].present?
|
|
authenticate_legacy_api_key(params[:login], params[:password_hash])
|
|
end
|
|
end
|
|
|
|
def authenticate_basic_auth
|
|
credentials = ::Base64.decode64(request.authorization.split(' ', 2).last || '')
|
|
login, api_key = credentials.split(/:/, 2)
|
|
authenticate_api_key(login, api_key)
|
|
end
|
|
|
|
def authenticate_api_key(name, api_key)
|
|
CurrentUser.user = User.authenticate_api_key(name, api_key)
|
|
|
|
if CurrentUser.user.nil?
|
|
raise AuthenticationFailure.new
|
|
end
|
|
end
|
|
|
|
def authenticate_legacy_api_key(name, password_hash)
|
|
CurrentUser.user = User.authenticate_hash(name, password_hash)
|
|
|
|
if CurrentUser.user.nil?
|
|
raise AuthenticationFailure.new
|
|
end
|
|
end
|
|
|
|
def load_session_user
|
|
CurrentUser.user = User.find_by_id(session[:user_id])
|
|
end
|
|
|
|
def load_cookie_user
|
|
CurrentUser.user = User.find_by_name(cookies.signed[:user_name])
|
|
session[:user_id] = CurrentUser.user.id
|
|
end
|
|
|
|
def cookie_password_hash_valid?
|
|
cookies[:password_hash] && cookies.signed[:user_name] && User.authenticate_cookie_hash(cookies.signed[:user_name], cookies[:password_hash])
|
|
end
|
|
|
|
def store_favorite_tags_in_cookies
|
|
if (cookies[:favorite_tags].blank? || cookies[:favorite_tags_with_categories].blank?) && CurrentUser.user.favorite_tags.present?
|
|
favorite_tags = CurrentUser.user.favorite_tags.slice(0, 1024)
|
|
cookies[:favorite_tags] = favorite_tags
|
|
cookies[:favorite_tags_with_categories] = Tag.categories_for(favorite_tags.scan(/\S+/)).to_a.flatten.join(" ")
|
|
end
|
|
end
|
|
|
|
def update_last_logged_in_at
|
|
return if CurrentUser.is_anonymous?
|
|
return if CurrentUser.last_logged_in_at && CurrentUser.last_logged_in_at > 1.week.ago
|
|
CurrentUser.user.update_attribute(:last_logged_in_at, Time.now)
|
|
end
|
|
|
|
def update_last_ip_addr
|
|
return if CurrentUser.is_anonymous?
|
|
return if CurrentUser.user.last_ip_addr == @request.remote_ip
|
|
CurrentUser.user.update_attribute(:last_ip_addr, @request.remote_ip)
|
|
end
|
|
|
|
def set_time_zone
|
|
Time.zone = CurrentUser.user.time_zone
|
|
end
|
|
end
|
|
|