MCPcopy
hub / github.com/semaphoreui/semaphore / AddKey

Method AddKey

api/projects/keys.go:80–130  ·  view source on GitHub ↗

AddKey adds a new key to the database

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

Source from the content-addressed store, hash-verified

78
79// AddKey adds a new key to the database
80func (c *KeyController) AddKey(w http.ResponseWriter, r *http.Request) {
81 project := helpers.GetFromContext(r, "project").(db.Project)
82 var key db.AccessKey
83
84 if !helpers.Bind(w, r, &key) {
85 return
86 }
87
88 if key.ProjectID == nil || *key.ProjectID != project.ID {
89 helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
90 "error": "Project ID in body and URL must be the same",
91 })
92 return
93 }
94
95 // Plain cannot be passed via a request
96 key.Plain = nil
97 key.IgnorePlain = true
98 key.Synchronized = false
99
100 //if err := key.Validate(true); err != nil {
101 // helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
102 // "error": err.Error(),
103 // })
104 // return
105 //}
106
107 newKey, err := c.accessKeyService.Create(key)
108
109 if err != nil {
110 helpers.WriteError(w, err)
111 return
112 }
113
114 helpers.EventLog(r, helpers.EventLogCreate, helpers.EventLogItem{
115 UserID: helpers.UserFromContext(r).ID,
116 ProjectID: *newKey.ProjectID,
117 ObjectType: db.EventKey,
118 ObjectID: newKey.ID,
119 Description: fmt.Sprintf("Access Key %s created", key.Name),
120 })
121
122 // Reload key to drop sensitive fields
123 key, err = helpers.Store(r).GetAccessKey(*newKey.ProjectID, newKey.ID)
124 if err != nil {
125 helpers.WriteError(w, err)
126 return
127 }
128
129 helpers.WriteJSON(w, http.StatusCreated, key)
130}
131
132// UpdateKey updates key in database
133// nolint: gocyclo

Callers

nothing calls this directly

Calls 9

GetFromContextFunction · 0.92
BindFunction · 0.92
WriteJSONFunction · 0.92
WriteErrorFunction · 0.92
EventLogFunction · 0.92
UserFromContextFunction · 0.92
StoreFunction · 0.92
CreateMethod · 0.65
GetAccessKeyMethod · 0.65

Tested by

no test coverage detected