granted 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +20 -0
- data/Gemfile +30 -0
- data/Gemfile.lock +198 -0
- data/Guardfile +19 -0
- data/LICENSE.txt +20 -0
- data/README.markdown +172 -0
- data/Rakefile +31 -0
- data/VERSION +1 -0
- data/console +27 -0
- data/granted.gemspec +146 -0
- data/lib/granted/db/migrations/create_grants.rb +19 -0
- data/lib/granted/grant_class_factory.rb +14 -0
- data/lib/granted/granter.rb +67 -0
- data/lib/granted/models/grant.rb +18 -0
- data/lib/granted/modules/for_granted.rb +81 -0
- data/lib/granted/modules/grantee.rb +18 -0
- data/lib/granted/tasks/granted_tasks.rake +16 -0
- data/lib/granted/tasks.rb +1 -0
- data/lib/granted/version.rb +3 -0
- data/lib/granted.rb +12 -0
- data/spec/dummy/.rspec +1 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +10 -0
- data/spec/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.gitkeep +0 -0
- data/spec/dummy/app/models/.gitkeep +0 -0
- data/spec/dummy/app/models/document.rb +7 -0
- data/spec/dummy/app/models/user.rb +5 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config/application.rb +65 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +27 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +67 -0
- data/spec/dummy/config/environments/test.rb +37 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +58 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/migrate/20130805113508_create_user.rb +9 -0
- data/spec/dummy/db/migrate/20130805113515_create_document.rb +10 -0
- data/spec/dummy/db/migrate/20132406101010_create_grants.rb +19 -0
- data/spec/dummy/db/schema.rb +41 -0
- data/spec/dummy/lib/assets/.gitkeep +0 -0
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/grant_class_factory_spec.rb +17 -0
- data/spec/models/grant_spec.rb +114 -0
- data/spec/spec_helper.rb +54 -0
- metadata +356 -0
data/.document
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
granted
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-1.9.3
|
data/.travis.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- "1.9.2"
|
4
|
+
- "1.9.3"
|
5
|
+
|
6
|
+
# Need to figure out the Gemfile and platforms
|
7
|
+
# - jruby-19mode
|
8
|
+
# - rbx-19mode
|
9
|
+
env:
|
10
|
+
- DB=mysql
|
11
|
+
- DB=postgresql
|
12
|
+
before_script:
|
13
|
+
- mysql -u travis -e 'create database granted_test;'
|
14
|
+
- psql -c 'create database granted_test' -U postgres
|
15
|
+
script:
|
16
|
+
- cd spec/dummy
|
17
|
+
- RAILS_ENV=test bundle exec rake db:migrate --trace
|
18
|
+
- bundle exec rake db:test:prepare
|
19
|
+
- cd ../..
|
20
|
+
- bundle exec rspec spec
|
data/Gemfile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'rails', '~>3.2.14'
|
4
|
+
|
5
|
+
group :development, :test do
|
6
|
+
gem 'jeweler'
|
7
|
+
gem 'guard-rspec'
|
8
|
+
gem 'rspec'
|
9
|
+
gem 'rspec-rails'
|
10
|
+
gem 'terminal-notifier-guard'
|
11
|
+
gem 'guard-bundler'
|
12
|
+
gem 'simplecov'
|
13
|
+
gem 'database_cleaner'
|
14
|
+
end
|
15
|
+
|
16
|
+
platform :ruby do
|
17
|
+
group :development, :test do
|
18
|
+
gem 'mysql2'
|
19
|
+
gem 'debugger'
|
20
|
+
gem 'activerecord-postgresql-adapter'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
platform :jruby do
|
25
|
+
group :development, :test do
|
26
|
+
gem 'activerecord-jdbc-adapter'
|
27
|
+
gem 'activerecord-jdbcpostgresql-adapter'
|
28
|
+
gem 'activerecord-jdbcmysql-adapter'
|
29
|
+
end
|
30
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,198 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
actionmailer (3.2.14)
|
5
|
+
actionpack (= 3.2.14)
|
6
|
+
mail (~> 2.5.4)
|
7
|
+
actionpack (3.2.14)
|
8
|
+
activemodel (= 3.2.14)
|
9
|
+
activesupport (= 3.2.14)
|
10
|
+
builder (~> 3.0.0)
|
11
|
+
erubis (~> 2.7.0)
|
12
|
+
journey (~> 1.0.4)
|
13
|
+
rack (~> 1.4.5)
|
14
|
+
rack-cache (~> 1.2)
|
15
|
+
rack-test (~> 0.6.1)
|
16
|
+
sprockets (~> 2.2.1)
|
17
|
+
activemodel (3.2.14)
|
18
|
+
activesupport (= 3.2.14)
|
19
|
+
builder (~> 3.0.0)
|
20
|
+
activerecord (3.2.14)
|
21
|
+
activemodel (= 3.2.14)
|
22
|
+
activesupport (= 3.2.14)
|
23
|
+
arel (~> 3.0.2)
|
24
|
+
tzinfo (~> 0.3.29)
|
25
|
+
activerecord-postgresql-adapter (0.0.1)
|
26
|
+
pg
|
27
|
+
activeresource (3.2.14)
|
28
|
+
activemodel (= 3.2.14)
|
29
|
+
activesupport (= 3.2.14)
|
30
|
+
activesupport (3.2.14)
|
31
|
+
i18n (~> 0.6, >= 0.6.4)
|
32
|
+
multi_json (~> 1.0)
|
33
|
+
addressable (2.3.5)
|
34
|
+
arel (3.0.2)
|
35
|
+
builder (3.0.4)
|
36
|
+
coderay (1.0.9)
|
37
|
+
columnize (0.3.6)
|
38
|
+
database_cleaner (1.1.1)
|
39
|
+
debugger (1.6.1)
|
40
|
+
columnize (>= 0.3.1)
|
41
|
+
debugger-linecache (~> 1.2.0)
|
42
|
+
debugger-ruby_core_source (~> 1.2.3)
|
43
|
+
debugger-linecache (1.2.0)
|
44
|
+
debugger-ruby_core_source (1.2.3)
|
45
|
+
diff-lcs (1.2.4)
|
46
|
+
erubis (2.7.0)
|
47
|
+
faraday (0.8.8)
|
48
|
+
multipart-post (~> 1.2.0)
|
49
|
+
ffi (1.9.0)
|
50
|
+
formatador (0.2.4)
|
51
|
+
git (1.2.5)
|
52
|
+
github_api (0.10.1)
|
53
|
+
addressable
|
54
|
+
faraday (~> 0.8.1)
|
55
|
+
hashie (>= 1.2)
|
56
|
+
multi_json (~> 1.4)
|
57
|
+
nokogiri (~> 1.5.2)
|
58
|
+
oauth2
|
59
|
+
guard (1.8.2)
|
60
|
+
formatador (>= 0.2.4)
|
61
|
+
listen (>= 1.0.0)
|
62
|
+
lumberjack (>= 1.0.2)
|
63
|
+
pry (>= 0.9.10)
|
64
|
+
thor (>= 0.14.6)
|
65
|
+
guard-bundler (1.0.0)
|
66
|
+
bundler (~> 1.0)
|
67
|
+
guard (~> 1.1)
|
68
|
+
guard-rspec (3.0.2)
|
69
|
+
guard (>= 1.8)
|
70
|
+
rspec (~> 2.13)
|
71
|
+
hashie (2.0.5)
|
72
|
+
highline (1.6.19)
|
73
|
+
hike (1.2.3)
|
74
|
+
httpauth (0.2.0)
|
75
|
+
i18n (0.6.4)
|
76
|
+
jeweler (1.8.6)
|
77
|
+
builder
|
78
|
+
bundler (~> 1.0)
|
79
|
+
git (>= 1.2.5)
|
80
|
+
github_api (= 0.10.1)
|
81
|
+
highline (>= 1.6.15)
|
82
|
+
nokogiri (= 1.5.10)
|
83
|
+
rake
|
84
|
+
rdoc
|
85
|
+
journey (1.0.4)
|
86
|
+
json (1.8.0)
|
87
|
+
jwt (0.1.8)
|
88
|
+
multi_json (>= 1.5)
|
89
|
+
listen (1.2.2)
|
90
|
+
rb-fsevent (>= 0.9.3)
|
91
|
+
rb-inotify (>= 0.9)
|
92
|
+
rb-kqueue (>= 0.2)
|
93
|
+
lumberjack (1.0.4)
|
94
|
+
mail (2.5.4)
|
95
|
+
mime-types (~> 1.16)
|
96
|
+
treetop (~> 1.4.8)
|
97
|
+
method_source (0.8.2)
|
98
|
+
mime-types (1.23)
|
99
|
+
multi_json (1.7.8)
|
100
|
+
multi_xml (0.5.4)
|
101
|
+
multipart-post (1.2.0)
|
102
|
+
mysql2 (0.3.13)
|
103
|
+
nokogiri (1.5.10)
|
104
|
+
oauth2 (0.9.2)
|
105
|
+
faraday (~> 0.8)
|
106
|
+
httpauth (~> 0.2)
|
107
|
+
jwt (~> 0.1.4)
|
108
|
+
multi_json (~> 1.0)
|
109
|
+
multi_xml (~> 0.5)
|
110
|
+
rack (~> 1.2)
|
111
|
+
pg (0.16.0)
|
112
|
+
polyglot (0.3.3)
|
113
|
+
pry (0.9.12.2)
|
114
|
+
coderay (~> 1.0.5)
|
115
|
+
method_source (~> 0.8)
|
116
|
+
slop (~> 3.4)
|
117
|
+
rack (1.4.5)
|
118
|
+
rack-cache (1.2)
|
119
|
+
rack (>= 0.4)
|
120
|
+
rack-ssl (1.3.3)
|
121
|
+
rack
|
122
|
+
rack-test (0.6.2)
|
123
|
+
rack (>= 1.0)
|
124
|
+
rails (3.2.14)
|
125
|
+
actionmailer (= 3.2.14)
|
126
|
+
actionpack (= 3.2.14)
|
127
|
+
activerecord (= 3.2.14)
|
128
|
+
activeresource (= 3.2.14)
|
129
|
+
activesupport (= 3.2.14)
|
130
|
+
bundler (~> 1.0)
|
131
|
+
railties (= 3.2.14)
|
132
|
+
railties (3.2.14)
|
133
|
+
actionpack (= 3.2.14)
|
134
|
+
activesupport (= 3.2.14)
|
135
|
+
rack-ssl (~> 1.3.2)
|
136
|
+
rake (>= 0.8.7)
|
137
|
+
rdoc (~> 3.4)
|
138
|
+
thor (>= 0.14.6, < 2.0)
|
139
|
+
rake (10.1.0)
|
140
|
+
rb-fsevent (0.9.3)
|
141
|
+
rb-inotify (0.9.0)
|
142
|
+
ffi (>= 0.5.0)
|
143
|
+
rb-kqueue (0.2.0)
|
144
|
+
ffi (>= 0.5.0)
|
145
|
+
rdoc (3.12.2)
|
146
|
+
json (~> 1.4)
|
147
|
+
rspec (2.14.1)
|
148
|
+
rspec-core (~> 2.14.0)
|
149
|
+
rspec-expectations (~> 2.14.0)
|
150
|
+
rspec-mocks (~> 2.14.0)
|
151
|
+
rspec-core (2.14.4)
|
152
|
+
rspec-expectations (2.14.0)
|
153
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
154
|
+
rspec-mocks (2.14.2)
|
155
|
+
rspec-rails (2.14.0)
|
156
|
+
actionpack (>= 3.0)
|
157
|
+
activesupport (>= 3.0)
|
158
|
+
railties (>= 3.0)
|
159
|
+
rspec-core (~> 2.14.0)
|
160
|
+
rspec-expectations (~> 2.14.0)
|
161
|
+
rspec-mocks (~> 2.14.0)
|
162
|
+
simplecov (0.7.1)
|
163
|
+
multi_json (~> 1.0)
|
164
|
+
simplecov-html (~> 0.7.1)
|
165
|
+
simplecov-html (0.7.1)
|
166
|
+
slop (3.4.6)
|
167
|
+
sprockets (2.2.2)
|
168
|
+
hike (~> 1.2)
|
169
|
+
multi_json (~> 1.0)
|
170
|
+
rack (~> 1.0)
|
171
|
+
tilt (~> 1.1, != 1.3.0)
|
172
|
+
terminal-notifier-guard (1.5.3)
|
173
|
+
thor (0.18.1)
|
174
|
+
tilt (1.4.1)
|
175
|
+
treetop (1.4.14)
|
176
|
+
polyglot
|
177
|
+
polyglot (>= 0.3.1)
|
178
|
+
tzinfo (0.3.37)
|
179
|
+
|
180
|
+
PLATFORMS
|
181
|
+
ruby
|
182
|
+
|
183
|
+
DEPENDENCIES
|
184
|
+
activerecord-jdbc-adapter
|
185
|
+
activerecord-jdbcmysql-adapter
|
186
|
+
activerecord-jdbcpostgresql-adapter
|
187
|
+
activerecord-postgresql-adapter
|
188
|
+
database_cleaner
|
189
|
+
debugger
|
190
|
+
guard-bundler
|
191
|
+
guard-rspec
|
192
|
+
jeweler
|
193
|
+
mysql2
|
194
|
+
rails (~> 3.2.14)
|
195
|
+
rspec
|
196
|
+
rspec-rails
|
197
|
+
simplecov
|
198
|
+
terminal-notifier-guard
|
data/Guardfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec', :cli => "--color", :version => 2 do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { "spec" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
watch(%r{^spec/dummy/app/models/(.+)\.rb$}) { "spec" }
|
9
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
10
|
+
watch('spec/dummy/config/routes.rb') { "spec/routing" }
|
11
|
+
watch('spec/dummy/app/controllers/application_controller.rb') { "spec/controllers" }
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
guard 'bundler' do
|
16
|
+
watch('Gemfile')
|
17
|
+
# Uncomment next line if Gemfile contain `gemspec' command
|
18
|
+
# watch(/^.+\.gemspec/)
|
19
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 moviepilot.com
|
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.markdown
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
This gem lets you define arbitrary permissions on a per object level (as opposed to roles).
|
2
|
+
They are implemented purely as active record associations and hence easy to understand.
|
3
|
+
Check out this readme on how to grant read/write permissions on individual documents to
|
4
|
+
individual users. This is a [moviepilot.com](http://moviepilot.com) project licensed
|
5
|
+
[MIT](LICENSE.txt).
|
6
|
+
|
7
|
+
[![Build Status](https://travis-ci.org/jayniz/granted.png?branch=master)](https://travis-ci.org/jayniz/granted)
|
8
|
+
|
9
|
+
# Quickstart
|
10
|
+
|
11
|
+
Install with bundler:
|
12
|
+
|
13
|
+
gem 'granted'
|
14
|
+
|
15
|
+
Add to Rakefile:
|
16
|
+
|
17
|
+
require 'granted/tasks'
|
18
|
+
|
19
|
+
Create the migration for the grants table:
|
20
|
+
|
21
|
+
rake granted:create_migration
|
22
|
+
rake db:migrate
|
23
|
+
|
24
|
+
And then:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
# Let's grant a user access to a document
|
28
|
+
user.grant(:read).on(document)
|
29
|
+
|
30
|
+
# Let's revoke a user's write access to a document
|
31
|
+
user.revoke(:grant).from(document)
|
32
|
+
|
33
|
+
# We can also do it rails association style
|
34
|
+
document.read_users << user
|
35
|
+
|
36
|
+
# Let's count all documents a user has read access to
|
37
|
+
user.readable_documents.count
|
38
|
+
|
39
|
+
# Let's count all documents a user has any access to
|
40
|
+
user.all_documents.count
|
41
|
+
|
42
|
+
# Define the things we took for granted (scuse me) above
|
43
|
+
class Document
|
44
|
+
include Granted::ForGranted
|
45
|
+
|
46
|
+
# Creates associations and grant/revoke methods
|
47
|
+
grantable :read, :write, :destroy, to: User
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
## How does it work
|
52
|
+
|
53
|
+
When creating the migration with `rake granted:create_migration`,
|
54
|
+
this gem will add a migration to your rails app that creates a
|
55
|
+
`grants` table when you run it. This is a polymorphic model sitting
|
56
|
+
between a `grantee` (e.g. `User` and a `subject` (e.g. `Document`).
|
57
|
+
It has only one attribute, and that is the `right` that it gives the
|
58
|
+
grantee to do with the subject.
|
59
|
+
|
60
|
+
### What does this code do?
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class Document < ActiveRecord::Base
|
64
|
+
include Granted::ForGranted
|
65
|
+
|
66
|
+
grantable :read, :write, to: User
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
### It does that:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
class Granted::WriteGrant < Granted::Grant; end
|
74
|
+
class Granted::ReadGrant < Granted::Grant; end
|
75
|
+
|
76
|
+
class Document < ActiveRecord::Base
|
77
|
+
has_many :grants, as: :subject, class_name: 'Granted::Grant', dependent: :destroy
|
78
|
+
has_many :write_grants, as: :subject, class_name: 'Granted::WriteGrant'
|
79
|
+
has_many :read_grants, as: :subject, class_name: 'Granted::ReadGrant'
|
80
|
+
|
81
|
+
has_many :write_users, source: :grantee, source_type: 'User', through: :write_grants
|
82
|
+
has_many :read_users, source: :grantee, source_type: 'User', through: :read_grants
|
83
|
+
has_many :all_users, source: :grantee, source_type: 'User', through: :grants, uniq: true
|
84
|
+
|
85
|
+
attr_accessible :write_users_attributes, :read_users_attributes
|
86
|
+
accepts_nested_attributes_for :write_users, :read_users
|
87
|
+
end
|
88
|
+
|
89
|
+
class User < ActiveRecord::Base
|
90
|
+
has_many :grants, as: :grantee, class_name: 'Granted::Grant', dependent: :destroy
|
91
|
+
has_many :write_grants, as: :grantee, class_name: 'Granted::WriteGrant'
|
92
|
+
has_many :read_grants, as: :grantee, class_name: 'Granted::ReadGrant'
|
93
|
+
|
94
|
+
has_many :writeable_documents, source: :subject, source_type: 'Document', through: :write_grants
|
95
|
+
has_many :readable_documents, source: :subject, source_type: 'Document', through: :read_grants
|
96
|
+
has_many :all_documents, source: :subject, source_type: 'Document', through: :grants, uniq: true
|
97
|
+
|
98
|
+
attr_accessible :writeable_documents_attributes, :readable_documents_attributes
|
99
|
+
accepts_nested_attributes_for :writeable_documents, :readable_documents
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
First it creates STI classes that inherit from `Granted::Grant`, one for
|
104
|
+
each right you defined as grantable (e.g. ReadGrant, WriteGrant).
|
105
|
+
It then creates the appropriate `has_many` relations to both `User` and
|
106
|
+
`Document`, so that they can be connected with a `Grant` instance.
|
107
|
+
So you have all the access control available via normal active record
|
108
|
+
associations (reading and writing).
|
109
|
+
|
110
|
+
PSA: You can only grant/revoke rights via the grantee side at the
|
111
|
+
moment, the other direction is not yet implemented:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
document.read_users << my_user # Works
|
115
|
+
my_user.readable_documents << document # Doesn't work yet
|
116
|
+
```
|
117
|
+
|
118
|
+
## Granting/revoking rights
|
119
|
+
|
120
|
+
So now that you know how querying grants/rights work, you might wonder
|
121
|
+
how you give or revoke certain access rights to a user and a document.
|
122
|
+
Consider this familiar snippet of code:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
class Document < ActiveRecord::Base
|
126
|
+
include Granted::ForGranted
|
127
|
+
|
128
|
+
grantable :read, :write, to: User
|
129
|
+
end
|
130
|
+
```
|
131
|
+
|
132
|
+
It does not only create the associations, it also creates the `grant`
|
133
|
+
and `revoke` methods on `User` and `Document`. They return a convenient
|
134
|
+
little object ([Grant::Granter](lib/granted/granter.rb), if you're curious).
|
135
|
+
You can grant/revoke access rights using Users or Documents as a starting
|
136
|
+
point, it's all the same:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
# Both ways to grant are identical
|
140
|
+
my_user.grant(:read).on(my_document)
|
141
|
+
my_document.grant(:read).to(my_user)
|
142
|
+
|
143
|
+
# Both ways to revoke are identical
|
144
|
+
my_user.revoke(:read).on(my_document)
|
145
|
+
my_document.revoke(:read).from(my_user)
|
146
|
+
|
147
|
+
# Clever: even weird grammatic yields identic results
|
148
|
+
my_user.on(my_document).revoke(:read)
|
149
|
+
my_document.revoke(:read).from(:my_user)
|
150
|
+
|
151
|
+
# This is what the grant/revoke methods do:
|
152
|
+
Granted::Granter.new.grant(:read).on(my_document).to(my_user)
|
153
|
+
Granted::Granter.new.revoke(:read).on(my_document).from(my_user)
|
154
|
+
```
|
155
|
+
|
156
|
+
## Interedasting things
|
157
|
+
|
158
|
+
You can use arrays or single objects in `grantable` both as access rights
|
159
|
+
and grantees:
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
class Document < ActiveRecord::Base
|
163
|
+
include Granted::ForGranted
|
164
|
+
|
165
|
+
grantable :read, to: [User, Editor]
|
166
|
+
|
167
|
+
grantable :update, :destroy, to: [Editor]
|
168
|
+
end
|
169
|
+
|
170
|
+
my_document.grant(:read, :write).to(my_user)
|
171
|
+
```
|
172
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler'
|
4
|
+
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
Bundler.require
|
13
|
+
|
14
|
+
require 'rake'
|
15
|
+
require 'jeweler'
|
16
|
+
|
17
|
+
Jeweler::Tasks.new do |gem|
|
18
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
19
|
+
gem.name = "granted"
|
20
|
+
gem.homepage = "http://github.com/jayniz/granted"
|
21
|
+
gem.license = "MIT"
|
22
|
+
gem.summary = %Q{Grant or revoke access to your ActiveRecord models}
|
23
|
+
gem.description = %Q{Takes care of defining what actions one model is allowed to do with another model.}
|
24
|
+
gem.email = "jannis@gmail.com"
|
25
|
+
gem.authors = ["Jannis Hermanns"]
|
26
|
+
# dependencies defined in Gemfile
|
27
|
+
end
|
28
|
+
Jeweler::RubygemsDotOrgTasks.new
|
29
|
+
|
30
|
+
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
|
31
|
+
Dir.glob('granted/tasks/*.rake').each { |r| import r }
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/console
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
|
3
|
+
|
4
|
+
require 'bundler'
|
5
|
+
Bundler.require
|
6
|
+
require 'debugger'
|
7
|
+
|
8
|
+
require 'irb'
|
9
|
+
require 'granted'
|
10
|
+
|
11
|
+
def reload!
|
12
|
+
@loaded_files ||= {}
|
13
|
+
count = 0
|
14
|
+
|
15
|
+
Dir['./lib/**/*.rb'].each do |file|
|
16
|
+
mtime = File.stat(file).mtime
|
17
|
+
if !@loaded_files.has_key?(file) or mtime > @loaded_files[file]
|
18
|
+
STDERR.puts "mtime for #{file} changed, reloading"
|
19
|
+
load file
|
20
|
+
@loaded_files[file] = mtime
|
21
|
+
count += 1
|
22
|
+
end
|
23
|
+
end
|
24
|
+
"reloaded #{count} files"
|
25
|
+
end
|
26
|
+
|
27
|
+
IRB.start
|
data/granted.gemspec
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "granted"
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jannis Hermanns"]
|
12
|
+
s.date = "2013-08-06"
|
13
|
+
s.description = "Takes care of defining what actions one model is allowed to do with another model."
|
14
|
+
s.email = "jannis@gmail.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.markdown"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".ruby-gemset",
|
22
|
+
".ruby-version",
|
23
|
+
".travis.yml",
|
24
|
+
"Gemfile",
|
25
|
+
"Gemfile.lock",
|
26
|
+
"Guardfile",
|
27
|
+
"LICENSE.txt",
|
28
|
+
"README.markdown",
|
29
|
+
"Rakefile",
|
30
|
+
"VERSION",
|
31
|
+
"console",
|
32
|
+
"granted.gemspec",
|
33
|
+
"lib/granted.rb",
|
34
|
+
"lib/granted/db/migrations/create_grants.rb",
|
35
|
+
"lib/granted/grant_class_factory.rb",
|
36
|
+
"lib/granted/granter.rb",
|
37
|
+
"lib/granted/models/grant.rb",
|
38
|
+
"lib/granted/modules/for_granted.rb",
|
39
|
+
"lib/granted/modules/grantee.rb",
|
40
|
+
"lib/granted/tasks.rb",
|
41
|
+
"lib/granted/tasks/granted_tasks.rake",
|
42
|
+
"lib/granted/version.rb",
|
43
|
+
"spec/dummy/.rspec",
|
44
|
+
"spec/dummy/README.rdoc",
|
45
|
+
"spec/dummy/Rakefile",
|
46
|
+
"spec/dummy/app/assets/javascripts/application.js",
|
47
|
+
"spec/dummy/app/assets/stylesheets/application.css",
|
48
|
+
"spec/dummy/app/controllers/application_controller.rb",
|
49
|
+
"spec/dummy/app/helpers/application_helper.rb",
|
50
|
+
"spec/dummy/app/mailers/.gitkeep",
|
51
|
+
"spec/dummy/app/models/.gitkeep",
|
52
|
+
"spec/dummy/app/models/document.rb",
|
53
|
+
"spec/dummy/app/models/user.rb",
|
54
|
+
"spec/dummy/app/views/layouts/application.html.erb",
|
55
|
+
"spec/dummy/config.ru",
|
56
|
+
"spec/dummy/config/application.rb",
|
57
|
+
"spec/dummy/config/boot.rb",
|
58
|
+
"spec/dummy/config/database.yml",
|
59
|
+
"spec/dummy/config/environment.rb",
|
60
|
+
"spec/dummy/config/environments/development.rb",
|
61
|
+
"spec/dummy/config/environments/production.rb",
|
62
|
+
"spec/dummy/config/environments/test.rb",
|
63
|
+
"spec/dummy/config/initializers/backtrace_silencers.rb",
|
64
|
+
"spec/dummy/config/initializers/inflections.rb",
|
65
|
+
"spec/dummy/config/initializers/mime_types.rb",
|
66
|
+
"spec/dummy/config/initializers/secret_token.rb",
|
67
|
+
"spec/dummy/config/initializers/session_store.rb",
|
68
|
+
"spec/dummy/config/initializers/wrap_parameters.rb",
|
69
|
+
"spec/dummy/config/locales/en.yml",
|
70
|
+
"spec/dummy/config/routes.rb",
|
71
|
+
"spec/dummy/db/migrate/20130805113508_create_user.rb",
|
72
|
+
"spec/dummy/db/migrate/20130805113515_create_document.rb",
|
73
|
+
"spec/dummy/db/migrate/20132406101010_create_grants.rb",
|
74
|
+
"spec/dummy/db/schema.rb",
|
75
|
+
"spec/dummy/lib/assets/.gitkeep",
|
76
|
+
"spec/dummy/log/.gitkeep",
|
77
|
+
"spec/dummy/public/404.html",
|
78
|
+
"spec/dummy/public/422.html",
|
79
|
+
"spec/dummy/public/500.html",
|
80
|
+
"spec/dummy/public/favicon.ico",
|
81
|
+
"spec/dummy/script/rails",
|
82
|
+
"spec/grant_class_factory_spec.rb",
|
83
|
+
"spec/models/grant_spec.rb",
|
84
|
+
"spec/spec_helper.rb"
|
85
|
+
]
|
86
|
+
s.homepage = "http://github.com/jayniz/granted"
|
87
|
+
s.licenses = ["MIT"]
|
88
|
+
s.require_paths = ["lib"]
|
89
|
+
s.rubygems_version = "1.8.25"
|
90
|
+
s.summary = "Grant or revoke access to your ActiveRecord models"
|
91
|
+
|
92
|
+
if s.respond_to? :specification_version then
|
93
|
+
s.specification_version = 3
|
94
|
+
|
95
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
96
|
+
s.add_runtime_dependency(%q<rails>, ["~> 3.2.14"])
|
97
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
98
|
+
s.add_development_dependency(%q<guard-rspec>, [">= 0"])
|
99
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
100
|
+
s.add_development_dependency(%q<rspec-rails>, [">= 0"])
|
101
|
+
s.add_development_dependency(%q<terminal-notifier-guard>, [">= 0"])
|
102
|
+
s.add_development_dependency(%q<guard-bundler>, [">= 0"])
|
103
|
+
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
104
|
+
s.add_development_dependency(%q<database_cleaner>, [">= 0"])
|
105
|
+
s.add_development_dependency(%q<mysql2>, [">= 0"])
|
106
|
+
s.add_development_dependency(%q<debugger>, [">= 0"])
|
107
|
+
s.add_development_dependency(%q<activerecord-postgresql-adapter>, [">= 0"])
|
108
|
+
s.add_development_dependency(%q<activerecord-jdbc-adapter>, [">= 0"])
|
109
|
+
s.add_development_dependency(%q<activerecord-jdbcpostgresql-adapter>, [">= 0"])
|
110
|
+
s.add_development_dependency(%q<activerecord-jdbcmysql-adapter>, [">= 0"])
|
111
|
+
else
|
112
|
+
s.add_dependency(%q<rails>, ["~> 3.2.14"])
|
113
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
114
|
+
s.add_dependency(%q<guard-rspec>, [">= 0"])
|
115
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
116
|
+
s.add_dependency(%q<rspec-rails>, [">= 0"])
|
117
|
+
s.add_dependency(%q<terminal-notifier-guard>, [">= 0"])
|
118
|
+
s.add_dependency(%q<guard-bundler>, [">= 0"])
|
119
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
120
|
+
s.add_dependency(%q<database_cleaner>, [">= 0"])
|
121
|
+
s.add_dependency(%q<mysql2>, [">= 0"])
|
122
|
+
s.add_dependency(%q<debugger>, [">= 0"])
|
123
|
+
s.add_dependency(%q<activerecord-postgresql-adapter>, [">= 0"])
|
124
|
+
s.add_dependency(%q<activerecord-jdbc-adapter>, [">= 0"])
|
125
|
+
s.add_dependency(%q<activerecord-jdbcpostgresql-adapter>, [">= 0"])
|
126
|
+
s.add_dependency(%q<activerecord-jdbcmysql-adapter>, [">= 0"])
|
127
|
+
end
|
128
|
+
else
|
129
|
+
s.add_dependency(%q<rails>, ["~> 3.2.14"])
|
130
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
131
|
+
s.add_dependency(%q<guard-rspec>, [">= 0"])
|
132
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
133
|
+
s.add_dependency(%q<rspec-rails>, [">= 0"])
|
134
|
+
s.add_dependency(%q<terminal-notifier-guard>, [">= 0"])
|
135
|
+
s.add_dependency(%q<guard-bundler>, [">= 0"])
|
136
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
137
|
+
s.add_dependency(%q<database_cleaner>, [">= 0"])
|
138
|
+
s.add_dependency(%q<mysql2>, [">= 0"])
|
139
|
+
s.add_dependency(%q<debugger>, [">= 0"])
|
140
|
+
s.add_dependency(%q<activerecord-postgresql-adapter>, [">= 0"])
|
141
|
+
s.add_dependency(%q<activerecord-jdbc-adapter>, [">= 0"])
|
142
|
+
s.add_dependency(%q<activerecord-jdbcpostgresql-adapter>, [">= 0"])
|
143
|
+
s.add_dependency(%q<activerecord-jdbcmysql-adapter>, [">= 0"])
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|