Skip to content

Commit f70c4f4

Browse files
committed
add support for filtering with an array of bearer roles
1 parent f3ced22 commit f70c4f4

3 files changed

Lines changed: 38 additions & 7 deletions

File tree

app/models/token.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ def actions = loaded? ? collect(&:action) : super
145145
type.blank? && id.blank? && role.blank?
146146

147147
scope = all
148-
scope = scope.for_bearer_type(type) if type.present?
149-
scope = scope.for_bearer_id(id) if id.present?
150-
scope = scope.for_bearer_role(role) if role.present?
148+
scope = scope.for_bearer_type(type) if type.present?
149+
scope = scope.for_bearer_id(id) if id.present?
150+
scope = scope.for_bearer_role(*role) if role.present?
151151

152152
scope
153153
} do
@@ -173,12 +173,15 @@ def users = for_bearer_type(:user)
173173
where(bearer_id: bearer_id)
174174
}
175175

176-
scope :for_bearer_role, -> role {
177-
name = role.to_s.underscore
176+
scope :for_bearer_role, -> *roles {
177+
names = roles.map { it.to_s.underscore } # support multiple roles
178+
.compact_blank
179+
.uniq
180+
178181
return none if
179-
name.empty?
182+
names.empty?
180183

181-
joins(<<~SQL.squish).where(roles: { name: })
184+
joins(<<~SQL.squish).where(roles: { name: names })
182185
INNER JOIN roles
183186
ON roles.resource_type = tokens.bearer_type
184187
AND roles.resource_id = tokens.bearer_id

features/api/v1/tokens/index.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ Feature: List authentication tokens
100100
Then the response status should be "200"
101101
And the response body should be an array of 1 "token"
102102

103+
Scenario: Admin requests tokens for multiple roles
104+
Given the current account is "test1"
105+
And the current account has 3 "products"
106+
And the current account has 1 "token" for each "product"
107+
And the current account has 5 "users"
108+
And the current account has 1 "token" for each "user"
109+
And the current account has 2 "licenses"
110+
And the current account has 1 "token" for each "license"
111+
And I am an admin of account "test1"
112+
And I use an authentication token
113+
When I send a GET request to "/accounts/test1/tokens?bearer[role][]=license&bearer[role][]=user"
114+
Then the response status should be "200"
115+
And the response body should be an array of 7 "tokens"
116+
103117
@ee
104118
Scenario: Isolated environment requests their tokens while authenticated
105119
Given the current account is "test1"

spec/models/token_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,20 @@
230230
expect(tokens).to_not include license_token
231231
end
232232

233+
it 'should filter by bearer roles' do
234+
admin_token = create(:token, account:, bearer: create(:admin, account:))
235+
user_token = create(:token, account:, bearer: create(:user, account:))
236+
product_token = create(:token, account:, bearer: create(:product, account:))
237+
license_token = create(:token, account:, bearer: create(:license, account:))
238+
239+
tokens = described_class.for_bearer(role: %i[user license])
240+
241+
expect(tokens).to_not include admin_token
242+
expect(tokens).to include user_token
243+
expect(tokens).to_not include product_token
244+
expect(tokens).to include license_token
245+
end
246+
233247
it 'should filter by bearer role' do
234248
admin_token = create(:token, account:, bearer: create(:admin, account:))
235249
user_token = create(:token, account:, bearer: create(:user, account:))

0 commit comments

Comments
 (0)