aclize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b2f95212250080779c4cd98f14d0b83073d25743
4
+ data.tar.gz: ec841f33a6a1e6f437e26a56cb1e28c9ad4a7d5e
5
+ SHA512:
6
+ metadata.gz: 023df60e150155e7a932f22b342ef5a0219b308ebcd16c2d19af882926bdeca89284a54efadc0e860019d4c391b4d0d5cbc9ebe6bcc0b1b49c29c8f636e6c71b
7
+ data.tar.gz: 92fed867177543f7ce28ebd27a1cab45e1189f494d5fabbc7ea03060e928e75665835de9cc67ef07470bc77d7a8399e7228b43a0f68a9d1032be16a73a6f08df
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.0
5
+ - 2.2.1
6
+ script:
7
+ - bundle exec rspec
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aclize.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem "rspec", "~> 3"
8
+ end
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # Aclize #
2
+
3
+ [![Build Status](https://travis-ci.org/serioja90/aclize.svg)](https://travis-ci.org/serioja90/aclize)
4
+
5
+ __Aclize__ is a Ruby gem that allows you to easily define an ACL (Access Controll List) to controllers and paths of your Ruby on Rails application.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'aclize'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install aclize
22
+
23
+ ## Usage
24
+
25
+ The __Aclize__ gem will automatically load and will wrap `ActionController::Base`, in order to allow you to define the ACL rules from inside of your `ApplicationController` or any other controller that inherits from it.
26
+
27
+ Here is an example of how to use __Aclize__ in your project:
28
+
29
+ ```ruby
30
+ class ApplicationController < ActionController::Base
31
+ before_filter :setup_acl
32
+
33
+ protected
34
+
35
+ def setup_acl
36
+ if current_user.admin?
37
+ # setup the ACL for admin users
38
+ define_acl({
39
+ controllers: {
40
+ "*" => { allow: ["*"] } # grant permissions to access any action of any controller
41
+ }
42
+ })
43
+ else
44
+ # setup the ACL for other users
45
+ define_acl({
46
+ controllers: {
47
+ posts: {
48
+ allow: ["index", "show"] # allow to access only #index and #show actions of PostsController
49
+ }
50
+ }
51
+ })
52
+ end
53
+
54
+ filter_access!
55
+ end
56
+ end
57
+ ```
58
+
59
+ In the example above we asume that the user passed the authentication, so that we know the type of account the user has.
60
+
61
+ __N.B:__ When you define the ACL with `define_acl(...)` you're defining it only for the current user.
62
+
63
+ Once you've defined the ACL, __Aclize__ will automatically manage the access control and will render the `403 Forbidden` page when the user doesn't have enough permissions to access it.
64
+
65
+ ### Customizing 403 Page ###
66
+
67
+ If you need to customize the `403 Forbidden` page, you could use the `if_unauthorized` helper for storing a callback, that will be executed when the access was denied to a user:
68
+
69
+ ```ruby
70
+ class ApplicationController < ActionController::Base
71
+ if_unauthorized do
72
+ respond_to do |format|
73
+ format.html { render 'custom/403', disposition: 'inline', status: 403 }
74
+ end
75
+ end
76
+
77
+ before_filter :setup_acl
78
+
79
+ protected
80
+
81
+ def setup_acl
82
+ # YOUR ACL DEFINITION
83
+ end
84
+ end
85
+ ```
86
+
87
+
88
+ ## Contributing
89
+
90
+ 1. Fork it ( https://github.com/serioja90/aclize/fork )
91
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
92
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
93
+ 4. Push to the branch (`git push origin my-new-feature`)
94
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/aclize.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aclize/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aclize"
8
+ spec.version = Aclize::VERSION
9
+ spec.authors = ["Groza Sergiu"]
10
+ spec.email = ["serioja90@gmail.com"]
11
+
12
+ if spec.respond_to?(:metadata)
13
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
14
+ end
15
+
16
+ spec.summary = %q{ACL for your Rails application}
17
+ spec.description = %q{This gem allows you to define an ACL (Access Control List) for your Ruby on Rails application. It is simple to use and allows you to define access permissions for controllers, actions an paths.}
18
+ spec.homepage = "https://github.com/serioja90/aclize"
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_runtime_dependency "actionpack", "~> 4.0"
28
+ spec.add_runtime_dependency "i18n", "~> 0.7"
29
+ end
@@ -0,0 +1,44 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>403 Forbidden</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 800px;
9
+ padding: 0 0em;
10
+ margin: 0 auto 0 auto;
11
+ }
12
+
13
+ div.dialog p {
14
+ font-size: 0.9em;
15
+ }
16
+
17
+ div.dialog h3 {
18
+ font-weight: normal;
19
+ color: #B71C1C;
20
+ }
21
+
22
+ h1 {
23
+ font-size: 500%;
24
+ color: #C62828;
25
+ line-height: 1em;
26
+ }
27
+
28
+ hr {
29
+ border: none;
30
+ border-bottom: solid 1px #DDD;
31
+ }
32
+ </style>
33
+ </head>
34
+
35
+ <body>
36
+ <h1>403</h1>
37
+ <div class="dialog">
38
+ <h3><%= t("aclize.unauthorized") %></h3>
39
+ <hr>
40
+ <p><%= t("aclize.suggestion") %></p>
41
+ <p><%= t("aclize.contact") %></p>
42
+ </div>
43
+ </body>
44
+ </html>
@@ -0,0 +1 @@
1
+ <%= yield %>
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "aclize"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+
2
+ en:
3
+ aclize:
4
+ unauthorized: "You don't have enough permissions to access this page"
5
+ suggestion: "Make sure the address is correct and that your account type allows you to access this page."
6
+ contact: "Please, contact your site administrator if you think this is a mistake."
@@ -0,0 +1,6 @@
1
+
2
+ it:
3
+ aclize:
4
+ unauthorized: "Non hai sufficienti permessi per accedere a questa pagina."
5
+ suggestion: "Assicurati che l'URL sia corretta e che il tuo account permetta di accedere a questa pagina."
6
+ contact: "Per favore, contatta l'amministratore del sito se credi che si tratti di un errore."
data/lib/aclize.rb ADDED
@@ -0,0 +1,144 @@
1
+ require "aclize/version"
2
+ require "aclize/helper"
3
+ require "i18n"
4
+ require "action_controller"
5
+
6
+ module Aclize
7
+
8
+ def self.included(base)
9
+ base.extend ClassMethods
10
+ base.send :prepend, Initializer
11
+ end
12
+
13
+
14
+ # The ClassMethods module will implement the methods that we want to be accessible as
15
+ # class methods. This will permit to setup callbacks to execute on unauthorized access.
16
+ module ClassMethods
17
+ def aclized?
18
+ true
19
+ end
20
+
21
+ def if_unauthorized(&block)
22
+ if block_given?
23
+ before_filter do
24
+ register_callback(&block)
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+
31
+ # The Initializer module will be used to initialize instance variables and to setup defaults.
32
+ module Initializer
33
+ def initialize
34
+ @_aclize_acl = {controllers: {}, paths: {} }.nested_under_indifferent_access
35
+ super
36
+ end
37
+ end
38
+
39
+ protected
40
+
41
+ # Returns the ACL definition as a Hash
42
+ def get_acl_definition
43
+ return @_aclize_acl
44
+ end
45
+
46
+ # Defines the structure of ACL for the current user
47
+ # TODO: implement a better or an alternative way for ACL definition
48
+ def define_acl(acl)
49
+ raise "Invalid ACL definition type: (expected: Hash, got: #{acl.class})" unless acl.is_a? Hash
50
+
51
+ if acl.has_key?(:controllers) && acl[:controllers].is_a?(Hash)
52
+ @_aclize_acl[:controllers] = acl[:controllers]
53
+ end
54
+
55
+ if acl.has_key?(:paths) && acl[:paths].is_a?(Hash)
56
+ @_aclize_acl[:paths] = acl[:paths]
57
+ end
58
+ end
59
+
60
+
61
+ # In no callbacks were defined for unauthorized access, Aclize will render a
62
+ # default 403 Forbidden page. Otherwise, the control will be passed to the callback.
63
+ def unauthorize!
64
+ path = request.path_info
65
+ flash.now[:alert] = I18n.t("aclize.unauthorized", path: path)
66
+
67
+ if @_aclize_callback.nil?
68
+ prepend_view_path File.expand_path("../../app/views", __FILE__)
69
+ respond_to do |format|
70
+ format.html { render 'aclize/403', disposition: "inline", status: 403, layout: false }
71
+ end
72
+ else
73
+ @_aclize_callback.call(path)
74
+ end
75
+ end
76
+
77
+
78
+ # Check if the current user have enough permissions to access the current controller/path
79
+ def filter_access!
80
+ unauthorize! if acl_action_denied? || acl_path_denied? || !(acl_action_allowed? || acl_path_allowed?)
81
+ end
82
+
83
+
84
+ # check if the current action is denied
85
+ def acl_action_denied?
86
+ actions = (@_aclize_acl[:controllers][controller_name] || @_aclize_acl[:controllers]["*"] || {})[:deny] || []
87
+ actions.map!{|action| action.to_s }
88
+
89
+ return actions.include?("*") || actions.include?(action_name)
90
+ end
91
+
92
+
93
+ # check if the current action is allowed
94
+ def acl_action_allowed?
95
+ actions = (@_aclize_acl[:controllers][controller_name] || @_aclize_acl[:controllers]["*"] || {})[:allow] || []
96
+ actions.map!{|action| action.to_s }
97
+
98
+ return actions.include?("*") || actions.include?(action_name)
99
+ end
100
+
101
+
102
+ # check if the current path is denied
103
+ def acl_path_denied?
104
+ paths = @_aclize_acl[:paths][:deny] || []
105
+ denied = false
106
+
107
+ paths.each do |path|
108
+ denied ||= !request.path_info.match(Regexp.new("^#{path}$")).nil?
109
+ break if denied
110
+ end
111
+
112
+ return denied
113
+ end
114
+
115
+
116
+ # check if the current path is allowed
117
+ def acl_path_allowed?
118
+ paths = @_aclize_acl[:paths][:allow] || []
119
+ allowed = false
120
+
121
+ paths.each do |path|
122
+ allowed ||= !request.path_info.match(Regexp.new("^#{path}$")).nil?
123
+ break if allowed
124
+ end
125
+
126
+ return allowed
127
+ end
128
+
129
+
130
+ # register a callback to call when the user is not authorized to access the page
131
+ def register_callback(&block)
132
+ @_aclize_callback = block
133
+ end
134
+ end
135
+
136
+ I18n.load_path += Dir[File.expand_path("../../config/locales/*.{rb,yml}", __FILE__)]
137
+
138
+ class ActionController::Base
139
+ include Aclize
140
+ end
141
+
142
+ module ApplicationHelper
143
+ include Aclize::Helper
144
+ end
@@ -0,0 +1,61 @@
1
+
2
+ module Aclize
3
+ module Helper
4
+ def aclized?
5
+ true
6
+ end
7
+
8
+ # Check if the user have permission to access the action
9
+ def action_allowed?(controller, action)
10
+ actions_allowed?(controller, [action], :all)
11
+ end
12
+
13
+
14
+ # Returns a boolean that indicates if the current used have enought permissions to access the
15
+ # specified list of actions. The policy argument indicates the type of verification. By default,
16
+ # its value is :all, that means the all the actions passed as argument have to be allowed. If the
17
+ # policy if :any, is sufficient that at least one of the specified actions to be allowed.
18
+ def actions_allowed?(controller, actions = [], policy = :all)
19
+ acl = @_aclize_acl[:controllers]
20
+ # If there's an entry for this controller in @acl, use that rule for permissions check.
21
+ # Otherwise, check if there's an '*' entry if @acl and use that rules.
22
+ methods = ( acl[controller.to_s] || acl['*'] || {} )
23
+ allow = methods["allow"] || []
24
+ deny = methods["deny"] || []
25
+
26
+ # If the array of methods is empty, the controller isn't allowed
27
+ return false if allow.empty?
28
+
29
+ # Force the list of actions to be an Array of strings
30
+ normalized_actions = (actions.is_a?(Array) ? actions : [actions]).map{|action| action.to_s }
31
+
32
+ # If all the methods of the current controller are allowed or the list of actions to check is empty, return true
33
+ return true if (allow.include?("*") && (deny & normalized_actions).empty?) || normalized_actions.empty?
34
+
35
+ case policy.to_sym
36
+ when :all then return (deny & normalized_actions).empty? && (allow & normalized_actions == normalized_actions) # all the actions have to be allowed
37
+ when :any then return !((allow & normalized_actions) - deny).empty? # at least one action have to be allowed
38
+ else
39
+ logger.warn "Invalid policy: #{policy}."
40
+ return false
41
+ end
42
+ end
43
+
44
+
45
+ # Verify if the path could be accessed by the user. Returns true when
46
+ # the path is accessible
47
+ def path_allowed?(path)
48
+ paths = @_aclize_acl[:paths]
49
+
50
+ (paths[:deny] || []).each do |filter|
51
+ return false if !path.match(Regexp(filter)).nil?
52
+ end
53
+
54
+ (paths[:allow] || []).each do |filter|
55
+ return true if !path.match(Regexp(filter)).nil?
56
+ end
57
+
58
+ return false
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module Aclize
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aclize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Groza Sergiu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionpack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: i18n
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.7'
55
+ description: This gem allows you to define an ACL (Access Control List) for your Ruby
56
+ on Rails application. It is simple to use and allows you to define access permissions
57
+ for controllers, actions an paths.
58
+ email:
59
+ - serioja90@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - README.md
70
+ - Rakefile
71
+ - aclize.gemspec
72
+ - app/views/aclize/403.html.erb
73
+ - app/views/layouts/aclize.html.erb
74
+ - bin/console
75
+ - bin/setup
76
+ - config/locales/en.yml
77
+ - config/locales/it.yml
78
+ - lib/aclize.rb
79
+ - lib/aclize/helper.rb
80
+ - lib/aclize/version.rb
81
+ homepage: https://github.com/serioja90/aclize
82
+ licenses:
83
+ - MIT
84
+ metadata:
85
+ allowed_push_host: https://rubygems.org
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.4.6
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: ACL for your Rails application
106
+ test_files: []