hydra-role-management 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.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/Gemfile +4 -0
- data/README.md +9 -0
- data/Rakefile +56 -0
- data/app/controllers/concerns/hydra/role_management/roles_behavior.rb +30 -0
- data/app/controllers/concerns/hydra/role_management/user_roles_behavior.rb +30 -0
- data/app/controllers/roles_controller.rb +4 -0
- data/app/controllers/user_roles_controller.rb +5 -0
- data/app/models/concerns/hydra/role_management/user_roles.rb +22 -0
- data/app/models/role.rb +11 -0
- data/app/views/roles/index.html.erb +11 -0
- data/app/views/roles/new.html.erb +5 -0
- data/app/views/roles/show.html.erb +19 -0
- data/config/routes.rb +7 -0
- data/hydra-role-management.gemspec +24 -0
- data/lib/generators/roles/roles_generator.rb +71 -0
- data/lib/generators/roles/templates/migrations/user_roles.rb +18 -0
- data/lib/hydra-role-management.rb +3 -0
- data/lib/hydra/role_management.rb +11 -0
- data/lib/hydra/role_management/version.rb +5 -0
- data/spec/.gitignore +1 -0
- data/spec/controllers/roles_controller_spec.rb +66 -0
- data/spec/controllers/user_roles_controller_spec.rb +61 -0
- data/spec/lib/user_roles_spec.rb +26 -0
- data/spec/models/role_spec.rb +35 -0
- data/spec/routing/role_management_routes_spec.rb +25 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/support/Gemfile +28 -0
- data/spec/support/app/models/sample.rb +37 -0
- data/spec/support/app/models/solr_document.rb +5 -0
- data/spec/support/config/initializers/hydra_config.rb +28 -0
- data/spec/support/lib/generators/test_app_generator.rb +47 -0
- data/spec/support/lib/tasks/rspec.rake +9 -0
- metadata +160 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f4279ccff40e9cb0d97b6c2b32865dd860aef573
|
4
|
+
data.tar.gz: b9fe3485cbe2a9e361e6fbbb74590953d1d90470
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e27635121fed9566b0c356483fd85943cb6320aa43abd8d957ffd0f0a0953eedf1e23753c31b10da06d059a6688d7869c8d8b34c69fa7bb4eeb3e6055ddb3064
|
7
|
+
data.tar.gz: 5dff568b6a59753dce5cba52c98d146e0f5f2c722cba626088dbad7468de0aa7174b88b187e38208c9847613a3a0a4902a7f9782cdd6e6290a448322f12014d8
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gemfile.lock
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
An engine gem to provide a RDBMS backed list of roles and their associated user. This replaces the hydra default role mapper.
|
2
|
+
|
3
|
+
|
4
|
+
Installing:
|
5
|
+
|
6
|
+
* Add: ```gem 'hydra-role-management'``` to your Gemfile and then ```bundle install```
|
7
|
+
* ```rails generate roles```
|
8
|
+
* ```rake db:migrate```
|
9
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
|
5
|
+
ENV["RAILS_ROOT"] ||= 'spec/internal'
|
6
|
+
|
7
|
+
desc 'Default: run specs.'
|
8
|
+
task :default => :spec
|
9
|
+
|
10
|
+
|
11
|
+
task :spec => [:generate] do |t|
|
12
|
+
focused_spec = ENV['SPEC'] ? " SPEC=#{File.join(GEM_ROOT, ENV['SPEC'])}" : ''
|
13
|
+
within_test_app do
|
14
|
+
system "rake myspec#{focused_spec}"
|
15
|
+
abort "Error running hydra-role-management" unless $?.success?
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
desc "Create the test rails app"
|
22
|
+
task :generate do
|
23
|
+
unless File.exists?('spec/internal/Rakefile')
|
24
|
+
puts "Generating rails app"
|
25
|
+
`rails new spec/internal`
|
26
|
+
puts "Copying gemfile"
|
27
|
+
`cp spec/support/Gemfile spec/internal`
|
28
|
+
puts "Copying generator"
|
29
|
+
`cp -r spec/support/lib/generators spec/internal/lib`
|
30
|
+
|
31
|
+
within_test_app do
|
32
|
+
puts "Bundle install"
|
33
|
+
puts `bundle install`
|
34
|
+
puts "running generator"
|
35
|
+
puts `rails generate test_app`
|
36
|
+
|
37
|
+
puts "running migrations"
|
38
|
+
puts `rake db:migrate db:test:prepare`
|
39
|
+
end
|
40
|
+
end
|
41
|
+
puts "Running specs"
|
42
|
+
end
|
43
|
+
|
44
|
+
desc "Clean out the test rails app"
|
45
|
+
task :clean do
|
46
|
+
puts "Removing sample rails app"
|
47
|
+
`rm -rf spec/internal`
|
48
|
+
end
|
49
|
+
|
50
|
+
def within_test_app
|
51
|
+
FileUtils.cd('spec/internal')
|
52
|
+
Bundler.with_clean_env do
|
53
|
+
yield
|
54
|
+
end
|
55
|
+
FileUtils.cd('../..')
|
56
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Hydra
|
2
|
+
module RoleManagement
|
3
|
+
module RolesBehavior
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
load_and_authorize_resource
|
8
|
+
end
|
9
|
+
|
10
|
+
def index
|
11
|
+
end
|
12
|
+
|
13
|
+
def show
|
14
|
+
end
|
15
|
+
|
16
|
+
def new
|
17
|
+
end
|
18
|
+
|
19
|
+
def create
|
20
|
+
@role.name = params[:role][:name]
|
21
|
+
if (@role.save)
|
22
|
+
redirect_to role_management.roles_path, notice: 'Role was successfully created.'
|
23
|
+
else
|
24
|
+
render action: "new"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Hydra
|
2
|
+
module RoleManagement
|
3
|
+
module UserRolesBehavior
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
load_and_authorize_resource :role
|
8
|
+
end
|
9
|
+
|
10
|
+
def create
|
11
|
+
authorize! :add_user, @role
|
12
|
+
u = ::User.find_by_user_key(params[:user_key])
|
13
|
+
if u
|
14
|
+
u.roles << @role
|
15
|
+
u.save!
|
16
|
+
redirect_to role_management.role_path(@role)
|
17
|
+
else
|
18
|
+
redirect_to role_management.role_path(@role), :flash=> {:error=>"Unable to find the user #{params[:user_key]}"}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def destroy
|
23
|
+
authorize! :remove_user, @role
|
24
|
+
@role.users.delete(::User.find(params[:id]))
|
25
|
+
redirect_to role_management.role_path(@role)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Hydra
|
2
|
+
module RoleManagement
|
3
|
+
module UserRoles
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
included do
|
6
|
+
has_and_belongs_to_many :roles
|
7
|
+
end
|
8
|
+
|
9
|
+
def groups
|
10
|
+
g = roles.map(&:name)
|
11
|
+
g += ['registered'] unless new_record? || guest?
|
12
|
+
g
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def admin?
|
17
|
+
roles.where(name: 'admin').exists?
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/app/models/role.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
class Role < ActiveRecord::Base
|
2
|
+
has_and_belongs_to_many :users
|
3
|
+
|
4
|
+
attr_accessible :name
|
5
|
+
|
6
|
+
validates :name,
|
7
|
+
uniqueness: true,
|
8
|
+
format: { with: /\A[a-zA-Z0-9._-]+\z/,
|
9
|
+
:message => "Only letters, numbers, hyphens, underscores and periods are allowed"}
|
10
|
+
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<h2>Roles</h2>
|
2
|
+
<ul>
|
3
|
+
<% @roles.each do |role| %>
|
4
|
+
<li><%=link_to role.name, role_management.role_path(role) %></li>
|
5
|
+
<% end %>
|
6
|
+
</ul>
|
7
|
+
|
8
|
+
<% if can? :create, Role %>
|
9
|
+
<%= button_to "Create a new role", role_management.new_role_path, method: :get, class: 'btn btn-primary' %>
|
10
|
+
<% end %>
|
11
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
<h2>Role: <%= @role.name %></h2>
|
2
|
+
<h3>Accounts:</h3>
|
3
|
+
<ul>
|
4
|
+
<% @role.users.each do |user| %>
|
5
|
+
<li><%= user.user_key %>
|
6
|
+
<% if can? :remove_user, Role %>
|
7
|
+
<%= button_to "Remove User", role_management.role_user_path(@role, user), :method=>:delete, :class=>'btn btn-danger' %>
|
8
|
+
<% end %>
|
9
|
+
</li>
|
10
|
+
<% end %>
|
11
|
+
</ul>
|
12
|
+
<h3>Add a new account:</h3>
|
13
|
+
<%= bootstrap_form_tag role_management.role_users_path(@role) do %>
|
14
|
+
<%= bootstrap_text_field_tag 'user_key', '', :label=>'User' %>
|
15
|
+
<%= bootstrap_actions do %>
|
16
|
+
<%= bootstrap_submit_tag "Add" %>
|
17
|
+
<%= bootstrap_cancel_tag %>
|
18
|
+
<% end %>
|
19
|
+
<% end %>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/hydra/role_management/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Justin Coyne"]
|
6
|
+
gem.email = ["justin@curationexperts.com"]
|
7
|
+
gem.description = %q{Rails engine to do user roles in an RDBMS for hydra-head}
|
8
|
+
gem.summary = %q{Rails engine to do user roles in an RDBMS for hydra-head}
|
9
|
+
gem.homepage = "https://github.com/projecthydra/hydra-role-managment"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "hydra-role-management"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Hydra::RoleManagement::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'cancan'
|
19
|
+
gem.add_dependency 'bootstrap_forms'
|
20
|
+
|
21
|
+
gem.add_development_dependency 'rake'
|
22
|
+
gem.add_development_dependency 'rails'
|
23
|
+
gem.add_development_dependency 'rspec-rails'
|
24
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'rails/generators'
|
3
|
+
require 'rails/generators/migration'
|
4
|
+
|
5
|
+
class RolesGenerator < Rails::Generators::Base
|
6
|
+
include Rails::Generators::Migration
|
7
|
+
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
|
+
|
10
|
+
argument :model_name, :type => :string , :default => "user"
|
11
|
+
desc """
|
12
|
+
This generator makes the following changes to your application:
|
13
|
+
1. Creates several database migrations if they do not exist in /db/migrate
|
14
|
+
2. Adds user behavior to the user model
|
15
|
+
2. Adds routes
|
16
|
+
"""
|
17
|
+
|
18
|
+
# Implement the required interface for Rails::Generators::Migration.
|
19
|
+
# taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
|
20
|
+
def self.next_migration_number(path)
|
21
|
+
unless @prev_migration_nr
|
22
|
+
@prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
|
23
|
+
else
|
24
|
+
@prev_migration_nr += 1
|
25
|
+
end
|
26
|
+
@prev_migration_nr.to_s
|
27
|
+
end
|
28
|
+
|
29
|
+
# Setup the database migrations
|
30
|
+
def copy_migrations
|
31
|
+
# Can't get this any more DRY, because we need this order.
|
32
|
+
%w{user_roles.rb}.each do |f|
|
33
|
+
better_migration_template f
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Add behaviors to the user model
|
38
|
+
def inject_user_roles_behavior
|
39
|
+
file_path = "app/models/#{model_name.underscore}.rb"
|
40
|
+
if File.exists?(file_path)
|
41
|
+
code = "# Connects this user object to Role-management behaviors. " +
|
42
|
+
"\n include Hydra::RoleManagement::UserRoles\n"
|
43
|
+
inject_into_file file_path, code, { :after => /include Hydra::User/ }
|
44
|
+
else
|
45
|
+
puts " \e[31mFailure\e[0m hydra-role-management requires a user object. This generators assumes that the model is defined in the file #{file_path}, which does not exist. If you used a different name, please re-run the generator and provide that name as an argument. Such as \b rails -g roles client"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
# The engine routes have to come after the devise routes so that /users/sign_in will work
|
51
|
+
def inject_routes
|
52
|
+
routing_code = "mount Hydra::RoleManagement::Engine => '/'"
|
53
|
+
sentinel = /devise_for :users/
|
54
|
+
inject_into_file 'config/routes.rb', "\n #{routing_code}\n", { :after => sentinel, :verbose => false }
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def better_migration_template (file)
|
61
|
+
begin
|
62
|
+
migration_template "migrations/#{file}", "db/migrate/#{file}"
|
63
|
+
sleep 1 # ensure scripts have different time stamps
|
64
|
+
rescue
|
65
|
+
puts " \e[1m\e[34mMigrations\e[0m " + $!.message
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class UserRoles < ActiveRecord::Migration
|
2
|
+
def up
|
3
|
+
create_table :roles do |t|
|
4
|
+
t.string :name
|
5
|
+
end
|
6
|
+
create_table :roles_users, :id => false do |t|
|
7
|
+
t.references :role
|
8
|
+
t.references :user
|
9
|
+
end
|
10
|
+
add_index :roles_users, [:role_id, :user_id]
|
11
|
+
add_index :roles_users, [:user_id, :role_id]
|
12
|
+
end
|
13
|
+
|
14
|
+
def down
|
15
|
+
drop_table :roles_users
|
16
|
+
drop_table :roles
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Hydra
|
2
|
+
module RoleManagement
|
3
|
+
class Engine < ::Rails::Engine
|
4
|
+
engine_name 'role_management'
|
5
|
+
|
6
|
+
# Rails 4 should do this automatically:
|
7
|
+
config.paths.add "app/controllers/concerns", eager_load: true
|
8
|
+
config.paths.add "app/models/concerns", eager_load: true
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/spec/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
/internal
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RolesController do
|
4
|
+
let(:ability) do
|
5
|
+
ability = Object.new
|
6
|
+
ability.extend(CanCan::Ability)
|
7
|
+
controller.stub(:current_ability).and_return(ability)
|
8
|
+
ability
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:role) do
|
12
|
+
Role.create(name: 'foo')
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
describe "with a user who cannot edit users" do
|
17
|
+
it "should redirect to the homepage" do
|
18
|
+
lambda { get :index }.should raise_error CanCan::AccessDenied
|
19
|
+
end
|
20
|
+
it "should redirect to the homepage" do
|
21
|
+
lambda { get :show, id: role }.should raise_error CanCan::AccessDenied
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "with a user who can edit users" do
|
26
|
+
before do
|
27
|
+
ability.can :read, Role
|
28
|
+
end
|
29
|
+
it "should be able to see the list of roles" do
|
30
|
+
get :index
|
31
|
+
response.should be_successful
|
32
|
+
assigns[:roles].should == [role]
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be able to see a single role" do
|
36
|
+
get :show, id: role
|
37
|
+
response.should be_successful
|
38
|
+
assigns[:role].should == role
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "with a user who can create roles" do
|
43
|
+
before do
|
44
|
+
ability.can :create, Role
|
45
|
+
end
|
46
|
+
it "should be able to make a new role" do
|
47
|
+
get :new
|
48
|
+
response.should be_successful
|
49
|
+
assigns[:role].should be_kind_of Role
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should be able to create a new role" do
|
53
|
+
post :create, :role=>{name: 'my_role'}
|
54
|
+
response.should redirect_to @routes.url_helpers.roles_path
|
55
|
+
assigns[:role].should_not be_new_record
|
56
|
+
assigns[:role].name.should == 'my_role'
|
57
|
+
end
|
58
|
+
it "should not create role with an error" do
|
59
|
+
post :create, :role=>{name: 'my role'}
|
60
|
+
assigns[:role].name.should == 'my role'
|
61
|
+
assigns[:role].errors[:name].should == ['Only letters, numbers, hyphens, underscores and periods are allowed']
|
62
|
+
response.should be_successful
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe UserRolesController do
|
4
|
+
let(:ability) do
|
5
|
+
ability = Object.new
|
6
|
+
ability.extend(CanCan::Ability)
|
7
|
+
controller.stub(:current_ability).and_return(ability)
|
8
|
+
ability
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:role) do
|
12
|
+
Role.create(name: 'foo')
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "with a user who cannot edit users" do
|
16
|
+
it "should not be able to add a user" do
|
17
|
+
lambda { post :create, role_id: role, user_key: 'foo@example.com'}.should raise_error CanCan::AccessDenied
|
18
|
+
end
|
19
|
+
it "should not be able to remove a user" do
|
20
|
+
lambda { delete :destroy, role_id: role, id: 7}.should raise_error CanCan::AccessDenied
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "with a user who can edit users" do
|
25
|
+
before do
|
26
|
+
ability.can :read, Role
|
27
|
+
end
|
28
|
+
describe "adding users" do
|
29
|
+
before do
|
30
|
+
ability.can :add_user, Role
|
31
|
+
end
|
32
|
+
it "should not be able to add a user that doesn't exist" do
|
33
|
+
User.should_receive(:find_by_user_key).with('foo@example.com').and_return(nil)
|
34
|
+
post :create, role_id: role, user_key: 'foo@example.com'
|
35
|
+
flash[:error].should == "Unable to find the user foo@example.com"
|
36
|
+
end
|
37
|
+
it "should be able to add a user" do
|
38
|
+
u = User.create!(email: 'foo@example.com', password: 'password', password_confirmation: 'password')
|
39
|
+
post :create, role_id: role, user_key: 'foo@example.com'
|
40
|
+
role.reload.users.should == [u]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
describe "removing users" do
|
44
|
+
before do
|
45
|
+
ability.can :remove_user, Role
|
46
|
+
end
|
47
|
+
let (:user) do
|
48
|
+
u = User.new(email: 'foo@example.com', password: 'password', password_confirmation: 'password')
|
49
|
+
u.roles = [role]
|
50
|
+
u.save!
|
51
|
+
u
|
52
|
+
end
|
53
|
+
it "should be able to remove a user" do
|
54
|
+
user.roles.should == [role]
|
55
|
+
delete :destroy, role_id: role, id: user.id
|
56
|
+
role.reload.users.should == []
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hydra::RoleManagement::UserRoles do
|
4
|
+
subject do
|
5
|
+
User.create!(email: 'fred@example.com', password: 'password')
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should have admin?" do
|
9
|
+
subject.should_not be_admin
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should have roles" do
|
13
|
+
subject.roles.should == []
|
14
|
+
subject.roles << Role.create!(name: 'librarian')
|
15
|
+
subject.roles.first.name.should == 'librarian'
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should have groups" do
|
20
|
+
subject.roles.should == []
|
21
|
+
subject.roles << Role.create!(name: 'librarian')
|
22
|
+
subject.save!
|
23
|
+
subject.groups.should include('registered', 'librarian')
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Role do
|
4
|
+
it "should require a name" do
|
5
|
+
subject.should_not be_valid
|
6
|
+
subject.name = 'foo'
|
7
|
+
subject.should be_valid
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should not allow space in the name" do
|
11
|
+
subject.name = 'foo bar'
|
12
|
+
subject.should_not be_valid
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should not allow comma in the name" do
|
16
|
+
subject.name = 'foo,bar'
|
17
|
+
subject.should_not be_valid
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should not allow ampersand in the name" do
|
21
|
+
subject.name = 'foo&bar'
|
22
|
+
subject.should_not be_valid
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should not allow less-than in the name" do
|
26
|
+
subject.name = 'foo<bar'
|
27
|
+
subject.should_not be_valid
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should validate uniqueness" do
|
31
|
+
subject.name ='foo'
|
32
|
+
subject.save!
|
33
|
+
Role.new(name: 'foo').should_not be_valid
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Routes for role_management" do
|
4
|
+
before(:each) do
|
5
|
+
@routes = Hydra::RoleManagement::Engine.routes
|
6
|
+
# so we have to do this instead:
|
7
|
+
# engine routes broke in rspec rails 2.12.1, so we had to add this:
|
8
|
+
assertion_instance.instance_variable_set(:@routes, @routes)
|
9
|
+
end
|
10
|
+
it "should route index" do
|
11
|
+
{ :get => '/roles' }.should route_to( :controller => "roles", :action => "index")
|
12
|
+
end
|
13
|
+
it "should create roles" do
|
14
|
+
{ :post => '/roles' }.should route_to( :controller => "roles", :action => "create")
|
15
|
+
end
|
16
|
+
it "should show roles" do
|
17
|
+
{ :get => '/roles/7' }.should route_to( :controller => "roles", :action => "show", :id => '7')
|
18
|
+
end
|
19
|
+
it "should add users" do
|
20
|
+
{ :post => '/roles/7/users' }.should route_to( :controller => "user_roles", :role_id=>'7', :action => "create")
|
21
|
+
end
|
22
|
+
it "should remove users" do
|
23
|
+
{ :delete => '/roles/7/users/5' }.should route_to( :controller => "user_roles", :role_id=>'7', :id=>'5', :action => "destroy")
|
24
|
+
end
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
ENV["RAILS_ENV"] ||= 'test'
|
4
|
+
|
5
|
+
require File.expand_path("config/environment", ENV['RAILS_ROOT'] || File.expand_path("../internal", __FILE__))
|
6
|
+
require 'rspec/rails'
|
7
|
+
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.use_transactional_fixtures = true
|
11
|
+
config.before(:each, :type=>"controller") { @routes = Hydra::RoleManagement::Engine.routes }
|
12
|
+
config.include Devise::TestHelpers, :type => :controller
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'rails'
|
4
|
+
|
5
|
+
# Bundle edge Rails instead:
|
6
|
+
# gem 'rails', :git => 'git://github.com/rails/rails.git'
|
7
|
+
|
8
|
+
gem 'sqlite3'
|
9
|
+
gem 'blacklight'
|
10
|
+
gem 'hydra-head'
|
11
|
+
|
12
|
+
|
13
|
+
# Gems used only for assets and not required
|
14
|
+
# in production environments by default.
|
15
|
+
group :assets do
|
16
|
+
gem 'sass-rails', '~> 3.2.3'
|
17
|
+
gem 'coffee-rails', '~> 3.2.1'
|
18
|
+
|
19
|
+
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
|
20
|
+
#gem 'therubyracer', :platforms => :ruby
|
21
|
+
|
22
|
+
gem 'uglifier', '>= 1.0.3'
|
23
|
+
end
|
24
|
+
|
25
|
+
gem 'jquery-rails'
|
26
|
+
|
27
|
+
gem 'hydra-role-management', :path=>'../../'
|
28
|
+
gem 'rspec-rails'
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class Sample
|
2
|
+
# This is a stub model for testing.
|
3
|
+
|
4
|
+
cattr_accessor :objects
|
5
|
+
self.objects = {}
|
6
|
+
|
7
|
+
def self.create(params={})
|
8
|
+
obj = Sample.new
|
9
|
+
obj.save
|
10
|
+
obj
|
11
|
+
end
|
12
|
+
|
13
|
+
def save()
|
14
|
+
@pid ||= "sample:#{(rand * 1000).to_i}"
|
15
|
+
self.class.objects[@pid] = self
|
16
|
+
end
|
17
|
+
|
18
|
+
def update_attributes(attributes)
|
19
|
+
attributes.each do |k, v|
|
20
|
+
instance_variable_set "@#{k.to_s}".to_sym, v
|
21
|
+
|
22
|
+
self.class.send :attr_accessor, k
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.find(pid)
|
27
|
+
objects[pid]
|
28
|
+
end
|
29
|
+
|
30
|
+
def pid
|
31
|
+
@pid
|
32
|
+
end
|
33
|
+
|
34
|
+
def destroy
|
35
|
+
self.class.objects.delete(@pid)
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# The following lines determine which user attributes your hydrangea app will use
|
2
|
+
# This configuration allows you to use the out of the box ActiveRecord associations between users and user_attributes
|
3
|
+
# It also allows you to specify your own user attributes
|
4
|
+
# The easiest way to override these methods would be to create your own module to include in User
|
5
|
+
# For example you could create a module for your local LDAP instance called MyLocalLDAPUserAttributes:
|
6
|
+
# User.send(:include, MyLocalLDAPAttributes)
|
7
|
+
# As long as your module includes methods for full_name, affiliation, and photo the personalization_helper should function correctly
|
8
|
+
#
|
9
|
+
|
10
|
+
# windows doesn't properly require hydra-head (from the gemfile), so we need to require it explicitly here:
|
11
|
+
require 'hydra/head' unless defined? Hydra
|
12
|
+
|
13
|
+
if Hydra.respond_to?(:configure)
|
14
|
+
Hydra.configure(:shared) do |config|
|
15
|
+
# This specifies the solr field names of permissions-related fields.
|
16
|
+
# You only need to change these values if you've indexed permissions by some means other than the Hydra's built-in tooling.
|
17
|
+
# If you change these, you must also update the permissions request handler in your solrconfig.xml to return those values
|
18
|
+
indexer = Solrizer::Descriptor.new(:string, :stored, :indexed, :multivalued)
|
19
|
+
config[:permissions] = {
|
20
|
+
:discover => {:group =>ActiveFedora::SolrService.solr_name("discover_access_group", indexer), :individual=>ActiveFedora::SolrService.solr_name("discover_access_person", indexer)},
|
21
|
+
:read => {:group =>ActiveFedora::SolrService.solr_name("read_access_group", indexer), :individual=>ActiveFedora::SolrService.solr_name("read_access_person", indexer)},
|
22
|
+
:edit => {:group =>ActiveFedora::SolrService.solr_name("edit_access_group", indexer), :individual=>ActiveFedora::SolrService.solr_name("edit_access_person", indexer)},
|
23
|
+
:owner => ActiveFedora::SolrService.solr_name("depositor", indexer),
|
24
|
+
:embargo_release_date => ActiveFedora::SolrService.solr_name("embargo_release_date", Solrizer::Descriptor.new(:date, :stored, :indexed))
|
25
|
+
}
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
class TestAppGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../../../../support", __FILE__)
|
5
|
+
|
6
|
+
# # Inject call to Hydra::BatchEdit.add_routes in config/routes.rb
|
7
|
+
# def inject_routes
|
8
|
+
# insert_into_file "config/routes.rb", :after => '.draw do' do
|
9
|
+
# "\n # Add BatchEdit routes."
|
10
|
+
# "\n Hydra::BatchEdit.add_routes(self)"
|
11
|
+
# end
|
12
|
+
# end
|
13
|
+
|
14
|
+
|
15
|
+
def run_blacklight_generator
|
16
|
+
say_status("warning", "GENERATING BL", :yellow)
|
17
|
+
|
18
|
+
generate 'blacklight', '--devise'
|
19
|
+
end
|
20
|
+
|
21
|
+
def run_hydra_head_generator
|
22
|
+
say_status("warning", "GENERATING HH", :yellow)
|
23
|
+
|
24
|
+
generate 'hydra:head', '-f'
|
25
|
+
end
|
26
|
+
|
27
|
+
def run_roles_generator
|
28
|
+
say_status("warning", "GENERATING ROLES", :yellow)
|
29
|
+
|
30
|
+
generate 'roles', '-f'
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
# def copy_test_models
|
35
|
+
# copy_file "app/models/sample.rb"
|
36
|
+
# copy_file "app/models/solr_document.rb"
|
37
|
+
# copy_file "db/migrate/20111101221803_create_searches.rb"
|
38
|
+
# end
|
39
|
+
|
40
|
+
def copy_rspec_rake_task
|
41
|
+
copy_file "lib/tasks/rspec.rake"
|
42
|
+
end
|
43
|
+
|
44
|
+
def copy_hydra_config
|
45
|
+
copy_file "config/initializers/hydra_config.rb"
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'rspec/core/rake_task'
|
2
|
+
desc "run the hydra-batch-edit gem spec"
|
3
|
+
gem_home = File.expand_path('../../../../..', __FILE__)
|
4
|
+
RSpec::Core::RakeTask.new(:myspec) do |t|
|
5
|
+
t.pattern = gem_home + '/spec/**/*_spec.rb'
|
6
|
+
t.rspec_opts = "--colour"
|
7
|
+
t.ruby_opts = "-I#{gem_home}/spec"
|
8
|
+
end
|
9
|
+
|
metadata
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hydra-role-management
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Justin Coyne
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-04-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: cancan
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bootstrap_forms
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Rails engine to do user roles in an RDBMS for hydra-head
|
84
|
+
email:
|
85
|
+
- justin@curationexperts.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- Gemfile
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- app/controllers/concerns/hydra/role_management/roles_behavior.rb
|
95
|
+
- app/controllers/concerns/hydra/role_management/user_roles_behavior.rb
|
96
|
+
- app/controllers/roles_controller.rb
|
97
|
+
- app/controllers/user_roles_controller.rb
|
98
|
+
- app/models/concerns/hydra/role_management/user_roles.rb
|
99
|
+
- app/models/role.rb
|
100
|
+
- app/views/roles/index.html.erb
|
101
|
+
- app/views/roles/new.html.erb
|
102
|
+
- app/views/roles/show.html.erb
|
103
|
+
- config/routes.rb
|
104
|
+
- hydra-role-management.gemspec
|
105
|
+
- lib/generators/roles/roles_generator.rb
|
106
|
+
- lib/generators/roles/templates/migrations/user_roles.rb
|
107
|
+
- lib/hydra-role-management.rb
|
108
|
+
- lib/hydra/role_management.rb
|
109
|
+
- lib/hydra/role_management/version.rb
|
110
|
+
- spec/.gitignore
|
111
|
+
- spec/controllers/roles_controller_spec.rb
|
112
|
+
- spec/controllers/user_roles_controller_spec.rb
|
113
|
+
- spec/lib/user_roles_spec.rb
|
114
|
+
- spec/models/role_spec.rb
|
115
|
+
- spec/routing/role_management_routes_spec.rb
|
116
|
+
- spec/spec_helper.rb
|
117
|
+
- spec/support/Gemfile
|
118
|
+
- spec/support/app/models/sample.rb
|
119
|
+
- spec/support/app/models/solr_document.rb
|
120
|
+
- spec/support/config/initializers/hydra_config.rb
|
121
|
+
- spec/support/lib/generators/test_app_generator.rb
|
122
|
+
- spec/support/lib/tasks/rspec.rake
|
123
|
+
homepage: https://github.com/projecthydra/hydra-role-managment
|
124
|
+
licenses: []
|
125
|
+
metadata: {}
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - '>='
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 2.0.0
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: Rails engine to do user roles in an RDBMS for hydra-head
|
146
|
+
test_files:
|
147
|
+
- spec/.gitignore
|
148
|
+
- spec/controllers/roles_controller_spec.rb
|
149
|
+
- spec/controllers/user_roles_controller_spec.rb
|
150
|
+
- spec/lib/user_roles_spec.rb
|
151
|
+
- spec/models/role_spec.rb
|
152
|
+
- spec/routing/role_management_routes_spec.rb
|
153
|
+
- spec/spec_helper.rb
|
154
|
+
- spec/support/Gemfile
|
155
|
+
- spec/support/app/models/sample.rb
|
156
|
+
- spec/support/app/models/solr_document.rb
|
157
|
+
- spec/support/config/initializers/hydra_config.rb
|
158
|
+
- spec/support/lib/generators/test_app_generator.rb
|
159
|
+
- spec/support/lib/tasks/rspec.rake
|
160
|
+
has_rdoc:
|