Skip to content

Commit

Permalink
Merge pull request #746 from datacite/issue-1260
Browse files Browse the repository at this point in the history
Issue #1260 - Fabrica is not showing assigned Roles to contac…
  • Loading branch information
svogt0511 authored Jul 16, 2021
2 parents d122d9f + 4b86e0a commit 5408ea1
Show file tree
Hide file tree
Showing 45 changed files with 1,827 additions and 2 deletions.
33 changes: 33 additions & 0 deletions app/controllers/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class ContactsController < ApplicationController

before_action :set_contact, only: %i[show update destroy]
before_action :authenticate_user!
after_action :set_provider_contacts, only: %i[create update]
after_action :remove_provider_contacts, only: %i[destroy]
load_and_authorize_resource

def index
Expand Down Expand Up @@ -181,6 +183,37 @@ def set_include
end

private
def set_provider_contacts
if @contact.valid?
Contact.roles.each do | role |
if @contact.has_role?(role)
@contact.set_provider_role(role, { 'email': @contact.email, 'given_name': @contact.given_name, 'family_name': @contact.family_name })
elsif @contact.has_provider_role?(role)
@contact.set_provider_role(role, nil)
end
end

# Make sure no other contact with this provider claims these roles.
@contact.provider.contacts.each do | contact |
if !@contact.is_me?(contact)
if contact.remove_roles!(@contact.role_name)
contact.update_attribute("role_name", contact.role_name)
end
end
end

end
end

def remove_provider_contacts
@contact.role_name.each do | role |
if @contact.has_provider_role?(role)
@contact.set_provider_role(role, nil)
end
end
@contact.update_attribute("role_name", [])
end

def safe_params
if params[:data].blank?
fail JSON::ParserError,
Expand Down
59 changes: 59 additions & 0 deletions app/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,65 @@ def self.import_from_providers
end
end

def self.roles
ROLES
end

def has_role?(role = "")
role_name.include?(role)
end

def remove_roles!(roles = [])
roles.each do | role |
role_name.delete(role)
end
self.changed?
end

def is_me?(contact = nil)
uid == contact.uid
end

def has_provider_role?(role)
case role
when "voting"
provider.voting_contact_email == email ? true : nil
when "billing"
provider.billing_contact_email == email ? true : nil
when "secondary_billing"
provider.secondary_billing_contact_email == email ? true : nil
when "service"
provider.service_contact_email == email ? true : nil
when "secondary_service"
provider.secondary_service_contact_email == email ? true : nil
when "technical"
provider.technical_contact_email == email ? true : nil
when "secondary_technical"
provider.secondary_technical_contact_email == email ? true : nil
else
nil
end
end

def set_provider_role(role, contact)
case role
when "voting"
provider.update_attribute("voting_contact", contact)
when "billing"
provider.update_attribute("billing_contact", contact)
when "secondary_billing"
provider.update_attribute("secondary_billing_contact", contact)
when "service"
provider.update_attribute("service_contact", contact)
when "secondary_service"
provider.update_attribute("secondary_service_contact", contact)
when "technical"
provider.update_attribute("technical_contact", contact)
when "secondary_technical"
provider.update_attribute("secondary_technical_contact", contact)
end
end

private
# uuid for public id
def set_uid
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5408ea1

Please sign in to comment.