acts_as_privilege 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ ����cp�#]罒D[H�oT-��ㅎp�2�����o�lw��蒤b�R.�NW���;'�k�ݜ
2
+ a���:p�k��XJ� ��˙{8g+�L��F�z&��m ������Q�?�%YgV���y͙Z��n��A5����7��@Ahl?�����t����Ly++� ��g�%U�R��>�Y��#?��@�P�e��q�G<�E�t@F�����]7 <�e��2�����s��)#���i��+�zn
@@ -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.
@@ -0,0 +1,13 @@
1
+ MIT-LICENSE
2
+ README.rdoc
3
+ Rakefile
4
+ VERSION.yml
5
+ generators/acts_as_privilege/USAGE
6
+ generators/acts_as_privilege/acts_as_privilege_generator.rb
7
+ generators/acts_as_privilege/templates/migration.rb
8
+ init.rb
9
+ lib/ability.rb
10
+ lib/acts_as_privilege.rb
11
+ lib/entity.rb
12
+ lib/privileges_helper.rb
13
+ Manifest
@@ -0,0 +1,48 @@
1
+ = Acts as privilege
2
+
3
+ "Acts as" extension which provides the capabilities to restrict system access to authorized users. This ACL-based security model is designed as a role-based access control, where each role can be a group of users.
4
+
5
+ == Install
6
+
7
+ To install as a plugin:
8
+
9
+ script/plugin install git://github.com/cyril/acts_as_privilege.git
10
+
11
+ Generate and apply the migration:
12
+
13
+ script/generate acts_as_privilege model
14
+ rake db:migrate
15
+
16
+ Then you can populate Ability and Entity models using something like that:
17
+
18
+ rest_actions = %w[index show new create edit update destroy]
19
+ controllers = {
20
+ :groups => rest_actions,
21
+ :users => rest_actions,
22
+ :articles => rest_actions,
23
+ :comments => rest_actions }
24
+
25
+ controllers.each_pair do |controller, actions|
26
+ entity = Entity.create(:name => controller.to_s)
27
+ actions.each do |action|
28
+ Ability.create(:name => action, :entity_id => entity.id)
29
+ end
30
+ end
31
+
32
+ == Example
33
+
34
+ script/generate acts_as_privilege Group
35
+ rake db:migrate
36
+
37
+ class Group < ActiveRecord::Base
38
+ acts_as_privilege
39
+ has_many :users
40
+ end
41
+
42
+ # Check the current user capability to destroy articles:
43
+ current_user.group.has_privilege?('articles', 'destroy')
44
+
45
+ # Form helper that generate field to manage group privileges:
46
+ <%= privileges_field(@group, :group) %>
47
+
48
+ Copyright (c) 2009 Cyril Wack, released under the MIT license
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('acts_as_privilege', '1.2.0') do |p|
6
+ p.description = "Simple Rails plugin to restrict system access to authorized users."
7
+ p.url = "http://github.com/cyril/acts_as_privilege"
8
+ p.author = "Cyril Wack"
9
+ p.email = "cyril.wack@gmail.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 1
3
+ :minor: 2
4
+ :patch: 0
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{acts_as_privilege}
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 restrict system access to authorized users.}
12
+ s.email = %q{cyril.wack@gmail.com}
13
+ s.extra_rdoc_files = ["README.rdoc", "lib/ability.rb", "lib/acts_as_privilege.rb", "lib/entity.rb", "lib/privileges_helper.rb"]
14
+ s.files = ["MIT-LICENSE", "README.rdoc", "Rakefile", "VERSION.yml", "generators/acts_as_privilege/USAGE", "generators/acts_as_privilege/acts_as_privilege_generator.rb", "generators/acts_as_privilege/templates/migration.rb", "init.rb", "lib/ability.rb", "lib/acts_as_privilege.rb", "lib/entity.rb", "lib/privileges_helper.rb", "Manifest", "acts_as_privilege.gemspec"]
15
+ s.homepage = %q{http://github.com/cyril/acts_as_privilege}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Acts_as_privilege", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{acts_as_privilege}
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 restrict system access to authorized users.}
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,17 @@
1
+ Description:
2
+ Creates Entity (representing controllers) and Ability (representing
3
+ controller actions) models, and then links them with any roles that your
4
+ application users might need.
5
+
6
+ Usage:
7
+ Pass the name of the model that you want to apply privileges.
8
+
9
+ Examples:
10
+ script/generate acts_as_privilege Role
11
+
12
+ Will expand roles with privileges. Then you can manage user roles
13
+ and attributed privileges for each one.
14
+
15
+ script/generate acts_as_privilege Group
16
+
17
+ Same, with the Group model.
@@ -0,0 +1,34 @@
1
+ class ActsAsPrivilegeGenerator < Rails::Generator::NamedBase
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template 'migration.rb', "db/migrate", {:assigns => privileges_local_assigns, :migration_file_name => "create_privileges_for_#{plural_name}"}
5
+ end
6
+ end
7
+
8
+ def class_name
9
+ name.camelize
10
+ end
11
+
12
+ def plural_name
13
+ custom_name = class_name.underscore.downcase
14
+ custom_name = custom_name.pluralize if ActiveRecord::Base.pluralize_table_names
15
+ custom_name
16
+ end
17
+
18
+ def plural_class_name
19
+ plural_name.camelize
20
+ end
21
+
22
+ def singular_name
23
+ class_name.underscore.downcase
24
+ end
25
+
26
+ private
27
+
28
+ def privileges_local_assigns
29
+ returning(assigns = {}) do
30
+ assigns[:class_name] = "create_privileges_for_#{plural_name}"
31
+ assigns[:table_name] = plural_name
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,28 @@
1
+ class CreatePrivilegesFor<%= plural_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :abilities do |t|
4
+ t.integer :entity_id, :null => false
5
+
6
+ t.string :name, :limit => 255, :null => false
7
+ end
8
+
9
+ add_index(:abilities, [:entity_id, :name])
10
+
11
+ create_table :entities do |t|
12
+ t.string :name, :limit => 255, :null => false
13
+ end
14
+
15
+ add_index(:entities, :name, { :unique => true })
16
+
17
+ create_table :<%= ['abilities', plural_name].sort.join('_') %>, :id => false, :force => true do |t|
18
+ t.integer :ability_id, :null => false
19
+ t.integer :<%= "#{singular_name}_id" %>, :null => false
20
+ end
21
+ end
22
+
23
+ def self.down
24
+ drop_table :<%= ['abilities', plural_name].sort.join('_') %>
25
+ drop_table :entities
26
+ drop_table :abilities
27
+ end
28
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'acts_as_privilege'
@@ -0,0 +1,12 @@
1
+ class Ability < ActiveRecord::Base
2
+ # security
3
+ attr_readonly :name, :entity_id
4
+
5
+ # relations
6
+ belongs_to :entity
7
+ has_and_belongs_to_many :groups
8
+
9
+ # validates
10
+ validates_format_of :name, :with => /^[a-z0-9_]+$/, :allow_nil => false
11
+ validates_uniqueness_of :name, :case_sensitive => false, :scope => :entity_id
12
+ end
@@ -0,0 +1,31 @@
1
+ require 'active_record/base'
2
+
3
+ module ActsAsPrivilege
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_privilege
10
+ has_and_belongs_to_many :abilities
11
+
12
+ class_eval <<-EOV
13
+ include ActsAsPrivilege::InstanceMethods
14
+ EOV
15
+ end
16
+ end
17
+
18
+ module InstanceMethods
19
+ def has_privilege?(controller, action)
20
+ self.abilities.each do |ability|
21
+ if ability.name == action
22
+ return true if ability.entity.name == controller
23
+ end
24
+ end
25
+ false
26
+ end
27
+ end
28
+ end
29
+
30
+ ActiveRecord::Base.class_eval { include ActsAsPrivilege }
31
+ ActionController::Base.helper PrivilegesHelper
@@ -0,0 +1,11 @@
1
+ class Entity < ActiveRecord::Base
2
+ # security
3
+ attr_readonly :name
4
+
5
+ # relations
6
+ has_many :abilities, :dependent => :destroy
7
+
8
+ # validates
9
+ validates_format_of :name, :with => /^[a-z0-9_]+$/, :allow_nil => false
10
+ validates_uniqueness_of :name, :case_sensitive => false
11
+ end
@@ -0,0 +1,15 @@
1
+ module PrivilegesHelper
2
+ def privileges_field(resource, object_name = params[:controller].singularize)
3
+ content_tag(:fieldset, :id => "#{object_name}_privileges") do
4
+ content_tag(:legend, "Privileges") +
5
+ content_tag(:p) do
6
+ label(object_name, :ability_ids) +
7
+ tag('br') + "\n" +
8
+ select(object_name, "ability_ids",
9
+ option_groups_from_collection_for_select(Entity.all, :abilities,
10
+ :name, :id, :name, resource.abilities.collect { |ability| ability.id }),
11
+ {}, {:multiple => 'multiple'})
12
+ end
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_privilege
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
+ -----BEGIN CERTIFICATE-----
18
+ MIIDNjCCAh6gAwIBAgIBADANBgkqhkiG9w0BAQUFADBBMRMwEQYDVQQDDApjeXJp
19
+ bC53YWNrMRUwEwYKCZImiZPyLGQBGRYFZ21haWwxEzARBgoJkiaJk/IsZAEZFgNj
20
+ b20wHhcNMTAwNDExMjI1OTI4WhcNMTEwNDExMjI1OTI4WjBBMRMwEQYDVQQDDApj
21
+ eXJpbC53YWNrMRUwEwYKCZImiZPyLGQBGRYFZ21haWwxEzARBgoJkiaJk/IsZAEZ
22
+ FgNjb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDGJFqPSXGYdS6t
23
+ t+kXyFuLg7uIbQHToLhdbfpu5j7dl65EWAspRI37ZE/FIFosmwQ0DAGiJ35gVX7K
24
+ 5/rj745EUW9vijemlNHZjTY40AQAP2avlcMd6DnA7pl/x8dxC9G2dW/IS0nmjH0E
25
+ +X7X0BZ8WIY7PBvLsq5ptpGaoaxpmqRjJDANolnODwyBjFWtUqpvOGeUWL24orZ3
26
+ xwcW6d1vl8hraZ3UUJtIVXFg85lHclyrP33DYxj5sstgRwovaCPrvUQ6ZZ+hX/iJ
27
+ MYaEFZsw74WVD4RLHl1bEz2RQGDgSwfFnOnrQ1gi2SaeqUN7uFThAEbbyiJK+rNL
28
+ xapWOFs9AgMBAAGjOTA3MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgSwMB0GA1UdDgQW
29
+ BBSsO/YPzLP2/ZtBKQfWveZNRK3uKDANBgkqhkiG9w0BAQUFAAOCAQEAGaGpPlEc
30
+ Z8A7Jtfws1tpdLOlQrQQXfIgBrPvfjO18MxT2BVgnusYcMuJgrY1skbH6RDxhdia
31
+ EetICD0kvyGnbK+dHdhRwvmmiqc7ZOaiFb3RNLcW6jduxafH4zgKUeg23KpfJYy3
32
+ MOqVgHckM1hMZTWz7nmrXJBAjj/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 restrict system access to authorized users.
43
+ email: cyril.wack@gmail.com
44
+ executables: []
45
+
46
+ extensions: []
47
+
48
+ extra_rdoc_files:
49
+ - README.rdoc
50
+ - lib/ability.rb
51
+ - lib/acts_as_privilege.rb
52
+ - lib/entity.rb
53
+ - lib/privileges_helper.rb
54
+ files:
55
+ - MIT-LICENSE
56
+ - README.rdoc
57
+ - Rakefile
58
+ - VERSION.yml
59
+ - generators/acts_as_privilege/USAGE
60
+ - generators/acts_as_privilege/acts_as_privilege_generator.rb
61
+ - generators/acts_as_privilege/templates/migration.rb
62
+ - init.rb
63
+ - lib/ability.rb
64
+ - lib/acts_as_privilege.rb
65
+ - lib/entity.rb
66
+ - lib/privileges_helper.rb
67
+ - Manifest
68
+ - acts_as_privilege.gemspec
69
+ has_rdoc: true
70
+ homepage: http://github.com/cyril/acts_as_privilege
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options:
75
+ - --line-numbers
76
+ - --inline-source
77
+ - --title
78
+ - Acts_as_privilege
79
+ - --main
80
+ - README.rdoc
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ segments:
95
+ - 1
96
+ - 2
97
+ version: "1.2"
98
+ requirements: []
99
+
100
+ rubyforge_project: acts_as_privilege
101
+ rubygems_version: 1.3.6
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Simple Rails plugin to restrict system access to authorized users.
105
+ test_files: []
106
+
Binary file