simple_permissions 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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 3.0.3"
5
+
6
+ gem "rails", "~> 3.0.0"
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "shoulda", ">= 0"
12
+ gem "bundler", "~> 1.0.0"
13
+ gem "jeweler", "~> 1.5.1"
14
+ gem "rcov", ">= 0"
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,82 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.3)
6
+ actionpack (= 3.0.3)
7
+ mail (~> 2.2.9)
8
+ actionpack (3.0.3)
9
+ activemodel (= 3.0.3)
10
+ activesupport (= 3.0.3)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.4)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.13)
16
+ rack-test (~> 0.5.6)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.3)
19
+ activesupport (= 3.0.3)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.4)
22
+ activerecord (3.0.3)
23
+ activemodel (= 3.0.3)
24
+ activesupport (= 3.0.3)
25
+ arel (~> 2.0.2)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.3)
28
+ activemodel (= 3.0.3)
29
+ activesupport (= 3.0.3)
30
+ activesupport (3.0.3)
31
+ arel (2.0.4)
32
+ builder (2.1.2)
33
+ erubis (2.6.6)
34
+ abstract (>= 1.0.0)
35
+ git (1.2.5)
36
+ i18n (0.4.2)
37
+ jeweler (1.5.1)
38
+ bundler (~> 1.0.0)
39
+ git (>= 1.2.5)
40
+ rake
41
+ mail (2.2.10)
42
+ activesupport (>= 2.3.6)
43
+ i18n (~> 0.4.1)
44
+ mime-types (~> 1.16)
45
+ treetop (~> 1.4.8)
46
+ mime-types (1.16)
47
+ polyglot (0.3.1)
48
+ rack (1.2.1)
49
+ rack-mount (0.6.13)
50
+ rack (>= 1.0.0)
51
+ rack-test (0.5.6)
52
+ rack (>= 1.0)
53
+ rails (3.0.3)
54
+ actionmailer (= 3.0.3)
55
+ actionpack (= 3.0.3)
56
+ activerecord (= 3.0.3)
57
+ activeresource (= 3.0.3)
58
+ activesupport (= 3.0.3)
59
+ bundler (~> 1.0)
60
+ railties (= 3.0.3)
61
+ railties (3.0.3)
62
+ actionpack (= 3.0.3)
63
+ activesupport (= 3.0.3)
64
+ rake (>= 0.8.7)
65
+ thor (~> 0.14.4)
66
+ rake (0.8.7)
67
+ rcov (0.9.9)
68
+ shoulda (2.11.3)
69
+ thor (0.14.6)
70
+ treetop (1.4.9)
71
+ polyglot (>= 0.3.1)
72
+ tzinfo (0.3.23)
73
+
74
+ PLATFORMS
75
+ ruby
76
+
77
+ DEPENDENCIES
78
+ bundler (~> 1.0.0)
79
+ jeweler (~> 1.5.1)
80
+ rails (~> 3.0.0)
81
+ rcov
82
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Thiago Nuic Vidigal
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,123 @@
1
+ = Simple permissions
2
+
3
+ Simple permissions aims to make available a simple way to check for roles in Rails. This gem has been tested on Rails 3 only. If you use `:lib => false` and include the modules correctly it should work fine on rails 2.3.
4
+
5
+ == Configuration
6
+
7
+ To configure you just need to change the config constants below:
8
+
9
+ <dl>
10
+ <dt>SimplePermissions::Config.current_user_method = :current_user</dt>
11
+ <dd>This sets the method which will be used inside simple permissions to get the logged user and check for roles.</dd>
12
+
13
+ <dt>SimplePermissions::Config.permissions_method = :permissions</dt>
14
+ <dd>This sets the name of the method from the user model which will be called to get the permissions.</dd>
15
+
16
+ <dt>SimplePermissions::Config.permission_type = :role</dt>
17
+ <dd>This sets the type of permission which will be used to check the user roles. Allowed types are `:role` and `:read_write`. Both types will be described below.</dd>
18
+ </dl>
19
+
20
+ == Setup
21
+
22
+ Include in your Gemfile:
23
+
24
+ gem 'simple_permissions'
25
+
26
+ Run:
27
+
28
+ bundle install
29
+
30
+ In the user model include methods for authorization like the code bellow:
31
+
32
+ class User < ActiveRecord::Base
33
+ include SimplePermissions::UserModelMethods
34
+ end
35
+
36
+ == How it works
37
+
38
+ === Model methods
39
+
40
+ The model methods included by the gem to check the user authorization expect the user model to respond to a `permissions` method that will return the user permissions according to the permission type defined.
41
+
42
+ === Permission types
43
+
44
+ ==== :role
45
+
46
+ Using this permission type, the `permissions` method from the user model should return an array of strings containing the code of the permissions, like the example below:
47
+
48
+ ['CRUD_USER', 'CRUD_PROFILE']
49
+
50
+ The suggested models for this approach are the following:
51
+
52
+ <pre>
53
+
54
+ ______ _________ _____________ ____________________
55
+ | User |________| Profile |_______| Permission |________| PermissionCategory |
56
+ |______| N 1 |_________| N M |_____________| N 1 |____________________|
57
+ | code |
58
+ | description |
59
+ |_____________|
60
+
61
+ </pre>
62
+
63
+ ==== :read_write
64
+
65
+ Using this permission type, the `permissions` method from the user model should return an hash having the permission code as key and the literal string `r` or `w` as value, like the example below:
66
+
67
+ {'CRUD_USER' => 'w', 'CRUD_PROFILE' => 'r'}
68
+
69
+ The suggested models for this approach are the following:
70
+
71
+ ______ _________ ___________________ _____________ ____________________
72
+ | User |________| Profile |_______| ProfilePermission |________| Permission |________| PermissionCategory |
73
+ |______| N 1 |_________| 1 N |___________________| N 1 |_____________| N 1 |____________________|
74
+ | read_write | | code |
75
+ |___________________| | description |
76
+ |_____________|
77
+
78
+ === Controllers
79
+
80
+ After installing the gem there will be two methods available for authorization: `has_permission` and `has_permission!`. Both receiving an array of permission codes or a hash depending on the permission type configured.
81
+
82
+ At controllers it is recommended to put a `has_permission!` call as the first line of each action to validate the user credentials.
83
+
84
+ class SampleController < ApplicationController
85
+ def index
86
+ has_permission!('CRUD_COMPANY')
87
+ ...
88
+ end
89
+ end
90
+
91
+ This credential check will raise an `SimplePermissions::AccessDeniedException` exception, so in order to capture nonauthorized actions it is recommended to include the following code in the application_controller.rb
92
+
93
+ class ApplicationController < ActionController::Base
94
+ rescue_from SimplePermissions::AccessDeniedException do |exception|
95
+ flash[:alert] = 'Access denied.'
96
+ redirect_to :root
97
+ end
98
+ end
99
+
100
+ === Helpers
101
+
102
+ Both methods available for controllers are also available for the helpers, so you can use `has_permission` to show/hide stuff on views.
103
+
104
+ == TODO
105
+
106
+ * Generators for models, migrations for both permission types
107
+ * Somehow authenticate routes
108
+
109
+ == Contributing to simple_permissions
110
+
111
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
112
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
113
+ * Fork the project
114
+ * Start a feature/bugfix branch
115
+ * Commit and push until you are happy with your contribution
116
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
117
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
118
+
119
+ == Copyright
120
+
121
+ Copyright (c) 2010 Thiago Nuic Vidigal. See LICENSE.txt for
122
+ further details.
123
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "simple_permissions"
16
+ gem.homepage = "http://github.com/thiagonuic/simple_permissions"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Easy and simple access control.}
19
+ gem.description = %Q{Easy and simple access control.}
20
+ gem.email = "tvidigal@gmail.com"
21
+ gem.authors = ["thiagonuic"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "simple_permissions #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/configs.rb ADDED
@@ -0,0 +1,31 @@
1
+ module SimplePermissions
2
+ module Config
3
+ @@current_user_method = :current_user
4
+ @@permissions_method = :permissions
5
+ @@permission_type = :role
6
+
7
+ def self.permission_type
8
+ @@permission_type
9
+ end
10
+
11
+ def self.permission_type=(value)
12
+ @@permission_type = value
13
+ end
14
+
15
+ def self.current_user_method
16
+ @@current_user_method
17
+ end
18
+
19
+ def self.current_user_method=(value)
20
+ @@current_user_method = value
21
+ end
22
+
23
+ def self.permissions_method
24
+ @@permissions_method
25
+ end
26
+
27
+ def self.permissions_method=(value)
28
+ @@permissions_method = value
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,8 @@
1
+ module SimplePermissions
2
+ module ControllerMethods
3
+ include SimplePermissionsHelper
4
+ end
5
+ end
6
+
7
+
8
+ ActionController::Base.send :include, SimplePermissions::ControllerMethods
data/lib/exceptions.rb ADDED
@@ -0,0 +1,3 @@
1
+ module SimplePermissions
2
+ class AccessDeniedException < StandardError; end
3
+ end
data/lib/helpers.rb ADDED
@@ -0,0 +1,36 @@
1
+ module SimplePermissionsHelper
2
+
3
+ def has_permission!(permissions)
4
+ raise SimplePermissions::AccessDeniedException if !self.has_permission(permissions)
5
+ end
6
+
7
+ def has_permission(p_permissions)
8
+ logged_user = self.send SimplePermissions::Config.current_user_method
9
+ has_access = false
10
+
11
+ if (p_permissions.class == String)
12
+ permissions = [p_permissions]
13
+ else
14
+ permissions = p_permissions
15
+ end
16
+
17
+ if SimplePermissions::Config.permission_type == :read_write
18
+ permissions.each do |permission, read_write|
19
+ if logged_user.has_permission_read_write(permission, read_write)
20
+ has_access = true
21
+ end
22
+ end
23
+ elsif SimplePermissions::Config.permission_type == :role
24
+ permissions.each do |permission|
25
+ if logged_user.has_permission_role(permission)
26
+ has_access = true
27
+ end
28
+ end
29
+ end
30
+
31
+ return has_access
32
+ end
33
+
34
+ end
35
+
36
+ ActionView::Base.send :include, SimplePermissionsHelper
@@ -0,0 +1,5 @@
1
+ require 'configs'
2
+ require 'exceptions'
3
+ require 'user_model'
4
+ require 'helpers'
5
+ require 'controllers'
data/lib/user_model.rb ADDED
@@ -0,0 +1,41 @@
1
+ module SimplePermissions
2
+ module UserModelMethods
3
+
4
+ def has_permission_role!(permission_code)
5
+ raise SimplePermissions::AccessDeniedException if !self.has_permission_role(permission_code)
6
+ end
7
+
8
+ def has_permission_role(permission_code)
9
+ profile_permissions = self.send SimplePermissions::Config.permissions_method
10
+
11
+ profile_permissions.each do |per_code|
12
+ if permission_code.to_s == per_code.to_s
13
+ return true
14
+ end
15
+ end
16
+ return false
17
+ end
18
+
19
+ def has_permission_read_write!(permission_code, read_write)
20
+ raise SimplePermissions::AccessDeniedException if !self.has_permission_read_write(permission_code, read_write)
21
+ end
22
+
23
+ def has_permission_read_write(permission_code, read_write)
24
+ profile_permissions = self.send SimplePermissions::Config.permissions_method
25
+
26
+ profile_permissions.each do |per_code, per_read_write|
27
+ if permission_code.to_s == per_code.to_s
28
+ if read_write.to_s == 'w'
29
+ if per_read_write.to_s == 'w'
30
+ return true
31
+ end
32
+ else
33
+ return true
34
+ end
35
+ end
36
+ end
37
+ return false
38
+ end
39
+
40
+ end
41
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'simple_permissions'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestSimplePermissions < Test::Unit::TestCase
4
+ should "default values" do
5
+ assert SimplePermissions::Config.permission_type == :role
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_permissions
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - thiagonuic
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-21 00:00:00 -02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 7
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 0
32
+ version: 3.0.0
33
+ type: :runtime
34
+ name: rails
35
+ prerelease: false
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ name: shoulda
49
+ prerelease: false
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ hash: 23
58
+ segments:
59
+ - 1
60
+ - 0
61
+ - 0
62
+ version: 1.0.0
63
+ type: :development
64
+ name: bundler
65
+ prerelease: false
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ~>
72
+ - !ruby/object:Gem::Version
73
+ hash: 1
74
+ segments:
75
+ - 1
76
+ - 5
77
+ - 1
78
+ version: 1.5.1
79
+ type: :development
80
+ name: jeweler
81
+ prerelease: false
82
+ version_requirements: *id004
83
+ - !ruby/object:Gem::Dependency
84
+ requirement: &id005 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ type: :development
94
+ name: rcov
95
+ prerelease: false
96
+ version_requirements: *id005
97
+ description: Easy and simple access control.
98
+ email: tvidigal@gmail.com
99
+ executables: []
100
+
101
+ extensions: []
102
+
103
+ extra_rdoc_files:
104
+ - LICENSE.txt
105
+ - README.rdoc
106
+ files:
107
+ - .document
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.rdoc
112
+ - Rakefile
113
+ - VERSION
114
+ - lib/configs.rb
115
+ - lib/controllers.rb
116
+ - lib/exceptions.rb
117
+ - lib/helpers.rb
118
+ - lib/simple_permissions.rb
119
+ - lib/user_model.rb
120
+ - test/helper.rb
121
+ - test/test_simple_permissions.rb
122
+ has_rdoc: true
123
+ homepage: http://github.com/thiagonuic/simple_permissions
124
+ licenses:
125
+ - MIT
126
+ post_install_message:
127
+ rdoc_options: []
128
+
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ hash: 3
137
+ segments:
138
+ - 0
139
+ version: "0"
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ hash: 3
146
+ segments:
147
+ - 0
148
+ version: "0"
149
+ requirements: []
150
+
151
+ rubyforge_project:
152
+ rubygems_version: 1.3.7
153
+ signing_key:
154
+ specification_version: 3
155
+ summary: Easy and simple access control.
156
+ test_files:
157
+ - test/helper.rb
158
+ - test/test_simple_permissions.rb