Skip to content

Commit 367a282

Browse files
committed
Fix graphql permission issues
1 parent 391f0c7 commit 367a282

File tree

3 files changed

+21
-36
lines changed

3 files changed

+21
-36
lines changed

app/graphql/types/query_type.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ def user(id:)
9292
end
9393

9494
def users
95-
unless Ability.allowed?(current_authentication, :list_users, :global)
96-
raise GraphQL::UnauthorizedError, 'You do not have permission to list all users'
97-
end
95+
return User.none unless Ability.allowed?(context[:current_authentication], :list_users, :global)
9896

9997
User.all
10098
end

docs/graphql/object/query.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,13 @@ Returns [`Organization`](../object/organization.md).
6868
|------|------|-------------|
6969
| `id` | [`OrganizationID`](../scalar/organizationid.md) | GlobalID of the target organization |
7070
| `name` | [`String`](../scalar/string.md) | Name of the target organization |
71+
72+
### user
73+
74+
Find a user
75+
76+
Returns [`User`](../object/user.md).
77+
78+
| Name | Type | Description |
79+
|------|------|-------------|
80+
| `id` | [`UserID!`](../scalar/userid.md) | GlobalID of the target user |

spec/requests/graphql/query/users_query_spec.rb

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
RSpec.describe 'users Query' do
66
include GraphqlHelpers
77

8-
subject(:query!) { post_graphql query, current_user: current_user }
9-
108
let(:query) do
119
<<~QUERY
1210
query {
@@ -20,56 +18,35 @@
2018
QUERY
2119
end
2220

23-
let!(:user1) { create(:user) }
24-
let!(:user2) { create(:user) }
25-
let!(:user3) { create(:user) }
21+
before do
22+
create(:user)
23+
create(:user)
24+
create(:user)
25+
26+
post_graphql query, current_user: current_user
27+
end
2628

2729
context 'when anonymous' do
2830
let(:current_user) { nil }
2931

3032
it 'returns an error' do
31-
query!
32-
33-
expect(graphql_errors).to include(
34-
a_hash_including(
35-
'message' => 'You do not have permission to list all users'
36-
)
37-
)
33+
expect(graphql_data_at(:users, :nodes)).to be_empty
3834
end
3935
end
4036

4137
context 'when logged in as regular user' do
4238
let(:current_user) { create(:user) }
4339

4440
it 'returns an error' do
45-
query!
46-
47-
expect(graphql_errors).to include(
48-
a_hash_including(
49-
'message' => 'You do not have permission to list all users'
50-
)
51-
)
41+
expect(graphql_data_at(:users, :nodes)).to be_empty
5242
end
5343
end
5444

5545
context 'when logged in as admin user' do
5646
let(:current_user) { create(:user, :admin) }
5747

5848
it 'returns all users' do
59-
query!
60-
61-
expect(graphql_data_at(:users, :nodes)).to contain_exactly(
62-
a_graphql_entity_for(user1),
63-
a_graphql_entity_for(user2),
64-
a_graphql_entity_for(user3),
65-
a_graphql_entity_for(current_user)
66-
)
67-
end
68-
69-
it 'does not return errors' do
70-
query!
71-
72-
expect(graphql_errors).to be_nil
49+
expect(graphql_data_at(:users, :nodes)).to have_attributes(length: 4)
7350
end
7451
end
7552
end

0 commit comments

Comments
 (0)