summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/grant.rb
blob: 1f47f320a293058b0a46bd25ea7642b2245b64c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# frozen_string_literal: true

module Gitlab
  module Database
    # Model that can be used for querying permissions of a SQL user.
    class Grant < ActiveRecord::Base
      include FromUnion

      self.table_name = 'information_schema.role_table_grants'

      # Returns true if the current user can create and execute triggers on the
      # given table.
      def self.create_and_execute_trigger?(table)
        # We _must not_ use quote_table_name as this will produce double
        # quotes on PostgreSQL and for "has_table_privilege" we need single
        # quotes.
        quoted_table = connection.quote(table)

        begin
          from(nil)
            .pluck(Arel.sql("has_table_privilege(#{quoted_table}, 'TRIGGER')"))
            .first
        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
        end
      end
    end
  end
end