Skip to content

Commit 709237f

Browse files
committed
Revert "CV2-6604: Remove global keys permission (#2379)"
This reverts commit 43fe1ce.
1 parent 0e0e59a commit 709237f

32 files changed

+173
-105
lines changed

app/controllers/api/v1/admin_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def slack_user
66
user = User.find_with_omniauth(params[:uid].to_s, 'slack')
77
slack_account = user.accounts.where(provider: 'slack').first unless user.nil?
88
user = { token: slack_account.token } unless slack_account.nil?
9+
user = nil unless @key.bot_user.nil? # Allow global API keys only
910
render_user user, 'slack_uid'
1011
end
1112

app/controllers/api/v1/base_api_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def render_user(user = nil, source = nil)
8686
def authenticate_from_token!
8787
header = CheckConfig.get('authorization_header', 'X-Token')
8888
token = request.headers[header]
89-
@key = ApiKey.where(access_token: token).where('expire_at > ?', Time.now).first
89+
@key = ApiKey.where(access_token: token).where('expire_at > ?', Time.now).last
9090
(render_unauthorized and return false) if @key.nil?
9191
end
9292

@@ -102,7 +102,7 @@ def authenticate_user
102102
def identify_user(mandatory)
103103
header = CheckConfig.get('authorization_header', 'X-Token')
104104
token = request.headers[header].to_s
105-
key = ApiKey.where(access_token: token).where('expire_at > ?', Time.now).first
105+
key = ApiKey.where(access_token: token).where('expire_at > ?', Time.now).last
106106
if key.nil?
107107
ApiKey.current = nil
108108
user = User.find_with_token(token)

app/controllers/test_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ def install_bot
2828
team = Team.where(slug: params[:slug]).last
2929
login = params[:bot]
3030
settings = begin JSON.parse(params[:settings]) rescue params[:settings].to_h end
31-
bot = BotUser.find_by_login(login) || BotUser.create!(team_author_id: team.id, login: login, name: login.capitalize, settings: { approved: true })
32-
team_user = bot.team_users.first
31+
bot = BotUser.find_by_login(login) || BotUser.create!(login: login, name: login.capitalize, settings: { approved: true })
32+
team_user = bot.install_to!(team)
33+
team_user = TeamUser.find(team_user.id)
3334
team_user.settings = team_user.settings.merge(settings)
3435
team_user.save!
3536
render_success 'team', team.reload

app/graph/types/query_type.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,32 @@ def search(query:)
159159
CheckSearch.new(query, context[:file], team&.id)
160160
end
161161

162-
field :dynamic_annotation_field, DynamicAnnotationFieldType, null: true, deprecation_reason: "The field is deprecated" do
162+
field :dynamic_annotation_field, DynamicAnnotationFieldType, null: true do
163163
argument :query, GraphQL::Types::String, required: true
164164
argument :only_cache, GraphQL::Types::Boolean, required: false, camelize: false
165165
end
166166

167167
def dynamic_annotation_field(query:, only_cache: nil)
168-
# This field was previously used to query DynamicAnnotation::Field for the Check Slack Bot integration.
169-
# It was not used by Check itself and required global permissions to read the field across all teams.
170-
# So, I removed the global permission and updated the callback to return nil to avoid breaking the Check Slack Bot integration.
171-
nil
168+
ability = context[:ability] || Ability.new
169+
if ability.can?(:find_by_json_fields, DynamicAnnotation::Field.new)
170+
cache_key =
171+
"dynamic-annotation-field-" + Digest::MD5.hexdigest(query)
172+
obj = nil
173+
if Rails.cache.read(cache_key) || only_cache
174+
obj =
175+
DynamicAnnotation::Field.where(
176+
id: Rails.cache.read(cache_key).to_i
177+
).last
178+
else
179+
query = JSON.parse(query)
180+
json = query.delete("json")
181+
obj = DynamicAnnotation::Field.where(query)
182+
obj = obj.find_in_json(json) unless json.blank?
183+
obj = obj.last
184+
Rails.cache.write(cache_key, obj&.id)
185+
end
186+
obj
187+
end
172188
end
173189

174190
field :feed_invitation, FeedInvitationType, description: 'Information about a feed invitation, given its database ID or feed database ID (and then the current user email is used)', null: true do

