2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-18 08:26:23 -04:00
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
# Model that can be used for querying permissions of a SQL user.
|
2020-01-31 13:09:11 -05:00
|
|
|
class Grant
|
2017-08-18 08:26:23 -04:00
|
|
|
# Returns true if the current user can create and execute triggers on the
|
|
|
|
# given table.
|
|
|
|
def self.create_and_execute_trigger?(table)
|
2019-07-24 09:59:55 -04:00
|
|
|
# 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-01-31 13:09:11 -05:00
|
|
|
connection = ActiveRecord::Base.connection
|
2019-07-24 09:59:55 -04:00
|
|
|
quoted_table = connection.quote(table)
|
2017-11-06 12:50:38 -05:00
|
|
|
|
2019-07-24 09:59:55 -04:00
|
|
|
begin
|
2020-01-31 13:09:11 -05:00
|
|
|
connection.select_one("SELECT has_table_privilege(#{quoted_table}, 'TRIGGER')").present?
|
2019-07-24 09:59:55 -04:00
|
|
|
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-01-22 07:43:18 -05:00
|
|
|
end
|
2017-08-18 08:26:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|