-
Notifications
You must be signed in to change notification settings - Fork 943
feat: notify about created user account #14010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
c18ada0
feat: notify about created user account
mtojek 1cc3adb
Merge branch 'main' into 17-account-notifs
mtojek ca2bdde
migrations
mtojek d6e8964
Username
mtojek 2d74c4e
events
mtojek 3c3b5af
fix
mtojek 312d9fa
WIP
mtojek 82ec37f
fix version
mtojek d7b2c73
simplify
mtojek b81bb6a
WIP
mtojek 00275dd
fix: versions
mtojek e356ba8
test
mtojek 6bc1d2d
fix test
mtojek 67a5137
users notified
mtojek 6431a0e
Merge branch 'main' into 17-account-notifs
mtojek 42d9ba1
post merge
mtojek ecc7d30
fmt
mtojek b2dcb3b
skip notif
mtojek 70e2d2c
Danny's feedback
mtojek 8696b70
given when then
mtojek a92d059
Fix tests
mtojek b5b0d90
Skip notifications
mtojek 2de1786
Merge branch 'main' into 17-account-notifs
mtojek b843232
fix: created_account_name
mtojek 2a53a0b
api.Database -> tx
mtojek 77c0c28
pq issue
mtojek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
coderd/database/migrations/000233_notifications_user_created.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DELETE FROM notification_templates WHERE id = '4e19c0ac-94e1-4532-9515-d1801aa283b2'; |
9 changes: 9 additions & 0 deletions
9
coderd/database/migrations/000233_notifications_user_created.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
INSERT INTO notification_templates (id, name, title_template, body_template, "group", actions) | ||
VALUES ('4e19c0ac-94e1-4532-9515-d1801aa283b2', 'User account created', E'User account "{{.Labels.user_account_name}}" created', | ||
E'Hi {{.UserName}},\n\New user account **{{.Labels.user_account_name}}** has been created.', | ||
'Workspace Events', '[ | ||
{ | ||
"label": "View accounts", | ||
"url": "{{ base_url }}/deployment/users?filter=status%3Aactive" | ||
} | ||
]'::jsonb); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,11 @@ import ( | |
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/render" | ||
"github.com/google/uuid" | ||
"golang.org/x/sync/errgroup" | ||
"golang.org/x/xerrors" | ||
|
||
"cdr.dev/slog" | ||
|
||
"github.com/coder/coder/v2/coderd/audit" | ||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/database/db2sdk" | ||
|
@@ -20,6 +23,7 @@ import ( | |
"github.com/coder/coder/v2/coderd/gitsshkey" | ||
"github.com/coder/coder/v2/coderd/httpapi" | ||
"github.com/coder/coder/v2/coderd/httpmw" | ||
"github.com/coder/coder/v2/coderd/notifications" | ||
"github.com/coder/coder/v2/coderd/rbac" | ||
"github.com/coder/coder/v2/coderd/rbac/policy" | ||
"github.com/coder/coder/v2/coderd/searchquery" | ||
|
@@ -1200,7 +1204,8 @@ func (api *API) organizationByUserAndName(rw http.ResponseWriter, r *http.Reques | |
|
||
type CreateUserRequest struct { | ||
codersdk.CreateUserRequest | ||
LoginType database.LoginType | ||
LoginType database.LoginType | ||
SkipNotifications bool | ||
} | ||
|
||
func (api *API) CreateUser(ctx context.Context, store database.Store, req CreateUserRequest) (database.User, uuid.UUID, error) { | ||
|
@@ -1211,7 +1216,7 @@ func (api *API) CreateUser(ctx context.Context, store database.Store, req Create | |
} | ||
|
||
var user database.User | ||
return user, req.OrganizationID, store.InTx(func(tx database.Store) error { | ||
err := store.InTx(func(tx database.Store) error { | ||
orgRoles := make([]string, 0) | ||
// Organization is required to know where to allocate the user. | ||
if req.OrganizationID == uuid.Nil { | ||
|
@@ -1272,6 +1277,48 @@ func (api *API) CreateUser(ctx context.Context, store database.Store, req Create | |
} | ||
return nil | ||
}, nil) | ||
if err == nil && !req.SkipNotifications { | ||
mtojek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Notify user admins | ||
// Get all users with user admin permission including owners | ||
var owners, userAdmins []database.GetUsersRow | ||
var eg errgroup.Group | ||
eg.Go(func() error { | ||
var err error | ||
owners, err = api.Database.GetUsers(ctx, database.GetUsersParams{ | ||
RbacRole: []string{codersdk.RoleOwner}, | ||
dannykopping marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
if err != nil { | ||
return xerrors.Errorf("get owners: %w", err) | ||
} | ||
return nil | ||
}) | ||
eg.Go(func() error { | ||
var err error | ||
userAdmins, err = api.Database.GetUsers(ctx, database.GetUsersParams{ | ||
RbacRole: []string{codersdk.RoleUserAdmin}, | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("get user admins: %w", err) | ||
} | ||
return nil | ||
}) | ||
err := eg.Wait() | ||
if err != nil { | ||
return database.User{}, uuid.Nil, err | ||
} | ||
|
||
for _, u := range append(owners, userAdmins...) { | ||
if _, err := api.NotificationsEnqueuer.Enqueue(ctx, u.ID, notifications.TemplateUserAccountCreated, | ||
map[string]string{ | ||
"user_account_name": user.Username, | ||
mtojek marked this c
8000
onversation as resolved.
Show resolved
Hide resolved
|
||
}, "api-users-create", | ||
user.ID, | ||
); err != nil { | ||
api.Logger.Warn(ctx, "unable to notify about created user", slog.F("created_user", user.Name), slog.Error(err)) | ||
mtojek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
return user, req.OrganizationID, err | ||
} | ||
|
||
func convertUsers(users []database.User, organizationIDsByUserID map[uuid.UUID][]uuid.UUID) []codersdk.User { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7D88
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.