Skip to content

feat: remove "view all users" from members #8447

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
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
chore: templates conditionally return created by
If omitted, the caller does not have permission to view said data
  • Loading branch information
Emyrk committed Jul 12, 2023
commit 76e724c31ac9c2d1ffd03ed28bfdee299675ddd4
16 changes: 15 additions & 1 deletion coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,11 +679,19 @@ func (api *API) templateExamples(rw http.ResponseWriter, r *http.Request) {
httpapi.Write(ctx, rw, http.StatusOK, ex)
}

// getCreatedByNamesByTemplateIDs returns a map of template IDs to the
// usernames of the users who created them. If the caller does not have
// permission to view the given creator, then the username will be the empty
// string.
func getCreatedByNamesByTemplateIDs(ctx context.Context, db database.Store, templates []database.Template) (map[string]string, error) {
creators := make(map[string]string, len(templates))
for _, template := range templates {
creator, err := db.GetUserByID(ctx, template.CreatedBy)
if err != nil {
if errors.Is(err, sql.ErrNoRows) || dbauthz.IsNotAuthorizedError(err) {
// Users might be omitted if the caller does not have access.
continue
}
return map[string]string{}, err
}
creators[template.ID.String()] = creator.Username
Expand Down Expand Up @@ -713,6 +721,12 @@ func (api *API) convertTemplate(

buildTimeStats := api.metricsCache.TemplateBuildTimeStats(template.ID)

// Only include this uuid if the user has permission to view the user.
// We know this if the username is not empty.
createdBy := uuid.Nil
if createdByName != "" {
createdBy = template.CreatedBy
}
return codersdk.Template{
ID: template.ID,
CreatedAt: template.CreatedAt,
Expand All @@ -728,7 +742,7 @@ func (api *API) convertTemplate(
Icon: template.Icon,
DefaultTTLMillis: time.Duration(template.DefaultTTL).Milliseconds(),
MaxTTLMillis: time.Duration(template.MaxTTL).Milliseconds(),
CreatedByID: template.CreatedBy,
CreatedByID: createdBy,
CreatedByName: createdByName,
AllowUserAutostart: template.AllowUserAutostart,
AllowUserAutostop: template.AllowUserAutostop,
Expand Down
34 changes: 21 additions & 13 deletions coderd/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/coder/coder/coderd/audit"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/database/dbauthz"
"github.com/coder/coder/coderd/gitauth"
"github.com/coder/coder/coderd/httpapi"
"github.com/coder/coder/coderd/httpmw"
Expand Down Expand Up @@ -53,8 +54,9 @@ func (api *API) templateVersion(rw http.ResponseWriter, r *http.Request) {
return
}

// User can be the empty user if the caller does not have permission.
user, err := api.Database.GetUserByID(ctx, templateVersion.CreatedBy)
if err != nil {
if err != nil && !dbauthz.IsNotAuthorizedError(err) {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error on fetching user.",
Detail: err.Error(),
Expand Down Expand Up @@ -165,7 +167,7 @@ func (api *API) patchTemplateVersion(rw http.ResponseWriter, r *http.Request) {
}

user, err := api.Database.GetUserByID(ctx, templateVersion.CreatedBy)
if err != nil {
if err != nil && !dbauthz.IsNotAuthorizedError(err) {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error on fetching user.",
Detail: err.Error(),
Expand Down Expand Up @@ -843,7 +845,7 @@ func (api *API) templateVersionByName(rw http.ResponseWriter, r *http.Request) {
}

user, err := api.Database.GetUserByID(ctx, templateVersion.CreatedBy)
if err != nil {
if err != nil && !dbauthz.IsNotAuthorizedError(err) {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error on fetching user.",
Detail: err.Error(),
Expand Down Expand Up @@ -1012,7 +1014,7 @@ func (api *API) previousTemplateVersionByOrganizationTemplateAndName(rw http.Res
}

user, err := api.Database.GetUserByID(ctx, templateVersion.CreatedBy)
if err != nil {
if err != nil && !dbauthz.IsNotAuthorizedError(err) {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error on fetching user.",
Detail: err.Error(),
Expand Down Expand Up @@ -1325,7 +1327,7 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
aReq.New = templateVersion

user, err := api.Database.GetUserByID(ctx, templateVersion.CreatedBy)
if err != nil {
if err != nil && !dbauthz.IsNotAuthorizedError(err) {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error on fetching user.",
Detail: err.Error(),
Expand Down Expand Up @@ -1404,14 +1406,20 @@ func (api *API) templateVersionLogs(rw http.ResponseWriter, r *http.Request) {
}

func convertTemplateVersion(version database.TemplateVersion, job codersdk.ProvisionerJob, user database.User, warnings []codersdk.TemplateVersionWarning) codersdk.TemplateVersion {
createdBy := codersdk.User{
ID: user.ID,
Username: user.Username,
Email: user.Email,
CreatedAt: user.CreatedAt,
Status: codersdk.UserStatus(user.Status),
Roles: []codersdk.Role{},
AvatarURL: user.AvatarURL.String,
// Only populate these fields if the user is not nil.
// It is usually nil because the caller cannot access the user
// resource in question.
var createdBy codersdk.User
if user.ID != uuid.Nil {
createdBy = codersdk.User{
ID: user.ID,
Username: user.Username,
Email: user.Email,
CreatedAt: user.CreatedAt,
Status: codersdk.UserStatus(user.Status),
Roles: []codersdk.Role{},
AvatarURL: user.AvatarURL.String,
}
}

return codersdk.TemplateVersion{
Expand Down
11 changes: 7 additions & 4 deletions site/src/components/TemplateStats/TemplateStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
formatTemplateActiveDevelopers,
} from "utils/templates"
import { Template, TemplateVersion } from "../../api/typesGenerated"
import { Maybe } from "components/Conditionals/Maybe"

const Language = {
usedByLabel: "Used by",
Expand Down Expand Up @@ -56,10 +57,12 @@ export const TemplateStats: FC<TemplateStatsProps> = ({
label={Language.lastUpdateLabel}
value={createDayString(template.updated_at)}
/>
<StatsItem
label={Language.createdByLabel}
value={template.created_by_name}
/>
<Maybe condition={template.created_by_name !== ""}>
<StatsItem
label={Language.createdByLabel}
value={template.created_by_name}
/>
</Maybe>
</Stats>
)
}