Skip to content

Commit

Permalink
Rough surgical backport of samvera/hyrax#477
Browse files Browse the repository at this point in the history
Fixes #3098
  • Loading branch information
mjgiarlo committed Mar 2, 2017
1 parent 1ea6baa commit 51baf02
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Expand Up @@ -81,6 +81,7 @@ RSpec/ExampleLength:
- 'spec/models/generic_work_spec.rb'
- 'spec/models/featured_work_spec.rb'
- 'spec/services/sufia/actor_factory_spec.rb'
- 'spec/services/sufia/admin_set_create_service_spec.rb'
- 'spec/services/sufia/user_stat_importer_spec.rb'
- 'spec/views/catalog/_index_list_default.html.erb_spec.rb'
- 'spec/views/collections/_form.html.erb_spec.rb'
Expand Down
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -161,6 +161,16 @@ rake hydra:server

And now you should be able to browse to [localhost:3000](http://localhost:3000/) and see the application. Note that this web server is purely for development purposes; you will want to use a more fully featured [web server](#web-server) for production-like environments.

## Add Default Admin Set

After Fedora and Solr are running, create the default administrative set by running the following rake task:

```
rake sufia:default_admin_set:create
```

You will want to run this command the first time this code is deployed to a new environment as well.

# Managing a Sufia-based app

The [Sufia Management Guide](https://github.com/projecthydra/sufia/wiki/Sufia-Management-Guide) provides tips for how to manage, customize, and enhance your Sufia application, including guidance specific to:
Expand Down
31 changes: 29 additions & 2 deletions app/services/sufia/admin_set_create_service.rb
@@ -1,6 +1,21 @@
module Sufia
# Creates AdminSets
class AdminSetCreateService
DEFAULT_ID = 'admin_sets/default'.freeze

def self.create_default!
return if AdminSet.exists?(DEFAULT_ID)
admin_set = AdminSet.new(id: DEFAULT_ID, title: ['Default Admin Set'])
begin
new(admin_set, nil).create
rescue ActiveFedora::IllegalOperation
# It is possible that another thread created the AdminSet just before this method
# was called, so ActiveFedora will raise IllegalOperation. In this case we can safely
# ignore the error.
Rails.logger.error("AdminSet ID=#{DEFAULT_ID} may or may not have been created due to threading issues.")
end
end

# @param admin_set [AdminSet] the admin set to operate on
# @param creating_user [User] the user who created the admin set.
def initialize(admin_set, creating_user)
Expand All @@ -15,8 +30,20 @@ def initialize(admin_set, creating_user)
def create
admin_set.read_groups = ['public']
admin_set.edit_groups = ['admin']
admin_set.creator = [creating_user.user_key]
admin_set.save
admin_set.creator = [creating_user.user_key] if creating_user
admin_set.save.tap do |result|
create_permission_template if result
end
end

def access_grants_attributes
return [] unless creating_user
[{ agent_type: 'user', agent_id: creating_user.user_key, access: 'manage' }]
end

def create_permission_template
PermissionTemplate.create!(admin_set_id: admin_set.id,
access_grants_attributes: access_grants_attributes)
end
end
end
8 changes: 8 additions & 0 deletions lib/tasks/default_admin_set.rake
@@ -0,0 +1,8 @@
namespace :sufia do
namespace :default_admin_set do
desc "Create the default Admin Set"
task create: :environment do
Sufia::AdminSetCreateService.create_default!
end
end
end
19 changes: 18 additions & 1 deletion spec/services/sufia/admin_set_create_service_spec.rb
Expand Up @@ -9,12 +9,16 @@
subject { service.create }

context "when the admin_set is valid" do
it "is successful" do
let(:permission_template) { Sufia::PermissionTemplate.find_by(admin_set_id: admin_set.id) }
let(:grant) { permission_template.access_grants.first }
it "creates an AdminSet, PermissionTemplate, and sets access" do
expect do
expect(subject).to be true
end.to change { admin_set.persisted? }.from(false).to(true)
expect(admin_set.read_groups).to eq ['public']
expect(admin_set.edit_groups).to eq ['admin']
expect(grant.agent_id).to eq user.user_key
expect(grant.access).to eq 'manage'
expect(admin_set.creator).to eq [user.user_key]
end
end
Expand All @@ -24,4 +28,17 @@
it { is_expected.to be false }
end
end

describe '.create_default!' do
let(:default_admin_set_id) { 'admin_sets/default' }
let(:permission_template) { Sufia::PermissionTemplate.find_by!(admin_set_id: default_admin_set_id) }
# It is important to test the side-effects as a default admin set is a fundamental assumption for Sufia >= 7.3
it 'creates AdminSet, PermissionTemplate' do
expect(AdminSet).not_to exist(default_admin_set_id)
described_class.create_default!
admin_set = AdminSet.find(default_admin_set_id)
expect(admin_set).to be_persisted
expect(permission_template).to be_persisted
end
end
end

0 comments on commit 51baf02

Please sign in to comment.