aegis 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -89,6 +89,13 @@ Aegis assumes that the corresponding database table has a string-valued column
89
89
  called +role_name+. You may override the name with the <tt>:name_accessor =>
90
90
  :my_role_column</tt> option.
91
91
 
92
+ You can define a default role for a model by saying
93
+ class User < ActiveRecord::Base
94
+ has_role :default => :admin
95
+ end
96
+ All this will do, is initialize the +role_name+ with the given default when
97
+ +User.new+ is called.
98
+
92
99
  The roles and permissions themselves are defined in a class inheriting from
93
100
  <b>Aegis::Permissions</b>. To define roles you create a model <tt>permissions.rb</tt>
94
101
  and use the *role* method:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.4
1
+ 1.1.5
data/aegis.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{aegis}
5
- s.version = "1.1.4"
5
+ s.version = "1.1.5"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Henning Koch"]
9
- s.date = %q{2009-11-05}
9
+ s.date = %q{2009-11-11}
10
10
  s.description = %q{Aegis is a role-based permission system, where all users are given a role. It is possible to define detailed and complex permissions for each role very easily.}
11
11
  s.email = %q{github@makandra.de}
12
12
  s.extra_rdoc_files = [
@@ -29,10 +29,13 @@ Gem::Specification.new do |s|
29
29
  "lib/aegis/role.rb",
30
30
  "lib/rails/active_record.rb",
31
31
  "test/app_root/app/controllers/application_controller.rb",
32
+ "test/app_root/app/models/old_soldier.rb",
32
33
  "test/app_root/app/models/permissions.rb",
33
34
  "test/app_root/app/models/soldier.rb",
35
+ "test/app_root/app/models/trust_fund_kid.rb",
34
36
  "test/app_root/app/models/user.rb",
35
37
  "test/app_root/app/models/user_subclass.rb",
38
+ "test/app_root/app/models/veteran_soldier.rb",
36
39
  "test/app_root/config/boot.rb",
37
40
  "test/app_root/config/database.yml",
38
41
  "test/app_root/config/environment.rb",
@@ -44,6 +47,8 @@ Gem::Specification.new do |s|
44
47
  "test/app_root/config/routes.rb",
45
48
  "test/app_root/db/migrate/20090408115228_create_users.rb",
46
49
  "test/app_root/db/migrate/20090429075648_create_soldiers.rb",
50
+ "test/app_root/db/migrate/20091110075648_create_veteran_soldiers.rb",
51
+ "test/app_root/db/migrate/20091110075649_create_trust_fund_kids.rb",
47
52
  "test/app_root/lib/console_with_fixtures.rb",
48
53
  "test/app_root/log/.gitignore",
49
54
  "test/app_root/script/console",
@@ -59,9 +64,12 @@ Gem::Specification.new do |s|
59
64
  s.rubygems_version = %q{1.3.5}
60
65
  s.summary = %q{Role-based permissions for your user models.}
61
66
  s.test_files = [
62
- "test/app_root/app/models/permissions.rb",
67
+ "test/app_root/app/models/trust_fund_kid.rb",
68
+ "test/app_root/app/models/veteran_soldier.rb",
69
+ "test/app_root/app/models/permissions.rb",
63
70
  "test/app_root/app/models/soldier.rb",
64
71
  "test/app_root/app/models/user_subclass.rb",
72
+ "test/app_root/app/models/old_soldier.rb",
65
73
  "test/app_root/app/models/user.rb",
66
74
  "test/app_root/app/controllers/application_controller.rb",
67
75
  "test/app_root/config/environment.rb",
@@ -74,6 +82,8 @@ Gem::Specification.new do |s|
74
82
  "test/app_root/config/routes.rb",
75
83
  "test/app_root/db/migrate/20090429075648_create_soldiers.rb",
76
84
  "test/app_root/db/migrate/20090408115228_create_users.rb",
85
+ "test/app_root/db/migrate/20091110075649_create_trust_fund_kids.rb",
86
+ "test/app_root/db/migrate/20091110075648_create_veteran_soldiers.rb",
77
87
  "test/app_root/lib/console_with_fixtures.rb",
78
88
  "test/validation_test.rb",
79
89
  "test/test_helper.rb",
@@ -13,18 +13,33 @@ module Aegis
13
13
 
14
14
  def has_role(options = {})
15
15
 
16
- if options[:name_accessor]
17
- options[:name_reader] = "#{options[:name_accessor]}"
18
- options[:name_writer] = "#{options[:name_accessor]}="
19
- options.delete(:name_accessor)
16
+ # Legacy parameter names
17
+ options[:accessor] ||= options.delete(:name_accessor)
18
+ options[:reader] ||= options.delete(:name_reader)
19
+ options[:writer] ||= options.delete(:name_writer)
20
+
21
+ if options[:accessor]
22
+ options[:reader] = "#{options[:accessor]}"
23
+ options[:writer] = "#{options[:accessor]}="
24
+ options.delete(:accessor)
20
25
  end
21
26
 
22
27
  self.class_eval do
23
28
 
24
- class_inheritable_accessor :aegis_role_name_reader, :aegis_role_name_writer
29
+ class_inheritable_accessor :aegis_role_name_reader, :aegis_role_name_writer, :aegis_default_role_name
25
30
 
26
- self.aegis_role_name_reader = (options[:name_reader] || "role_name").to_sym
27
- self.aegis_role_name_writer = (options[:name_writer] || "role_name=").to_sym
31
+ unless method_defined?(:after_initialize)
32
+ def after_initialize
33
+ end
34
+ end
35
+
36
+ if options[:default]
37
+ self.aegis_default_role_name = options[:default].to_s
38
+ after_initialize :set_default_aegis_role_name
39
+ end
40
+
41
+ self.aegis_role_name_reader = (options[:reader] || "role_name").to_sym
42
+ self.aegis_role_name_writer = (options[:writer] || "role_name=").to_sym
28
43
 
29
44
  def aegis_role_name_reader
30
45
  self.class.class_eval{ aegis_role_name_reader }
@@ -70,6 +85,12 @@ module Aegis
70
85
 
71
86
  alias_method_chain :method_missing, :aegis_permissions
72
87
 
88
+ def set_default_aegis_role_name
89
+ if new_record?
90
+ self.aegis_role_name ||= self.class.aegis_default_role_name
91
+ end
92
+ end
93
+
73
94
  end
74
95
 
75
96
  end
@@ -10,7 +10,6 @@ module Aegis
10
10
  end
11
11
 
12
12
  module ClassMethods
13
-
14
13
 
15
14
  def role(role_name, options = {})
16
15
  role_name = role_name.to_sym
@@ -53,8 +52,8 @@ module Aegis
53
52
  end
54
53
 
55
54
  def evaluate_permission_blocks(role, blocks, *args)
56
- evaluator = Aegis::PermissionEvaluator.new(role)
57
- evaluator.evaluate(blocks, args)
55
+ evaluator = Aegis::PermissionEvaluator.new(role)
56
+ evaluator.evaluate(blocks, args)
58
57
  end
59
58
 
60
59
  def denied?(*args)
@@ -0,0 +1,6 @@
1
+ class VeteranSoldier < ActiveRecord::Base
2
+
3
+ # Use legacy parameter :name_accessor instead of :accessor
4
+ has_role :name_accessor => "rank"
5
+
6
+ end
@@ -1,5 +1,5 @@
1
1
  class Soldier < ActiveRecord::Base
2
2
 
3
- has_role :name_accessor => "rank"
3
+ has_role :accessor => "rank"
4
4
 
5
5
  end
@@ -0,0 +1,5 @@
1
+ class TrustFundKid < ActiveRecord::Base
2
+
3
+ has_role :default => :admin
4
+
5
+ end
@@ -0,0 +1,6 @@
1
+ class VeteranSoldier < ActiveRecord::Base
2
+
3
+ # Using legacy parameter names
4
+ has_role :name_accessor => "rank"
5
+
6
+ end
@@ -2,7 +2,7 @@ class CreateUsers < ActiveRecord::Migration
2
2
 
3
3
  def self.up
4
4
  create_table :users do |t|
5
- t.string "role_name"
5
+ t.string :role_name
6
6
  t.timestamps
7
7
  end
8
8
  end
@@ -1,12 +1,10 @@
1
1
  class CreateSoldiers < ActiveRecord::Migration
2
2
 
3
3
  def self.up
4
-
5
4
  create_table :soldiers do |t|
6
5
  t.string :rank
7
6
  t.timestamps
8
7
  end
9
-
10
8
  end
11
9
 
12
10
  def self.down
@@ -0,0 +1,14 @@
1
+ class CreateVeteranSoldiers < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ create_table :veteran_soldiers do |t|
5
+ t.string :rank
6
+ t.timestamps
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :veteran_soldiers
12
+ end
13
+
14
+ end
@@ -0,0 +1,15 @@
1
+ class CreateTrustFundKids < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ create_table :trust_fund_kids do |t|
5
+ t.string :role_name
6
+ t.integer :account_balance
7
+ t.timestamps
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :trust_fund_kids
13
+ end
14
+
15
+ end
@@ -10,11 +10,12 @@ class HasRoleOptionsTest < ActiveSupport::TestCase
10
10
 
11
11
  should "allow its role to be written and read" do
12
12
  @soldier.role = "guest"
13
- assert "guest", @soldier.role.name
13
+ assert_equal :guest, @soldier.role.name
14
14
  end
15
15
 
16
16
  should "store the role name in the custom field" do
17
- assert "guest", @soldier.rank
17
+ @soldier.role = "guest"
18
+ assert_equal "guest", @soldier.rank
18
19
  end
19
20
 
20
21
  should "still work with permissions" do
@@ -25,4 +26,35 @@ class HasRoleOptionsTest < ActiveSupport::TestCase
25
26
 
26
27
  end
27
28
 
29
+ context "A record wiring up its role using legacy parameter names" do
30
+
31
+ setup do
32
+ @vetaran_soldier = VeteranSoldier.new
33
+ end
34
+
35
+ should "allow its role to be written and read" do
36
+ @vetaran_soldier.role = "guest"
37
+ assert_equal :guest, @vetaran_soldier.role.name
38
+ end
39
+
40
+ end
41
+
42
+ context "A record with a default role" do
43
+
44
+ should "create new instances with that role" do
45
+ assert_equal :admin, TrustFundKid.new.role.name
46
+ end
47
+
48
+ should "ignore the default if another role is given" do
49
+ assert_equal :student, TrustFundKid.new(:role_name => "student").role.name
50
+ end
51
+
52
+ should "not update existing records with the default role" do
53
+ kid = TrustFundKid.create!(:role_name => "student")
54
+ kid.update_attributes(:account_balance => 10_000_000)
55
+ assert_equal :student, kid.reload.role.name
56
+ end
57
+
58
+ end
59
+
28
60
  end
@@ -12,10 +12,10 @@ class HasRoleTest < ActiveSupport::TestCase
12
12
  end
13
13
 
14
14
  should "know their role" do
15
- assert :guest, @guest.role.name
16
- assert :student, @student.role.name
17
- assert :student_subclass, @student.role.name
18
- assert :admin, @admin.role.name
15
+ assert_equal :guest, @guest.role.name
16
+ assert_equal :student, @student.role.name
17
+ assert_equal :student, @student_subclass.role.name
18
+ assert_equal :admin, @admin.role.name
19
19
  end
20
20
 
21
21
  should "know if they belong to a role" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aegis
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henning Koch
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-05 00:00:00 +01:00
12
+ date: 2009-11-11 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -38,10 +38,13 @@ files:
38
38
  - lib/aegis/role.rb
39
39
  - lib/rails/active_record.rb
40
40
  - test/app_root/app/controllers/application_controller.rb
41
+ - test/app_root/app/models/old_soldier.rb
41
42
  - test/app_root/app/models/permissions.rb
42
43
  - test/app_root/app/models/soldier.rb
44
+ - test/app_root/app/models/trust_fund_kid.rb
43
45
  - test/app_root/app/models/user.rb
44
46
  - test/app_root/app/models/user_subclass.rb
47
+ - test/app_root/app/models/veteran_soldier.rb
45
48
  - test/app_root/config/boot.rb
46
49
  - test/app_root/config/database.yml
47
50
  - test/app_root/config/environment.rb
@@ -53,6 +56,8 @@ files:
53
56
  - test/app_root/config/routes.rb
54
57
  - test/app_root/db/migrate/20090408115228_create_users.rb
55
58
  - test/app_root/db/migrate/20090429075648_create_soldiers.rb
59
+ - test/app_root/db/migrate/20091110075648_create_veteran_soldiers.rb
60
+ - test/app_root/db/migrate/20091110075649_create_trust_fund_kids.rb
56
61
  - test/app_root/lib/console_with_fixtures.rb
57
62
  - test/app_root/log/.gitignore
58
63
  - test/app_root/script/console
@@ -90,9 +95,12 @@ signing_key:
90
95
  specification_version: 3
91
96
  summary: Role-based permissions for your user models.
92
97
  test_files:
98
+ - test/app_root/app/models/trust_fund_kid.rb
99
+ - test/app_root/app/models/veteran_soldier.rb
93
100
  - test/app_root/app/models/permissions.rb
94
101
  - test/app_root/app/models/soldier.rb
95
102
  - test/app_root/app/models/user_subclass.rb
103
+ - test/app_root/app/models/old_soldier.rb
96
104
  - test/app_root/app/models/user.rb
97
105
  - test/app_root/app/controllers/application_controller.rb
98
106
  - test/app_root/config/environment.rb
@@ -105,6 +113,8 @@ test_files:
105
113
  - test/app_root/config/routes.rb
106
114
  - test/app_root/db/migrate/20090429075648_create_soldiers.rb
107
115
  - test/app_root/db/migrate/20090408115228_create_users.rb
116
+ - test/app_root/db/migrate/20091110075649_create_trust_fund_kids.rb
117
+ - test/app_root/db/migrate/20091110075648_create_veteran_soldiers.rb
108
118
  - test/app_root/lib/console_with_fixtures.rb
109
119
  - test/validation_test.rb
110
120
  - test/test_helper.rb