Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account editing cleanup #1983

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions app/assets/stylesheets/scss/_admin-edit.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,51 @@
.o-div__placeholder {
width: 270px;
}

#role_form_flex {
display: flex;
flex-wrap: wrap;
column-gap: 2ch;
row-gap: 1ch;
align-items: flex-start;

button {
order: 2;
flex-basis: 100%;
flex-shrink: 0;
}
fieldset {
order: 1;
margin: 1rem 0;
flex-basis: 48%;
flex-shrink: 0;
flex-grow: 1;

p, div {
margin: 0 auto 1rem;
}

div > div {
display: inline-block;
}

& > ul {
margin: 0;
padding: 0;
list-style-type: none;

li {
padding-left: 2ch;
text-indent: -2ch;
margin-bottom: .3rem;

span {
font-size: .9em;
margin-left: .5ch;
padding-left: .75ch;
border-left: 1px solid black;
}
}
}
}
}
17 changes: 5 additions & 12 deletions app/controllers/stash_engine/journal_admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ class JournalAdminController < ApplicationController
helper SortableTableHelper
before_action :require_user_login
before_action :setup_paging, only: :index
before_action :load, only: %i[popup edit]

def index
setup_sponsors
Expand All @@ -26,14 +25,13 @@ def index
@journals = @journals.page(@page).per(@page_size)
end

def popup
strings = { issn: 'ISSN(s)', payment_plan_type: 'payment plan type', notify_contacts: 'publication contacts',
review_contacts: 'PPR contacts', default_to_ppr: 'PPR by default', sponsor_id: 'journal sponsor', title: 'title' }
@desc = strings[@field.to_sym]
def edit
@journal = authorize StashEngine::Journal.find(params[:id])
respond_to(&:js)
end

def edit
def update
@journal = authorize StashEngine::Journal.find(params[:id])
@journal.update(update_hash)
update_issns if edit_params.key?(:issn)
respond_to(&:js)
Expand Down Expand Up @@ -67,11 +65,6 @@ def setup_sponsors
@sponsors.flatten!
end

def load
@journal = authorize StashEngine::Journal.find(params[:id]), :popup?
@field = params[:field]
end

def update_hash
valid = %i[title default_to_ppr payment_plan_type sponsor_id]
update = edit_params.slice(*valid)
Expand All @@ -93,7 +86,7 @@ def update_issns
end

def edit_params
params.permit(:id, :field, :title, :issn, :payment_plan_type, :notify_contacts, :review_contacts, :default_to_ppr, :sponsor_id)
params.permit(:id, :title, :issn, :payment_plan_type, :notify_contacts, :review_contacts, :default_to_ppr, :sponsor_id)
end

end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ class JournalOrganizationAdminController < ApplicationController
helper SortableTableHelper
before_action :require_user_login
before_action :setup_paging, only: :index
before_action :load, only: %i[popup edit]

def index
setup_sponsors
Expand All @@ -25,13 +24,13 @@ def index
@orgs = @orgs.page(@page).per(@page_size)
end

def popup
strings = { name: 'name', contact: 'contacts', parent_org_id: 'parent organization' }
@desc = strings[@field.to_sym]
def edit
@org = authorize StashEngine::JournalOrganization.find(params[:id])
respond_to(&:js)
end

def edit
def update
@org = authorize StashEngine::JournalOrganization.find(params[:id])
@org.update(update_hash)
respond_to(&:js)
end
Expand Down Expand Up @@ -71,13 +70,8 @@ def update_hash
update
end

def load
@org = authorize StashEngine::JournalOrganization.find(params[:id]), :popup?
@field = params[:field]
end

def edit_params
params.permit(:id, :field, :name, :contact, :parent_org_id)
params.permit(:id, :name, :contact, :parent_org_id)
end

end
Expand Down
55 changes: 30 additions & 25 deletions app/controllers/stash_engine/tenant_admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,39 @@ class TenantAdminController < ApplicationController
helper SortableTableHelper
before_action :require_user_login
before_action :setup_paging, only: :index
before_action :load, only: %i[popup edit]

def index
authorize %i[stash_engine tenant], :admin?
setup_sponsors
setup_consortia

@tenants = StashEngine::Tenant.includes(%i[logo tenant_ror_orgs ror_orgs])
@tenants = StashEngine::Tenant

