tight-engine 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile +1 -0
- data/lib/tight-engine.rb +23 -0
- data/lib/tight-engine/defer.rb +30 -0
- data/lib/tight-engine/init.rb +120 -0
- data/lib/tight-engine/locale.rb +40 -0
- data/lib/tight-engine/render.rb +193 -0
- data/lib/tight-engine/template.rb +60 -0
- data/lib/tight-engine/url.rb +27 -0
- data/lib/tight-engine/utils.rb +62 -0
- data/lib/tight-engine/version.rb +5 -0
- data/tight-engine.gemspec +2 -2
- metadata +13 -19
- data/lib/tight-auth.rb +0 -10
- data/lib/tight-auth/access.rb +0 -148
- data/lib/tight-auth/login.rb +0 -138
- data/lib/tight-auth/login/controller.rb +0 -20
- data/lib/tight-auth/login/layout.slim +0 -10
- data/lib/tight-auth/login/new.slim +0 -37
- data/lib/tight-auth/permissions.rb +0 -180
- data/lib/tight/version.rb +0 -3
- data/test/auth_helper.rb +0 -83
- data/test/test_padrino_access.rb +0 -124
- data/test/test_padrino_auth.rb +0 -38
- data/test/test_padrino_login.rb +0 -76
@@ -0,0 +1,27 @@
|
|
1
|
+
module Tight
|
2
|
+
module Engine
|
3
|
+
module Url
|
4
|
+
def se_url( obj, method = :show, opts = {} )
|
5
|
+
if method.kind_of? Hash
|
6
|
+
opts = method
|
7
|
+
method = :show
|
8
|
+
end
|
9
|
+
url = case obj
|
10
|
+
when NewsArticle
|
11
|
+
'/news' / method / obj.slug
|
12
|
+
when FormsCard
|
13
|
+
'/forms' / method / obj.slug
|
14
|
+
when Page
|
15
|
+
obj.path
|
16
|
+
else
|
17
|
+
swift.module_root ? swift.module_root / method / obj.slug : '/'
|
18
|
+
end
|
19
|
+
if opts[:absolute]
|
20
|
+
absolute_url url
|
21
|
+
else
|
22
|
+
url
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'cgi'
|
3
|
+
|
4
|
+
module Tight
|
5
|
+
module Engine
|
6
|
+
module Utils
|
7
|
+
def report_error( error, subsystem = 'system', fallback = nil )
|
8
|
+
@_out_buf ||= ''.html_safe # !!! FIXME this might be fixed at tilt 1.3.8+
|
9
|
+
if Padrino.env == :production
|
10
|
+
messages = ''
|
11
|
+
[ "Swift caught a runtime error at #{subsystem}",
|
12
|
+
"Fallback for development was #{fallback||'empty'}, production displayed empty string.",
|
13
|
+
error.message,
|
14
|
+
].each do |message|
|
15
|
+
logger.error message
|
16
|
+
messages << message + "\r\n"
|
17
|
+
end
|
18
|
+
error.backtrace.reject{ |e| e.match /phusion_passenger/ }.each do |step|
|
19
|
+
logger << step
|
20
|
+
messages << step + "\r\n"
|
21
|
+
end
|
22
|
+
swift.error_messages ||= []
|
23
|
+
swift.error_messages << messages
|
24
|
+
''
|
25
|
+
else
|
26
|
+
fallback || raise
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def url_replace( target, *args )
|
31
|
+
uri = URI.parse(URI::DEFAULT_PARSER.escape target)
|
32
|
+
uri.path = CGI.escape(args.first) if args.first.kind_of?(String)
|
33
|
+
if args.last.kind_of?(Hash)
|
34
|
+
query = uri.query ? CGI.parse(uri.query) : {}
|
35
|
+
args.last.each{ |k,v| v ? query[k.to_s] = v.to_s : query.delete(k.to_s) }
|
36
|
+
uri.query = query.any? && URI.encode_www_form(query)
|
37
|
+
end
|
38
|
+
CGI.unescape(uri.to_s)
|
39
|
+
end
|
40
|
+
|
41
|
+
def mk_datetime(date, time)
|
42
|
+
DateTime.new date.year, date.month, date.day, time.hour, time.min, time.sec
|
43
|
+
end
|
44
|
+
|
45
|
+
def show_asset(asset, options={})
|
46
|
+
@file = asset
|
47
|
+
@opts = options
|
48
|
+
element_view 'File/view'
|
49
|
+
end
|
50
|
+
|
51
|
+
def icon_for(filename)
|
52
|
+
iconfile = 'images/extname/16/file_extension_'+File.extname(filename)[1..-1]+'.png'
|
53
|
+
iconpath = Padrino.root('public', iconfile)
|
54
|
+
if File.file?(iconpath)
|
55
|
+
image_tag '/'+iconfile
|
56
|
+
else
|
57
|
+
image_tag '/images/extname/16/file_extension_bin.png'
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/tight-engine.gemspec
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
$LOAD_PATH << File.expand_path('../lib', __FILE__)
|
2
|
-
require 'tight/version'
|
2
|
+
require 'tight-engine/version'
|
3
3
|
|
4
4
|
Gem::Specification.new do |spec|
|
5
5
|
spec.name = 'tight-engine'
|
6
|
-
spec.version = Tight::VERSION
|
6
|
+
spec.version = Tight::Engine::VERSION
|
7
7
|
spec.description = 'Tight engine for Swift CMS'
|
8
8
|
spec.summary = 'A tight engine for a swift content management system'
|
9
9
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tight-engine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Bochkariov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -74,21 +74,19 @@ extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
76
|
- ".gitignore"
|
77
|
+
- Gemfile
|
77
78
|
- LICENSE
|
78
79
|
- README.md
|
79
80
|
- Rakefile
|
80
|
-
- lib/tight-
|
81
|
-
- lib/tight-
|
82
|
-
- lib/tight-
|
83
|
-
- lib/tight-
|
84
|
-
- lib/tight-
|
85
|
-
- lib/tight-
|
86
|
-
- lib/tight-
|
87
|
-
- lib/tight/
|
88
|
-
-
|
89
|
-
- test/test_padrino_access.rb
|
90
|
-
- test/test_padrino_auth.rb
|
91
|
-
- test/test_padrino_login.rb
|
81
|
+
- lib/tight-engine.rb
|
82
|
+
- lib/tight-engine/defer.rb
|
83
|
+
- lib/tight-engine/init.rb
|
84
|
+
- lib/tight-engine/locale.rb
|
85
|
+
- lib/tight-engine/render.rb
|
86
|
+
- lib/tight-engine/template.rb
|
87
|
+
- lib/tight-engine/url.rb
|
88
|
+
- lib/tight-engine/utils.rb
|
89
|
+
- lib/tight-engine/version.rb
|
92
90
|
- tight-engine.gemspec
|
93
91
|
homepage: https://github.com/ujifgc/tight-engine
|
94
92
|
licenses:
|
@@ -114,9 +112,5 @@ rubygems_version: 2.2.2
|
|
114
112
|
signing_key:
|
115
113
|
specification_version: 4
|
116
114
|
summary: A tight engine for a swift content management system
|
117
|
-
test_files:
|
118
|
-
- test/auth_helper.rb
|
119
|
-
- test/test_padrino_access.rb
|
120
|
-
- test/test_padrino_auth.rb
|
121
|
-
- test/test_padrino_login.rb
|
115
|
+
test_files: []
|
122
116
|
has_rdoc:
|
data/lib/tight-auth.rb
DELETED
data/lib/tight-auth/access.rb
DELETED
@@ -1,148 +0,0 @@
|
|
1
|
-
require 'tight-auth/permissions'
|
2
|
-
|
3
|
-
module Tight
|
4
|
-
##
|
5
|
-
# Tight authorization module.
|
6
|
-
#
|
7
|
-
# @example
|
8
|
-
# class Nifty::Application < Tight::Application
|
9
|
-
# # optional settings
|
10
|
-
# set :credentials_reader, :visitor # the name of getter method in helpers
|
11
|
-
# # required statement
|
12
|
-
# register Tight::Access
|
13
|
-
# # example persistance storage
|
14
|
-
# enable :sessions
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
# # optional helpers
|
18
|
-
# Nifty::Application.helpers do
|
19
|
-
# def visitor
|
20
|
-
# session[:visitor] ||= Visitor.guest_account
|
21
|
-
# end
|
22
|
-
# end
|
23
|
-
#
|
24
|
-
# # example visitor model
|
25
|
-
# module Visitor
|
26
|
-
# extend self
|
27
|
-
# def guest_account
|
28
|
-
# OpenStruct.new(:role => :guest, :id => 1)
|
29
|
-
# end
|
30
|
-
# end
|
31
|
-
#
|
32
|
-
# # example controllers
|
33
|
-
# Nifty::Application.controller :public_area do
|
34
|
-
# set_access :*
|
35
|
-
# get(:index){ 'public content' }
|
36
|
-
# end
|
37
|
-
# Nifty::Application.controller :members_area do
|
38
|
-
# set_access :member
|
39
|
-
# get(:index){ 'secret content' }
|
40
|
-
# end
|
41
|
-
# Nifty::Application.controller :login do
|
42
|
-
# set_access :*
|
43
|
-
# get(:index){ session[:visitor] = OpenStruct.new(:role => :guest, :id => 1) }
|
44
|
-
# end
|
45
|
-
#
|
46
|
-
module Access
|
47
|
-
class << self
|
48
|
-
def registered(app)
|
49
|
-
included(app)
|
50
|
-
app.default(:credentials_reader, :credentials)
|
51
|
-
app.default(:access_errors, true)
|
52
|
-
app.send :attr_reader, app.credentials_reader unless app.instance_methods.include?(app.credentials_reader)
|
53
|
-
app.set :permissions, Permissions.new
|
54
|
-
app.login_permissions if app.respond_to?(:login_permissions)
|
55
|
-
app.before do
|
56
|
-
authorized? || error(403, '403 Forbidden')
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def included(base)
|
61
|
-
base.send(:include, InstanceMethods)
|
62
|
-
base.extend(ClassMethods)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
module ClassMethods
|
67
|
-
##
|
68
|
-
# Empties the list of permission.
|
69
|
-
#
|
70
|
-
def reset_access!
|
71
|
-
permissions.clear!
|
72
|
-
end
|
73
|
-
|
74
|
-
##
|
75
|
-
# Allows access to action with objects.
|
76
|
-
#
|
77
|
-
# @example
|
78
|
-
# # in application
|
79
|
-
# set_access :*, :with => :login # allows everyone to interact with :login controller
|
80
|
-
# # in controller
|
81
|
-
# App.controller :members_area do
|
82
|
-
# set_access :member # allows all members to access :members_area controller
|
83
|
-
# end
|
84
|
-
#
|
85
|
-
def set_access(*args)
|
86
|
-
options = args.extract_options!
|
87
|
-
options[:object] ||= Array(@_controller).first.to_s.singularize.to_sym if @_controller.present?
|
88
|
-
permissions.add(*args, options)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
module InstanceMethods
|
93
|
-
##
|
94
|
-
# Checks if current visitor has access to current action with current controller.
|
95
|
-
#
|
96
|
-
def authorized?
|
97
|
-
access_action?
|
98
|
-
end
|
99
|
-
|
100
|
-
##
|
101
|
-
# Returns current visitor.
|
102
|
-
#
|
103
|
-
def access_subject
|
104
|
-
send settings.credentials_reader
|
105
|
-
end
|
106
|
-
|
107
|
-
##
|
108
|
-
# Checks if current visitor is one of the specified roles. Can accept a block.
|
109
|
-
#
|
110
|
-
def access_role?(*roles, &block)
|
111
|
-
settings.permissions.check(access_subject, :have => roles, &block)
|
112
|
-
end
|
113
|
-
|
114
|
-
##
|
115
|
-
# Checks if current visitor is allowed to to the action with object. Can accept a block.
|
116
|
-
#
|
117
|
-
def access_action?(action = nil, object = nil, &block)
|
118
|
-
return true if response.status/100 == 4 && settings.access_errors
|
119
|
-
if respond_to?(:request) && action.nil? && object.nil?
|
120
|
-
object = request.controller
|
121
|
-
action = request.action
|
122
|
-
if object.nil? && action.present? && action.to_s.index('/')
|
123
|
-
object, action = request.env['PATH_INFO'].to_s.scan(/\/([^\/]*)/).map(&:first)
|
124
|
-
end
|
125
|
-
object ||= :''
|
126
|
-
action ||= :index
|
127
|
-
object = object.to_sym
|
128
|
-
action = action.to_sym
|
129
|
-
end
|
130
|
-
settings.permissions.check(access_subject, :allow => action, :with => object, &block)
|
131
|
-
end
|
132
|
-
|
133
|
-
##
|
134
|
-
# Check if current visitor is allowed to interact with object by action. Can accept a block.
|
135
|
-
#
|
136
|
-
def access_object?(object = nil, action = nil, &block)
|
137
|
-
allow_action action, object, &block
|
138
|
-
end
|
139
|
-
|
140
|
-
##
|
141
|
-
# Populates the list of objects the current visitor is allowed to interact with.
|
142
|
-
#
|
143
|
-
def access_objects(subject = access_subject, action = nil)
|
144
|
-
settings.permissions.find_objects(subject, action)
|
145
|
-
end
|
146
|
-
end
|
147
|
-
end
|
148
|
-
end
|
data/lib/tight-auth/login.rb
DELETED
@@ -1,138 +0,0 @@
|
|
1
|
-
require 'tight-auth/login/controller'
|
2
|
-
|
3
|
-
module Tight
|
4
|
-
##
|
5
|
-
# Tight authentication module.
|
6
|
-
#
|
7
|
-
# @example
|
8
|
-
# class Nifty::Application < Tight::Application
|
9
|
-
# # optional settings
|
10
|
-
# set :session_key, "visitor_id" # visitor key name in session storage, defaults to "_login_#{app.app_name}")
|
11
|
-
# set :login_model, :visitor # model name for visitor storage, defaults to :account, must be constantizable
|
12
|
-
# set :credentials_accessor, :visitor # the name of setter/getter method in helpers, defaults to :credentials
|
13
|
-
# enable :login_bypass # enables or disables login bypass in development mode, defaults to disable
|
14
|
-
# set :login_url, '/sign/in' # sets the utl to be redirected to if not logged in and in restricted area, defaults to '/login'
|
15
|
-
# disable :login_permissions # sets initial login permissions, defaults to { set_access(:*, :allow => :*, :with => :login) }
|
16
|
-
# disable :login_controller # disables default login controller to show an example of the custom one
|
17
|
-
#
|
18
|
-
# # required statement
|
19
|
-
# register Tight::Login
|
20
|
-
# # example persistance storage
|
21
|
-
# enable :sessions
|
22
|
-
# end
|
23
|
-
#
|
24
|
-
# TODO: example controllers
|
25
|
-
#
|
26
|
-
module Login
|
27
|
-
class << self
|
28
|
-
def registered(app)
|
29
|
-
warn 'Tight::Login must be registered before Tight::Access' if app.respond_to?(:set_access)
|
30
|
-
included(app)
|
31
|
-
setup_storage(app)
|
32
|
-
setup_controller(app)
|
33
|
-
app.before do
|
34
|
-
log_in if authorization_required?
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def included(base)
|
39
|
-
base.send(:include, InstanceMethods)
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
def setup_storage(app)
|
45
|
-
app.default(:session_key, "_login_#{app.app_name}")
|
46
|
-
app.default(:login_model, :account)
|
47
|
-
app.default(:credentials_accessor, :credentials)
|
48
|
-
app.send :attr_reader, app.credentials_accessor unless app.instance_methods.include?(app.credentials_accessor)
|
49
|
-
app.send :attr_writer, app.credentials_accessor unless app.instance_methods.include?(:"#{app.credentials_accessor}=")
|
50
|
-
app.default(:login_bypass, false)
|
51
|
-
end
|
52
|
-
|
53
|
-
def setup_controller(app)
|
54
|
-
app.default(:login_url, '/login')
|
55
|
-
app.default(:login_permissions) { set_access(:*, :allow => :*, :with => :login) }
|
56
|
-
app.default(:login_controller, true)
|
57
|
-
app.controller(:login) { include Controller } if app.login_controller
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
module InstanceMethods
|
62
|
-
# Returns the model used to authenticate visitors.
|
63
|
-
def login_model
|
64
|
-
@login_model ||= settings.login_model.to_s.classify.constantize
|
65
|
-
end
|
66
|
-
|
67
|
-
# Authenticates the visitor.
|
68
|
-
def authenticate
|
69
|
-
resource = login_model.authenticate(:email => params[:email], :password => params[:password])
|
70
|
-
resource ||= login_model.authenticate(:bypass => true) if settings.login_bypass && params[:bypass]
|
71
|
-
save_credentials(resource)
|
72
|
-
end
|
73
|
-
|
74
|
-
# Checks if the visitor is authenticated.
|
75
|
-
def logged_in?
|
76
|
-
!!(send(settings.credentials_accessor) || restore_credentials)
|
77
|
-
end
|
78
|
-
|
79
|
-
# Looks for authorization routine and calls it to check if the visitor is authorized.
|
80
|
-
def unauthorized?
|
81
|
-
respond_to?(:authorized?) && !authorized?
|
82
|
-
end
|
83
|
-
|
84
|
-
# Checks if the current location needs the visitor to be authorized.
|
85
|
-
def authorization_required?
|
86
|
-
if logged_in?
|
87
|
-
if unauthorized?
|
88
|
-
# 403 Forbidden, provided credentials were successfully
|
89
|
-
# authenticated but the credentials still do not grant
|
90
|
-
# the client permission to access the resource
|
91
|
-
error 403, '403 Forbidden'
|
92
|
-
else
|
93
|
-
false
|
94
|
-
end
|
95
|
-
else
|
96
|
-
unauthorized?
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
# Logs the visitor in using redirect to login page url.
|
101
|
-
def log_in
|
102
|
-
login_url = settings.login_url
|
103
|
-
if request.env['PATH_INFO'] != login_url
|
104
|
-
save_location
|
105
|
-
# 302 Found
|
106
|
-
redirect url(login_url)
|
107
|
-
# 401 Unauthorized, authentication is required and
|
108
|
-
# has not yet been provided
|
109
|
-
error 401, '401 Unauthorized'
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
# Saves credentials in session.
|
114
|
-
def save_credentials(resource)
|
115
|
-
session[settings.session_key] = resource.respond_to?(:id) ? resource.id : resource
|
116
|
-
send(:"#{settings.credentials_accessor}=", resource)
|
117
|
-
end
|
118
|
-
|
119
|
-
# Restores credentials from session using visitor model.
|
120
|
-
def restore_credentials
|
121
|
-
resource = login_model.authenticate(:id => session[settings.session_key])
|
122
|
-
send(:"#{settings.credentials_accessor}=", resource)
|
123
|
-
end
|
124
|
-
|
125
|
-
# Redirects back to saved location or '/'
|
126
|
-
def restore_location
|
127
|
-
redirect session.delete(:return_to) || url('/')
|
128
|
-
end
|
129
|
-
|
130
|
-
# Saves location to session for following redirect in case of successful authentication.
|
131
|
-
def save_location
|
132
|
-
uri = env['REQUEST_URI'] || url(env['PATH_INFO'])
|
133
|
-
return if uri.blank? || uri.match(/\.css$|\.js$|\.png$/)
|
134
|
-
session[:return_to] = "#{ENV['RACK_BASE_URI']}#{uri}"
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|