Module: MarkdownServer::Helpers::AdminHelpers

Defined in:
lib/markdown_server/helpers/admin_helpers.rb

Instance Method Summary collapse

Instance Method Details

#admin?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/markdown_server/helpers/admin_helpers.rb', line 33

def admin?
  return true if settings.admin_all
  return true if session[:admin]

  adm = setup_config["admin"]
  return false unless adm.is_a?(Hash)

  return true if ip_allowed?(adm["ip"], client_ip)

  if adm["user"] && adm["pw"]
    auth = request.env["HTTP_AUTHORIZATION"].to_s
    if auth.start_with?("Basic ")
      user, pw = Base64.decode64(auth[6..]).split(":", 2)
      return true if user == adm["user"].to_s && pw == adm["pw"].to_s
    end
  end

  false
end

#admin_only_mode?Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/markdown_server/helpers/admin_helpers.rb', line 24

def admin_only_mode?
  return true if settings.admin_only
  setup_config["admin_only"] == true
end

#admin_only_public_route?(path) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/markdown_server/helpers/admin_helpers.rb', line 29

def admin_only_public_route?(path)
  path == "/admin/login" || path == "/admin/logout" || path == "/robots.txt"
end

#client_ipObject



15
16
17
18
19
20
21
22
# File 'lib/markdown_server/helpers/admin_helpers.rb', line 15

def client_ip
  if settings.behind_proxy
    fwd = env["HTTP_X_FORWARDED_FOR"].to_s.split(",").map(&:strip).first
    fwd && !fwd.empty? ? fwd : env["REMOTE_ADDR"]
  else
    env["REMOTE_ADDR"]
  end
end

#setup_configObject



6
7
8
9
10
11
12
13
# File 'lib/markdown_server/helpers/admin_helpers.rb', line 6

def setup_config
  @setup_config ||= begin
    path = File.join(root_dir, ".setup.yml")
    (File.exist?(path) && YAML.safe_load(File.read(path))) || {}
  rescue StandardError
    {}
  end
end