if params[:q]
q = params[:q]
# search the query in any searchable field
@tenants = @tenants.where('LOWER(short_name) LIKE LOWER(?) OR LOWER(long_name) LIKE LOWER(?) OR LOWER(id) LIKE LOWER(?)',
@tenants = @tenants.where('LOWER(short_name) LIKE LOWER(?) OR LOWER(long_name) LIKE LOWER(?) OR LOWER(stash_engine_tenants.id) LIKE LOWER(?)',
"%#{q}%", "%#{q}%", "%#{q}%")
end

ord = helpers.sortable_table_order(whitelist: %w[id short_name long_name authentication covers_dpc partner_display enabled])
@tenants = @tenants.order(ord)

@tenants = @tenants.where('id = ? or sponsor_id= ?', params[:sponsor], params[:sponsor]) if params[:sponsor].present?
if params[:consortium].present?
rors = StashEngine::Tenant.find(params[:consortium]).ror_ids
@tenants = @tenants.joins(:tenant_ror_orgs).where(tenant_ror_orgs: { ror_id: rors }).distinct
end

# paginate for display
@tenants = @tenants.page(@page).per(@page_size)
@tenants = @tenants.includes(%i[logo ror_orgs]).page(@page).per(@page_size)
end

def popup
strings = { campus_contacts: 'contacts', partner_display: 'member display', ror_orgs: 'ROR organizations', enabled: 'active membership',
covers_dpc: 'payment', short_name: 'member name', long_name: 'full member name',
sponsor_id: 'sponsor', logo: 'logo', authentication: 'authentication strategy' }
@desc = strings[@field.to_sym]
def edit
@tenant = authorize StashEngine::Tenant.find(params[:id])
respond_to(&:js)
end

def edit
def update
@tenant = authorize StashEngine::Tenant.find(params[:id])
@tenant.update(update_hash)
update_associations
respond_to(&:js)
Expand Down Expand Up @@ -65,15 +65,18 @@ def setup_paging
end
end

def setup_sponsors
@sponsors = [OpenStruct.new(id: '', name: '')]
@sponsors << StashEngine::Tenant.sponsored.order(:short_name).map { |t| OpenStruct.new(id: t.id, name: t.short_name) }
@sponsors.flatten!
end

def load
@tenant = authorize StashEngine::Tenant.find(params[:id]), :popup?
@field = params[:field]
def setup_consortia
@consortia = [OpenStruct.new(id: '', name: '')]
rors = StashEngine::TenantRorOrg.select(:ror_id).group(:ror_id).having('count(ror_id) > 1')
tenants = StashEngine::Tenant.includes([:tenant_ror_orgs]).joins(:tenant_ror_orgs).where(tenant_ror_orgs: { ror_id: rors })
.distinct.order(:short_name)
tenants.each do |t|
if t.tenant_ror_orgs.length > 1 &&
StashEngine::Tenant.joins(:tenant_ror_orgs).where(tenant_ror_orgs: { ror_id: t.ror_ids }).distinct.length > 2
@consortia << OpenStruct.new(id: t.id, name: t.short_name)
end
end
@consortia.flatten!
end

def update_hash
Expand All @@ -94,23 +97,25 @@ def update_hash
end

def update_associations
if edit_params.key?(:logo)
if edit_params.key?(:logo) && edit_params[:logo] != @tenant.logo&.data
@tenant.logo = StashEngine::Logo.new unless @tenant.logo.present?
@tenant.logo.data = edit_params[:logo]
@tenant.logo.save
end

if edit_params.key?(:ror_orgs)
@tenant.tenant_ror_orgs.destroy_all
orgs = edit_params[:ror_orgs].split("\n")
orgs.each { |o| StashEngine::TenantRorOrg.create(ror_id: o.strip, tenant_id: @tenant.id) }
if @tenant.ror_ids.difference(orgs).any?
@tenant.tenant_ror_orgs.destroy_all
orgs.each { |o| StashEngine::TenantRorOrg.create(ror_id: o.strip, tenant_id: @tenant.id) }
end
end

@tenant.reload
end

def edit_params
params.permit(:id, :field, :short_name, :long_name, :logo, :campus_contacts, :covers_dpc, :partner_display, :enabled, :ror_orgs, :sponsor_id,
params.permit(:id, :short_name, :long_name, :logo, :campus_contacts, :covers_dpc, :partner_display, :enabled, :ror_orgs, :sponsor_id,
authentication: %i[strategy ranges entity_id entity_domain])
end

Expand Down
69 changes: 27 additions & 42 deletions app/controllers/stash_engine/user_admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ module StashEngine
class UserAdminController < ApplicationController
helper SortableTableHelper
before_action :require_user_login
before_action :load, only: %i[popup edit set_role user_profile]
before_action :setup_roles, only: %i[set_role user_profile]
before_action :setup_paging, only: %i[index user_profile]

def index
Expand Down Expand Up @@ -39,20 +37,20 @@ def index
@users = @users.page(@page).per(@page_size)
end

def popup
authorize %i[stash_engine user]
strings = { email: 'email', tenant_id: 'member institution' }
@desc = strings[@field.to_sym]
def edit
@user = authorize User.find(params[:id])
setup_roles
respond_to(&:js)
end

def edit
authorize %i[stash_engine user]
def update
@user = authorize User.find(params[:id])
valid = %i[email tenant_id]
check_tenant_role
@user.roles.tenant_roles.delete_all if edit_params[:tenant_id] != @user.tenant_id
setup_roles
update = edit_params.slice(*valid)
@user.update(update)

set_roles
respond_to(&:js)
end

Expand All @@ -78,32 +76,16 @@ def merge
respond_to(&:js)
end

# sets the user roles
def set_role
# set system role
save_role(role_params[:role], @system_role)
# set tenant role
save_role(role_params[:tenant_role], @tenant_role, @user.tenant)
# set publisher role
save_role(role_params[:publisher_role], @publisher_role, StashEngine::JournalOrganization.find_by(id: role_params[:publisher]))
# set journal role
save_role(role_params[:journal_role], @journal_role, StashEngine::Journal.find_by(id: role_params.dig(:journal, :value)))
# set funder role
save_role(role_params[:funder_role], @funder_role, StashEngine::Funder.find_by(id: role_params[:funder]))
# reload roles
setup_roles
respond_to(&:js)
end

# profile for a user showing stats and datasets
def user_profile
@user = User.find(params[:id])
@user = authorize User.find(params[:id])
@progress_count = @user.resources.in_progress.distinct.count
# some of these columns are calculated values for display that aren't stored (publication date)
@resources = @user.resources.latest_per_dataset.distinct.joins(:last_curation_activity)
.select('stash_engine_resources.*, stash_engine_curation_activities.status')
ord = helpers.sortable_table_order(whitelist: %w[title status publication_date total_file_size updated_at current_editor_id])
add_profile_filters
@resources = @resources.includes(%i[identifier current_resource_state last_curation_activity editor])
@resources = @resources.order(ord).page(@page).per(@page_size)
end

Expand All @@ -118,11 +100,6 @@ def setup_paging
end
end

def load
@user = User.find(params[:id])
@field = params[:field]
end

def setup_roles
@system_role = @user.roles.system_roles&.first
@tenant_role = @user.roles.tenant_roles&.first
Expand All @@ -131,6 +108,21 @@ def setup_roles
@funder_role = @user.roles.funder_roles&.first
end

# sets the user roles
def set_roles
# set system role
save_role(role_params[:role], @system_role)
# set tenant role
save_role(role_params[:tenant_role], @tenant_role, @user.tenant)
# set publisher role
save_role(role_params[:publisher_role], @publisher_role, StashEngine::JournalOrganization.find_by(id: role_params[:publisher]))
# set journal role
save_role(role_params[:journal_role], @journal_role, StashEngine::Journal.find_by(id: role_params.dig(:journal, :value)))
# set funder role
save_role(role_params[:funder_role], @funder_role, StashEngine::Funder.find_by(id: role_params[:funder]))
# reload roles
end

def save_role(role, existing, object = nil)
if role.blank?
existing.delete if existing
Expand All @@ -141,13 +133,6 @@ def save_role(role, existing, object = nil)
end
end

def check_tenant_role
return unless edit_params[:tenant_id] != @user.tenant_id

@user.roles.tenant_roles.delete_all
setup_roles
end

def setup_facets
@tenant_facets = StashEngine::Tenant.enabled.sort_by(&:short_name)
end
Expand All @@ -161,8 +146,8 @@ def setup_tenants
end

def add_filters
@users = @users.joins(:roles).where(roles: { role: params[:role] }).distinct if params[:role].present?
@users = @users.where(tenant_id: params[:tenant_id]) if params[:tenant_id].present?
@users = @users.joins(:roles).where(roles: { role: params[:role_filter] }).distinct if params[:role_filter].present?
@users = @users.where(tenant_id: params[:tenant_filter]) if params[:tenant_filter].present?
end

def add_profile_filters
Expand Down
2 changes: 1 addition & 1 deletion app/models/stash_engine/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Tenant < ApplicationRecord
belongs_to :logo, class_name: 'StashEngine::Logo', dependent: :destroy, optional: true
belongs_to :sponsor, class_name: 'Tenant', inverse_of: :sponsored, optional: true
has_many :sponsored, class_name: 'Tenant', primary_key: :id, foreign_key: :sponsor_id, inverse_of: :sponsor
has_many :tenant_ror_orgs, class_name: 'StashEngine::TenantRorOrg', dependent: :destroy
has_many :tenant_ror_orgs, -> { order(:created_at) }, class_name: 'StashEngine::TenantRorOrg', dependent: :destroy
has_many :ror_orgs, class_name: 'StashEngine::RorOrg', through: :tenant_ror_orgs
has_many :roles, class_name: 'StashEngine::Role', as: :role_object, dependent: :destroy
has_many :users, through: :roles
Expand Down
Loading