simple_access_control 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 05b58bbac8fe078b96840194466c3a8fa6fa3986
4
+ data.tar.gz: 10705b5218f0f4a33f28b15d615ab43cd4713f30
5
+ SHA512:
6
+ metadata.gz: 20cad57bd4e6e8995758a6129c70b75eafcdf253118aee813dd8c9aedb9e1332ecb6abd75cf39ddbc44c7fa2d7ca57d47881f2e4a7e122fcc960b0de4b0e716c
7
+ data.tar.gz: c587e42a6decf3e39472e6f607790fc51c1d10438822df0623d34f442c2fc3594ef095232a7e847149621432b01ddff435643aa6bd0cbb1b2ddc20510f178826
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
@@ -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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_access_control.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Owain Lewis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,234 @@
1
+ # SimpleAccessControl
2
+
3
+ Ruby gem port of an old Ruby Plugin
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'simple_access_control'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install simple_access_control
20
+
21
+ ## Usage
22
+
23
+ To test locally you can do
24
+
25
+ 1. git clone git@github.com:owainlewis/simple-access-control.git && cd simple-access-control
26
+ 2. rake test
27
+ 3. rake install (to install the gem locally)
28
+
29
+ Once installed locally you can put it into your Gemfile like this
30
+
31
+ ```
32
+ source 'https://rubygems.org'
33
+
34
+ gem 'simple_access_control'
35
+ ```
36
+
37
+ To include the code add the following to ApplicationController.rb
38
+
39
+ ```ruby
40
+ class ApplicationController < ActionController::Base
41
+
42
+ include SimpleAccessControl
43
+
44
+ def current_user
45
+ # return the current user
46
+ end
47
+ helper_method :current_user
48
+
49
+ def logged_in?
50
+ # return boolean user logged in or not
51
+ end
52
+ helper_method :logged_in?
53
+
54
+ protect_from_forgery with: :exception
55
+ end
56
+
57
+ ```
58
+
59
+ ## Development
60
+
61
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
62
+
63
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it ( https://github.com/[my-github-username]/simple_access_control/fork )
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create a new Pull Request
72
+
73
+ # Full Information
74
+
75
+ SimpleAccessControl is a streamlined, intuitive authorisation system. It derives heavily from
76
+ acl_system2 and has made clear some problems which plagued me when first using it. Some
77
+ fixes to acl_system2's design:
78
+
79
+ * a normal Rails syntax:
80
+ access_rule 'admin', :only => :index
81
+ access_rule '(moderator || admin)', :only => :new
82
+ * error handling for helper methods (permit? bombed when current_user == nil)
83
+ * one-line parser, easy to replace or alter
84
+ * proper before_filter usage, meaning access rules are parsed only when needed
85
+ * no overrideable default (which I found counter-intuitive in the end)
86
+
87
+ Also, it has two methods, access_control and permit?, for those moving from acl_system2.
88
+
89
+ But, let me stress, everyone likes a slightly different system, so this one may not be
90
+ your style. I find it synchronises very well with the interface of Acts as Authenticated (even
91
+ though I have modified it so much that it's now called Authenticated Cookie).
92
+
93
+ INSTALLATION
94
+ ============
95
+
96
+ Create the following migration:
97
+
98
+ create_table "roles", :force => true do |t|
99
+ t.column "title", :string
100
+ end
101
+ create_table "roles_users", :id => false, :force => true do |t|
102
+ t.column "role_id", :integer
103
+ t.column "user_id", :integer
104
+ end
105
+
106
+ In your User model, you must have:
107
+
108
+ has_and_belongs_to_many :roles
109
+
110
+ In your Roles model, you must have:
111
+
112
+ has_and_belongs_to_many :users
113
+
114
+ Your controllers must have the following two methods or variants of them:
115
+
116
+ # Returns a User object
117
+ def current_user
118
+ @current_user
119
+ end
120
+
121
+ # Returns true or false if a User object exists for this session
122
+ def logged_in?
123
+ @current_user.is_a? User
124
+ end
125
+
126
+
127
+ SPECIAL NEEDS
128
+ =============
129
+
130
+ If you want to permit anonymous users without demanding that they are logged in, first you
131
+ must ensure that logged_in? returns true in all cases, otherwise permission will be denied.
132
+ The following approach should work:
133
+
134
+ 1. Create the 'guest' and 'user' roles, e.g.:
135
+
136
+ guest = Role.create(:title => 'guest')
137
+ user = Role.create(:title => 'user')
138
+
139
+ 2. In your registration/user creation area, ensure all real users have the 'user' role, e.g.:
140
+
141
+ @user = User.create(params[:user])
142
+ unless @user.roles.any? { |r| r.title == 'user' }
143
+ @user.roles << Role.find_by_title('user')
144
+ end
145
+ @user.save
146
+
147
+ [At this point you have two options: a real or virtual anonymous account]
148
+
149
+ First Approach: Real Anonymous User
150
+
151
+ 3a. Create an anonymous user, e.g.:
152
+
153
+ @anonymous = User.create(:login => 'anonymous', :password => '*', :activated => true)
154
+
155
+ 4a. Add the role to the Anonymous user (in a migration or in script/console), e.g.:
156
+
157
+ anonymous.roles << Role.find_by_title('guest')
158
+ anonymous.save
159
+
160
+ 5a. In your ApplicationController, set unauthenticated users as 'anonymous', e.g.:
161
+
162
+ before_filter :default_to_guest
163
+
164
+ def default_to_guest
165
+ self.current_user = User.find_by_login('anonymous', :include => :roles) unless logged_in?
166
+ end
167
+
168
+
169
+ Second Approach: Virtual Anonymous User
170
+
171
+ 3a. In your ApplicationController, create a virtual anonymous account if unauthenticated:
172
+
173
+ before_filter :default_to_virtual_guest
174
+ def default_to_virtual_guest
175
+ self.current_user = self.anonymous_user unless logged_in?
176
+ end
177
+
178
+ def anonymous_user
179
+ anonymous = User.new(:login => 'anonymous', :name => 'Guest')
180
+ anonymous.roles << Role.new(:title => 'guest')
181
+ anonymous.readonly!
182
+ anonymous
183
+ end
184
+
185
+
186
+ USAGE
187
+ =====
188
+
189
+ The plugin is automatically hooked into ActionController::Base.
190
+
191
+ In your controllers, add access rules like so:
192
+
193
+ access_rule 'admin', :only => :destroy
194
+ access_rule 'user || admin', :only => [:new, :create, :edit, :update]
195
+
196
+ Note the use of Ruby-style operators. These strings are real conditionals and should be treated as
197
+ such. Every grouping of non-operator characters will be considered a role title.
198
+
199
+ In your views, you can use the following:
200
+
201
+ ```ruby
202
+ <% restrict_to 'admin || moderator' do %>
203
+ <%= link_to "Admin Area", admin_area_url %>
204
+ <% end %>
205
+ ```
206
+
207
+ AND
208
+
209
+ ```ruby
210
+ <%= link_to("Admin Area", admin_area_url) if has_permission?('admin || moderator') %>
211
+ ```
212
+
213
+ There are also transitional methods which help you move from acl_system2 to this plugin -- I do this
214
+ not to denegrate acl_system2 but because I did this for myself and decided to include it. The two
215
+ systems are rather similar.
216
+
217
+ Also, there are two callbacks, permission_granted and permission_denied, which may define in your
218
+ controllers to customise their response. For example:
219
+
220
+ ```ruby
221
+
222
+ def permission_granted
223
+ logger.info("[authentication] Permission granted to %s at %s for %s" %
224
+ [(logged_in? ? current_user.login : 'guest'), Time.now, request.request_uri])
225
+ end
226
+
227
+ def permission_denied
228
+ logger.info("[authentication] Permission denied to %s at %s for %s" %
229
+ [(logged_in? ? current_user.login : 'guest'), Time.now, request.request_uri])
230
+ end
231
+ ```
232
+
233
+ That's it!
234
+
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = "test/*_spec.rb"
7
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "simple_access_control"
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,74 @@
1
+ require "simple_access_control/version"
2
+ require "simple_access_control/access_control_handler"
3
+
4
+ module SimpleAccessControl
5
+
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ if base.respond_to?(:helper_method)
9
+ base.send :helper_method, :restrict_to
10
+ base.send :helper_method, :has_permission?
11
+ base.send :helper_method, :permit?
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+
17
+ def access_control(ruleset = {})
18
+ ruleset.each do |actions, rule|
19
+ case actions
20
+ when :DEFAULT
21
+ access_rule rule
22
+ when Array, Symbol, String
23
+ access_rule rule, :only => actions
24
+ end
25
+ end
26
+ end
27
+
28
+ # This is the core of the filtering system and it couldn't be simpler:
29
+ # access_rule '(admin || moderator)', :only => [:edit, :update]
30
+ def access_rule(rule, filter_options = {})
31
+ before_filter (filter_options||{}) { |c| c.send :permission_required, rule }
32
+ end
33
+ end
34
+
35
+ def permission_required(rule = nil)
36
+ if respond_to?(:logged_in?) && logged_in? && has_permission?(rule)
37
+ send(:permission_granted) if respond_to?(:permission_granted)
38
+ true
39
+ else
40
+ send(:permission_denied) if respond_to?(:permission_denied)
41
+ false
42
+ end
43
+ end
44
+
45
+ # For use in both controllers and views.
46
+ #
47
+ # Check if a role has permission?
48
+ #
49
+ # has_permission?('role')
50
+ # has_permission?('admin', other_user)
51
+ #
52
+ # @return Boolean
53
+ def has_permission?(rule, user = nil)
54
+ user ||= (send(:current_user) if respond_to?(:current_user)) || nil
55
+ access_controller.process(rule, user)
56
+ end
57
+
58
+ def permit?(rule, context = {})
59
+ has_permission?(rule, (context && context[:user] ? context[:user] : nil))
60
+ end
61
+
62
+ # Execute a code block ONLY if the user has access
63
+ #
64
+ # restrict_to "admin | moderator" do
65
+ # link_to "foo"
66
+ # end
67
+ def restrict_to(rule, user = nil)
68
+ yield if block_given? && has_permission?(rule, user)
69
+ end
70
+
71
+ def access_controller #:nodoc:
72
+ @access_controller ||= AccessControlHandler.new
73
+ end
74
+ end
@@ -0,0 +1,44 @@
1
+ module SimpleAccessControl
2
+ class AccessControlHandler
3
+
4
+ # Is this is a simple or compound rule?
5
+ #
6
+ # @return Boolean
7
+ def simple_rule? string
8
+ string =~ /^([^()\|&!]+)$/
9
+ end
10
+
11
+ # Process a string rule and a user
12
+ #
13
+ def process(string, user)
14
+ return check('', user) if (string.nil? || string.empty?)
15
+ if simple_rule?(string)
16
+ check(string, user)
17
+ else
18
+ # This is awful!
19
+ instance_eval("!! (#{parse(string)})") # give it the going-over
20
+ end
21
+ end
22
+
23
+ # Parse a string and do some horrific ruby magic to instance eval it! : (
24
+ #
25
+ def parse(string)
26
+ string.gsub(/(\|+|\&+)/) { $1[0,1]*2 }.gsub(/([^()|&! ]+)/) { "check('#{$1}', user)" }
27
+ end
28
+
29
+ # Check if a role applies to a user
30
+ #
31
+ # check('admin', user)
32
+ #
33
+ # @return Boolean
34
+ def check(role, user)
35
+ if user.nil? || !user.respond_to?(:roles)
36
+ false
37
+ elsif (role.nil? || role.empty?)
38
+ true
39
+ else
40
+ user.roles.map{ |r| r.title.downcase }.include? role.downcase
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleAccessControl
2
+ VERSION = "0.1.0"
3
+ end
@@ -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 'simple_access_control/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_access_control"
8
+ spec.version = SimpleAccessControl::VERSION
9
+ spec.authors = ["Owain Lewis"]
10
+ spec.email = ["owain@owainlewis.com"]
11
+
12
+ spec.summary = %q{A simple gem for access control management}
13
+ spec.description = %q{A simple ACL system for Ruby on Rails projects}
14
+ spec.homepage = "https://testlodge.com"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ if spec.respond_to?(:metadata)
23
+
24
+ end
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.9"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest"
29
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_access_control
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Owain Lewis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A simple ACL system for Ruby on Rails projects
56
+ email:
57
+ - owain@owainlewis.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/simple_access_control.rb
72
+ - lib/simple_access_control/access_control_handler.rb
73
+ - lib/simple_access_control/version.rb
74
+ - simple_access_control.gemspec
75
+ homepage: https://testlodge.com
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.4.6
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: A simple gem for access control management
99
+ test_files: []