ubicloud/model/object_tag.rb
Jeremy Evans 215f09541a Make access_tag only for project <-> accounts join table
For the includers of HyperTagMethods, this changes the authorization
code and object_tag member validation code to look at the project_id
column for the object, instead of looking a row with the project and
object in the access_tag table.

This removes all calls to associate_with_project, other than
those for Account.  It removes the projects association for
the includers of HyperTagMethods, and adds a project association to
the models that didn't already have one, since there is only a single
project for each object now.

Most HyperTagMethods code is inlined into Account, since it is only
user of the code now.  Temporarily, other models will still include
HyperTagMethods for the before_destroy hook, but eventually it will
go away completely.

The associations in Projects that previous used access_tag as a join
table, are changed from many_to_many to one_to_many, except for
Account (which still uses the join table).

Project#has_resources now needs separate queries for all of the
resource classes to see if there any associated objects.

This causes a lot of fallout in the specs, but unfortunately that is
unavoidable due the extensive use of projects.first in the specs to
get the related project for the objects, as well as the extensive
use of associate_with_project.
2025-01-17 08:32:46 -08:00

69 lines
2.1 KiB
Ruby

# frozen_string_literal: true
require_relative "../model"
class ObjectTag < Sequel::Model
include ResourceMethods
include AccessControlModelTag
module Cleanup
def before_destroy
AccessControlEntry.where(object_id: id).destroy
DB[:applied_object_tag].where(object_id: id).delete
super
end
end
def self.options_for_project(project)
{
{"label" => "Tag (grants access to objects contained in tag)", "id" => "object-tag-group"} => project.object_tags,
"Project" => [project],
"Vm" => project.vms,
"PostgresSQL Server" => project.postgres_resources,
"Private Subnet" => project.private_subnets,
"Firewall" => project.firewalls,
"LoadBalancer" => project.load_balancers,
"InferenceApiKey" => project.api_keys,
"InferenceEndpoint" => project.inference_endpoints,
"SubjectTag" => project.subject_tags,
"ActionTag" => project.action_tags,
{"label" => "ObjectTag (grants access to tag itself)", "id" => "object-metatag-group"} => project.object_tags.map(&:metatag)
}
end
def self.valid_member?(project_id, object)
case object
when ObjectTag, ObjectMetatag, SubjectTag, ActionTag, InferenceEndpoint, Vm, PrivateSubnet, PostgresResource, Firewall, LoadBalancer
object.project_id == project_id
when Project
object.id == project_id
when ApiKey
object.owner_table == "project" && object.owner_id == project_id
end
end
def metatag
ObjectMetatag.new(self)
end
def metatag_ubid
ObjectMetatag.to_meta(ubid)
end
def metatag_uuid
UBID.to_uuid(metatag_ubid)
end
end
# Table: object_tag
# Columns:
# id | uuid | PRIMARY KEY
# project_id | uuid | NOT NULL
# name | text | NOT NULL
# Indexes:
# object_tag_pkey | PRIMARY KEY btree (id)
# object_tag_project_id_name_index | UNIQUE btree (project_id, name)
# Foreign key constraints:
# object_tag_project_id_fkey | (project_id) REFERENCES project(id)
# Referenced By:
# applied_object_tag | applied_object_tag_tag_id_fkey | (tag_id) REFERENCES object_tag(id)