Repository navigation
Expand file tree
/
Copy pathchatprojectparam.go
More file actions
59 lines (52 loc) · 1.84 KB
/
Copy pathchatprojectparam.go
File metadata and controls
59 lines (52 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package httpmw
import (
"context"
"net/http"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/codersdk"
)
type chatProjectParamContextKey struct{}
// ChatProjectParam returns the chat project from the ExtractChatProjectParam handler.
func ChatProjectParam(r *http.Request) database.ChatProject {
project, ok := r.Context().Value(chatProjectParamContextKey{}).(database.ChatProject)
if !ok {
panic("developer error: chat project param middleware not provided")
}
return project
}
// ExtractChatProjectParam grabs a chat project from the "project" URL parameter.
// It must run after ExtractOrganizationParam; projects outside the route
// organization are reported as not found.
func ExtractChatProjectParam(db database.Store) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
projectID, parsed := ParseUUIDParam(rw, r, "project")
if !parsed {
return
}
// Route extraction resolves identity before the handler authorizes its action.
//nolint:gocritic // Restrict system access to the identity lookup.
project, err := db.GetChatProjectByID(dbauthz.AsSystemRestricted(ctx), projectID)
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching chat project.",
Detail: err.Error(),
})
return
}
if project.OrganizationID != OrganizationParam(r).ID {
httpapi.ResourceNotFound(rw)
return
}
ctx = context.WithValue(ctx, chatProjectParamContextKey{}, project)
next.ServeHTTP(rw, r.WithContext(ctx))
})
}
}