mirror of
https://github.com/indentlabs/notebook.git
synced 2025-10-26 11:19:22 +00:00
139 lines
3.4 KiB
Ruby
139 lines
3.4 KiB
Ruby
# Superclass for all model controllers
|
|
class ApplicationController < ActionController::Base
|
|
protect_from_forgery
|
|
|
|
helper :html
|
|
helper :my_content
|
|
|
|
helper_method :nl2br
|
|
|
|
helper_method :universe_filter
|
|
|
|
# Rails changed cookie format in rails 4, so log out all old users so that
|
|
# they get the new version
|
|
rescue_from JSON::ParserError, with: :force_user_logout
|
|
def force_user_logout
|
|
reset_session
|
|
redirect_to login_path
|
|
end
|
|
|
|
# View Helpers
|
|
def nl2br(string)
|
|
# simple_format string
|
|
string.gsub('\n\r', '<br>').gsub('\r', '').gsub('\n', '<br />').html_safe
|
|
end
|
|
|
|
def universe_filter
|
|
return if Universe.where(user_id: session[:user]).length.zero?
|
|
return if @selected_universe_filter
|
|
@selected_universe_filter = t(:all_universes)
|
|
end
|
|
|
|
# Authentication
|
|
def logged_in?
|
|
session && session[:user]
|
|
end
|
|
|
|
def redirect_if_not_logged_in
|
|
return if logged_in?
|
|
redirect_to signup_path, notice: t(:must_be_logged_in)
|
|
end
|
|
|
|
def create_anonymous_account_if_not_logged_in
|
|
return if logged_in?
|
|
|
|
@user = create_anonymous_user
|
|
|
|
if @user.save
|
|
session[:user] = @user.id
|
|
session[:anon_user] = true
|
|
else
|
|
# layman's collision detection
|
|
create_anonymous_account_if_not_logged_in
|
|
end
|
|
end
|
|
|
|
# Require ownership
|
|
|
|
def require_ownership_of_character
|
|
redirect_if_not_owned Character.find(params[:id]), character_list_path
|
|
end
|
|
|
|
def require_ownership_of_equipment
|
|
redirect_if_not_owned Equipment.find(params[:id]), equipment_list_path
|
|
end
|
|
|
|
def require_ownership_of_language
|
|
redirect_if_not_owned Language.find(params[:id]), language_list_path
|
|
end
|
|
|
|
def require_ownership_of_location
|
|
redirect_if_not_owned Location.find(params[:id]), location_list_path
|
|
end
|
|
|
|
def require_ownership_of_magic
|
|
redirect_if_not_owned Magic.find(params[:id]), magic_list_path
|
|
end
|
|
|
|
def require_ownership_of_universe
|
|
redirect_if_not_owned Universe.find(params[:id]), universe_list_path
|
|
end
|
|
|
|
# Hide, if private
|
|
|
|
def hide_private_universe
|
|
return if Universe.find(params[:id]).privacy.downcase == 'public'
|
|
redirect_to universe_list_path, notice: t(:no_view_permission)
|
|
end
|
|
|
|
def hide_private_character
|
|
redirect_if_private Character.find(params[:id]), character_list_path
|
|
end
|
|
|
|
def hide_private_equipment
|
|
redirect_if_private Equipment.find(params[:id]), equipment_list_path
|
|
end
|
|
|
|
def hide_private_language
|
|
redirect_if_private Language.find(params[:id]), language_list_path
|
|
end
|
|
|
|
def hide_private_location
|
|
redirect_if_private Location.find(params[:id]), location_list_path
|
|
end
|
|
|
|
def hide_private_magic
|
|
redirect_if_private Magic.find(params[:id]), magic_list_path
|
|
end
|
|
|
|
private
|
|
|
|
def create_anonymous_user
|
|
id = rand(10_000_000).to_s + rand(10_000_000).to_s # lol
|
|
|
|
User.new(
|
|
name: 'Anonymous-' + id.to_s,
|
|
email: id.to_s + '@localhost',
|
|
password: id.to_s)
|
|
end
|
|
|
|
def redirect_if_not_owned(object_to_check, redirect_path)
|
|
return if owned_by_current_user? object_to_check
|
|
redirect_to redirect_path, notice: t(:no_do_permission)
|
|
end
|
|
|
|
def redirect_if_private(object_to_check, redirect_path)
|
|
return if public? object_to_check
|
|
redirect_to redirect_path, notice: t(:no_view_permission)
|
|
end
|
|
|
|
def owned_by_current_user?(object)
|
|
session[:user] && session[:user] == object.user.id
|
|
end
|
|
|
|
def public?(object)
|
|
(owned_by_current_user? object) || \
|
|
(object.universe && object.universe.privacy.downcase == 'public')
|
|
end
|
|
end
|