thelinuxlich-aegis 1.1.7

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.
Files changed (46) hide show
  1. data/.gitignore +3 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +195 -0
  4. data/Rakefile +37 -0
  5. data/VERSION +1 -0
  6. data/aegis.gemspec +107 -0
  7. data/lib/aegis.rb +10 -0
  8. data/lib/aegis/constants.rb +7 -0
  9. data/lib/aegis/has_role.rb +110 -0
  10. data/lib/aegis/normalization.rb +26 -0
  11. data/lib/aegis/permission_error.rb +5 -0
  12. data/lib/aegis/permission_evaluator.rb +34 -0
  13. data/lib/aegis/permissions.rb +107 -0
  14. data/lib/aegis/role.rb +55 -0
  15. data/lib/rails/active_record.rb +5 -0
  16. data/test/app_root/app/controllers/application_controller.rb +2 -0
  17. data/test/app_root/app/models/old_soldier.rb +6 -0
  18. data/test/app_root/app/models/permissions.rb +49 -0
  19. data/test/app_root/app/models/soldier.rb +5 -0
  20. data/test/app_root/app/models/trust_fund_kid.rb +5 -0
  21. data/test/app_root/app/models/user.rb +6 -0
  22. data/test/app_root/app/models/user_subclass.rb +2 -0
  23. data/test/app_root/app/models/veteran_soldier.rb +6 -0
  24. data/test/app_root/config/boot.rb +114 -0
  25. data/test/app_root/config/database.yml +21 -0
  26. data/test/app_root/config/environment.rb +14 -0
  27. data/test/app_root/config/environments/in_memory.rb +0 -0
  28. data/test/app_root/config/environments/mysql.rb +0 -0
  29. data/test/app_root/config/environments/postgresql.rb +0 -0
  30. data/test/app_root/config/environments/sqlite.rb +0 -0
  31. data/test/app_root/config/environments/sqlite3.rb +0 -0
  32. data/test/app_root/config/routes.rb +4 -0
  33. data/test/app_root/db/migrate/20090408115228_create_users.rb +14 -0
  34. data/test/app_root/db/migrate/20090429075648_create_soldiers.rb +14 -0
  35. data/test/app_root/db/migrate/20091110075648_create_veteran_soldiers.rb +14 -0
  36. data/test/app_root/db/migrate/20091110075649_create_trust_fund_kids.rb +15 -0
  37. data/test/app_root/lib/console_with_fixtures.rb +4 -0
  38. data/test/app_root/log/.gitignore +1 -0
  39. data/test/app_root/script/console +7 -0
  40. data/test/has_role_options_test.rb +64 -0
  41. data/test/has_role_test.rb +54 -0
  42. data/test/permissions_test.rb +109 -0
  43. data/test/test_helper.rb +23 -0
  44. data/test/validation_test.rb +55 -0
  45. data/thelinuxlich-aegis.gemspec +109 -0
  46. metadata +131 -0
