Skip to content

Commit ff7bd81

Browse files
committed
feat: Add some perms to users
1 parent 28a099f commit ff7bd81

File tree

4 files changed

+13
-24
lines changed

4 files changed

+13
-24
lines changed

coderd/rbac/builtin.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ var (
6464
return Role{
6565
Name: member,
6666
DisplayName: "Member",
67-
Site: permissions(map[Object][]Action{}),
67+
Site: permissions(map[Object][]Action{
68+
// TODO: @emyrk in EE we should restrict this to only certain fields.
69+
// All users can read all other users and know they exist.
70+
ResourceUser: {ActionRead},
71+
}),
6872
User: permissions(map[Object][]Action{
6973
ResourceWildcard: {WildcardSymbol},
7074
}),
@@ -117,11 +121,13 @@ var (
117121
// All org members can read the other members in their org.
118122
ResourceType: ResourceOrganizationMember.Type,
119123
Action: ActionRead,
124+
ResourceID: "*",
120125
},
121126
{
122127
// All org members can read the organization
123128
ResourceType: ResourceOrganization.Type,
124129
Action: ActionRead,
130+
ResourceID: "*",
125131
},
126132
},
127133
},

coderd/users.go

+1-20
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,6 @@ func (api *api) users(rw http.ResponseWriter, r *http.Request) {
162162

163163
// Creates a new user.
164164
func (api *api) postUser(rw http.ResponseWriter, r *http.Request) {
165-
apiKey := httpmw.APIKey(r)
166-
167165
// Create the user on the site
168166
if !api.Authorize(rw, r, rbac.ActionCreate, rbac.ResourceUser) {
169167
return
@@ -199,7 +197,7 @@ func (api *api) postUser(rw http.ResponseWriter, r *http.Request) {
199197
return
200198
}
201199

202-
organization, err := api.Database.GetOrganizationByID(r.Context(), createUser.OrganizationID)
200+
_, err = api.Database.GetOrganizationByID(r.Context(), createUser.OrganizationID)
203201
if errors.Is(err, sql.ErrNoRows) {
204202
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
205203
Message: "organization does not exist with the provided id",
@@ -212,23 +210,6 @@ func (api *api) postUser(rw http.ResponseWriter, r *http.Request) {
212210
})
213211
return
214212
}
215-
// Check if the caller has permissions to the organization requested.
216-
_, err = api.Database.GetOrganizationMemberByUserID(r.Context(), database.GetOrganizationMemberByUserIDParams{
217-
OrganizationID: organization.ID,
218-
UserID: apiKey.UserID,
219-
})
220-
if errors.Is(err, sql.ErrNoRows) {
221-
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
222-
Message: "you are not authorized to add members to that organization",
223-
})
224-
return
225-
}
226-
if err != nil {
227-
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
228-
Message: fmt.Sprintf("get organization member: %s", err),
229-
})
230-
return
231-
}
232213

233214
user, _, err := api.createUser(r.Context(), createUser)
234215
if err != nil {

coderd/users_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,14 @@ func TestPostUsers(t *testing.T) {
172172
t.Parallel()
173173
client := coderdtest.New(t, nil)
174174
first := coderdtest.CreateFirstUser(t, client)
175+
notInOrg := coderdtest.CreateAnotherUser(t, client, first.OrganizationID)
175176
other := coderdtest.CreateAnotherUser(t, client, first.OrganizationID)
176177
org, err := other.CreateOrganization(context.Background(), codersdk.Me, codersdk.CreateOrganizationRequest{
177178
Name: "another",
178179
})
179180
require.NoError(t, err)
180181

181-
_, err = client.CreateUser(context.Background(), codersdk.CreateUserRequest{
182+
_, err = notInOrg.CreateUser(context.Background(), codersdk.CreateUserRequest{
182183
Email: "some@domain.com",
183184
Username: "anotheruser",
184185
Password: "testing",
@@ -582,7 +583,8 @@ func TestOrganizationByUserAndName(t *testing.T) {
582583
_, err := client.OrganizationByName(context.Background(), codersdk.Me, "nothing")
583584
var apiErr *codersdk.Error
584585
require.ErrorAs(t, err, &apiErr)
585-
require.Equal(t, http.StatusNotFound, apiErr.StatusCode())
586+
// Returns unauthorized to not leak if the org exists or not
587+
require.Equal(t, http.StatusUnauthorized, apiErr.StatusCode())
586588
})
587589

588590
t.Run("NoMember", func(t *testing.T) {

codersdk/roles.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (c *Client) ListSiteRoles(ctx context.Context) ([]Role, error) {
3232
// ListOrganizationRoles lists all available roles for a given organization.
3333
// This is not user specific.
3434
func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]Role, error) {
35-
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/organizations/%s/members/roles/", org.String()), nil)
35+
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/organizations/%s/members/roles", org.String()), nil)
3636
if err != nil {
3737
return nil, err
3838
}

0 commit comments

Comments
 (0)