radiant-rbac_base-extension 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/.gitignore +1 -0
  2. data/HELP_developer.md +18 -0
  3. data/README.markdown +30 -0
  4. data/Rakefile +137 -0
  5. data/VERSION +1 -0
  6. data/app/controllers/admin/roles_controller.rb +91 -0
  7. data/app/helpers/admin/alterations_helper.rb +9 -0
  8. data/app/helpers/admin/roles_helper.rb +5 -0
  9. data/app/models/role.rb +33 -0
  10. data/app/models/role_action_observer.rb +13 -0
  11. data/app/models/role_user.rb +2 -0
  12. data/app/views/admin/roles/_add_role_form.html.haml +4 -0
  13. data/app/views/admin/roles/index.html.haml +30 -0
  14. data/app/views/admin/roles/show.html.haml +29 -0
  15. data/app/views/admin/users/preferences.html.haml +34 -0
  16. data/config/locales/en.yml +3 -0
  17. data/config/routes.rb +8 -0
  18. data/cucumber.yml +1 -0
  19. data/db/migrate/001_create_roles.rb +11 -0
  20. data/db/migrate/002_create_role_users.rb +16 -0
  21. data/db/migrate/003_setup_standard_roles.rb +29 -0
  22. data/db/migrate/004_alter_roles.rb +10 -0
  23. data/db/migrate/005_add_standard_role_details.rb +20 -0
  24. data/db/migrate/006_add_user_info.rb +10 -0
  25. data/db/migrate/20100705182511_rename_role_developer_to_designer.rb +13 -0
  26. data/features/support/env.rb +16 -0
  27. data/features/support/paths.rb +14 -0
  28. data/lib/rbac_support.rb +19 -0
  29. data/lib/tasks/rbac_base_extension_tasks.rake +55 -0
  30. data/public/javascripts/rbac/admin/role_details.js +162 -0
  31. data/public/stylesheets/rbac/rbac.css +20 -0
  32. data/radiant-rbac_base-extension.gemspec +91 -0
  33. data/rbac_base_extension.rb +28 -0
  34. data/spec/controllers/admin/roles_controller_spec.rb +142 -0
  35. data/spec/controllers/admin/roles_routing_spec.rb +27 -0
  36. data/spec/helpers/admin/roles_helper_spec.rb +12 -0
  37. data/spec/models/role_spec.rb +82 -0
  38. data/spec/models/user_spec.rb +9 -0
  39. data/spec/spec.opts +6 -0
  40. data/spec/spec_helper.rb +36 -0
  41. data/spec/views/admin/roles/index_spec.rb +17 -0
  42. data/spec/views/admin/roles/show_spec.rb +14 -0
  43. metadata +129 -0
