1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class Log < ArvadosModel
10 include CommonApiTemplate
11 serialize :properties, Hash
12 before_validation :set_default_event_at
13 after_save :send_notify
14 after_commit { AuditLogs.tidy_in_background }
16 api_accessible :user, extend: :common do |t|
19 t.add :object_owner_uuid
28 if k = ArvadosModel::resource_class_for_uuid(object_uuid)
33 def fill_object(thing)
34 self.object_uuid ||= thing.uuid
35 if respond_to? :object_owner_uuid=
36 # Skip this if the object_owner_uuid migration hasn't happened
37 # yet, i.e., we're in the process of migrating an old database.
38 self.object_owner_uuid = thing.owner_uuid
40 self.summary ||= "#{self.event_type} of #{thing.uuid}"
44 def fill_properties(age, etag_prop, attrs_prop)
45 self.properties.merge!({"#{age}_etag" => etag_prop,
46 "#{age}_attributes" => attrs_prop})
50 fill_properties('new', thing.andand.etag, thing.andand.logged_attributes)
53 self.event_at = thing.created_at
55 self.event_at = thing.modified_at
57 self.event_at = db_current_time
62 def self.readable_by(*users_list)
63 if users_list.last.is_a? Hash
66 if users_list.select { |u| u.is_admin }.any?
69 user_uuids = users_list.map { |u| u.uuid }
71 joins("LEFT JOIN container_requests ON container_requests.container_uuid=logs.object_uuid").
72 where("EXISTS(SELECT target_uuid FROM #{PERMISSION_VIEW} "+
73 "WHERE user_uuid IN (:user_uuids) AND perm_level >= 1 AND "+
74 "target_uuid IN (container_requests.uuid, container_requests.owner_uuid, logs.object_uuid, logs.owner_uuid, logs.object_owner_uuid))",
75 user_uuids: user_uuids)
80 def permission_to_create
84 def permission_to_update
85 current_user.andand.is_admin
88 alias_method :permission_to_delete, :permission_to_update
90 def set_default_event_at
91 self.event_at ||= db_current_time
95 # don't log start state on logs
98 def log_change(event_type)
99 # Don't log changes to logs.
102 def ensure_valid_uuids
103 # logs can have references to deleted objects
107 ActiveRecord::Base.connection.execute "NOTIFY logs, '#{self.id}'"