mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-09-10 15:14:33 +08:00
Fix visibility of save as draft button, fix permissions for new drafts [SCI-8145][SCI-8157]
This commit is contained in:
parent
7fff3e8f78
commit
cf599a8277
10 changed files with 40 additions and 94 deletions
|
@ -111,7 +111,7 @@ class ProtocolsController < ApplicationController
|
|||
title: I18n.t('protocols.index.linked_children.title',
|
||||
protocol: escape_input(@protocol.name)),
|
||||
html: render_to_string(partial: 'protocols/index/linked_children_modal_body.html.erb',
|
||||
locals: { protocol: @protocol })
|
||||
locals: { protocol: @protocol })
|
||||
}
|
||||
end
|
||||
end
|
||||
|
@ -1016,8 +1016,7 @@ class ProtocolsController < ApplicationController
|
|||
|
||||
def check_save_as_draft_permissions
|
||||
@protocol = Protocol.find_by(id: params[:id])
|
||||
render_403 unless @protocol.present? &&
|
||||
can_save_protocol_as_draft_in_repository?(@protocol)
|
||||
render_403 unless @protocol.present? && can_save_protocol_version_as_draft?(@protocol)
|
||||
end
|
||||
|
||||
def check_delete_draft_permissions
|
||||
|
|
|
@ -53,7 +53,7 @@ class LoadFromRepositoryProtocolsDatatable < CustomDatatable
|
|||
'1': record.version_number,
|
||||
'2': parent.code,
|
||||
'3': keywords_html(record),
|
||||
'4': escape_input(record.published_by.full_name),
|
||||
'4': escape_input(record.published_by&.full_name),
|
||||
'5': I18n.l(record.published_on, format: :full)
|
||||
}
|
||||
end
|
||||
|
|
|
@ -183,9 +183,9 @@ class ProtocolsDatatable < CustomDatatable
|
|||
'ON "protocol_protocol_keywords"."protocol_keyword_id" = "protocol_keywords"."id"')
|
||||
.joins('LEFT OUTER JOIN "users" "archived_users" ON "archived_users"."id" = "protocols"."archived_by_id"')
|
||||
.joins('LEFT OUTER JOIN "users" ON "users"."id" = "protocols"."published_by_id"')
|
||||
.joins('LEFT OUTER JOIN "user_assignments" "all_user_assignments" '\
|
||||
'ON "all_user_assignments"."assignable_type" = \'Protocol\' '\
|
||||
'AND "all_user_assignments"."assignable_id" = "protocols"."id"')
|
||||
.joins('LEFT OUTER JOIN "user_assignments" "all_user_assignments" ' \
|
||||
'ON "all_user_assignments"."assignable_type" = \'Protocol\' ' \
|
||||
'AND "all_user_assignments"."assignable_id" = "protocols"."id"')
|
||||
.group('"protocols"."id"')
|
||||
|
||||
records = filter_protocols_records(records)
|
||||
|
|
|
@ -18,8 +18,7 @@
|
|||
</a>
|
||||
<button class="btn btn-light" @click="openVersionsModal">{{ i18n.t("protocols.header.versions") }}</button>
|
||||
<button v-if="protocol.attributes.urls.publish_url" @click="$emit('publish')" class="btn btn-primary">{{ i18n.t("protocols.header.publish") }}</button>
|
||||
<button v-if="protocol.attributes.urls.save_as_draft_url" @click="saveAsdraft" class="btn btn-secondary">{{ i18n.t("protocols.header.save_as_draft") }}</button>
|
||||
<button v-bind:disabled="protocol.attributes.disabled_drafting" v-if="protocol.attributes.disabled_drafting" @click="saveAsdraft" class="btn btn-secondary">{{ i18n.t("protocols.header.save_as_draft") }}</button>
|
||||
<button v-if="protocol.attributes.urls.save_as_draft_url" v-bind:disabled="protocol.attributes.has_draft" @click="saveAsdraft" class="btn btn-secondary">{{ i18n.t("protocols.header.save_as_draft") }}</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="details-container" class="protocol-details collapse in">
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module UserAssignments
|
||||
class ProtocolGroupAssignmentJob < ApplicationJob
|
||||
queue_as :high_priority
|
||||
|
||||
def perform(team, protocol, assigned_by)
|
||||
@team = team
|
||||
@assigned_by = assigned_by
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
team.users.where.not(id: assigned_by).find_each do |user|
|
||||
user_assignment = UserAssignment.find_or_initialize_by(
|
||||
user: user,
|
||||
assignable: protocol
|
||||
)
|
||||
|
||||
next if user_assignment.manually_assigned?
|
||||
|
||||
user_assignment.update!(
|
||||
user_role: protocol.default_public_user_role || UserRole.find_predefined_viewer_role,
|
||||
assigned_by: @assigned_by
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -16,13 +16,10 @@ class Protocol < ApplicationRecord
|
|||
include PermissionCheckableModel
|
||||
include TinyMceImages
|
||||
|
||||
after_create :auto_assign_protocol_members, if: :visible?
|
||||
after_destroy :decrement_linked_children
|
||||
after_save :update_user_assignments, if: -> { saved_change_to_visibility? && in_repository? }
|
||||
after_save :update_linked_children
|
||||
skip_callback :create, :after, :create_users_assignments, if: -> { in_module? }
|
||||
after_create :update_automatic_user_assignments, if: -> { visible? && in_repository? && parent.blank? }
|
||||
before_update :change_visibility, if: :default_public_user_role_id_changed?
|
||||
after_update :sync_protocol_assignments, if: :visibility_changed?
|
||||
after_update :update_automatic_user_assignments, if: -> { saved_change_to_visibility? && in_repository? }
|
||||
skip_callback :create, :after, :create_users_assignments, if: -> { in_module? }
|
||||
|
||||
enum visibility: { hidden: 0, visible: 1 }
|
||||
enum protocol_type: {
|
||||
|
@ -238,13 +235,23 @@ class Protocol < ApplicationRecord
|
|||
in_module? ? my_module.created_by : added_by
|
||||
end
|
||||
|
||||
# Only for original published protocol
|
||||
def published_versions_with_original
|
||||
return Protocol.none unless in_repository_published_original?
|
||||
|
||||
team.protocols
|
||||
.in_repository_published_version
|
||||
.where(parent: self)
|
||||
.or(team.protocols.in_repository_published_original.where(id: id))
|
||||
end
|
||||
|
||||
# Only for original published protocol
|
||||
def all_linked_children
|
||||
return Protocol.none unless in_repository_published_original?
|
||||
|
||||
Protocol.linked.where(parent: published_versions_with_original)
|
||||
end
|
||||
|
||||
def initial_draft?
|
||||
in_repository_draft? && parent.blank?
|
||||
end
|
||||
|
@ -539,6 +546,7 @@ class Protocol < ApplicationRecord
|
|||
draft.version_comment = nil
|
||||
draft.previous_version = self
|
||||
draft.last_modified_by = current_user
|
||||
draft.skip_user_assignments = true
|
||||
|
||||
return draft if draft.invalid?
|
||||
|
||||
|
@ -546,6 +554,10 @@ class Protocol < ApplicationRecord
|
|||
draft = deep_clone(draft, current_user)
|
||||
end
|
||||
|
||||
parent_protocol.user_assignments.each do |parent_user_assignment|
|
||||
parent_protocol.sync_child_protocol_user_assignment(parent_user_assignment, draft.id)
|
||||
end
|
||||
|
||||
draft
|
||||
end
|
||||
|
||||
|
@ -671,15 +683,7 @@ class Protocol < ApplicationRecord
|
|||
sync_child_protocol_user_assignment(user_assignment)
|
||||
end
|
||||
|
||||
def auto_assign_protocol_members
|
||||
UserAssignments::ProtocolGroupAssignmentJob.perform_now(
|
||||
team,
|
||||
self,
|
||||
last_modified_by || created_by
|
||||
)
|
||||
end
|
||||
|
||||
def update_user_assignments
|
||||
def update_automatic_user_assignments
|
||||
case visibility
|
||||
when 'visible'
|
||||
create_public_user_assignments!(added_by)
|
||||
|
@ -706,25 +710,6 @@ class Protocol < ApplicationRecord
|
|||
clone
|
||||
end
|
||||
|
||||
def update_linked_children
|
||||
# Increment/decrement the parent's nr of linked children
|
||||
if saved_change_to_parent_id?
|
||||
unless parent_id_before_last_save.nil?
|
||||
p = Protocol.find_by_id(parent_id_before_last_save)
|
||||
p.record_timestamps = false
|
||||
p.decrement!(:nr_of_linked_children)
|
||||
end
|
||||
unless parent_id.nil?
|
||||
parent.record_timestamps = false
|
||||
parent.increment!(:nr_of_linked_children)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def decrement_linked_children
|
||||
parent.decrement!(:nr_of_linked_children) if parent.present?
|
||||
end
|
||||
|
||||
def prevent_update
|
||||
errors.add(:base, I18n.t('activerecord.errors.models.protocol.unchangable'))
|
||||
end
|
||||
|
@ -765,12 +750,4 @@ class Protocol < ApplicationRecord
|
|||
def change_visibility
|
||||
self.visibility = default_public_user_role_id.present? ? :visible : :hidden
|
||||
end
|
||||
|
||||
def sync_protocol_assignments
|
||||
if visible?
|
||||
auto_assign_protocol_members
|
||||
else
|
||||
user_assignments.where(assigned: :automatically).destroy_all
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -76,7 +76,7 @@ Canaid::Permissions.register_for(Protocol) do
|
|||
clone_protocol_in_repository
|
||||
publish_protocol_in_repository
|
||||
delete_protocol_draft_in_repository
|
||||
save_protocol_as_draft_in_repository)
|
||||
save_protocol_version_as_draft)
|
||||
.each do |perm|
|
||||
can perm do |_, protocol|
|
||||
protocol.active?
|
||||
|
@ -129,11 +129,10 @@ Canaid::Permissions.register_for(Protocol) do
|
|||
can_manage_protocol_draft_in_repository?(user, protocol)
|
||||
end
|
||||
|
||||
can :save_protocol_as_draft_in_repository do |user, protocol|
|
||||
next false unless can_create_protocols_in_repository?(user, protocol.team)
|
||||
can :save_protocol_version_as_draft do |user, protocol|
|
||||
next false unless protocol.in_repository_published?
|
||||
|
||||
%(in_repository_published_original in_repository_published_version).include?(protocol.protocol_type) &&
|
||||
(protocol.parent || protocol).draft.blank?
|
||||
protocol.permission_granted?(user, ProtocolPermissions::MANAGE_DRAFT)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ class ProtocolSerializer < ActiveModel::Serializer
|
|||
|
||||
attributes :name, :id, :urls, :description, :description_view, :updated_at, :in_repository,
|
||||
:created_at_formatted, :updated_at_formatted, :added_by, :authors, :keywords, :version, :code,
|
||||
:published, :version_comment, :archived, :linked, :disabled_drafting
|
||||
:published, :version_comment, :archived, :linked, :has_draft
|
||||
|
||||
def updated_at
|
||||
object.updated_at.to_i
|
||||
|
@ -85,12 +85,13 @@ class ProtocolSerializer < ActiveModel::Serializer
|
|||
!object.in_module?
|
||||
end
|
||||
|
||||
def disabled_drafting
|
||||
return false unless can_create_protocols_in_repository?(object.team)
|
||||
# rubocop:disable Naming/PredicateName
|
||||
def has_draft
|
||||
return false unless object.in_repository_published?
|
||||
|
||||
%(in_repository_published_original in_repository_published_version).include?(object.protocol_type) &&
|
||||
(object.parent || object).draft.present? && !object.archived
|
||||
(object.parent || object).draft.present?
|
||||
end
|
||||
# rubocop:enable Naming/PredicateName
|
||||
|
||||
def linked
|
||||
object.linked?
|
||||
|
@ -201,7 +202,7 @@ class ProtocolSerializer < ActiveModel::Serializer
|
|||
end
|
||||
|
||||
def save_as_draft_url
|
||||
return unless can_save_protocol_as_draft_in_repository?(object)
|
||||
return unless can_save_protocol_version_as_draft?(object)
|
||||
|
||||
save_as_draft_protocol_path(object)
|
||||
end
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<% linked_children = protocol.linked_children %>
|
||||
<% published_versions = protocol.published_versions_with_original.order(version_number: :desc)%>
|
||||
|
||||
<% if protocol.nr_of_linked_children > 0 %>
|
||||
<% if protocol.all_linked_children.any? %>
|
||||
<div class="version-dropdown">
|
||||
<%= label_tag "version-selector".to_sym, t("protocols.index.linked_children.show_version") %>
|
||||
<%= select_tag "version",
|
||||
|
|
|
@ -82,11 +82,11 @@
|
|||
published_by: protocol_version.published_by&.full_name) %>
|
||||
</div>
|
||||
<div class="protocol-actions">
|
||||
<% if @protocol.active? %>
|
||||
<% if can_save_protocol_version_as_draft?(@protocol) %>
|
||||
<%= button_to save_as_draft_protocol_path(protocol_version),
|
||||
class: "btn btn-light icon-btn save-as-draft",
|
||||
title: t('protocols.index.versions.save_as_draft'),
|
||||
disabled: !can_save_protocol_as_draft_in_repository?(protocol_version),
|
||||
disabled: @protocol.draft.present?,
|
||||
data: {
|
||||
placement: :left,
|
||||
toggle: :tooltip
|
||||
|
|
Loading…
Add table
Reference in a new issue