2023-12-05 03:59:16 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class UserAssignmentSerializer < ActiveModel::Serializer
|
|
|
|
include Canaid::Helpers::PermissionsHelper
|
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
|
2023-12-11 22:41:03 +08:00
|
|
|
attributes :id, :assigned, :assignable_type, :user, :user_role, :last_owner, :inherit_message
|
|
|
|
|
|
|
|
def assigned
|
|
|
|
parent_assignment(parent).assigned
|
|
|
|
end
|
|
|
|
|
|
|
|
def parent
|
|
|
|
object.assignable.permission_parent
|
|
|
|
end
|
2023-12-05 03:59:16 +08:00
|
|
|
|
|
|
|
def user
|
|
|
|
{
|
|
|
|
id: object.user.id,
|
|
|
|
name: object.user.name,
|
|
|
|
avatar_url: avatar_path(object, :icon_small)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_role
|
|
|
|
{
|
|
|
|
id: object.user_role.id,
|
|
|
|
name: object.user_role.name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_owner
|
2023-12-11 22:41:03 +08:00
|
|
|
parent_assignment(parent).last_with_permission?(ProjectPermissions::USERS_MANAGE, assigned: :manually)
|
|
|
|
end
|
|
|
|
|
|
|
|
def inherit_message
|
|
|
|
user_assignment_resource_role_name(object.user, object.assignable, inherit = '')
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def parent_assignment(parent)
|
|
|
|
return object if parent.blank?
|
|
|
|
|
|
|
|
case parent
|
|
|
|
when Project
|
|
|
|
parent.user_assignments.find_by(user: object.user)
|
|
|
|
when Experiment
|
2023-12-12 19:17:38 +08:00
|
|
|
parent_assignment(parent.permission_parent)
|
2023-12-11 22:41:03 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_assignment_resource_role_name(user, resource, inherit = '')
|
|
|
|
user_assignment = resource.user_assignments.find_by(user: user)
|
|
|
|
|
|
|
|
return '' if [Project, Protocol].include?(resource.class) && inherit.blank?
|
|
|
|
|
|
|
|
if user_assignment.automatically_assigned? && resource.permission_parent.present?
|
|
|
|
parent = resource.permission_parent
|
|
|
|
return user_assignment_resource_role_name(user, parent, '_inherit')
|
|
|
|
end
|
|
|
|
|
|
|
|
I18n.t("access_permissions.partials.#{resource.class.to_s.downcase}_tooltip#{inherit}")
|
2023-12-05 03:59:16 +08:00
|
|
|
end
|
|
|
|
end
|