Skip to content

Commit

Permalink
fix permissions for consortium
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 24, 2020
1 parent fc4f332 commit a0b90ab
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 3 deletions.
30 changes: 29 additions & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,37 @@ def initialize(user)
can :export, :repositories
elsif user.role_id == "staff_user"
can :read, :all
elsif user.role_id == "provider_admin" && user.provider_id.present? && user.consortium_id.present?
can [:update, :read, :read_billing_information], Provider, symbol: user.provider_id.upcase
can [:manage], Provider do |provider|
provider.consortium_id == user.consortium_id.upcase
end
can [:read], Provider
can [:manage], ProviderPrefix do |provider_prefix|
provider_prefix.provider.consortium_id == user.consortium_id.upcase
end
can [:manage], Client do |client|
client.provider.consortium_id == user.consortium_id.upcase
end
can [:manage], ClientPrefix #, :client_id => user.provider_id

# if Flipper[:delete_doi].enabled?(user)
# can [:manage], Doi, :provider_id => user.provider_id
# else
# can [:read, :update], Doi, :provider_id => user.provider_id
# end

can [:read, :get_url, :transfer, :read_landing_page_results], Doi do |doi|
doi.provider.consortium_id == user.provider_id.upcase
end
can [:read], Doi
can [:read], User
can [:read], Phrase
can [:read], Activity do |activity|
activity.doi.findable? || activity.doi.provider.consortium_id == user.consortium_id.upcase
end
elsif user.role_id == "provider_admin" && user.provider_id.present?
can [:update, :read, :read_billing_information], Provider, symbol: user.provider_id.upcase
can [:manage], Provider, consortium_id: user.provider_id
can [:read], Provider
can [:manage], ProviderPrefix, provider_id: user.provider_id
can [:manage], Client, provider_id: user.provider_id
Expand Down
6 changes: 5 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class User
# include helper module for caching infrequently changing resources
include Cacheable

attr_accessor :name, :uid, :email, :role_id, :jwt, :password, :provider_id, :client_id, :beta_tester, :errors
attr_accessor :name, :uid, :email, :role_id, :jwt, :password, :consortium_id, :provider_id, :client_id, :beta_tester, :errors

def initialize(credentials, options={})
if credentials.present? && options.fetch(:type, "").downcase == "basic"
Expand Down Expand Up @@ -75,6 +75,10 @@ def is_beta_tester?
beta_tester
end

def consortium_id
provider_id if provider && provider.role_name == "ROLE_CONSORTIUM"
end

def provider
return nil unless provider_id.present?

Expand Down
35 changes: 34 additions & 1 deletion spec/models/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
describe User, type: :model do
let(:token) { User.generate_token }
let(:user) { User.new(token) }
let(:provider) { create(:provider) }
let(:consortium) { create(:provider, role_name: "ROLE_CONSORTIUM") }
let(:provider) { create(:provider, consortium: consortium, role_name: "ROLE_CONSORTIUM_ORGANIZATION") }
let(:client) { create(:client, provider: provider) }
let(:prefix) { create(:prefix, prefix: "10.14454") }
let!(:client_prefix) { create(:client_prefix, client: client, prefix: prefix) }
Expand Down Expand Up @@ -146,6 +147,38 @@
it{ is_expected.not_to be_able_to(:destroy, doi) }
end

context "when is a provider admin for a consortium" do
let(:token){ User.generate_token(role_id: "provider_admin", provider_id: consortium.symbol.downcase) }

it{ is_expected.to be_able_to(:read, user) }

it{ is_expected.to be_able_to(:read, provider) }
it{ is_expected.to be_able_to(:create, provider) }
it{ is_expected.to be_able_to(:update, provider) }
it{ is_expected.to be_able_to(:destroy, provider) }

it{ is_expected.to be_able_to(:read, client) }
it{ is_expected.to be_able_to(:create, client) }
it{ is_expected.to be_able_to(:update, client) }
it{ is_expected.to be_able_to(:destroy, client) }

it{ is_expected.not_to be_able_to(:read, prefix) }
it{ is_expected.not_to be_able_to(:create, prefix) }
it{ is_expected.not_to be_able_to(:update, prefix) }
it{ is_expected.not_to be_able_to(:destroy, prefix) }

it{ is_expected.to be_able_to(:read, provider_prefix) }
it{ is_expected.to be_able_to(:create, provider_prefix) }
it{ is_expected.to be_able_to(:update, provider_prefix) }
it{ is_expected.to be_able_to(:destroy, provider_prefix) }

it{ is_expected.to be_able_to(:read, doi) }
it{ is_expected.to be_able_to(:transfer, doi) }
it{ is_expected.not_to be_able_to(:create, doi) }
it{ is_expected.not_to be_able_to(:update, doi) }
it{ is_expected.not_to be_able_to(:destroy, doi) }
end

context "when is a provider user" do
let(:token){ User.generate_token(role_id: "provider_user", provider_id: provider.symbol.downcase) }

Expand Down
37 changes: 37 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
expect(user.role_id).to eq("staff_admin")
end

it "has no consortium_id" do
expect(user.consortium_id).to be_nil
end

it "has no provider_id" do
expect(user.provider_id).to be_nil
end
Expand All @@ -46,6 +50,35 @@
expect(user.role_id).to eq("provider_admin")
end

it "has no consortium_id" do
expect(user.consortium_id).to be_nil
end

it "has provider" do
expect(user.provider_id).to eq(provider.symbol.downcase)
expect(user.provider.name).to eq(provider.name)
end

it "has name" do
expect(user.name).to eq("My provider")
end
end
end

describe "from basic_auth consortium" do
let(:provider) { create(:provider, password_input: "12345", role_name: "ROLE_CONSORTIUM") }
let(:credentials) { provider.encode_auth_param(username: provider.symbol, password: 12345) }
let(:user) { User.new(credentials, type: "basic") }

describe 'User attributes' do
it "has role_id" do
expect(user.role_id).to eq("provider_admin")
end

it "has consortium_id" do
expect(user.consortium_id).to eq(provider.symbol.downcase)
end

it "has provider" do
expect(user.provider_id).to eq(provider.symbol.downcase)
expect(user.provider.name).to eq(provider.name)
Expand All @@ -67,6 +100,10 @@
expect(user.role_id).to eq("client_admin")
end

it "has no consortium_id" do
expect(user.consortium_id).to be_nil
end

it "has provider_id" do
expect(user.provider_id).to eq(client.symbol.downcase.split(".").first)
end
Expand Down

0 comments on commit a0b90ab

Please sign in to comment.