10216: Added use_existing field to Container Request. When false and CR is committed...
[arvados.git] / services / api / app / models / log.rb
1 class Log < ArvadosModel
2   include HasUuid
3   include KindAndEtag
4   include CommonApiTemplate
5   serialize :properties, Hash
6   before_validation :set_default_event_at
7   attr_accessor :object, :object_kind
8   after_save :send_notify
9
10   api_accessible :user, extend: :common do |t|
11     t.add :id
12     t.add :object_uuid
13     t.add :object_owner_uuid
14     t.add :object_kind
15     t.add :event_at
16     t.add :event_type
17     t.add :summary
18     t.add :properties
19   end
20
21   def object_kind
22     if k = ArvadosModel::resource_class_for_uuid(object_uuid)
23       k.kind
24     end
25   end
26
27   def fill_object(thing)
28     self.object_uuid ||= thing.uuid
29     if respond_to? :object_owner_uuid=
30       # Skip this if the object_owner_uuid migration hasn't happened
31       # yet, i.e., we're in the process of migrating an old database.
32       self.object_owner_uuid = thing.owner_uuid
33     end
34     self.summary ||= "#{self.event_type} of #{thing.uuid}"
35     self
36   end
37
38   def fill_properties(age, etag_prop, attrs_prop)
39     self.properties.merge!({"#{age}_etag" => etag_prop,
40                              "#{age}_attributes" => attrs_prop})
41   end
42
43   def update_to(thing)
44     fill_properties('new', thing.andand.etag, thing.andand.logged_attributes)
45     case event_type
46     when "create"
47       self.event_at = thing.created_at
48     when "update"
49       self.event_at = thing.modified_at
50     when "destroy"
51       self.event_at = db_current_time
52     end
53     self
54   end
55
56   def self.readable_by(*users_list)
57     if users_list.select { |u| u.is_admin }.any?
58       return self
59     end
60     user_uuids = users_list.map { |u| u.uuid }
61     uuid_list = user_uuids + users_list.flat_map { |u| u.groups_i_can(:read) }
62     uuid_list.uniq!
63     permitted = "(SELECT head_uuid FROM links WHERE link_class='permission' AND tail_uuid IN (:uuids))"
64     joins("LEFT JOIN container_requests ON container_requests.container_uuid=logs.object_uuid").
65       where("logs.object_uuid IN #{permitted} OR "+
66             "container_requests.uuid IN (:uuids) OR "+
67             "container_requests.owner_uuid IN (:uuids) OR "+
68             "logs.object_uuid IN (:uuids) OR "+
69             "logs.owner_uuid IN (:uuids) OR "+
70             "logs.object_owner_uuid IN (:uuids)",
71             uuids: uuid_list)
72   end
73
74   protected
75
76   def permission_to_create
77     true
78   end
79
80   def permission_to_update
81     current_user.andand.is_admin
82   end
83
84   alias_method :permission_to_delete, :permission_to_update
85
86   def set_default_event_at
87     self.event_at ||= db_current_time
88   end
89
90   def log_start_state
91     # don't log start state on logs
92   end
93
94   def log_change(event_type)
95     # Don't log changes to logs.
96   end
97
98   def ensure_valid_uuids
99     # logs can have references to deleted objects
100   end
101
102   def send_notify
103     connection.execute "NOTIFY logs, '#{self.id}'"
104   end
105
106 end