debian-mirror-gitlab/lib/gitlab/database/grant.rb

28 lines
979 B
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module Gitlab
module Database
# Model that can be used for querying permissions of a SQL user.
2020-03-09 13:42:32 +05:30
class Grant
2018-03-17 18:26:18 +05:30
# Returns true if the current user can create and execute triggers on the
# given table.
def self.create_and_execute_trigger?(table)
2019-10-12 21:52:04 +05:30
# We _must not_ use quote_table_name as this will produce double
# quotes on PostgreSQL and for "has_table_privilege" we need single
# quotes.
2020-03-09 13:42:32 +05:30
connection = ActiveRecord::Base.connection
2019-10-12 21:52:04 +05:30
quoted_table = connection.quote(table)
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
begin
2020-03-09 13:42:32 +05:30
connection.select_one("SELECT has_table_privilege(#{quoted_table}, 'TRIGGER')").present?
2019-10-12 21:52:04 +05:30
rescue ActiveRecord::StatementInvalid
# This error is raised when using a non-existing table name. In this
# case we just want to return false as a user technically can't
# create triggers for such a table.
false
2018-03-17 18:26:18 +05:30
end
end
end
end
end