app/models/ability.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ def initialize(user = nil, team = nil)
1212
global_admin_perms
1313
else
1414
extra_perms_for_all_users
15+
if !@api_key.nil? && !@user.id
16+
global_api_key_perms
17+
end
1518
if @user.id
1619
authenticated_perms
1720
end
@@ -24,7 +27,7 @@ def initialize(user = nil, team = nil)
2427
if @user.role?(:admin, @context_team)
2528
admin_perms
2629
end
27-
unless @api_key.nil? || @api_key.team_id.nil?
30+
unless @api_key.nil?
2831
api_key_perms
2932
end
3033
Workflow::Workflow.workflows.each do |w|
@@ -40,14 +43,14 @@ def api_key_perms
4043
cannot [:create, :destroy], Team
4144
cannot :cud, User
4245
cannot :cud, TeamUser
43-
can :read, [FactCheck, ClaimDescription] do |obj|
44-
obj.team.present? && obj.team == @api_key.team
45-
end
4646
can :update, User, :id => @user.id
4747
can :update, BotUser, :id => @user.id
48-
can :update, [Dynamic, DynamicAnnotation::Field], ['annotation_type = ?', 'smooch_user'] do |obj|
49-
obj.team.present? && obj.team == @api_key.team
50-
end
48+
end
49+
50+
def global_api_key_perms
51+
can :read, :all
52+
can :find_by_json_fields, DynamicAnnotation::Field
53+
can :update, [Dynamic, DynamicAnnotation::Field], annotation_type: 'smooch_user'
5154
end
5255

5356
def admin_perms

app/models/api_key.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@ class Check::TooManyRequestsError < StandardError
22
end
33

44
class ApiKey < ApplicationRecord
5-
attr_accessor :skip_create_bot_user
6-
75
belongs_to :team, optional: true
86
belongs_to :user, optional: true
97

10-
validates_presence_of :access_token, :expire_at, :team
8+
validates_presence_of :access_token, :expire_at
119
validates_uniqueness_of :access_token
1210
validates :title, uniqueness: { scope: :team }
1311

1412
before_validation :generate_access_token, on: :create
1513
before_validation :calculate_expiration_date, on: :create
1614
before_validation :set_user_and_team
17-
after_create :create_bot_user, unless: proc { |key| key.skip_create_bot_user }
15+
after_create :create_bot_user
1816

1917
validate :validate_team_api_keys_limit, on: :create
2018

@@ -65,8 +63,8 @@ def delete_bot_user
6563
end
6664

6765
def set_user_and_team
68-
self.user ||= User.current
69-
self.team ||= Team.current
66+
self.user = User.current unless User.current.nil?
67+
self.team = Team.current unless Team.current.nil?
7068
end
7169

7270
def calculate_expiration_date

app/models/bot_user.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,12 +439,10 @@ def set_default_identifier
439439
def create_api_key
440440
if self.api_key_id.blank?
441441
api_key = ApiKey.new(bot_user: self)
442-
api_key.team_id = self.team_author_id
443442
api_key.skip_check_ability = true
444443
api_key.title = self.name
445444
api_key.save!
446445
api_key.expire_at = api_key.expire_at.since(100.years)
447-
api_key.skip_create_bot_user = true
448446
api_key.save!
449447
self.api_key_id = api_key.id
450448
end
@@ -472,7 +470,7 @@ def set_default_version
472470
end
473471

474472
def set_default_team_author_id
475-
self.team_author_id ||= Team.current&.id
473+
self.team_author_id = Team.current&.id if self.team_author_id.blank?
476474
end
477475

478476
def self.get_user(login)

db/migrate/20251215082146_add_not_null_constraint_to_api_keys_team_id.rb

Lines changed: 0 additions & 5 deletions
This file was deleted.

db/schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@
195195
t.string "access_token", default: "", null: false
196196
t.string "title"
197197
t.integer "user_id"
198-
t.integer "team_id", null: false
198+
t.integer "team_id"
199199
t.datetime "expire_at"
200200
t.jsonb "rate_limits", default: {}
201201
t.string "application"

lib/relay.idl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11501,7 +11501,7 @@ type Query {
1150111501
Information about the bot_user with given id
1150211502
"""
1150311503
bot_user(id: ID!): BotUser
11504-
dynamic_annotation_field(only_cache: Boolean, query: String!): DynamicAnnotationField @deprecated(reason: "The field is deprecated")
11504+
dynamic_annotation_field(only_cache: Boolean, query: String!): DynamicAnnotationField
1150511505

1150611506
"""
1150711507
Information about the explainer with given id

0 commit comments

Comments
 (0)