Skip to content
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

Use the correct org display name for invitations #3769

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 20 additions & 6 deletions internal/controlplane/handlers_authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/stacklok/minder/internal/engine/engcontext"
"github.com/stacklok/minder/internal/flags"
"github.com/stacklok/minder/internal/invite"
"github.com/stacklok/minder/internal/projects"
"github.com/stacklok/minder/internal/util"
minder "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)
Expand Down Expand Up @@ -191,20 +192,20 @@ func getDefaultProjectID(
// Therefore, we assume it's safe output that the user is not found.
return uuid.UUID{}, util.UserVisibleError(codes.NotFound, "user not found")
}
projects, err := authzClient.ProjectsForUser(ctx, userInfo.IdentitySubject)
prjs, err := authzClient.ProjectsForUser(ctx, userInfo.IdentitySubject)
if err != nil {
return uuid.UUID{}, status.Errorf(codes.Internal, "cannot find projects for user: %v", err)
}

if len(projects) == 0 {
if len(prjs) == 0 {
return uuid.UUID{}, util.UserVisibleError(codes.PermissionDenied, "User has no role grants in projects")
}

if len(projects) != 1 {
if len(prjs) != 1 {
return uuid.UUID{}, util.UserVisibleError(codes.PermissionDenied, "Cannot determine default project. Please specify one.")
}

return projects[0], nil
return prjs[0], nil
}

// Permissions API
Expand Down Expand Up @@ -385,6 +386,12 @@ func (s *Server) inviteUser(
return nil, status.Errorf(codes.Internal, "failed to get target project: %s", err)
}

// Parse the project metadata, so we can get the display name set by project owner
meta, err := projects.ParseMetadata(&prj)
if err != nil {
return nil, status.Errorf(codes.Internal, "error parsing project metadata: %v", err)
}

// Create the invitation
userInvite, err = s.store.CreateInvitation(ctx, db.CreateInvitationParams{
Code: invite.GenerateCode(),
Expand All @@ -398,7 +405,7 @@ func (s *Server) inviteUser(
}

// Publish the event for sending the invitation email
msg, err := email.NewMessage(userInvite.Email, userInvite.Code, userInvite.Role, prj.Name, sponsorDisplay)
msg, err := email.NewMessage(userInvite.Email, userInvite.Code, userInvite.Role, meta.Public.DisplayName, sponsorDisplay)
if err != nil {
return nil, fmt.Errorf("error generating UUID: %w", err)
}
Expand Down Expand Up @@ -730,6 +737,13 @@ func (s *Server) updateInvite(
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get project: %s", err)
}

// Parse the project metadata, so we can get the display name set by project owner
meta, err := projects.ParseMetadata(&prj)
if err != nil {
return nil, status.Errorf(codes.Internal, "error parsing project metadata: %v", err)
}

// Resolve the sponsor's identity and display name
identity, err := s.idClient.Resolve(ctx, currentUser.IdentitySubject)
if err != nil {
Expand All @@ -746,7 +760,7 @@ func (s *Server) updateInvite(
// This will happen only if the role is updated (existingInvites[0].Role != authzRole.String())
// or the role stayed the same, but the last invite update was more than a day ago
if existingInvites[0].Role != authzRole.String() || userInvite.UpdatedAt.Sub(existingInvites[0].UpdatedAt) > 24*time.Hour {
msg, err := email.NewMessage(userInvite.Email, userInvite.Code, userInvite.Role, prj.Name, identity.Human())
msg, err := email.NewMessage(userInvite.Email, userInvite.Code, userInvite.Role, meta.Public.DisplayName, identity.Human())
if err != nil {
return nil, fmt.Errorf("error generating UUID: %w", err)
}
Expand Down
9 changes: 8 additions & 1 deletion internal/controlplane/handlers_invites.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"google.golang.org/grpc/status"

"github.com/stacklok/minder/internal/invite"
"github.com/stacklok/minder/internal/projects"
"github.com/stacklok/minder/internal/util"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)
Expand All @@ -49,6 +50,12 @@ func (s *Server) GetInviteDetails(ctx context.Context, req *pb.GetInviteDetailsR
return nil, status.Errorf(codes.Internal, "failed to get project: %s", err)
}

// Parse the project metadata, so we can get the display name set by project owner
meta, err := projects.ParseMetadata(&targetProject)
if err != nil {
return nil, status.Errorf(codes.Internal, "error parsing project metadata: %v", err)
}

// Resolve the sponsor's identity and display name
identity, err := s.idClient.Resolve(ctx, retInvite.IdentitySubject)
if err != nil {
Expand All @@ -57,7 +64,7 @@ func (s *Server) GetInviteDetails(ctx context.Context, req *pb.GetInviteDetailsR
}

return &pb.GetInviteDetailsResponse{
ProjectDisplay: targetProject.Name,
ProjectDisplay: meta.Public.DisplayName,
SponsorDisplay: identity.Human(),
ExpiresAt: invite.GetExpireIn7Days(retInvite.UpdatedAt),
Expired: invite.IsExpired(retInvite.UpdatedAt),
Expand Down