may_may 0.0.1

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 (54) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +85 -0
  3. data/Rakefile +38 -0
  4. data/lib/may_may/version.rb +3 -0
  5. data/lib/may_may.rb +102 -0
  6. data/lib/tasks/may_may_tasks.rake +4 -0
  7. data/test/dummy/README.rdoc +261 -0
  8. data/test/dummy/Rakefile +7 -0
  9. data/test/dummy/app/assets/javascripts/application.js +15 -0
  10. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  11. data/test/dummy/app/controllers/application_controller.rb +7 -0
  12. data/test/dummy/app/controllers/may_mays_controller.rb +27 -0
  13. data/test/dummy/app/helpers/application_helper.rb +2 -0
  14. data/test/dummy/app/models/may.rb +9 -0
  15. data/test/dummy/app/models/role.rb +22 -0
  16. data/test/dummy/app/models/user.rb +22 -0
  17. data/test/dummy/app/models/user_role.rb +4 -0
  18. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  19. data/test/dummy/app/views/may_mays/new.html.erb +3 -0
  20. data/test/dummy/config/application.rb +56 -0
  21. data/test/dummy/config/boot.rb +10 -0
  22. data/test/dummy/config/database.yml +25 -0
  23. data/test/dummy/config/environment.rb +5 -0
  24. data/test/dummy/config/environments/development.rb +37 -0
  25. data/test/dummy/config/environments/production.rb +67 -0
  26. data/test/dummy/config/environments/test.rb +37 -0
  27. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  28. data/test/dummy/config/initializers/inflections.rb +15 -0
  29. data/test/dummy/config/initializers/mime_types.rb +5 -0
  30. data/test/dummy/config/initializers/secret_token.rb +7 -0
  31. data/test/dummy/config/initializers/session_store.rb +8 -0
  32. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  33. data/test/dummy/config/locales/en.yml +5 -0
  34. data/test/dummy/config/routes.rb +3 -0
  35. data/test/dummy/config.ru +4 -0
  36. data/test/dummy/db/development.sqlite3 +0 -0
  37. data/test/dummy/db/migrate/20120718155853_create_roles.rb +10 -0
  38. data/test/dummy/db/migrate/20120718160104_create_users.rb +10 -0
  39. data/test/dummy/db/migrate/20120718160138_create_user_roles.rb +11 -0
  40. data/test/dummy/db/schema.rb +35 -0
  41. data/test/dummy/db/test.sqlite3 +0 -0
  42. data/test/dummy/log/development.log +118 -0
  43. data/test/dummy/log/test.log +4792 -0
  44. data/test/dummy/public/404.html +26 -0
  45. data/test/dummy/public/422.html +26 -0
  46. data/test/dummy/public/500.html +25 -0
  47. data/test/dummy/public/favicon.ico +0 -0
  48. data/test/dummy/script/rails +6 -0
  49. data/test/may_may_test.rb +92 -0
  50. data/test/may_mays_controller_test.rb +54 -0
  51. data/test/role_test.rb +7 -0
  52. data/test/test_helper.rb +10 -0
  53. data/test/user_test.rb +8 -0
  54. metadata +170 -0
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,92 @@
1
+ require 'test_helper'
2
+
3
+ class Roles
4
+ def self.before_filter(*params)
5
+ end
6
+
7
+ def self.helper_method(*params)
8
+ end
9
+
10
+ include MayMay::MayMayACExtensions
11
+ MayMay::MayMayACExtensions.setup(self)
12
+
13
+ attr_accessor :current_roles
14
+
15
+ def initialize(roles = [])
16
+ roles = [roles] unless roles.respond_to? :each
17
+ self.current_roles = roles
18
+ end
19
+ end
20
+
21
+ class MayMayTest < ActiveSupport::TestCase
22
+ test "access_denied if not specifically permitted" do
23
+ May.permissions_setup {}
24
+ assert !May.permission_to?(:test_1_action, :people, Roles.new)
25
+ end
26
+
27
+ test "access allowed for anyone if named but no roles specified" do
28
+ May.permissions_setup { controller(:people) { may :test_2_action } }
29
+ assert May.permission_to?(:test_2_action, :people, Roles.new)
30
+ end
31
+
32
+ test "access allowed for specified role only" do
33
+ May.permissions_setup { controller(:people) { may :test_3_action, :only => :a_role } }
34
+ with_correct_role = May.permission_to?(:test_3_action, :people, Roles.new(:a_role))
35
+ without_correct_role = May.permission_to?(:test_3_action, :people, Roles.new(:b_role))
36
+ assert_equal [true, false], [with_correct_role, without_correct_role]
37
+ end
38
+
39
+ test "access allowed for specified roles" do
40
+ May.permissions_setup { controller(:people) { may :test_4_action, :only => [:a_role1, :a_role2] } }
41
+ with_first_role = May.permission_to?(:test_4_action, :people, Roles.new(:a_role1))
42
+ with_second_role = May.permission_to?(:test_4_action, :people, Roles.new(:a_role2))
43
+ with_both_roles = May.permission_to?(:test_4_action, :people, Roles.new([:a_role1, :a_role2]))
44
+ with_neither_role = May.permission_to?(:test_4_action, :people, Roles.new(:b_role))
45
+ assert_equal [true, true, true, false], [with_first_role, with_second_role, with_both_roles, with_neither_role]
46
+ end
47
+
48
+ test "access denied for specified roles" do
49
+ May.permissions_setup { controller(:people) { may :test_5_action, :except => [:a_role1, :a_role2] } }
50
+ with_first_role = May.permission_to?(:test_5_action, :people, Roles.new(:a_role1))
51
+ with_second_role = May.permission_to?(:test_5_action, :people, Roles.new(:a_role2))
52
+ with_both_roles = May.permission_to?(:test_5_action, :people, Roles.new([:a_role1, :a_role2]))
53
+ with_neither_role = May.permission_to?(:test_5_action, :people, Roles.new(:b_role))
54
+ assert_equal [false, false, false, true], [with_first_role, with_second_role, with_both_roles, with_neither_role]
55
+ end
56
+
57
+ test "access denied by block" do
58
+ May.permissions_setup { controller(:people) { may(:test_6_action) {|controller| false } } }
59
+ assert !May.permission_to?(:test_6_action, :people, Roles.new)
60
+ end
61
+
62
+ test "access allowed by block" do
63
+ May.permissions_setup { controller(:people) { may(:test_7_action) {|controller| true } } }
64
+ assert May.permission_to?(:test_7_action, :people, Roles.new)
65
+ end
66
+
67
+ test "controller may method works" do
68
+ May.permissions_setup { controller(:people) { may(:test_8_action, :only => [:a_role]) } }
69
+ with_role = Roles.new(:a_role).may?(:test_8_action, :people)
70
+ without_role = Roles.new.may?(:test_8_action, :people)
71
+ assert_equal [true, false], [with_role, without_role]
72
+ end
73
+
74
+ test "controller may method with block" do
75
+ May.permissions_setup { controller(:people) { may(:test_9_action, :only => [:a_role]) } }
76
+ with_role = false
77
+ Roles.new(:a_role).may?(:test_9_action, :people) { with_role = 'allowed!' }
78
+ without_role = Roles.new.may?(:test_9_action, :people) { without_role = 'not allowed!' }
79
+ assert_equal ['allowed!', false], [with_role, without_role]
80
+ end
81
+
82
+ test "controller may with permission block" do
83
+ can = true
84
+ May.permissions_setup {controller(:people) { may(:test_10_action) { can } } }
85
+ should = false
86
+ Roles.new.may?(:test_10_action, :people) { should = 'allowed!' }
87
+ can = false
88
+ should_not = false
89
+ should_not = Roles.new.may?(:test_10_action, :people) { should_not = 'not allowed!' }
90
+ assert_equal ['allowed!', false], [should, should_not]
91
+ end
92
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class MayMaysControllerTest < ActionController::TestCase
4
+ test "all may index" do
5
+ get :index, user: :guest
6
+ assert_response :success
7
+ end
8
+
9
+ test "standard may call new but no edit link returned" do
10
+ get :new, user: :standard
11
+ assert_response :success
12
+ assert_equal '', response.body
13
+ end
14
+
15
+ test "guest may show" do
16
+ get :show, :id => 1, user: :guest
17
+ assert_response :success
18
+ end
19
+
20
+ test "standard may show" do
21
+ get :show, :id => 1, user: :standard
22
+ assert_response :success
23
+ end
24
+
25
+ test "admin may not show" do
26
+ get :show, :id => 1, user: :admin
27
+ assert_response 403
28
+ end
29
+
30
+ test "new includes edit link for admin only" do
31
+ get :new, user: :admin
32
+ assert_response :success
33
+ assert_equal 'Edit link', response.body.strip
34
+ end
35
+
36
+ test "standard cannot destroy" do
37
+ delete :destroy, :id => 1, user: :standard
38
+ assert_response 403
39
+ end
40
+
41
+ test "admin may destroy" do
42
+ delete :destroy, :id => 1, user: :admin
43
+ end
44
+
45
+ test "guest may not create" do
46
+ post :create, user: :guest
47
+ assert_response 403
48
+ end
49
+
50
+ test "admin may create" do
51
+ post :create, user: :admin
52
+ assert_response :success
53
+ end
54
+ end
data/test/role_test.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class RoleTest < ActiveSupport::TestCase
4
+ test 'role names are symbols' do
5
+ assert_equal :admin, Role.admin.name
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+ require "rails/test_help"
6
+
7
+ Rails.backtrace_cleaner.remove_silencers!
8
+
9
+ # Load support files
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
data/test/user_test.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class UserTest < ActiveSupport::TestCase
4
+ test "admin has admin and standard roles" do
5
+ u = User.admin
6
+ assert u.role_names.include?(:admin) && u.role_names.include?(:standard)
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: may_may
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Without Software (James Roscoe)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-19 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70277837961100 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70277837961100
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3
27
+ requirement: &70277837960680 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70277837960680
36
+ description: ! 'Manage controller/action permissions easily in Rails. Returns 403
37
+ (Access Denied) unless current user has permission to perform the requested controller
38
+ action. Easy syntax for use in views to check permission: may?(:action, :controller).
39
+ Source at https://github.com/without/may_may.git'
40
+ email:
41
+ - james@withoutsoftware.com
42
+ executables: []
43
+ extensions: []
44
+ extra_rdoc_files: []
45
+ files:
46
+ - lib/may_may/version.rb
47
+ - lib/may_may.rb
48
+ - lib/tasks/may_may_tasks.rake
49
+ - MIT-LICENSE
50
+ - Rakefile
51
+ - README.md
52
+ - test/dummy/app/assets/javascripts/application.js
53
+ - test/dummy/app/assets/stylesheets/application.css
54
+ - test/dummy/app/controllers/application_controller.rb
55
+ - test/dummy/app/controllers/may_mays_controller.rb
56
+ - test/dummy/app/helpers/application_helper.rb
57
+ - test/dummy/app/models/may.rb
58
+ - test/dummy/app/models/role.rb
59
+ - test/dummy/app/models/user.rb
60
+ - test/dummy/app/models/user_role.rb
61
+ - test/dummy/app/views/layouts/application.html.erb
62
+ - test/dummy/app/views/may_mays/new.html.erb
63
+ - test/dummy/config/application.rb
64
+ - test/dummy/config/boot.rb
65
+ - test/dummy/config/database.yml
66
+ - test/dummy/config/environment.rb
67
+ - test/dummy/config/environments/development.rb
68
+ - test/dummy/config/environments/production.rb
69
+ - test/dummy/config/environments/test.rb
70
+ - test/dummy/config/initializers/backtrace_silencers.rb
71
+ - test/dummy/config/initializers/inflections.rb
72
+ - test/dummy/config/initializers/mime_types.rb
73
+ - test/dummy/config/initializers/secret_token.rb
74
+ - test/dummy/config/initializers/session_store.rb
75
+ - test/dummy/config/initializers/wrap_parameters.rb
76
+ - test/dummy/config/locales/en.yml
77
+ - test/dummy/config/routes.rb
78
+ - test/dummy/config.ru
79
+ - test/dummy/db/development.sqlite3
80
+ - test/dummy/db/migrate/20120718155853_create_roles.rb
81
+ - test/dummy/db/migrate/20120718160104_create_users.rb
82
+ - test/dummy/db/migrate/20120718160138_create_user_roles.rb
83
+ - test/dummy/db/schema.rb
84
+ - test/dummy/db/test.sqlite3
85
+ - test/dummy/log/development.log
86
+ - test/dummy/log/test.log
87
+ - test/dummy/public/404.html
88
+ - test/dummy/public/422.html
89
+ - test/dummy/public/500.html
90
+ - test/dummy/public/favicon.ico
91
+ - test/dummy/Rakefile
92
+ - test/dummy/README.rdoc
93
+ - test/dummy/script/rails
94
+ - test/may_may_test.rb
95
+ - test/may_mays_controller_test.rb
96
+ - test/role_test.rb
97
+ - test/test_helper.rb
98
+ - test/user_test.rb
99
+ homepage: http://withoutsoftware.com
100
+ licenses: []
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.10
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: Rails Controller action permission management
123
+ test_files:
124
+ - test/dummy/app/assets/javascripts/application.js
125
+ - test/dummy/app/assets/stylesheets/application.css
126
+ - test/dummy/app/controllers/application_controller.rb
127
+ - test/dummy/app/controllers/may_mays_controller.rb
128
+ - test/dummy/app/helpers/application_helper.rb
129
+ - test/dummy/app/models/may.rb
130
+ - test/dummy/app/models/role.rb
131
+ - test/dummy/app/models/user.rb
132
+ - test/dummy/app/models/user_role.rb
133
+ - test/dummy/app/views/layouts/application.html.erb
134
+ - test/dummy/app/views/may_mays/new.html.erb
135
+ - test/dummy/config/application.rb
136
+ - test/dummy/config/boot.rb
137
+ - test/dummy/config/database.yml
138
+ - test/dummy/config/environment.rb
139
+ - test/dummy/config/environments/development.rb
140
+ - test/dummy/config/environments/production.rb
141
+ - test/dummy/config/environments/test.rb
142
+ - test/dummy/config/initializers/backtrace_silencers.rb
143
+ - test/dummy/config/initializers/inflections.rb
144
+ - test/dummy/config/initializers/mime_types.rb
145
+ - test/dummy/config/initializers/secret_token.rb
146
+ - test/dummy/config/initializers/session_store.rb
147
+ - test/dummy/config/initializers/wrap_parameters.rb
148
+ - test/dummy/config/locales/en.yml
149
+ - test/dummy/config/routes.rb
150
+ - test/dummy/config.ru
151
+ - test/dummy/db/development.sqlite3
152
+ - test/dummy/db/migrate/20120718155853_create_roles.rb
153
+ - test/dummy/db/migrate/20120718160104_create_users.rb
154
+ - test/dummy/db/migrate/20120718160138_create_user_roles.rb
155
+ - test/dummy/db/schema.rb
156
+ - test/dummy/db/test.sqlite3
157
+ - test/dummy/log/development.log
158
+ - test/dummy/log/test.log
159
+ - test/dummy/public/404.html
160
+ - test/dummy/public/422.html
161
+ - test/dummy/public/500.html
162
+ - test/dummy/public/favicon.ico
163
+ - test/dummy/Rakefile
164
+ - test/dummy/README.rdoc
165
+ - test/dummy/script/rails
166
+ - test/may_may_test.rb
167
+ - test/may_mays_controller_test.rb
168
+ - test/role_test.rb
169
+ - test/test_helper.rb
170
+ - test/user_test.rb