flexible_accessibility 0.1.6.pre → 0.1.8.pre
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.
- data/Rakefile +1 -1
- data/flexible_accessibility.gemspec +4 -2
- data/lib/flexible_accessibility/controller_methods.rb +10 -29
- data/lib/flexible_accessibility/exceptions.rb +2 -2
- data/lib/flexible_accessibility/filters.rb +42 -0
- data/lib/flexible_accessibility/permission.rb +22 -0
- data/lib/flexible_accessibility/utils.rb +25 -0
- data/lib/flexible_accessibility.rb +3 -1
- metadata +6 -4
- data/lib/flexible_accessibility/permissions.rb +0 -20
data/Rakefile
CHANGED
@@ -15,7 +15,7 @@ require 'jeweler'
|
|
15
15
|
Jeweler::Tasks.new do |gem|
|
16
16
|
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
17
|
gem.name = "flexible_accessibility"
|
18
|
-
gem.version = "0.1.
|
18
|
+
gem.version = "0.1.8.pre"
|
19
19
|
gem.homepage = "http://github.com/mochnatiy/flexible_accessibility"
|
20
20
|
gem.license = "MIT"
|
21
21
|
gem.summary = %Q{Flexible access control system}
|
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "flexible_accessibility"
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.8.pre"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Sergey Awanesov"]
|
@@ -30,7 +30,9 @@ Gem::Specification.new do |s|
|
|
30
30
|
"lib/flexible_accessibility.rb",
|
31
31
|
"lib/flexible_accessibility/controller_methods.rb",
|
32
32
|
"lib/flexible_accessibility/exceptions.rb",
|
33
|
-
"lib/flexible_accessibility/
|
33
|
+
"lib/flexible_accessibility/filters.rb",
|
34
|
+
"lib/flexible_accessibility/permission.rb",
|
35
|
+
"lib/flexible_accessibility/utils.rb",
|
34
36
|
"test/helper.rb",
|
35
37
|
"test/test_flexible_accessibility.rb"
|
36
38
|
]
|
@@ -4,44 +4,25 @@ module FlexibleAccessibility
|
|
4
4
|
|
5
5
|
# Macro for skip authorization
|
6
6
|
def skip_authorization_here
|
7
|
-
self.instance_variable_set :@
|
8
|
-
self.instance_variable_set :@
|
9
|
-
self.send :before_filter, :check_if_route_permitted
|
7
|
+
self.instance_variable_set :@_route_permitted, true
|
8
|
+
self.instance_variable_set :@_checkable_routes, []
|
9
|
+
#self.send :before_filter, :check_if_route_permitted
|
10
10
|
end
|
11
11
|
|
12
12
|
# Macro for define authorization
|
13
13
|
def authorize args={}
|
14
|
-
self.instance_variable_set :@
|
15
|
-
self.send :before_filter, :check_permission_to_route
|
16
|
-
self.send :before_filter, :check_if_route_permitted
|
14
|
+
self.instance_variable_set :@_route_permitted, false
|
15
|
+
#self.send :before_filter, :check_permission_to_route
|
16
|
+
#self.send :before_filter, :check_if_route_permitted
|
17
17
|
set_actions_to_authorize args
|
18
18
|
end
|
19
19
|
|
20
20
|
private
|
21
|
-
#
|
21
|
+
# Set actions for authorize as instance variable
|
22
22
|
def set_actions_to_authorize args={}
|
23
|
-
self.instance_variable_set :@
|
23
|
+
self.instance_variable_set :@_checkable_routes, args[:only] unless args[:only].nil?
|
24
24
|
# TODO: understand and fix it
|
25
|
-
self.instance_variable_set :@
|
26
|
-
end
|
27
|
-
|
28
|
-
#
|
29
|
-
def current_route
|
30
|
-
path = ActionController::Routing::Routes.recognize_path request.env["PATH_INFO"]
|
31
|
-
[path[:controller], path[:action]]
|
32
|
-
end
|
33
|
-
|
34
|
-
# We checks access to route
|
35
|
-
# And we expected the existing of current_user helper
|
36
|
-
def check_permission_to_route
|
37
|
-
if self.instance_variable_get(:@checkable_routes).include? current_route[1].to_sym
|
38
|
-
self.instance_variable_set(:@route_permitted, true) unless Permissions.is_action_permitted_for_user? "#{current_route[0]}##{current_route[1]}", current_user
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
# We checks @authorized variable state
|
43
|
-
def check_if_route_permitted
|
44
|
-
raise FlexibleAccessibility::AccessDeniedException unless self.instance_variable_get :@route_permitted
|
25
|
+
self.instance_variable_set :@_checkable_routes, self.action_methods - args[:except] unless args[:except].nil?
|
45
26
|
end
|
46
27
|
end
|
47
28
|
|
@@ -53,7 +34,7 @@ module FlexibleAccessibility
|
|
53
34
|
|
54
35
|
# We checks url for each link in view to show it
|
55
36
|
def has_access? controller, action
|
56
|
-
|
37
|
+
Permission.is_action_permitted_for_user? "#{controller}##{action}", current_user
|
57
38
|
end
|
58
39
|
end
|
59
40
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module FlexibleAccessibility
|
2
2
|
class AccessDeniedException < StandardError
|
3
3
|
attr_reader :action, :subject
|
4
4
|
attr_writer :default_message
|
@@ -7,7 +7,7 @@ module FlexibleAccessibilty
|
|
7
7
|
@message = message
|
8
8
|
@action = action
|
9
9
|
@subject = subject
|
10
|
-
@default_message = I18n.t('errors.access_denied')
|
10
|
+
@default_message = I18n.t('flexible_accessibility.errors.access_denied', :action => @action)
|
11
11
|
end
|
12
12
|
|
13
13
|
def to_s
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module FlexibleAccessibility
|
2
|
+
module Filters
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
append_before_filter :check_permission_to_route
|
7
|
+
append_before_filter :check_if_route_permitted
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
# Detect current controller and action and return a permission
|
12
|
+
def current_resource
|
13
|
+
# ActionController::Routing::Routes.recognize_path request.env["PATH_INFO"][:controller]
|
14
|
+
params[:controller]
|
15
|
+
end
|
16
|
+
|
17
|
+
def current_action
|
18
|
+
# ActionController::Routing::Routes.recognize_path request.env["PATH_INFO"][:action]
|
19
|
+
params[:action]
|
20
|
+
end
|
21
|
+
|
22
|
+
def current_route
|
23
|
+
"#{current_resource}##{current_action}"
|
24
|
+
end
|
25
|
+
# We checks access to route
|
26
|
+
# And we expected the existing of current_user helper
|
27
|
+
def check_permission_to_route
|
28
|
+
if self.class.instance_variable_get(:@_checkable_routes).include? current_action.to_sym
|
29
|
+
self.class.instance_variable_set :@_route_permitted, Permission.is_action_permitted_for_user?(current_route, current_user)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# We checks @authorized variable state
|
34
|
+
def check_if_route_permitted
|
35
|
+
raise AccessDeniedException.new(nil, current_route, nil) unless self.class.instance_variable_get :@_route_permitted
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
ActiveSupport.on_load(:action_controller) do
|
40
|
+
ActionController::Base.send(:include, Filters)
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module FlexibleAccessibility
|
2
|
+
class Permission
|
3
|
+
class << self
|
4
|
+
def all
|
5
|
+
permissions = {}
|
6
|
+
Utils.new.get_controllers.each do |klass|
|
7
|
+
permissions[klass.to_sym] = klass.camelize.constantize.instance_variable_get(:@_checkable_routes).collect{ |a| a.to_s }.join(', ')
|
8
|
+
end
|
9
|
+
permissions
|
10
|
+
end
|
11
|
+
|
12
|
+
# Stub methods
|
13
|
+
def is_action_permitted? action
|
14
|
+
false
|
15
|
+
end
|
16
|
+
|
17
|
+
def is_action_permitted_for_user? action, user
|
18
|
+
false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module FlexibleAccessibility
|
2
|
+
class Utils
|
3
|
+
def initialize
|
4
|
+
@path = "#{::Rails.root}/app/controllers/"
|
5
|
+
@controllers = []
|
6
|
+
end
|
7
|
+
|
8
|
+
def get_controllers
|
9
|
+
get_controllers_recursive @path
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_controllers_recursive path
|
13
|
+
(Dir.new(path).entries - ["..", "."]).each do |entry|
|
14
|
+
if File.directory? path + entry
|
15
|
+
# TODO: Add namespace handling here
|
16
|
+
# get_controllers_recursive path + entry + '/'
|
17
|
+
next
|
18
|
+
else
|
19
|
+
@controllers << File.basename(path + entry, ".*") unless File.basename(path + entry, ".*") == "application_controller"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
@controllers
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
1
|
require 'flexible_accessibility/controller_methods.rb'
|
2
2
|
require 'flexible_accessibility/exceptions.rb'
|
3
|
-
require 'flexible_accessibility/
|
3
|
+
require 'flexible_accessibility/permission.rb'
|
4
|
+
require 'flexible_accessibility/filters.rb'
|
5
|
+
require 'flexible_accessibility/utils.rb'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flexible_accessibility
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 961916012
|
5
5
|
prerelease: 6
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
9
|
+
- 8
|
10
10
|
- pre
|
11
|
-
version: 0.1.
|
11
|
+
version: 0.1.8.pre
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- Sergey Awanesov
|
@@ -116,7 +116,9 @@ files:
|
|
116
116
|
- lib/flexible_accessibility.rb
|
117
117
|
- lib/flexible_accessibility/controller_methods.rb
|
118
118
|
- lib/flexible_accessibility/exceptions.rb
|
119
|
-
- lib/flexible_accessibility/
|
119
|
+
- lib/flexible_accessibility/filters.rb
|
120
|
+
- lib/flexible_accessibility/permission.rb
|
121
|
+
- lib/flexible_accessibility/utils.rb
|
120
122
|
- test/helper.rb
|
121
123
|
- test/test_flexible_accessibility.rb
|
122
124
|
homepage: http://github.com/mochnatiy/flexible_accessibility
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module FlexibleAccessibility
|
2
|
-
class Permissions
|
3
|
-
class << self
|
4
|
-
def get_permissions
|
5
|
-
permissions = {}
|
6
|
-
ApplicationController.subclasses.each do |klass|
|
7
|
-
permissions[klass.to_s.tableize.singularize.to_sym] = klass.instance_variable_get(:@checkable_routes).collect{ |a| a.to_s }.join(', ')
|
8
|
-
end
|
9
|
-
permissions
|
10
|
-
end
|
11
|
-
|
12
|
-
def is_action_permitted? action
|
13
|
-
end
|
14
|
-
|
15
|
-
def is_action_permitted_for_user? action, user
|
16
|
-
!self.where(["action = ? and user_id = ?", action, user.id]).empty?
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|