diff options
author | Saturnino Abril <saturnino.abril@gmail.com> | 2017-04-08 02:06:09 +0900 |
---|---|---|
committer | Corey Hulen <corey@hulen.com> | 2017-04-07 10:06:09 -0700 |
commit | a3f5cffd4693446eecdc6f88ed336a4c35c1e708 (patch) | |
tree | 3c2a1987e308f07df65f81d85a8a19ebf34cbdba /api4/command.go | |
parent | f7b39caf318cf0839c7889d615cbc3ab16c285b2 (diff) | |
download | chat-a3f5cffd4693446eecdc6f88ed336a4c35c1e708.tar.gz chat-a3f5cffd4693446eecdc6f88ed336a4c35c1e708.tar.bz2 chat-a3f5cffd4693446eecdc6f88ed336a4c35c1e708.zip |
APIv4 PUT /commands/{command_id} (#5999)
* APIv4 PUT /commands/{command_id}
* update client parameter and api4 test
Diffstat (limited to 'api4/command.go')
-rw-r--r-- | api4/command.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/api4/command.go b/api4/command.go index d6102bd70..2466567c1 100644 --- a/api4/command.go +++ b/api4/command.go @@ -19,6 +19,8 @@ func InitCommand() { BaseRoutes.Commands.Handle("", ApiSessionRequired(createCommand)).Methods("POST") BaseRoutes.Commands.Handle("", ApiSessionRequired(listCommands)).Methods("GET") + BaseRoutes.Command.Handle("", ApiSessionRequired(updateCommand)).Methods("PUT") + BaseRoutes.Team.Handle("/commands/autocomplete", ApiSessionRequired(listAutocompleteCommands)).Methods("GET") } @@ -49,6 +51,54 @@ func createCommand(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(rcmd.ToJson())) } +func updateCommand(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequireCommandId() + if c.Err != nil { + return + } + + cmd := model.CommandFromJson(r.Body) + if cmd == nil || cmd.Id != c.Params.CommandId { + c.SetInvalidParam("command") + return + } + + c.LogAudit("attempt") + + oldCmd, err := app.GetCommand(c.Params.CommandId) + if err != nil { + c.Err = err + return + } + + if cmd.TeamId != oldCmd.TeamId { + c.Err = model.NewAppError("updateCommand", "api.command.team_mismatch.app_error", nil, "user_id="+c.Session.UserId, http.StatusBadRequest) + return + } + + if !app.SessionHasPermissionToTeam(c.Session, oldCmd.TeamId, model.PERMISSION_MANAGE_SLASH_COMMANDS) { + c.LogAudit("fail - inappropriate permissions") + c.SetPermissionError(model.PERMISSION_MANAGE_SLASH_COMMANDS) + return + } + + if c.Session.UserId != oldCmd.CreatorId && !app.SessionHasPermissionToTeam(c.Session, oldCmd.TeamId, model.PERMISSION_MANAGE_OTHERS_SLASH_COMMANDS) { + c.LogAudit("fail - inappropriate permissions") + c.SetPermissionError(model.PERMISSION_MANAGE_OTHERS_SLASH_COMMANDS) + return + } + + rcmd, err := app.UpdateCommand(oldCmd, cmd) + if err != nil { + c.Err = err + return + } + + c.LogAudit("success") + + w.Write([]byte(rcmd.ToJson())) +} + func listCommands(c *Context, w http.ResponseWriter, r *http.Request) { customOnly, failConv := strconv.ParseBool(r.URL.Query().Get("custom_only")) if failConv != nil { |