forked from code-dot-org/code-dot-org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.rb
126 lines (103 loc) · 3.1 KB
/
helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require 'cdo/aws/s3'
require 'rack/csrf'
require_relative '../shared/middleware/helpers/storage_id'
def avatar_image(name, width=320, square_photo=false)
basename = name.downcase.gsub(/\W/, '_').gsub(/_+/, '_')
path = resolve_image("images/avatars/#{basename}")
return nil unless path
dimensions = "fit-#{width}"
dimensions = "fill-#{width}x#{width}" if square_photo == true
"/images/#{dimensions}/avatars/#{File.basename(path)}"
end
def authentication_required!(url=request.url)
dont_cache
return if dashboard_user_helper
redirect((request.scheme || 'http') + ':' + CDO.studio_url("/users/sign_in?user_return_to=#{url}"), 302)
end
def dont_cache
cache_control(:private, :must_revalidate, max_age: 0)
end
def cache_for(seconds, proxy_seconds=nil)
proxy_seconds ||= seconds / 2
cache_control(:public, :must_revalidate, max_age: seconds, s_maxage: proxy_seconds)
end
# Sets caching headers based on the document type,
# based on the :x_max_age and :x_proxy_max_age Sinatra settings.
def cache(type)
max_age = settings.method("#{type}_max_age").call
proxy_max_age = settings.method("#{type}_proxy_max_age").call
cache_for(max_age, proxy_max_age)
end
def canonical_hostname(domain)
CDO.canonical_hostname(domain)
end
def studio_url(path = '')
port = (!rack_env?(:development) || CDO.https_development) ? '' : ":#{CDO.dashboard_port}"
"//#{canonical_hostname('studio.code.org')}#{port}/#{path}"
end
def code_org_url(path = '')
port = (!rack_env?(:development) || CDO.https_development) ? '' : ":#{CDO.pegasus_port}"
"//#{canonical_hostname('code.org')}#{port}/#{path}"
end
def forbidden!
halt(403, "Forbidden\n")
end
def form_error!(e)
halt(400, {'Content-Type' => 'text/json'}, e.errors.to_json)
end
def have_permission?(permission)
return false unless dashboard_user_helper
dashboard_user_helper.has_permission?(permission)
end
def no_content!
halt(204, "No content\n")
end
def not_authorized!
halt(401, "Not authorized\n")
end
def not_found!
path = resolve_template('views', settings.template_extnames, '404')
content = path ? document(path) : "Not found\n"
halt(404, content)
end
def only_for(site)
if site.is_a?(Array)
pass unless site.include?(request.site)
else
pass unless request.site == site
end
end
def service_unavailable!
halt(503, "Service Unavailable\n")
end
def unsupported_media_type!
halt(415, "Unsupported Media Type\n")
end
def csrf_token
Rack::Csrf.csrf_token(env)
end
def csrf_tag
Rack::Csrf.csrf_tag(env)
end
def language_dir_class(locale=request.locale)
# This list of RTL languages matches those in dashboard/config/locales.yml
if ["ar-SA", "fa-IR", "he-IL", "ur-PK"].include? locale
"rtl"
else
"ltr"
end
end
def curriculum_url(resource)
CDO.curriculum_url(request.locale, resource)
end
def verify_signature(token)
request.body.rewind
payload_body = request.body.read
signature = 'sha1=' + OpenSSL::HMAC.hexdigest(
OpenSSL::Digest.new('sha1'),
token,
payload_body
)
Rack::Utils.secure_compare(signature, request.env['HTTP_X_HUB_SIGNATURE'])
end
Dir.glob(pegasus_dir('helpers/*.rb')).sort.each {|path| require path}