diff --git a/lib/sufia/files_controller_behavior.rb b/lib/sufia/files_controller_behavior.rb index 1567ad2ed5..e155485dbb 100644 --- a/lib/sufia/files_controller_behavior.rb +++ b/lib/sufia/files_controller_behavior.rb @@ -1,18 +1,3 @@ -# -*- coding: utf-8 -*- -# Copyright © 2012 The Pennsylvania State University -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - module Sufia module FilesController autoload :LocalIngestBehavior, 'sufia/files_controller/local_ingest_behavior' diff --git a/spec/models/generic_file_spec.rb b/spec/models/generic_file_spec.rb index b8a4c8d637..c667858b21 100644 --- a/spec/models/generic_file_spec.rb +++ b/spec/models/generic_file_spec.rb @@ -90,22 +90,38 @@ end end + describe "visibility" do + it "should not be changed when it's new" do + subject.should_not be_visibility_changed + end + it "should be changed when it has been changed" do + subject.visibility= 'open' + subject.should be_visibility_changed + end + + it "should not be changed when it's set to its previous value" do + subject.visibility= 'private' + subject.should_not be_visibility_changed + end + + end + describe "attributes" do it "should have rightsMetadata" do - @file.rightsMetadata.should be_instance_of ParanoidRightsDatastream + subject.rightsMetadata.should be_instance_of ParanoidRightsDatastream end it "should have properties datastream for depositor" do - @file.properties.should be_instance_of PropertiesDatastream + subject.properties.should be_instance_of PropertiesDatastream end it "should have apply_depositor_metadata" do - @file.rightsMetadata.edit_access.should == ['jcoyne'] - @file.depositor.should == 'jcoyne' + subject.rightsMetadata.edit_access.should == ['jcoyne'] + subject.depositor.should == 'jcoyne' end it "should have a set of permissions" do - @file.read_groups=['group1', 'group2'] - @file.edit_users=['user1'] - @file.read_users=['user2', 'user3'] - @file.permissions.should == [{:type=>"group", :access=>"read", :name=>"group1"}, + subject.read_groups=['group1', 'group2'] + subject.edit_users=['user1'] + subject.read_users=['user2', 'user3'] + subject.permissions.should == [{:type=>"group", :access=>"read", :name=>"group1"}, {:type=>"group", :access=>"read", :name=>"group2"}, {:type=>"user", :access=>"read", :name=>"user2"}, {:type=>"user", :access=>"read", :name=>"user3"}, @@ -113,51 +129,51 @@ end describe "updating permissions" do it "should create new group permissions" do - @file.permissions = {:new_group_name => {'group1'=>'read'}} - @file.permissions.should == [{:type=>"group", :access=>"read", :name=>"group1"}, + subject.permissions = {:new_group_name => {'group1'=>'read'}} + subject.permissions.should == [{:type=>"group", :access=>"read", :name=>"group1"}, {:type=>"user", :access=>"edit", :name=>"jcoyne"}] end it "should create new user permissions" do - @file.permissions = {:new_user_name => {'user1'=>'read'}} - @file.permissions.should == [{:type=>"user", :access=>"read", :name=>"user1"}, + subject.permissions = {:new_user_name => {'user1'=>'read'}} + subject.permissions.should == [{:type=>"user", :access=>"read", :name=>"user1"}, {:type=>"user", :access=>"edit", :name=>"jcoyne"}] end it "should not replace existing groups" do - @file.permissions = {:new_group_name=> {'group1' => 'read'}} - @file.permissions = {:new_group_name=> {'group2' => 'read'}} - @file.permissions.should == [{:type=>"group", :access=>"read", :name=>"group1"}, + subject.permissions = {:new_group_name=> {'group1' => 'read'}} + subject.permissions = {:new_group_name=> {'group2' => 'read'}} + subject.permissions.should == [{:type=>"group", :access=>"read", :name=>"group1"}, {:type=>"group", :access=>"read", :name=>"group2"}, {:type=>"user", :access=>"edit", :name=>"jcoyne"}] end it "should not replace existing users" do - @file.permissions = {:new_user_name=>{'user1'=>'read'}} - @file.permissions = {:new_user_name=>{'user2'=>'read'}} - @file.permissions.should == [{:type=>"user", :access=>"read", :name=>"user1"}, + subject.permissions = {:new_user_name=>{'user1'=>'read'}} + subject.permissions = {:new_user_name=>{'user2'=>'read'}} + subject.permissions.should == [{:type=>"user", :access=>"read", :name=>"user1"}, {:type=>"user", :access=>"read", :name=>"user2"}, {:type=>"user", :access=>"edit", :name=>"jcoyne"}] end it "should update permissions on existing users" do - @file.permissions = {:new_user_name=>{'user1'=>'read'}} - @file.permissions = {:user=>{'user1'=>'edit'}} - @file.permissions.should == [{:type=>"user", :access=>"edit", :name=>"user1"}, + subject.permissions = {:new_user_name=>{'user1'=>'read'}} + subject.permissions = {:user=>{'user1'=>'edit'}} + subject.permissions.should == [{:type=>"user", :access=>"edit", :name=>"user1"}, {:type=>"user", :access=>"edit", :name=>"jcoyne"}] end it "should update permissions on existing groups" do - @file.permissions = {:new_group_name=>{'group1'=>'read'}} - @file.permissions = {:group=>{'group1'=>'edit'}} - @file.permissions.should == [{:type=>"group", :access=>"edit", :name=>"group1"}, + subject.permissions = {:new_group_name=>{'group1'=>'read'}} + subject.permissions = {:group=>{'group1'=>'edit'}} + subject.permissions.should == [{:type=>"group", :access=>"edit", :name=>"group1"}, {:type=>"user", :access=>"edit", :name=>"jcoyne"}] end end it "should have a characterization datastream" do - @file.characterization.should be_kind_of FitsDatastream + subject.characterization.should be_kind_of FitsDatastream end it "should have a dc desc metadata" do - @file.descMetadata.should be_kind_of GenericFileRdfDatastream + subject.descMetadata.should be_kind_of GenericFileRdfDatastream end it "should have content datastream" do - @file.add_file(File.open(fixture_path + '/world.png'), 'content', 'world.png') - @file.content.should be_kind_of FileContentDatastream + subject.add_file(File.open(fixture_path + '/world.png'), 'content', 'world.png') + subject.content.should be_kind_of FileContentDatastream end end describe "delegations" do diff --git a/sufia-models/app/models/generic_file.rb b/sufia-models/app/models/generic_file.rb index 34c1d9606e..c147e34e52 100644 --- a/sufia-models/app/models/generic_file.rb +++ b/sufia-models/app/models/generic_file.rb @@ -1,16 +1,3 @@ -# Copyright © 2012 The Pennsylvania State University -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. class GenericFile < ActiveFedora::Base include Sufia::GenericFile end diff --git a/sufia-models/lib/sufia/models/engine.rb b/sufia-models/lib/sufia/models/engine.rb index 5f2ff724a4..5c0ee7d443 100644 --- a/sufia-models/lib/sufia/models/engine.rb +++ b/sufia-models/lib/sufia/models/engine.rb @@ -46,13 +46,11 @@ class Engine < ::Rails::Engine require 'sufia/models/noid' require 'sufia/models/file_content' require 'sufia/models/file_content/versions' - require 'sufia/models/generic_file/actions' require 'sufia/models/generic_file/audit' require 'sufia/models/generic_file/characterization' require 'sufia/models/generic_file/derivatives' require 'sufia/models/generic_file/export' require 'sufia/models/generic_file/mime_types' - require 'sufia/models/generic_file/permissions' require 'sufia/models/generic_file/thumbnail' require 'sufia/models/generic_file' require 'sufia/models/user' diff --git a/sufia-models/lib/sufia/models/generic_file.rb b/sufia-models/lib/sufia/models/generic_file.rb index 4a697692c5..2cc1a24ce2 100644 --- a/sufia-models/lib/sufia/models/generic_file.rb +++ b/sufia-models/lib/sufia/models/generic_file.rb @@ -2,8 +2,9 @@ module Sufia module GenericFile extend ActiveSupport::Concern extend ActiveSupport::Autoload - autoload :Actions - autoload :Permissions + autoload :Actions, 'sufia/models/generic_file/actions' + autoload :Permissions, 'sufia/models/generic_file/permissions' + autoload :Visibility, 'sufia/models/generic_file/visibility' autoload :WebForm, 'sufia/models/generic_file/web_form' autoload :AccessibleAttributes, 'sufia/models/generic_file/accessible_attributes' include Sufia::ModelMethods diff --git a/sufia-models/lib/sufia/models/generic_file/permissions.rb b/sufia-models/lib/sufia/models/generic_file/permissions.rb index b107c66737..a339688219 100644 --- a/sufia-models/lib/sufia/models/generic_file/permissions.rb +++ b/sufia-models/lib/sufia/models/generic_file/permissions.rb @@ -2,33 +2,15 @@ module Sufia module GenericFile module Permissions extend ActiveSupport::Concern - extend Deprecation #we're overriding the permissions= method which is in RightsMetadata include Hydra::ModelMixins::RightsMetadata + include Sufia::GenericFile::Visibility + included do has_metadata :name => "rightsMetadata", :type => ParanoidRightsDatastream validate :paranoid_permissions end - def visibility= (visibility) - # only set explicit permissions - case visibility - when "open" - self.datastreams["rightsMetadata"].permissions({:group=>"public"}, "read") - when "psu" - self.datastreams["rightsMetadata"].permissions({:group=>"registered"}, "read") - self.datastreams["rightsMetadata"].permissions({:group=>"public"}, "none") - when "restricted" - self.datastreams["rightsMetadata"].permissions({:group=>"registered"}, "none") - self.datastreams["rightsMetadata"].permissions({:group=>"public"}, "none") - end - end - - def set_visibility(visibility) - Deprecation.warn Permissions, "set_visibility is deprecated, use visibility= instead. set_visibility will be removed in sufia 3.0", caller - self.visibility= visibility - end - def paranoid_permissions # let the rightsMetadata ds make this determination # - the object instance is passed in for easier access to the props ds @@ -62,8 +44,6 @@ def permission_hash end {'person'=>user_perms, 'group'=>group_perms} end - - end end end diff --git a/sufia-models/lib/sufia/models/generic_file/visibility.rb b/sufia-models/lib/sufia/models/generic_file/visibility.rb new file mode 100644 index 0000000000..9175677ad5 --- /dev/null +++ b/sufia-models/lib/sufia/models/generic_file/visibility.rb @@ -0,0 +1,59 @@ +module Sufia + module GenericFile + module Visibility + extend ActiveSupport::Concern + extend Deprecation + include ActiveModel::Dirty + + included do + define_attribute_methods :visibility + end + + def visibility= (value) + # only set explicit permissions + case value + when "open" + public_visibility! + when "psu" + registered_visibility! + when "restricted" + private_visibility! + end + end + + def public_visibility! + visibility_will_change! unless visibility == 'public' + self.datastreams["rightsMetadata"].permissions({:group=>"public"}, "read") + end + + def registered_visibility! + visibility_will_change! unless visibility == 'registered' + self.datastreams["rightsMetadata"].permissions({:group=>"registered"}, "read") + self.datastreams["rightsMetadata"].permissions({:group=>"public"}, "none") + end + + def private_visibility! + visibility_will_change! unless visibility == 'private' + self.datastreams["rightsMetadata"].permissions({:group=>"registered"}, "none") + self.datastreams["rightsMetadata"].permissions({:group=>"public"}, "none") + end + + def visibility + if read_groups.include? 'public' + 'public' + elsif read_groups.include? 'registered' + 'registered' + else + 'private' + end + end + + def set_visibility(visibility) + Deprecation.warn Permissions, "set_visibility is deprecated, use visibility= instead. set_visibility will be removed in sufia 3.0", caller + self.visibility= visibility + end + end + end +end + + diff --git a/sufia-models/lib/sufia/models/jobs/batch_update_job.rb b/sufia-models/lib/sufia/models/jobs/batch_update_job.rb index 9a7fee1770..4d173b49bc 100644 --- a/sufia-models/lib/sufia/models/jobs/batch_update_job.rb +++ b/sufia-models/lib/sufia/models/jobs/batch_update_job.rb @@ -1,17 +1,3 @@ -# Copyright © 2012 The Pennsylvania State University -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - class BatchUpdateJob include Hydra::PermissionsQuery include Rails.application.routes.url_helpers