acts_as_permission 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/Manifest +10 -0
- data/README.rdoc +49 -0
- data/Rakefile +12 -0
- data/VERSION.yml +4 -0
- data/acts_as_permission.gemspec +32 -0
- data/generators/acts_as_permission/USAGE +20 -0
- data/generators/acts_as_permission/acts_as_permission_generator.rb +40 -0
- data/init.rb +1 -0
- data/lib/acts_as_permission.rb +51 -0
- data/lib/permissions_helper.rb +17 -0
- data.tar.gz.sig +0 -0
- metadata +101 -0
- metadata.gz.sig +0 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Cyril Wack
|
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/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
= Acts as permission
|
2
|
+
|
3
|
+
This Rails plugin provides a list of permissions attached to a resource, according to ACL concept. Permissions determine specific access rights for a resource depending of its controller actions such as: show, edit, update, destroy, evaluated from the given resource and such as: index, new, create, evaluated from the parent of the given resource.
|
4
|
+
|
5
|
+
Each permission field attributed to a resource can be defined as true, false or nil. When a resource permission is set as nil, the same permission is searched from its nearest relative to its parent farthest and the first given boolean value is returned. If permission is nil for a resource and its potential parents, then the method returns false.
|
6
|
+
|
7
|
+
== Install
|
8
|
+
|
9
|
+
Install the gem (recommended):
|
10
|
+
|
11
|
+
sudo gem install acts_as_permission
|
12
|
+
|
13
|
+
Alternatively, you can install it as a Rails plugin:
|
14
|
+
|
15
|
+
script/plugin install git://github.com/cyril/acts_as_permission.git
|
16
|
+
|
17
|
+
Generate and apply the migration:
|
18
|
+
|
19
|
+
script/generate acts_as_permission model
|
20
|
+
rake db:migrate
|
21
|
+
|
22
|
+
== Example
|
23
|
+
|
24
|
+
script/generate acts_as_permission Category
|
25
|
+
script/generate acts_as_permission Article show edit update destroy
|
26
|
+
rake db:migrate
|
27
|
+
|
28
|
+
class Category < ActiveRecord::Base
|
29
|
+
acts_as_permission
|
30
|
+
belongs_to :user
|
31
|
+
has_many :articles
|
32
|
+
end
|
33
|
+
|
34
|
+
class Article < ActiveRecord::Base
|
35
|
+
acts_as_permission :category
|
36
|
+
belongs_to :category
|
37
|
+
belongs_to :user
|
38
|
+
end
|
39
|
+
|
40
|
+
# Check permission to list articles of the given category:
|
41
|
+
@category.has_permission?('index')
|
42
|
+
|
43
|
+
# Check permission to destroy the given article:
|
44
|
+
@article.has_permission?('destroy')
|
45
|
+
|
46
|
+
# Form helper that generate permissions fields for each article:
|
47
|
+
<%= permission_fields :article %>
|
48
|
+
|
49
|
+
Copyright (c) 2009 Cyril Wack, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('acts_as_permission', '1.2.0') do |p|
|
6
|
+
p.description = "Simple Rails plugin to assign a list of permissions on a resource."
|
7
|
+
p.url = "http://github.com/cyril/acts_as_permission"
|
8
|
+
p.author = "Cyril Wack"
|
9
|
+
p.email = "cyril.wack@gmail.com"
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.development_dependencies = []
|
12
|
+
end
|
data/VERSION.yml
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{acts_as_permission}
|
5
|
+
s.version = "1.2.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Cyril Wack"]
|
9
|
+
s.cert_chain = ["/Users/cyril/gem-public_cert.pem"]
|
10
|
+
s.date = %q{2010-04-12}
|
11
|
+
s.description = %q{Simple Rails plugin to assign a list of permissions on a resource.}
|
12
|
+
s.email = %q{cyril.wack@gmail.com}
|
13
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/acts_as_permission.rb", "lib/permissions_helper.rb"]
|
14
|
+
s.files = ["MIT-LICENSE", "README.rdoc", "Rakefile", "VERSION.yml", "generators/acts_as_permission/USAGE", "generators/acts_as_permission/acts_as_permission_generator.rb", "init.rb", "lib/acts_as_permission.rb", "lib/permissions_helper.rb", "Manifest", "acts_as_permission.gemspec"]
|
15
|
+
s.homepage = %q{http://github.com/cyril/acts_as_permission}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Acts_as_permission", "--main", "README.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{acts_as_permission}
|
19
|
+
s.rubygems_version = %q{1.3.6}
|
20
|
+
s.signing_key = %q{/Users/cyril/gem-private_key.pem}
|
21
|
+
s.summary = %q{Simple Rails plugin to assign a list of permissions on a resource.}
|
22
|
+
|
23
|
+
if s.respond_to? :specification_version then
|
24
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
25
|
+
s.specification_version = 3
|
26
|
+
|
27
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
28
|
+
else
|
29
|
+
end
|
30
|
+
else
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Description:
|
2
|
+
Alter the structure model in order to attache an access control list (ACL)
|
3
|
+
to its resources.
|
4
|
+
|
5
|
+
Usage:
|
6
|
+
Pass the name of the model, and an optional list of controller actions.
|
7
|
+
|
8
|
+
If no controller actions is specified, those following will be generated:
|
9
|
+
index, show, new, create, edit, update and destroy.
|
10
|
+
|
11
|
+
Examples:
|
12
|
+
script/generate acts_as_permission Article
|
13
|
+
|
14
|
+
Will alter the Article model table by adding a field for each seven
|
15
|
+
default actions (index, show, create, new, edit, update, and destroy).
|
16
|
+
|
17
|
+
script/generate acts_as_permission Category index new create
|
18
|
+
|
19
|
+
Will alter categories table with index, new and create action
|
20
|
+
permission fields.
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class ActsAsPermissionGenerator < Rails::Generator::NamedBase
|
2
|
+
def initialize(runtime_args, runtime_options = {})
|
3
|
+
super
|
4
|
+
@args = actions if @args.empty?
|
5
|
+
end
|
6
|
+
|
7
|
+
def manifest
|
8
|
+
record do |m|
|
9
|
+
m.migration_template 'migration:migration.rb', "db/migrate", {:assigns => permissions_local_assigns, :migration_file_name => "add_permissions_fields_to_#{plural_name}"}
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def class_name
|
14
|
+
name.camelize
|
15
|
+
end
|
16
|
+
|
17
|
+
def plural_name
|
18
|
+
custom_name = class_name.underscore.downcase
|
19
|
+
custom_name = custom_name.pluralize if ActiveRecord::Base.pluralize_table_names
|
20
|
+
custom_name
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def actions
|
26
|
+
%w[index show new create edit update destroy]
|
27
|
+
end
|
28
|
+
|
29
|
+
def permissions_local_assigns
|
30
|
+
returning(assigns = {}) do
|
31
|
+
assigns[:migration_action] = "add"
|
32
|
+
assigns[:class_name] = "add_permissions_fields_to_#{plural_name}"
|
33
|
+
assigns[:table_name] = plural_name
|
34
|
+
assigns[:attributes] = []
|
35
|
+
@args.each do |action|
|
36
|
+
assigns[:attributes] << Rails::Generator::GeneratedAttribute.new("#{action}_permission", "boolean")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'acts_as_permission'
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'active_record/base'
|
2
|
+
|
3
|
+
module ActsAsPermission
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
attr_accessor :parental_resource_permission
|
10
|
+
|
11
|
+
def acts_as_permission(resource = nil)
|
12
|
+
attr_accessible :index_permission, :new_permission, :create_permission, :show_permission, :edit_permission, :update_permission, :destroy_permission
|
13
|
+
before_save :complete_permissions_if_needed
|
14
|
+
|
15
|
+
self.parental_resource_permission = resource
|
16
|
+
|
17
|
+
class_eval <<-EOV
|
18
|
+
include ActsAsPermission::InstanceMethods
|
19
|
+
|
20
|
+
def self.could_have_permission_from_the_parent_resource?
|
21
|
+
!self.parental_resource_permission.nil?
|
22
|
+
end
|
23
|
+
EOV
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module InstanceMethods
|
28
|
+
def has_permission?(action)
|
29
|
+
if self.respond_to?("#{action}_permission")
|
30
|
+
return self.send("#{action}_permission") unless self.send("#{action}_permission").nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
if self.class.could_have_permission_from_the_parent_resource?
|
34
|
+
self.send(self.class.parental_resource_permission).has_permission?(action)
|
35
|
+
else
|
36
|
+
false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def complete_permissions_if_needed
|
43
|
+
self.update_permission = self.edit_permission if self.respond_to?('edit_permission') && self.respond_to?('update_permission')
|
44
|
+
self.create_permission = self.new_permission if self.respond_to?('new_permission') && self.respond_to?('create_permission')
|
45
|
+
true
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
ActiveRecord::Base.class_eval { include ActsAsPermission }
|
51
|
+
ActionController::Base.helper PermissionsHelper
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module PermissionsHelper
|
2
|
+
def permission_fields(object_name, actions = [])
|
3
|
+
actions = %w[index show new create edit update destroy] if actions.empty?
|
4
|
+
actions.delete('create') if actions.include?('new')
|
5
|
+
actions.delete('update') if actions.include?('edit')
|
6
|
+
content_tag(:fieldset, :id => "#{object_name}_permissions") do
|
7
|
+
content_tag(:legend, "Permissions") +
|
8
|
+
actions.collect do |action|
|
9
|
+
content_tag(:p) do
|
10
|
+
label(object_name, "#{action}_permission", action.to_s.humanize) +
|
11
|
+
tag('br') + "\n" +
|
12
|
+
select(object_name, "#{action}_permission", [ ["allow", true], ["deny", false] ], {:include_blank => 'ignore'})
|
13
|
+
end
|
14
|
+
end.join("\n")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data.tar.gz.sig
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_permission
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 1.2.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Cyril Wack
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain:
|
16
|
+
- |
|
17
|
+
-----BEG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Ajj/48jFOPrwtTed8kd6KpIjUz4e2oTwT+JIVnryF
|
33
|
+
sYFesvR4DywbXL88T29gq5biCHsAgbK89DW5DNx1Yg1HNLxCdJurJFrcQQS3XQco
|
34
|
+
h2svBTlG7Yg1wLZAGkVx4RSkrFujrxpgLsz5bfmdnbiEgKcF9njIOdVO4P4vwyoS
|
35
|
+
G4VzQZjAGxprTw==
|
36
|
+
-----END CERTIFICATE-----
|
37
|
+
|
38
|
+
date: 2010-04-12 00:00:00 +02:00
|
39
|
+
default_executable:
|
40
|
+
dependencies: []
|
41
|
+
|
42
|
+
description: Simple Rails plugin to assign a list of permissions on a resource.
|
43
|
+
email: cyril.wack@gmail.com
|
44
|
+
executables: []
|
45
|
+
|
46
|
+
extensions: []
|
47
|
+
|
48
|
+
extra_rdoc_files:
|
49
|
+
- README.rdoc
|
50
|
+
- lib/acts_as_permission.rb
|
51
|
+
- lib/permissions_helper.rb
|
52
|
+
files:
|
53
|
+
- MIT-LICENSE
|
54
|
+
- README.rdoc
|
55
|
+
- Rakefile
|
56
|
+
- VERSION.yml
|
57
|
+
- generators/acts_as_permission/USAGE
|
58
|
+
- generators/acts_as_permission/acts_as_permission_generator.rb
|
59
|
+
- init.rb
|
60
|
+
- lib/acts_as_permission.rb
|
61
|
+
- lib/permissions_helper.rb
|
62
|
+
- Manifest
|
63
|
+
- acts_as_permission.gemspec
|
64
|
+
has_rdoc: true
|
65
|
+
homepage: http://github.com/cyril/acts_as_permission
|
66
|
+
licenses: []
|
67
|
+
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options:
|
70
|
+
- --line-numbers
|
71
|
+
- --inline-source
|
72
|
+
- --title
|
73
|
+
- Acts_as_permission
|
74
|
+
- --main
|
75
|
+
- README.rdoc
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
segments:
|
90
|
+
- 1
|
91
|
+
- 2
|
92
|
+
version: "1.2"
|
93
|
+
requirements: []
|
94
|
+
|
95
|
+
rubyforge_project: acts_as_permission
|
96
|
+
rubygems_version: 1.3.6
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: Simple Rails plugin to assign a list of permissions on a resource.
|
100
|
+
test_files: []
|
101
|
+
|
metadata.gz.sig
ADDED
Binary file
|