Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9b48f1e

Browse files
committed
feat: use default org for PostUser
1 parenta6baf6b commit9b48f1e

File tree

10 files changed

+101
-13
lines changed

10 files changed

+101
-13
lines changed

‎coderd/database/dbauthz/dbauthz.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,12 @@ func (q *querier) GetDERPMeshKey(ctx context.Context) (string, error) {
10161016
returnq.db.GetDERPMeshKey(ctx)
10171017
}
10181018

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

‎coderd/database/dbauthz/dbauthz_test.go

Lines changed: 4 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,18 @@ func (q *FakeQuerier) GetDERPMeshKey(_ context.Context) (string, error) {
16571657
returnq.derpMeshKey,nil
16581658
}
16591659

1660+
func (q*FakeQuerier)GetDefaultOrganization(_ context.Context) (database.Organization,error) {
1661+
q.mutex.RLock()
1662+
deferq.mutex.RUnlock()
1663+
1664+
for_,org:=rangeq.organizations {
1665+
iforg.IsDefault {
1666+
returnorg,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

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/dbmock/dbmock.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/queries.sql.go

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/queries/organizations.sql

Lines changed: 10 additions & 0 deletions
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

Lines changed: 12 additions & 9 deletions
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
iferr!=nil {
407+
ifhttpapi.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-
iflen(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
varloginType database.LoginType

‎coderd/users_test.go

Lines changed: 9 additions & 4 deletions
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
defercancel()
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)

[8]ページ先頭

©2009-2025 Movatter.jp