8000 feat: notify about created user account by mtojek · Pull Request #14010 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

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 26 commits into from
Jul 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP
  • Loading branch information
mtojek committed Jul 29, 2024
commit b81bb6a2af5040d172c246af473d83133fb883a4
24 changes: 19 additions & 5 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"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"
Expand All @@ -21,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"
Expand Down Expand Up @@ -1277,19 +1280,21 @@ func (api *API) CreateUser(ctx context.Context, store database.Store, req Create
if err == nil {
// Notify user admins
// Get all users with user admin permission including owners
var owners, users []database.User
var owners, userAdmins []database.GetUsersRow
var eg errgroup.Group
eg.Go(func() error {
owners, err := api.Database.GetUsers(ctx, database.GetUsersParams{
var err error
owners, err = api.Database.GetUsers(ctx, database.GetUsersParams{
RbacRole: []string{codersdk.RoleOwner},
})
if err != nil {
return xerrors.Errorf("get owner: %w", err)
return xerrors.Errorf("get owners: %w", err)
}
return nil
})
eg.Go(func() error {
userAdmin, err := api.Database.GetUsers(ctx, database.GetUsersParams{
var err error
userAdmins, err = api.Database.GetUsers(ctx, database.GetUsersParams{
RbacRole: []string{codersdk.RoleOrganizationUserAdmin},
})
if err != nil {
Expand All @@ -1302,7 +1307,16 @@ func (api *API) CreateUser(ctx context.Context, store database.Store, req Create
return database.User{}, uuid.Nil, err
}

// Enqueue N notifications
for _, u := range append(owners, userAdmins...) {
if _, err := api.NotificationsEnqueuer.Enqueue(ctx, u.ID, notifications.TemplateUserAccountCreated,
map[string]string{
"user_account_name": user.Name,
}, "api-users-create",
u.ID,
); err != nil {
api.Logger.Warn(ctx, "unable to notify about created user", slog.F("created_user", user.Name), slog.Error(err))
}
}
}
return user, req.OrganizationID, err
}
Expand Down
Loading
0