MCPcopy Index your code
hub / github.com/coder/coder / postOrganizationMember

Method postOrganizationMember

coderd/members.go:34–91  ·  view source on GitHub ↗

@Summary Add organization member @ID add-organization-member @Security CoderSessionToken @Produce json @Tags Members @Param organization path string true "Organization ID" @Param user path string true "User ID, name, or me" @Success 200 {object} codersdk.OrganizationMember @Router /api/v2/organizati

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

32// @Success 200 {object} codersdk.OrganizationMember
33// @Router /api/v2/organizations/{organization}/members/{user} [post]
34func (api *API) postOrganizationMember(rw http.ResponseWriter, r *http.Request) {
35 var (
36 ctx = r.Context()
37 organization = httpmw.OrganizationParam(r)
38 user = httpmw.UserParam(r)
39 auditor = api.Auditor.Load()
40 aReq, commitAudit = audit.InitRequest[database.AuditableOrganizationMember](rw, &audit.RequestParams{
41 OrganizationID: organization.ID,
42 Audit: *auditor,
43 Log: api.Logger,
44 Request: r,
45 Action: database.AuditActionCreate,
46 })
47 )
48 aReq.Old = database.AuditableOrganizationMember{}
49 defer commitAudit()
50
51 if !api.manualOrganizationMembership(ctx, rw, user) {
52 return
53 }
54
55 member, err := api.Database.InsertOrganizationMember(ctx, database.InsertOrganizationMemberParams{
56 OrganizationID: organization.ID,
57 UserID: user.ID,
58 CreatedAt: dbtime.Now(),
59 UpdatedAt: dbtime.Now(),
60 Roles: []string{},
61 })
62 if httpapi.Is404Error(err) {
63 httpapi.ResourceNotFound(rw)
64 return
65 }
66 if database.IsUniqueViolation(err, database.UniqueOrganizationMembersPkey) {
67 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
68 Message: "User is already an organization member",
69 Detail: fmt.Sprintf("%s is already a member of %s", user.Username, organization.DisplayName),
70 })
71 return
72 }
73 if err != nil {
74 httpapi.InternalServerError(rw, err)
75 return
76 }
77
78 aReq.New = member.Auditable(user.Username)
79 resp, err := convertOrganizationMembers(ctx, api.Database, []database.OrganizationMember{member})
80 if err != nil {
81 httpapi.InternalServerError(rw, err)
82 return
83 }
84
85 if len(resp) == 0 {
86 httpapi.InternalServerError(rw, xerrors.Errorf("marshal member"))
87 return
88 }
89
90 httpapi.Write(ctx, rw, http.StatusOK, resp[0])
91}

Callers

nothing calls this directly

Calls 15

OrganizationParamFunction · 0.92
UserParamFunction · 0.92
InitRequestFunction · 0.92
NowFunction · 0.92
Is404ErrorFunction · 0.92
ResourceNotFoundFunction · 0.92
IsUniqueViolationFunction · 0.92
WriteFunction · 0.92
InternalServerErrorFunction · 0.92
ContextMethod · 0.65

Tested by

no test coverage detected