Skip to content

Commit 29bf34f

Browse files
committed
feat: use default org for PostUser
1 parent 5bb0ffc commit 29bf34f

File tree

10 files changed

+101
-13
lines changed

10 files changed

+101
-13
lines changed

coderd/database/dbauthz/dbauthz.go

+6
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,12 @@ func (q *querier) GetDERPMeshKey(ctx context.Context) (string, error) {
10161016
return q.db.GetDERPMeshKey(ctx)
10171017
}
10181018

1019+
func (q *querier) GetDefaultOrganization(ctx context.Context) (database.Organization, error) {
1020+
return fetch(q.log, q.auth, func(ctx context.Context, _ any) (database.Organization, error) {
1021+
return q.db.GetDefaultOrganization(ctx)
1022+
})(ctx, nil)
1023+
}
1024+
10191025
func (q *querier) GetDefaultProxyConfig(ctx context.Context) (database.GetDefaultProxyConfigRow, error) {
10201026
// No authz checks
10211027
return q.db.GetDefaultProxyConfig(ctx)

coderd/database/dbauthz/dbauthz_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,10 @@ func (s *MethodTestSuite) TestOrganization() {
570570
o := dbgen.Organization(s.T(), db, database.Organization{})
571571
check.Args(o.ID).Asserts(o, rbac.ActionRead).Returns(o)
572572
}))
573+
s.Run("GetDefaultOrganization", s.Subtest(func(db database.Store, check *expects) {
574+
o := dbgen.Organization(s.T(), db, database.Organization{})
575+
check.Ar.Asserts(o, rbac.ActionRead).Returns(o)
576+
}))
573577
s.Run("GetOrganizationByName", s.Subtest(func(db database.Store, check *expects) {
574578
o := dbgen.Organization(s.T(), db, database.Organization{})
575579
check.Args(o.Name).Asserts(o, rbac.ActionRead).Returns(o)

coderd/database/dbmem/dbmem.go

+12
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,18 @@ func (q *FakeQuerier) GetDERPMeshKey(_ context.Context) (string, error) {
16571657
return q.derpMeshKey, nil
16581658
}
16591659

1660+
func (q *FakeQuerier) GetDefaultOrganization(_ context.Context) (database.Organization, error) {
1661+
q.mutex.RLock()
1662+
defer q.mutex.RUnlock()
1663+
1664+
for _, org := range q.organizations {
1665+
if org.IsDefault {
1666+
return org, nil
1667+
}
1668+
}
1669+
return database.Organization{}, sql.ErrNoRows
1670+
}
1671+
16601672
func (q *FakeQuerier) GetDefaultProxyConfig(_ context.Context) (database.GetDefaultProxyConfigRow, error) {
16611673
return database.GetDefaultProxyConfigRow{
16621674
DisplayName: q.defaultProxyDisplayName,

coderd/database/dbmetrics/dbmetrics.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/organizations.sql

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
-- name: GetDefaultOrganization :one
2+
SELECT
3+
*
4+
FROM
5+
organizations
6+
WHERE
7+
is_default = true
8+
LIMIT
9+
1;
10+
111
-- name: GetOrganizations :many
212
SELECT
313
*

coderd/users.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -401,23 +401,26 @@ func (api *API) postUser(rw http.ResponseWriter, r *http.Request) {
401401
return
402402
}
403403
} else {
404-
// If no organization is provided, add the user to the first
405-
// organization.
406-
organizations, err := api.Database.GetOrganizations(ctx)
404+
// If no organization is provided, add the user to the default
405+
defaultOrg, err := api.Database.GetDefaultOrganization(ctx)
407406
if err != nil {
407+
if httpapi.Is404Error(err) {
408+
httpapi.Write(ctx, rw, http.StatusNotFound,
409+
codersdk.Response{
410+
Message: "Resource not found or you do not have access to this resource",
411+
Detail: "Organization not found",
412+
},
413+
)
414+
return
415+
}
408416
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
409417
Message: "Internal error fetching orgs.",
410418
Detail: err.Error(),
411419
})
412420
return
413421
}
414422

415-
if len(organizations) > 0 {
416-
// Add the user to the first organization. Once multi-organization
417-
// support is added, we should enable a configuration map of user
418-
// email to organization.
419-
req.OrganizationID = organizations[0].ID
420-
}
423+
req.OrganizationID = defaultOrg.ID
421424
}
422425

423426
var loginType database.LoginType

coderd/users_test.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -493,21 +493,26 @@ func TestPostUsers(t *testing.T) {
493493
t.Parallel()
494494
auditor := audit.NewMock()
495495
client := coderdtest.New(t, &coderdtest.Options{Auditor: auditor})
496-
numLogs := len(auditor.AuditLogs())
497-
498496
firstUser := coderdtest.CreateFirstUser(t, client)
499-
numLogs++ // add an audit log for user create
500-
numLogs++ // add an audit log for login
501497

502498
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
503499
defer cancel()
504500

501+
// Add an extra org to try and confuse user creation
502+
_, err := client.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{
503+
Name: "foobar",
504+
})
505+
require.NoError(t, err)
506+
507+
numLogs := len(auditor.AuditLogs())
508+
505509
user, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
506510
Email: "another@user.org",
507511
Username: "someone-else",
508512
Password: "SomeSecurePassword!",
509513
})
510514
require.NoError(t, err)
515+
numLogs++ // add an audit log for user create
511516

512517
require.Len(t, auditor.AuditLogs(), numLogs)
513518
require.Equal(t, database.AuditActionCreate, auditor.AuditLogs()[numLogs-1].Action)

0 commit comments

Comments
 (0)