@@ -0,0 +1,21 @@
1
+ in_memory:
2
+ adapter: sqlite3
3
+ database: ":memory:"
4
+ verbosity: quiet
5
+ sqlite:
6
+ adapter: sqlite
7
+ dbfile: plugin_test.sqlite.db
8
+ sqlite3:
9
+ adapter: sqlite3
10
+ dbfile: plugin_test.sqlite3.db
11
+ postgresql:
12
+ adapter: postgresql
13
+ username: postgres
14
+ password: postgres
15
+ database: plugin_test
16
+ mysql:
17
+ adapter: mysql
18
+ host: localhost
19
+ username: root
20
+ password:
21
+ database: plugin_test
@@ -0,0 +1,14 @@
1
+ require File.join(File.dirname(__FILE__), 'boot')
2
+
3
+ Rails::Initializer.run do |config|
4
+ config.cache_classes = false
5
+ config.whiny_nils = true
6
+ config.action_controller.session = { :key => "_myapp_session", :secret => "gwirofjweroijger8924rt2zfwehfuiwehb1378rifowenfoqwphf23" }
7
+ config.plugin_locators.unshift(
8
+ Class.new(Rails::Plugin::Locator) do
9
+ def plugins
10
+ [Rails::Plugin.new(File.expand_path('.'))]
11
+ end
12
+ end
13
+ ) unless defined?(PluginTestHelper::PluginLocator)
14
+ end
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,4 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ map.connect ':controller/:action/:id'
3
+ map.connect ':controller/:action/:id.:format'
4
+ end
@@ -0,0 +1,14 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ create_table :users do |t|
5
+ t.string :role_name
6
+ t.timestamps
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :users
12
+ end
13
+
14
+ end
@@ -0,0 +1,14 @@
1
+ class CreateSoldiers < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ create_table :soldiers do |t|
5
+ t.string :rank
6
+ t.timestamps
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :soldiers
12
+ end
13
+
14
+ end
@@ -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
@@ -0,0 +1,4 @@
1
+ # Loads fixtures into the database when running the test app via the console
2
+ (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(Rails.root, '../fixtures/*.{yml,csv}'))).each do |fixture_file|
3
+ Fixtures.create_fixtures(File.join(Rails.root, '../fixtures'), File.basename(fixture_file, '.*'))
4
+ end
@@ -0,0 +1 @@
1
+ *.log
@@ -0,0 +1,7 @@
1
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
2
+ libs = " -r irb/completion"
3
+ libs << " -r test/test_helper"
4
+ libs << " -r console_app"
5
+ libs << " -r console_with_helpers"
6
+ libs << " -r console_with_fixtures"
7
+ exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,64 @@
1
+ require "test/test_helper"
2
+
3
+ class HasRoleOptionsTest < ActiveSupport::TestCase
4
+
5
+ context "A record with a custom role field" do
6
+
7
+ setup do
8
+ @soldier = Soldier.new
9
+ end
10
+
11
+ should "allow its role to be written and read" do
12
+ @soldier.role = "guest"
13
+ assert_equal :guest, @soldier.role.name
14
+ end
15
+
16
+ should "store the role name in the custom field" do
17
+ @soldier.role = "guest"
18
+ assert_equal "guest", @soldier.rank
19
+ end
20
+
21
+ should "still work with permissions" do
22
+ @soldier.role = "guest"
23
+ assert @soldier.may_hug?
24
+ assert !@soldier.may_update_users?
25
+ end
26
+
27
+ end
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 "set that role if the initial role name is blank" do
49
+ assert_equal :admin, TrustFundKid.new(:role_name => "").role.name
50
+ end
51
+
52
+ should "ignore the default if another role is given" do
53
+ assert_equal :student, TrustFundKid.new(:role_name => "student").role.name
54
+ end
55
+
56
+ should "not update existing records with the default role" do
57
+ kid = TrustFundKid.create!(:role_name => "student")
58
+ kid.update_attributes(:account_balance => 10_000_000)
59
+ assert_equal :student, kid.reload.role.name
60
+ end
61
+
62
+ end
63
+
64
+ end
@@ -0,0 +1,54 @@
1
+ require "test/test_helper"
2
+
3
+ class HasRoleTest < ActiveSupport::TestCase
4
+
5
+ context "Objects that have an aegis role" do
6
+
7
+ setup do
8
+ @guest = User.new(:role_name => "guest")
9
+ @student = User.new(:role_name => "student")
10
+ @student_subclass = UserSubclass.new(:role_name => "student")
11
+ @admin = User.new(:role_name => "admin")
12
+ end
13
+
14
+ should "know their role" do
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
+ end
20
+
21
+ should "know if they belong to a role" do
22
+ assert @guest.guest?
23
+ assert !@guest.student?
24
+ assert !@guest.admin?
25
+ assert !@student.guest?
26
+ assert !@student_subclass.guest?
27
+ assert @student.student?
28
+ assert @student_subclass.student?
29
+ assert !@student.admin?
30
+ assert !@student_subclass.admin?
31
+ assert !@admin.guest?
32
+ assert !@admin.student?
33
+ assert @admin.admin?
34
+ end
35
+
36
+ should "still behave as usual when a method ending in a '?' does not map to a role query" do
37
+ assert_raise NoMethodError do
38
+ @guest.nonexisting_method?
39
+ end
40
+ end
41
+
42
+ should "know that they respond to permission methods" do
43
+ assert @guest.respond_to?(:may_foo?)
44
+ assert @guest.respond_to?(:may_foo!)
45
+ end
46
+
47
+ should "retain the usual respond_to behaviour for non-permission methods" do
48
+ assert !@guest.respond_to?(:nonexisting_method)
49
+ assert @guest.respond_to?(:to_s)
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,109 @@
1
+ require "test/test_helper"
2
+
3
+ class PermissionsTest < ActiveSupport::TestCase
4
+
5
+ context "Aegis permissions" do
6
+
7
+ setup do
8
+ @guest = User.new(:role_name => "guest")
9
+ @student = User.new(:role_name => "student")
10
+ @student_subclass = UserSubclass.new(:role_name => "student")
11
+ @admin = User.new(:role_name => "admin")
12
+ end
13
+
14
+ should "use the default permission for actions without any allow or grant directives" do
15
+ assert !@guest.may_use_empty?
16
+ assert !@student.may_use_empty?
17
+ assert !@student_subclass.may_use_empty?
18
+ assert @admin.may_use_empty?
19
+ end
20
+
21
+ should "understand simple allow and deny directives" do
22
+ assert !@guest.may_use_simple?
23
+ assert @student.may_use_simple?
24
+ assert @student_subclass.may_use_simple?
25
+ assert !@admin.may_use_simple?
26
+ end
27
+
28
+ should 'raise exceptions when a denied action is queried with an exclamation mark' do
29
+ assert_raise Aegis::PermissionError do
30
+ @guest.may_use_simple!
31
+ end
32
+ assert_raise Aegis::PermissionError do
33
+ @admin.may_use_simple!
34
+ end
35
+ end
36
+
37
+ should 'do nothing if an allowed action is queried with an exclamation mark' do
38
+ assert_nothing_raised do
39
+ @student.may_use_simple!
40
+ @student_subclass.may_use_simple!
41
+ end
42
+ end
43
+
44
+ should "implicate the singular form of an action described in plural form" do
45
+ assert !@guest.may_update_users?
46
+ assert !@guest.may_update_user?("foo")
47
+ assert @student.may_update_users?
48
+ assert @student_subclass.may_update_users?
49
+ assert @student.may_update_user?("foo")
50
+ assert @student_subclass.may_update_user?("foo")
51
+ assert !@admin.may_update_users?
52
+ assert !@admin.may_update_user?("foo")
53
+ end
54
+
55
+ should 'implicate create, read, update and destroy forms for actions named "crud_..."' do
56
+ assert @student.may_create_projects?
57
+ assert @student_subclass.may_create_projects?
58
+ assert @student.may_read_projects?
59
+ assert @student_subclass.may_read_projects?
60
+ assert @student.may_update_projects?
61
+ assert @student_subclass.may_update_projects?
62
+ assert @student.may_destroy_projects?
63
+ assert @student_subclass.may_destroy_projects?
64
+ end
65
+
66
+ should 'perform normalization of CRUD verbs (e.g. "edit" and "update")' do
67
+ assert !@guest.may_edit_drinks?
68
+ assert @student.may_edit_drinks?
69
+ assert @student_subclass.may_edit_drinks?
70
+ assert !@admin.may_edit_drinks?
71
+ assert !@guest.may_update_drinks?
72
+ assert @student.may_update_drinks?
73
+ assert @student_subclass.may_update_drinks?
74
+ assert !@admin.may_update_drinks?
75
+ end
76
+
77
+ should "be able to grant or deny actions to all roles using :everyone" do
78
+ assert @guest.may_hug?
79
+ assert @student.may_hug?
80
+ assert @student_subclass.may_hug?
81
+ assert @admin.may_hug?
82
+ end
83
+
84
+ should "allow the definition of parametrized actions" do
85
+ assert !@guest.may_divide?(10, 2)
86
+ assert @student.may_divide?(10, 2)
87
+ assert @student_subclass.may_divide?(10, 2)
88
+ assert !@student.may_divide?(10, 0)
89
+ assert !@student_subclass.may_divide?(10, 0)
90
+ assert @admin.may_divide?(10, 2)
91
+ assert @admin.may_divide?(10, 0)
92
+ end
93
+
94
+ should 'use default permissions for undefined actions' do
95
+ !@student.may_do_undefined_stuff?("foo")
96
+ !@student_subclass.may_do_undefined_stuff?("foo")
97
+ @admin.may_do_undefined_stuff?("foo")
98
+ end
99
+
100
+ should 'overshadow previous action definitions with the same name' do
101
+ assert @guest.may_draw?
102
+ assert !@student.may_draw?
103
+ assert !@student_subclass.may_draw?
104
+ assert !@admin.may_draw?
105
+ end
106
+
107
+ end
108
+
109
+ end
@@ -0,0 +1,23 @@
1
+ # Set the default environment to sqlite3's in_memory database
2
+ ENV['RAILS_ENV'] ||= 'in_memory'
3
+
4
+ # Load the Rails environment and testing framework
5
+ require "#{File.dirname(__FILE__)}/app_root/config/environment"
6
+ require "#{File.dirname(__FILE__)}/../lib/aegis"
7
+ require 'test_help'
8
+ require 'action_view/test_case' # Load additional test classes not done automatically by < Rails 2.2.2
9
+
10
+ require "shoulda"
11
+
12
+ # Undo changes to RAILS_ENV
13
+ silence_warnings {RAILS_ENV = ENV['RAILS_ENV']}
14
+
15
+ # Run the migrations
16
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")
17
+
18
+ # Set default fixture loading properties
19
+ ActiveSupport::TestCase.class_eval do
20
+ self.use_transactional_fixtures = true
21
+ self.use_instantiated_fixtures = false
22
+ self.fixture_path = "#{File.dirname(__FILE__)}/fixtures"
23
+ end
@@ -0,0 +1,55 @@
1
+ require "test/test_helper"
2
+
3
+ class ValidationTest < ActiveSupport::TestCase
4
+
5
+ context "A model that has and validates its role" do
6
+
7
+ setup do
8
+ @user = User.new()
9
+ end
10
+
11
+ context "that has a role_name mapping to a role" do
12
+
13
+ setup do
14
+ @user.role_name = "admin"
15
+ end
16
+
17
+ should "be valid" do
18
+ assert @user.valid?
19
+ end
20
+
21
+ end
22
+
23
+ context "that has a blank role_name" do
24
+
25
+ setup do
26
+ @user.role_name = ""
27
+ end
28
+
29
+ should "not be valid" do
30
+ assert !@user.valid?
31
+ end
32
+
33
+ end
34
+
35
+ context "that has a role_name not mapping to a role" do
36
+
37
+ setup do
38
+ @user.role_name = "nonexisting_role_name"
39
+ end
40
+
41
+ should "not be valid" do
42
+ assert !@user.valid?
43
+ end
44
+
45
+ end
46
+
47
+ should "use add the default inclusion error message on role_name" do
48
+ @user.role_name = ""
49
+ @user.valid?
50
+ assert_equal I18n.translate('activerecord.errors.messages.inclusion'), @user.errors.on(:role_name)
51
+ end
52
+
53
+ end
54
+
55
+ end