locomotivecms_steam 1.2.1 → 1.3.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/Gemfile +3 -2
- data/Gemfile.lock +57 -50
- data/LICENSE +1 -1
- data/README.md +1 -1
- data/config/locales/nl.yml +2 -2
- data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_entry.rb +8 -0
- data/lib/locomotive/steam/adapters/memory/condition.rb +1 -1
- data/lib/locomotive/steam/configuration.rb +1 -1
- data/lib/locomotive/steam/decorators/template_decorator.rb +1 -1
- data/lib/locomotive/steam/entities/content_entry.rb +17 -0
- data/lib/locomotive/steam/entities/content_type.rb +1 -0
- data/lib/locomotive/steam/entities/content_type_field.rb +1 -0
- data/lib/locomotive/steam/entities/editable_element.rb +4 -0
- data/lib/locomotive/steam/errors.rb +35 -5
- data/lib/locomotive/steam/initializers/sprockets.rb +6 -80
- data/lib/locomotive/steam/liquid/drops/page.rb +9 -2
- data/lib/locomotive/steam/liquid/filters/date.rb +109 -0
- data/lib/locomotive/steam/liquid/tags/action.rb +6 -1
- data/lib/locomotive/steam/liquid/tags/authorize.rb +65 -0
- data/lib/locomotive/steam/liquid/tags/snippet.rb +10 -2
- data/lib/locomotive/steam/liquid/template.rb +3 -1
- data/lib/locomotive/steam/middlewares/auth.rb +187 -0
- data/lib/locomotive/steam/middlewares/redirection.rb +24 -0
- data/lib/locomotive/steam/middlewares/renderer.rb +20 -3
- data/lib/locomotive/steam/middlewares/sitemap.rb +3 -1
- data/lib/locomotive/steam/middlewares/templatized_page.rb +1 -6
- data/lib/locomotive/steam/middlewares/thread_safe.rb +19 -0
- data/lib/locomotive/steam/middlewares/url_redirection.rb +14 -1
- data/lib/locomotive/steam/repositories.rb +5 -1
- data/lib/locomotive/steam/repositories/content_entry_repository.rb +1 -1
- data/lib/locomotive/steam/repositories/content_type_field_repository.rb +4 -0
- data/lib/locomotive/steam/server.rb +2 -0
- data/lib/locomotive/steam/services.rb +9 -1
- data/lib/locomotive/steam/services/action_service.rb +26 -7
- data/lib/locomotive/steam/services/asset_host_service.rb +6 -1
- data/lib/locomotive/steam/services/auth_service.rb +105 -0
- data/lib/locomotive/steam/services/content_entry_service.rb +14 -0
- data/lib/locomotive/steam/services/external_api_service.rb +59 -25
- data/lib/locomotive/steam/services/liquid_parser_service.rb +5 -1
- data/lib/locomotive/steam/services/page_redirection_service.rb +29 -0
- data/lib/locomotive/steam/version.rb +1 -1
- data/locomotivecms_steam.gemspec +18 -16
- data/spec/fixtures/default/app/content_types/accounts.yml +59 -0
- data/spec/fixtures/default/app/content_types/songs.yml +2 -1
- data/spec/fixtures/default/app/views/pages/about_us/john_doe.fr.liquid.haml +5 -1
- data/spec/fixtures/default/app/views/pages/about_us/john_doe.liquid.haml +1 -1
- data/spec/fixtures/default/app/views/pages/account/forgot_password.liquid +39 -0
- data/spec/fixtures/default/app/views/pages/account/me.liquid +15 -0
- data/spec/fixtures/default/app/views/pages/account/reset_password.liquid +42 -0
- data/spec/fixtures/default/app/views/pages/account/sign_in.liquid +49 -0
- data/spec/fixtures/default/app/views/pages/all.liquid.haml +1 -1
- data/spec/fixtures/default/app/views/pages/emails/reset_password.liquid +15 -0
- data/spec/fixtures/default/app/views/pages/songs/template.liquid.haml +1 -1
- data/spec/fixtures/default/config/metafields_schema.yml +10 -0
- data/spec/fixtures/default/config/site.yml +5 -0
- data/spec/fixtures/default/config/translations.yml +25 -1
- data/spec/fixtures/default/data/accounts.yml +15 -0
- data/spec/fixtures/mongodb/locomotive_accounts.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_accounts.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_activities.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_activities.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_content_assets.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_content_assets.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_content_entries.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_content_entries.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_content_types.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_content_types.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_pages.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_pages.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_sites.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_sites.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_snippets.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_snippets.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_theme_assets.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_theme_assets.metadata.json +1 -1
- data/spec/fixtures/mongodb/locomotive_translations.bson +0 -0
- data/spec/fixtures/mongodb/locomotive_translations.metadata.json +1 -1
- data/spec/integration/repositories/content_entry_repository_spec.rb +1 -1
- data/spec/integration/repositories/content_type_repository_spec.rb +1 -1
- data/spec/integration/repositories/page_repository_spec.rb +3 -3
- data/spec/integration/repositories/theme_asset_repository_spec.rb +1 -1
- data/spec/integration/repositories/translation_repository_spec.rb +1 -1
- data/spec/integration/server/auth_spec.rb +196 -0
- data/spec/integration/server/basic_spec.rb +18 -0
- data/spec/integration/server/nav_spec.rb +1 -1
- data/spec/integration/server/sitemap_spec.rb +1 -1
- data/spec/integration/services/content_entry_service_spec.rb +1 -1
- data/spec/integration/services/external_api_service_spec.rb +9 -0
- data/spec/support/helpers.rb +1 -1
- data/spec/unit/adapters/filesystem/yaml_loaders/content_entry_spec.rb +17 -5
- data/spec/unit/adapters/filesystem/yaml_loaders/content_type_spec.rb +4 -4
- data/spec/unit/adapters/filesystem/yaml_loaders/page_spec.rb +7 -7
- data/spec/unit/adapters/filesystem/yaml_loaders/site_spec.rb +1 -1
- data/spec/unit/adapters/filesystem/yaml_loaders/translation_spec.rb +1 -1
- data/spec/unit/entities/content_entry_spec.rb +10 -0
- data/spec/unit/errors_spec.rb +2 -2
- data/spec/unit/initializers/sprockets_spec.rb +0 -14
- data/spec/unit/liquid/drops/page_spec.rb +3 -2
- data/spec/unit/liquid/filters/date_spec.rb +219 -0
- data/spec/unit/liquid/tags/action_spec.rb +9 -0
- data/spec/unit/liquid/tags/authorize_spec.rb +51 -0
- data/spec/unit/liquid/tags/link_to_spec.rb +1 -1
- data/spec/unit/liquid/tags/paginate_spec.rb +1 -1
- data/spec/unit/liquid/tags/snippet_spec.rb +10 -0
- data/spec/unit/middlewares/auth_spec.rb +31 -0
- data/spec/unit/middlewares/redirection_spec.rb +37 -0
- data/spec/unit/middlewares/url_redirection_spec.rb +20 -1
- data/spec/unit/services/action_service_spec.rb +57 -1
- data/spec/unit/services/asset_host_service_spec.rb +15 -0
- data/spec/unit/services/auth_service_spec.rb +156 -0
- data/spec/unit/services/external_api_service_spec.rb +22 -0
- data/spec/unit/services/page_redirection_service_spec.rb +49 -0
- metadata +96 -40
- data/CHANGELOG.md +0 -29
- data/spec/fixtures/mongodb/system.indexes.bson +0 -0
@@ -2,69 +2,13 @@ require 'sprockets'
|
|
2
2
|
require 'sass'
|
3
3
|
require 'coffee_script'
|
4
4
|
require 'compass'
|
5
|
+
require 'uglifier'
|
5
6
|
require 'autoprefixer-rails'
|
6
|
-
require 'open3'
|
7
7
|
|
8
8
|
require 'execjs'
|
9
9
|
|
10
10
|
module Locomotive::Steam
|
11
11
|
|
12
|
-
class YUICompressorRuntimeError < RuntimeError
|
13
|
-
attr_reader :errors
|
14
|
-
#:nocov:
|
15
|
-
def initialize(msg, errors)
|
16
|
-
super(msg)
|
17
|
-
@errors = errors
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
module YUICompressorErrors
|
22
|
-
|
23
|
-
#:nocov:
|
24
|
-
def compress(stream_or_string)
|
25
|
-
streamify(stream_or_string) do |stream|
|
26
|
-
tempfile = new_tempfile(stream)
|
27
|
-
full_command = "%s %s" % [command, tempfile.path]
|
28
|
-
|
29
|
-
output, errors, exit_status = _compress(full_command, tempfile)
|
30
|
-
|
31
|
-
if exit_status.exitstatus.zero?
|
32
|
-
output
|
33
|
-
else
|
34
|
-
# Bourne shells tend to blow up here when the command fails, usually
|
35
|
-
# because java is missing
|
36
|
-
raise YUICompressorRuntimeError.new("Command '%s' returned non-zero exit status" %
|
37
|
-
full_command, errors)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
#:nocov:
|
43
|
-
def _compress(command, tempfile)
|
44
|
-
begin
|
45
|
-
# FIXME: catch only useful information from the stderr output
|
46
|
-
# output, errors, exit_status = '', [], nil
|
47
|
-
output, errors, exit_status = Open3.capture3(command)
|
48
|
-
errors = errors.split("\n").find_all { |l| l =~ /\s+[0-9]/ }
|
49
|
-
[output, errors, exit_status]
|
50
|
-
rescue Exception => e
|
51
|
-
# windows shells tend to blow up here when the command fails
|
52
|
-
raise RuntimeError, "compression failed: %s" % e.message
|
53
|
-
ensure
|
54
|
-
tempfile.close!
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
#:nocov:
|
59
|
-
def new_tempfile(stream)
|
60
|
-
Tempfile.new('yui_compress').tap do |tempfile|
|
61
|
-
tempfile.write stream.read
|
62
|
-
tempfile.flush
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
12
|
class SprocketsEnvironment < ::Sprockets::Environment
|
69
13
|
|
70
14
|
attr_reader :steam_path
|
@@ -76,7 +20,7 @@ module Locomotive::Steam
|
|
76
20
|
|
77
21
|
append_steam_paths
|
78
22
|
|
79
|
-
|
23
|
+
install_minifiers if options[:minify]
|
80
24
|
|
81
25
|
install_autoprefixer
|
82
26
|
|
@@ -97,24 +41,10 @@ module Locomotive::Steam
|
|
97
41
|
Compass::Frameworks::ALL.each { |f| append_path(f.stylesheets_directory) }
|
98
42
|
end
|
99
43
|
|
100
|
-
def
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
require 'yui/compressor'
|
105
|
-
|
106
|
-
[YUI::JavaScriptCompressor, YUI::CssCompressor].each do |klass|
|
107
|
-
klass.send(:include, YUICompressorErrors)
|
108
|
-
end
|
109
|
-
|
110
|
-
# minify javascripts and stylesheets
|
111
|
-
self.js_compressor = YUI::JavaScriptCompressor.new
|
112
|
-
self.css_compressor = YUI::CssCompressor.new
|
113
|
-
else
|
114
|
-
message = "[Important] YUICompressor requires java to be installed. The JAVA_HOME variable should also be set.\n"
|
115
|
-
Locomotive::Common::Logger.warn message.red
|
116
|
-
false
|
117
|
-
end
|
44
|
+
def install_minifiers
|
45
|
+
# minify javascripts and stylesheets
|
46
|
+
self.js_compressor = :uglify
|
47
|
+
self.css_compressor = :scss
|
118
48
|
end
|
119
49
|
|
120
50
|
def install_autoprefixer
|
@@ -134,10 +64,6 @@ module Locomotive::Steam
|
|
134
64
|
end
|
135
65
|
end
|
136
66
|
|
137
|
-
def is_java_installed?
|
138
|
-
`which java` != '' && (!ENV['JAVA_HOME'].blank? && File.exists?(ENV['JAVA_HOME']))
|
139
|
-
end
|
140
|
-
|
141
67
|
end
|
142
68
|
|
143
69
|
end
|
@@ -4,7 +4,7 @@ module Locomotive
|
|
4
4
|
module Drops
|
5
5
|
class Page < I18nBase
|
6
6
|
|
7
|
-
delegate :fullpath, :depth, :seo_title, :meta_keywords, :meta_description, :redirect_url, :handle, to: :@_source
|
7
|
+
delegate :position, :fullpath, :depth, :seo_title, :meta_keywords, :meta_description, :redirect_url, :handle, to: :@_source
|
8
8
|
delegate :listed?, :published?, :redirect?, :is_layout?, :templatized?, to: :@_source
|
9
9
|
|
10
10
|
def title
|
@@ -30,7 +30,14 @@ module Locomotive
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def breadcrumbs
|
33
|
-
@breadcrumbs
|
33
|
+
return @breadcrumbs if @breadcrumbs
|
34
|
+
|
35
|
+
# remove the last one and replace it by the current instance
|
36
|
+
# which may have a valid reference to a content entry (if templatized)
|
37
|
+
pages = liquify(*repository.ancestors_of(@_source))
|
38
|
+
pages.pop
|
39
|
+
|
40
|
+
@breadcrumbs = pages + [self]
|
34
41
|
end
|
35
42
|
|
36
43
|
def children
|
@@ -66,6 +66,115 @@ module Locomotive
|
|
66
66
|
I18n.l input, format: format, locale: locale
|
67
67
|
end
|
68
68
|
|
69
|
+
def end_of_year(input)
|
70
|
+
return '' if input.blank?
|
71
|
+
|
72
|
+
if input.respond_to? :end_of_year
|
73
|
+
return input.end_of_year
|
74
|
+
end
|
75
|
+
|
76
|
+
if input.is_a?(String)
|
77
|
+
Time.zone.parse(input).end_of_year rescue ''
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def beginning_of_year(input)
|
82
|
+
return '' if input.blank?
|
83
|
+
|
84
|
+
if input.respond_to? :beginning_of_year
|
85
|
+
return input.beginning_of_year
|
86
|
+
end
|
87
|
+
|
88
|
+
if input.is_a?(String)
|
89
|
+
Time.zone.parse(input).beginning_of_year rescue ''
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def end_of_month(input)
|
94
|
+
return '' if input.blank?
|
95
|
+
|
96
|
+
if input.respond_to? :end_of_month
|
97
|
+
return input.end_of_month
|
98
|
+
end
|
99
|
+
|
100
|
+
if input.is_a?(String)
|
101
|
+
Time.zone.parse(input).end_of_month rescue ''
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def beginning_of_month(input)
|
106
|
+
return '' if input.blank?
|
107
|
+
|
108
|
+
if input.respond_to? :beginning_of_month
|
109
|
+
return input.beginning_of_month
|
110
|
+
end
|
111
|
+
|
112
|
+
if input.is_a?(String)
|
113
|
+
Time.zone.parse(input).beginning_of_month rescue ''
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def end_of_week(input)
|
118
|
+
return '' if input.blank?
|
119
|
+
|
120
|
+
if input.respond_to? :end_of_week
|
121
|
+
return input.end_of_week
|
122
|
+
end
|
123
|
+
|
124
|
+
if input.is_a?(String)
|
125
|
+
Time.zone.parse(input).end_of_week rescue ''
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def beginning_of_week(input)
|
130
|
+
return '' if input.blank?
|
131
|
+
|
132
|
+
if input.respond_to? :beginning_of_week
|
133
|
+
return input.beginning_of_week
|
134
|
+
end
|
135
|
+
|
136
|
+
if input.is_a?(String)
|
137
|
+
Time.zone.parse(input).beginning_of_week rescue ''
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def end_of_day(input)
|
142
|
+
return '' if input.blank?
|
143
|
+
|
144
|
+
if input.respond_to? :end_of_day
|
145
|
+
return input.end_of_day
|
146
|
+
end
|
147
|
+
|
148
|
+
if input.is_a?(String)
|
149
|
+
Time.zone.parse(input).end_of_day rescue ''
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def beginning_of_day(input)
|
154
|
+
return '' if input.blank?
|
155
|
+
|
156
|
+
if input.respond_to? :beginning_of_day
|
157
|
+
return input.beginning_of_day
|
158
|
+
end
|
159
|
+
|
160
|
+
if input.is_a?(String)
|
161
|
+
Time.zone.parse(input).beginning_of_day rescue ''
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def adjust_date(input, adjustment, unit)
|
166
|
+
return '' if input.blank?
|
167
|
+
|
168
|
+
adjustment = adjustment.to_i
|
169
|
+
unit = unit.to_sym
|
170
|
+
|
171
|
+
if (adjustment.respond_to? unit)
|
172
|
+
input + adjustment.send(unit)
|
173
|
+
else
|
174
|
+
input
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
69
178
|
alias :format_date :localized_date
|
70
179
|
|
71
180
|
private
|
@@ -38,7 +38,12 @@ module Locomotive
|
|
38
38
|
|
39
39
|
def render(context)
|
40
40
|
Locomotive::Common::Logger.info "[action] executing #{@description}"
|
41
|
-
|
41
|
+
begin
|
42
|
+
service(context).run(super, safe_params(context), context)
|
43
|
+
rescue Locomotive::Steam::ActionError => e
|
44
|
+
e.action = @description
|
45
|
+
raise e
|
46
|
+
end
|
42
47
|
''
|
43
48
|
end
|
44
49
|
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Steam
|
3
|
+
module Liquid
|
4
|
+
module Tags
|
5
|
+
|
6
|
+
# Redirect the current site visitor to another page if she/he
|
7
|
+
# is not authenticated.
|
8
|
+
# More information about the authentication feature here:
|
9
|
+
# https://locomotive-v3.readme.io/v3.3/docs/introduction-1
|
10
|
+
#
|
11
|
+
# The Liquid tag requires 2 parameters:
|
12
|
+
# - the slug of the content type used for the authentication (a content type with a password field)
|
13
|
+
# - the handle of the page we want the user to be redirected to if unauthenticated
|
14
|
+
#
|
15
|
+
# Basically the authorize tag checks if the liquid context has
|
16
|
+
# a reference to the current authenticated content entry.
|
17
|
+
# If not, it raises a redirection exception forcing the Steam middleware stack
|
18
|
+
# to process a HTTP redirection.
|
19
|
+
#
|
20
|
+
# Example:
|
21
|
+
#
|
22
|
+
# {% authorize 'accounts', 'sign_in' %}
|
23
|
+
#
|
24
|
+
class Authorize < ::Liquid::Tag
|
25
|
+
|
26
|
+
Syntax = /(#{::Liquid::QuotedString}+)\s*,\s*(#{::Liquid::QuotedString}+)/o
|
27
|
+
|
28
|
+
def initialize(tag_name, markup, options)
|
29
|
+
if markup =~ Syntax
|
30
|
+
@content_type_slug, @page_handle = $1.try(:gsub, /['"]/, ''), $2.try(:gsub, /['"]/, '')
|
31
|
+
else
|
32
|
+
raise ::Liquid::SyntaxError.new("Syntax Error in 'authorize' - Valid syntax: authorize [content type slug], [page handle]")
|
33
|
+
end
|
34
|
+
|
35
|
+
super
|
36
|
+
end
|
37
|
+
|
38
|
+
def render(context)
|
39
|
+
@context = context
|
40
|
+
|
41
|
+
unless authenticated_entry = context["current_#{@content_type_slug.singularize}"]
|
42
|
+
services.page_redirection.redirect_to(@page_handle, locale)
|
43
|
+
end
|
44
|
+
|
45
|
+
''
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def locale
|
51
|
+
@context.registers[:locale]
|
52
|
+
end
|
53
|
+
|
54
|
+
def services
|
55
|
+
@context.registers[:services]
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
::Liquid::Template.register_tag('authorize'.freeze, Authorize)
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -6,7 +6,7 @@ module Locomotive
|
|
6
6
|
class Snippet < ::Liquid::Include
|
7
7
|
|
8
8
|
def parse(tokens)
|
9
|
-
ActiveSupport::Notifications.instrument(
|
9
|
+
ActiveSupport::Notifications.instrument('steam.parse.include', page: options[:page], name: @template_name)
|
10
10
|
|
11
11
|
# look for editable elements
|
12
12
|
name = evaluate_snippet_name
|
@@ -17,7 +17,15 @@ module Locomotive
|
|
17
17
|
|
18
18
|
def render(context)
|
19
19
|
@template_name = evaluate_snippet_name(context)
|
20
|
-
|
20
|
+
# @options doesn't include the page key if cache is on
|
21
|
+
@options[:page] = context.registers[:page]
|
22
|
+
|
23
|
+
begin
|
24
|
+
super
|
25
|
+
rescue Locomotive::Steam::ParsingRenderingError => e
|
26
|
+
e.file = @template_name + ' [Snippet]'
|
27
|
+
raise e
|
28
|
+
end
|
21
29
|
end
|
22
30
|
|
23
31
|
private
|
@@ -0,0 +1,187 @@
|
|
1
|
+
module Locomotive::Steam
|
2
|
+
module Middlewares
|
3
|
+
|
4
|
+
# Process all the authentication actions:
|
5
|
+
# - sign in
|
6
|
+
# - new reset password
|
7
|
+
# - reset password
|
8
|
+
# - sign out
|
9
|
+
#
|
10
|
+
# It is also in charge to load the current authenticated resource
|
11
|
+
# from the session and put it in the liquid context.
|
12
|
+
#
|
13
|
+
class Auth < ThreadSafe
|
14
|
+
|
15
|
+
include Helpers
|
16
|
+
|
17
|
+
def _call
|
18
|
+
load_authenticated_entry
|
19
|
+
|
20
|
+
auth_options = AuthOptions.new(site, params)
|
21
|
+
|
22
|
+
return unless auth_options.valid?
|
23
|
+
|
24
|
+
send(:"#{auth_options.action}", auth_options)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def sign_in(options)
|
30
|
+
return if authenticated?
|
31
|
+
|
32
|
+
status, entry = services.auth.sign_in(options)
|
33
|
+
|
34
|
+
if status == :signed_in
|
35
|
+
store_authenticated(entry)
|
36
|
+
redirect_to options.callback || mounted_on
|
37
|
+
end
|
38
|
+
|
39
|
+
append_message(status)
|
40
|
+
end
|
41
|
+
|
42
|
+
def sign_out(options)
|
43
|
+
return unless authenticated?
|
44
|
+
|
45
|
+
store_authenticated(nil)
|
46
|
+
|
47
|
+
append_message(:signed_out)
|
48
|
+
end
|
49
|
+
|
50
|
+
def forgot_password(options)
|
51
|
+
return if authenticated?
|
52
|
+
|
53
|
+
status = services.auth.forgot_password(options, default_liquid_context)
|
54
|
+
|
55
|
+
append_message(status)
|
56
|
+
end
|
57
|
+
|
58
|
+
def reset_password(options)
|
59
|
+
return if authenticated?
|
60
|
+
|
61
|
+
status, entry = services.auth.reset_password(options)
|
62
|
+
|
63
|
+
if status == :password_reset
|
64
|
+
store_authenticated(entry)
|
65
|
+
redirect_to options.callback || mounted_on
|
66
|
+
end
|
67
|
+
|
68
|
+
append_message(status)
|
69
|
+
end
|
70
|
+
|
71
|
+
def load_authenticated_entry
|
72
|
+
entry_type = request.session[:authenticated_entry_type]
|
73
|
+
entry_id = request.session[:authenticated_entry_id]
|
74
|
+
|
75
|
+
if entry = services.auth.find_authenticated_resource(entry_type, entry_id)
|
76
|
+
env['authenticated_entry'] = entry
|
77
|
+
liquid_assigns["current_#{entry_type.singularize}"] = entry
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def authenticated?
|
82
|
+
!!env['authenticated_entry']
|
83
|
+
end
|
84
|
+
|
85
|
+
def store_authenticated(entry)
|
86
|
+
type = entry ? entry.content_type.slug : request.session[:authenticated_entry_type]
|
87
|
+
|
88
|
+
request.session[:authenticated_entry_type] = type.to_s
|
89
|
+
request.session[:authenticated_entry_id] = entry.try(:_id).to_s
|
90
|
+
|
91
|
+
log "[Auth] authenticated #{type.to_s.singularize} ##{entry.try(:_id).to_s}"
|
92
|
+
|
93
|
+
liquid_assigns["current_#{type.singularize}"] = entry
|
94
|
+
end
|
95
|
+
|
96
|
+
def append_message(message)
|
97
|
+
log "[Auth] status message = #{message.inspect}"
|
98
|
+
|
99
|
+
message ||= 'error'
|
100
|
+
liquid_assigns["auth_#{message}"] = "auth_#{message}"
|
101
|
+
end
|
102
|
+
|
103
|
+
class AuthOptions
|
104
|
+
|
105
|
+
ACTIONS = %w(sign_in sign_out forgot_password reset_password)
|
106
|
+
|
107
|
+
attr_reader :site, :params
|
108
|
+
|
109
|
+
def initialize(site, params)
|
110
|
+
@site, @params = site, params
|
111
|
+
end
|
112
|
+
|
113
|
+
def valid?
|
114
|
+
ACTIONS.include?(action)
|
115
|
+
end
|
116
|
+
|
117
|
+
def action
|
118
|
+
params[:auth_action]
|
119
|
+
end
|
120
|
+
|
121
|
+
def type
|
122
|
+
params[:auth_content_type]
|
123
|
+
end
|
124
|
+
|
125
|
+
def id_field
|
126
|
+
params[:auth_id_field] || :email
|
127
|
+
end
|
128
|
+
|
129
|
+
def password_field
|
130
|
+
params[:auth_password_field].try(:to_sym) || :password
|
131
|
+
end
|
132
|
+
|
133
|
+
def id
|
134
|
+
params[:auth_id]
|
135
|
+
end
|
136
|
+
|
137
|
+
def password
|
138
|
+
params[:auth_password]
|
139
|
+
end
|
140
|
+
|
141
|
+
def callback
|
142
|
+
params[:auth_callback]
|
143
|
+
end
|
144
|
+
|
145
|
+
def reset_password_url
|
146
|
+
params[:auth_reset_password_url]
|
147
|
+
end
|
148
|
+
|
149
|
+
def reset_token
|
150
|
+
params[:auth_reset_token]
|
151
|
+
end
|
152
|
+
|
153
|
+
def from
|
154
|
+
params[:auth_email_from] || 'support@locomotivecms.com'
|
155
|
+
end
|
156
|
+
|
157
|
+
def subject
|
158
|
+
params[:auth_email_subject] || 'Instructions for changing your password'
|
159
|
+
end
|
160
|
+
|
161
|
+
def email_handle
|
162
|
+
params[:auth_email_handle]
|
163
|
+
end
|
164
|
+
|
165
|
+
def smtp
|
166
|
+
name = params[:auth_email_smtp_namespace] || 'smtp'
|
167
|
+
namespace = site.metafields.try(:[], name)
|
168
|
+
|
169
|
+
if namespace.blank?
|
170
|
+
Locomotive::Common::Logger.error "[Auth] Missing SMTP settings in the Site metafields. Namespace: #{name}".light_red
|
171
|
+
return {}
|
172
|
+
end
|
173
|
+
|
174
|
+
{
|
175
|
+
address: namespace[params[:auth_email_smtp_address_alias] || 'address'],
|
176
|
+
port: namespace[params[:auth_email_smtp_port_alias] || 'port'],
|
177
|
+
user_name: namespace[params[:auth_email_smtp_user_name_alias] || 'user_name'],
|
178
|
+
password: namespace[params[:auth_email_smtp_password_alias] || 'password']
|
179
|
+
}
|
180
|
+
end
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
end
|
185
|
+
|
186
|
+
end
|
187
|
+
end
|