@@ -0,0 +1,28 @@
1
+ # Uncomment this if you reference any of your controllers in activate
2
+ require_dependency 'application_controller'
3
+
4
+ class RbacBaseExtension < Radiant::Extension
5
+ version "#{File.read(File.expand_path(File.dirname(__FILE__)) + '/VERSION')}"
6
+ description "Allows other extensions to control access managed by the roles created here. Administrators may add and remove users from roles as needed without regard to the standard Radiant roles."
7
+ url "http://www.saturnflyer.com/"
8
+
9
+ def activate
10
+ Radiant::Config['roles.admin.sees_everything'] = 'true' unless Radiant::Config['roles.admin.sees_everything']
11
+ if Role.table_exists?
12
+ tab 'Settings' do
13
+ add_item('Roles', '/admin/roles')
14
+ end
15
+ User.send :has_and_belongs_to_many, :roles
16
+ User.send :include, RbacSupport
17
+ admin.users.edit[:form].delete('edit_roles')
18
+ UserActionObserver.instance.send :add_observer!, Role
19
+ end
20
+ Admin::UsersController.class_eval {
21
+ helper Admin::AlterationsHelper
22
+ }
23
+ end
24
+
25
+ def deactivate
26
+ end
27
+
28
+ end
@@ -0,0 +1,142 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Admin::RolesController do
4
+ let(:current_user){ mock_model(User)}
5
+ let(:role){mock_model(Role)}
6
+ before do
7
+ controller.stub!(:current_user).and_return(current_user)
8
+ current_user.stub!(:admin?).and_return(true)
9
+ current_user.stub(:has_role?).with(:admin).and_return(true)
10
+ current_user.stub(:locale).and_return(:en)
11
+ end
12
+ describe 'GET index' do
13
+ it "should assign all roles as roles" do
14
+ roles = []
15
+ Role.should_receive(:find).with(:all).and_return(roles)
16
+ get :index
17
+ assigns[:roles].should == roles
18
+ end
19
+ end
20
+ describe 'GET show' do
21
+ it "should find the role from the params" do
22
+ Role.should_receive(:find).with('1').and_return(role)
23
+ get :show, :id => '1'
24
+ end
25
+ it "should assign the found role as role" do
26
+ Role.stub!(:find).and_return(role)
27
+ get :show, :id => '1'
28
+ assigns[:role].should == role
29
+ end
30
+ end
31
+ describe 'POST create' do
32
+ it "should save a role from the params" do
33
+ role = mock_model(Role)
34
+ role.should_receive(:save!).and_return(true)
35
+ Role.should_receive(:new).and_return(role)
36
+ post :create
37
+ end
38
+ it "should redirect to the roles index" do
39
+ post :create
40
+ response.should redirect_to(admin_roles_path)
41
+ end
42
+ describe "with invalid params" do
43
+ before do
44
+ role = mock_model(Role)
45
+ @errors = []
46
+ @errors.stub!(:full_messages).and_return(['bad', 'error'])
47
+ role.stub!(:errors).and_return(@errors)
48
+ role.should_receive(:save!).and_raise(ActiveRecord::RecordInvalid.new(role))
49
+ Role.stub!(:new).and_return(role)
50
+ end
51
+ it "should render the index page" do
52
+ post :create
53
+ response.should render_template('index')
54
+ end
55
+ it "should set the flash error message to the record's full errors" do
56
+ @errors.should_receive(:full_messages).and_return('bad, error')
57
+ post :create
58
+ flash[:error].should == 'bad, error'
59
+ end
60
+ end
61
+ end
62
+ describe 'DELETE destroy' do
63
+ before do
64
+ role.stub!(:destroy)
65
+ role.stub(:standard?).and_return(false)
66
+ Role.stub!(:find).and_return(role)
67
+ end
68
+ it "should find the role from the params" do
69
+ Role.should_receive(:find).with('1').and_return(role)
70
+ delete :destroy, :id => '1'
71
+ end
72
+ it "should destroy the found role" do
73
+ role.should_receive(:destroy).and_return(true)
74
+ delete :destroy, :id => '1'
75
+ end
76
+ it "should redirect to the roles index" do
77
+ delete :destroy, :id => '1'
78
+ response.should redirect_to(admin_roles_path)
79
+ end
80
+ describe "with invalid params" do
81
+ it "should redirect to the roles index" do
82
+ Role.should_receive(:find).with('1').and_raise(ActiveRecord::RecordNotFound.new(role))
83
+ delete :destroy, :id => '1'
84
+ response.should redirect_to(admin_roles_path)
85
+ end
86
+ end
87
+ describe "for a standard Radiant role" do
88
+ before do
89
+ role.stub!(:standard?).and_return(true)
90
+ Role.stub!(:find).and_return(role)
91
+ end
92
+ it "should redirect to the roles index" do
93
+ delete :destroy, :id => '1'
94
+ response.should redirect_to(admin_roles_path)
95
+ end
96
+ it "should not delete the role" do
97
+ role.should_not_receive(:destroy)
98
+ delete :destroy, :id => '1'
99
+ end
100
+ end
101
+ end
102
+ describe 'DELETE remove_user' do
103
+ before do
104
+ @user = mock_model(User)
105
+ role.stub!(:remove_user)
106
+ Role.stub!(:find).and_return(role)
107
+ User.stub!(:find).and_return(@user)
108
+
109
+ end
110
+ it "should find the role from the params" do
111
+ Role.should_receive(:find).with('1').and_return(role)
112
+ delete :remove_user, :role_id => '1', :id => '2'
113
+ end
114
+ it "should find the user from the params" do
115
+ User.should_receive(:find).with('2').and_return(@user)
116
+ delete :remove_user, :role_id => '1', :id => '2'
117
+ end
118
+ it "should remove the user" do
119
+ role.should_receive(:remove_user).and_return(true)
120
+ delete :remove_user, :role_id => '1', :id => '2'
121
+ end
122
+ end
123
+ describe 'PUT update' do
124
+ before do
125
+ role.stub!(:update_attributes).and_return(true)
126
+ Role.stub!(:find).and_return(role)
127
+ end
128
+ it "should find the role from the params" do
129
+ Role.should_receive(:find).with('1').and_return(role)
130
+ put :update, :id => '1'
131
+ end
132
+ it "should update the role's attributes from the params" do
133
+ role_params = {'this' => 'that'}
134
+ role.should_receive(:update_attributes).with(role_params).and_return(true)
135
+ put :update, :id => '1', :role => role_params
136
+ end
137
+ it "should redirect to the role's page" do
138
+ put :update, :id => '1'
139
+ response.should redirect_to(admin_role_path(role))
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Admin::RolesController, 'routing' do
4
+ describe "route generation" do
5
+ it "should map #users" do
6
+ '/admin/roles/1/users'.should route_to(:controller => "admin/roles", :action => "users", :role_id => "1")
7
+ end
8
+ it "should map #add_user" do
9
+ {:post => '/admin/roles/1/users/2'}.should route_to(:controller => "admin/roles", :action => "add_user", :role_id => "1", :id => "2")
10
+ end
11
+ it "should map #remove_user" do
12
+ { :delete => '/admin/roles/1/users/2'}.should route_to(:controller => "admin/roles", :action => "remove_user", :role_id => "1", :id => "2")
13
+ end
14
+ end
15
+
16
+ describe "route recognition" do
17
+ it "should generate params for #add_user" do
18
+ params_from(:post, "/admin/roles/1/users/2").should == {:controller => 'admin/roles', :action => 'add_user', :role_id => '1', :id => '2'}
19
+ end
20
+ it "should generate params for #remove_user" do
21
+ params_from(:delete, "/admin/roles/1/users/2").should == {:controller => 'admin/roles', :action => 'remove_user', :role_id => '1', :id => '2'}
22
+ end
23
+ it "should generate params for #users" do
24
+ params_from(:get, "/admin/roles/1/users").should == {:controller => 'admin/roles', :action => 'users', :role_id => '1'}
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,12 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Admin::RolesHelper do
4
+ describe "named routes" do
5
+ it "should write admin_role_user_path to '/admin/roles/:role_id/users/:id'" do
6
+ admin_role_user_path('1','2').should == '/admin/roles/1/users/2'
7
+ end
8
+ it "should write admin_role_users_path to '/admin/roles/:role_id/users'" do
9
+ admin_role_users_path('1').should == '/admin/roles/1/users'
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,82 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Role do
4
+
5
+ it "should err that role_name 'has already been taken' when saving without a unique name" do
6
+ Role.new.save!
7
+ lambda {
8
+ Role.new.save!
9
+ }.should raise_error(ActiveRecord::RecordInvalid, /is already in use/)
10
+ end
11
+
12
+ it "should not err when saved with a standard Radiant Role name" do
13
+ lambda { Role.new(:role_name => 'admin').save! }.should_not raise_error(ActiveRecord::RecordInvalid,/may not be any of: admin, developer/)
14
+ end
15
+
16
+ describe 'Role::RADIANT_STANDARDS' do
17
+ it "should be an array of 'admin' and 'designer'" do
18
+ Role::RADIANT_STANDARDS.should == ['admin','designer']
19
+ end
20
+ end
21
+
22
+ it "should err when destroying a Radiant standard role" do
23
+ Role::RADIANT_STANDARDS.each do |role|
24
+ Role.create!(:role_name => role)
25
+ lambda { Role.find_by_role_name(role).destroy }.should raise_error(Role::ProtectedRoleError, /is a protected role and may not be removed/)
26
+ end
27
+ end
28
+
29
+ it "should have a description" do
30
+ Role.new.respond_to?(:description).should be_true
31
+ end
32
+ it "should have an allow_empty field" do
33
+ Role.new.respond_to?(:allow_empty).should be_true
34
+ end
35
+
36
+ describe 'remove_user' do
37
+ before do
38
+ @user = mock_model(User)
39
+ @users = [@user]
40
+ @users.stub!(:<<).and_return(true)
41
+ @role = Role.create!(:role_name => 'Test')
42
+ @role.stub!(:allow_empty).and_return(true)
43
+ @role.stub!(:users).and_return(@users)
44
+ end
45
+ it "should delete the user from the role" do
46
+ @role.users << @user
47
+ @users.stub!(:size).and_return(2)
48
+ @users.should_receive(:delete).with(@user).and_return(true)
49
+ @role.remove_user(@user)
50
+ end
51
+ it "should return true" do
52
+ @role.remove_user(@user).should be_true
53
+ end
54
+ describe "with 1 user and allow_empty set to false" do
55
+ before do
56
+ @users.should_receive(:size).and_return(1)
57
+ @role.should_receive(:allow_empty).and_return(false)
58
+ end
59
+ it "should not delete the user from the role" do
60
+ @users.should_not_receive(:delete)
61
+ @role.remove_user(@user)
62
+ end
63
+ it "should return false" do
64
+ @role.remove_user(@user).should_not be_true
65
+ end
66
+ end
67
+ end
68
+
69
+ describe 'users' do
70
+ it "should return an array" do
71
+ Role.new.users.should == []
72
+ end
73
+ end
74
+
75
+ describe "standard?" do
76
+ it "should return true if the role's downcased role_name is in the Radiant standard roles" do
77
+ @role = Role.new(:role_name => 'admin')
78
+ @role.standard?.should be_true
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe User do
4
+ describe "roles" do
5
+ it "should return an array of roles" do
6
+ User.new.roles.should == []
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --loadby
5
+ mtime
6
+ --reverse
@@ -0,0 +1,36 @@
1
+ unless defined? RADIANT_ROOT
2
+ ENV["RAILS_ENV"] = "test"
3
+ case
4
+ when ENV["RADIANT_ENV_FILE"]
5
+ require ENV["RADIANT_ENV_FILE"]
6
+ when File.dirname(__FILE__) =~ %r{vendor/radiant/vendor/extensions}
7
+ require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../../../")}/config/environment"
8
+ else
9
+ require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../")}/config/environment"
10
+ end
11
+ end
12
+ require "#{RADIANT_ROOT}/spec/spec_helper"
13
+
14
+ Dataset::Resolver.default << (File.dirname(__FILE__) + "/datasets")
15
+
16
+ if File.directory?(File.dirname(__FILE__) + "/matchers")
17
+ Dir[File.dirname(__FILE__) + "/matchers/*.rb"].each {|file| require file }
18
+ end
19
+
20
+ Spec::Runner.configure do |config|
21
+ # config.use_transactional_fixtures = true
22
+ # config.use_instantiated_fixtures = false
23
+ # config.fixture_path = RAILS_ROOT + '/spec/fixtures'
24
+
25
+ # You can declare fixtures for each behaviour like this:
26
+ # describe "...." do
27
+ # fixtures :table_a, :table_b
28
+ #
29
+ # Alternatively, if you prefer to declare them only once, you can
30
+ # do so here, like so ...
31
+ #
32
+ # config.global_fixtures = :table_a, :table_b
33
+ #
34
+ # If you declare global fixtures, be aware that they will be declared
35
+ # for all of your examples, even those that don't use them.
36
+ end
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/../../../spec_helper'
2
+
3
+ describe 'index' do
4
+ let(:users){[]}
5
+ let(:role){mock_model(Role,:role_name => 'Test', :description => 'The test role.', :users => users, :standard? => false)}
6
+ let(:roles){[role]}
7
+ before do
8
+ template.should_receive(:include_stylesheet).with('rbac/rbac')
9
+ users.stub!(:count)
10
+ assigns[:role] = role
11
+ assigns[:roles] = roles
12
+ end
13
+ it "should provide a link to edit each role" do
14
+ render 'admin/roles/index'
15
+ response.should have_tag('a[href=?]',/\/admin\/roles\/\d+/,'Test')
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/../../../spec_helper'
2
+
3
+ describe 'show' do
4
+ let(:role){mock_model(Role,:role_name => 'Test', :description => 'The test role.', :allow_empty => true)}
5
+ before do
6
+ template.should_receive(:include_stylesheet).with('rbac/rbac')
7
+ template.stub!(:include_javascript)
8
+ assigns[:role] = role
9
+ end
10
+ it "should display the role name" do
11
+ render 'admin/roles/show'
12
+ response.should have_tag('h1',/Test/)
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: radiant-rbac_base-extension
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 3
9
+ - 0
10
+ version: 1.3.0
11
+ platform: ruby
12
+ authors:
13
+ - Jim Gay
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-05 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: radiant
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 25
30
+ segments:
31
+ - 0
32
+ - 9
33
+ version: "0.9"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description: Flexible user role management for Radiant.
37
+ email: jim@saturnflyer.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - README.markdown
44
+ files:
45
+ - .gitignore
46
+ - HELP_developer.md
47
+ - README.markdown
48
+ - Rakefile
49
+ - VERSION
50
+ - app/controllers/admin/roles_controller.rb
51
+ - app/helpers/admin/alterations_helper.rb
52
+ - app/helpers/admin/roles_helper.rb
53
+ - app/models/role.rb
54
+ - app/models/role_action_observer.rb
55
+ - app/models/role_user.rb
56
+ - app/views/admin/roles/_add_role_form.html.haml
57
+ - app/views/admin/roles/index.html.haml
58
+ - app/views/admin/roles/show.html.haml
59
+ - app/views/admin/users/preferences.html.haml
60
+ - config/locales/en.yml
61
+ - config/routes.rb
62
+ - cucumber.yml
63
+ - db/migrate/001_create_roles.rb
64
+ - db/migrate/002_create_role_users.rb
65
+ - db/migrate/003_setup_standard_roles.rb
66
+ - db/migrate/004_alter_roles.rb
67
+ - db/migrate/005_add_standard_role_details.rb
68
+ - db/migrate/006_add_user_info.rb
69
+ - db/migrate/20100705182511_rename_role_developer_to_designer.rb
70
+ - features/support/env.rb
71
+ - features/support/paths.rb
72
+ - lib/rbac_support.rb
73
+ - lib/tasks/rbac_base_extension_tasks.rake
74
+ - public/javascripts/rbac/admin/role_details.js
75
+ - public/stylesheets/rbac/rbac.css
76
+ - radiant-rbac_base-extension.gemspec
77
+ - rbac_base_extension.rb
78
+ - spec/controllers/admin/roles_controller_spec.rb
79
+ - spec/controllers/admin/roles_routing_spec.rb
80
+ - spec/helpers/admin/roles_helper_spec.rb
81
+ - spec/models/role_spec.rb
82
+ - spec/models/user_spec.rb
83
+ - spec/spec.opts
84
+ - spec/spec_helper.rb
85
+ - spec/views/admin/roles/index_spec.rb
86
+ - spec/views/admin/roles/show_spec.rb
87
+ has_rdoc: true
88
+ homepage: http://github.com/saturnflyer/radiant-rbac_base-extension
89
+ licenses: []
90
+
91
+ post_install_message:
92
+ rdoc_options:
93
+ - --charset=UTF-8
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ requirements: []
115
+
116
+ rubyforge_project:
117
+ rubygems_version: 1.3.7
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: RBAC Base Extension for Radiant CMS
121
+ test_files:
122
+ - spec/controllers/admin/roles_controller_spec.rb
123
+ - spec/controllers/admin/roles_routing_spec.rb
124
+ - spec/helpers/admin/roles_helper_spec.rb
125
+ - spec/models/role_spec.rb
126
+ - spec/models/user_spec.rb
127
+ - spec/spec_helper.rb
128
+ - spec/views/admin/roles/index_spec.rb
129
+ - spec/views/admin/roles/show_spec.rb