scinote-web/app/models/concerns/assignable.rb
2023-02-22 15:37:39 +01:00

82 lines
2.4 KiB
Ruby

# frozen_string_literal: true
module Assignable
extend ActiveSupport::Concern
included do
include Canaid::Helpers::PermissionsHelper
attr_accessor :skip_user_assignments
has_many :user_assignments, as: :assignable, dependent: :destroy
has_many :automatic_user_assignments, -> { automatically_assigned },
as: :assignable,
class_name: 'UserAssignment',
inverse_of: :assignable
scope :readable_by_user, lambda { |user|
joins(user_assignments: :user_role)
.where(user_assignments: { user: user })
.where('? = ANY(user_roles.permissions)', "::#{self.class.to_s.split('::').first}Permissions".constantize::READ)
}
scope :managable_by_user, lambda { |user|
joins(user_assignments: :user_role)
.where(user_assignments: { user: user })
.where('? = ANY(user_roles.permissions)', "::#{self.class.to_s.split('::').first}Permissions".constantize::MANAGE)
}
scope :with_user_permission, lambda { |user, permission|
joins(user_assignments: :user_role)
.where(user_assignments: { user: user })
.where('? = ANY(user_roles.permissions)', permission)
}
after_create :create_users_assignments
def role_for_user(user)
user_assignments.find_by(user: user)&.user_role
end
def manually_assigned_users
User.joins(:user_assignments).where(user_assignments: { assigned: :manually, assignable: self })
end
def assigned_users
User.joins(:user_assignments).where(user_assignments: { assignable: self })
end
def deep_clone_user_assginments(object)
user_assignments.each do |original_assignment|
new_assignment = original_assignment.dup
new_assignment.assignable = object
new_assignment.save!
end
end
def top_level_assignable?
self.class.name.in?(Extends::TOP_LEVEL_ASSIGNABLES)
end
private
def create_users_assignments
return if skip_user_assignments
role = if top_level_assignable?
UserRole.find_predefined_owner_role
else
permission_parent.user_assignments.find_by(user: created_by).user_role
end
UserAssignment.create!(
user: created_by,
assignable: self,
assigned: top_level_assignable? ? :manually : :automatically,
user_role: role
)
UserAssignments::GenerateUserAssignmentsJob.perform_later(self, created_by)
end
end
end