mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
333 lines
9.9 KiB
Go
333 lines
9.9 KiB
Go
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package api
|
|
|
|
import (
|
|
l4g "github.com/alecthomas/log4go"
|
|
"github.com/gorilla/mux"
|
|
"github.com/mattermost/platform/model"
|
|
"github.com/mattermost/platform/utils"
|
|
"net/http"
|
|
)
|
|
|
|
func InitWebhook(r *mux.Router) {
|
|
l4g.Debug(utils.T("api.webhook.init.debug"))
|
|
|
|
sr := r.PathPrefix("/hooks").Subrouter()
|
|
sr.Handle("/incoming/create", ApiUserRequired(createIncomingHook)).Methods("POST")
|
|
sr.Handle("/incoming/delete", ApiUserRequired(deleteIncomingHook)).Methods("POST")
|
|
sr.Handle("/incoming/list", ApiUserRequired(getIncomingHooks)).Methods("GET")
|
|
|
|
sr.Handle("/outgoing/create", ApiUserRequired(createOutgoingHook)).Methods("POST")
|
|
sr.Handle("/outgoing/regen_token", ApiUserRequired(regenOutgoingHookToken)).Methods("POST")
|
|
sr.Handle("/outgoing/delete", ApiUserRequired(deleteOutgoingHook)).Methods("POST")
|
|
sr.Handle("/outgoing/list", ApiUserRequired(getOutgoingHooks)).Methods("GET")
|
|
}
|
|
|
|
func createIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
if !utils.Cfg.ServiceSettings.EnableIncomingWebhooks {
|
|
c.Err = model.NewLocAppError("createIncomingHook", "api.webhook.create_incoming.disabled.app_errror", nil, "")
|
|
c.Err.StatusCode = http.StatusNotImplemented
|
|
return
|
|
}
|
|
|
|
if *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations {
|
|
if !(c.IsSystemAdmin() || c.IsTeamAdmin()) {
|
|
c.Err = model.NewLocAppError("createIncomingHook", "api.command.admin_only.app_error", nil, "")
|
|
c.Err.StatusCode = http.StatusForbidden
|
|
return
|
|
}
|
|
}
|
|
|
|
c.LogAudit("attempt")
|
|
|
|
hook := model.IncomingWebhookFromJson(r.Body)
|
|
|
|
if hook == nil {
|
|
c.SetInvalidParam("createIncomingHook", "webhook")
|
|
return
|
|
}
|
|
|
|
cchan := Srv.Store.Channel().Get(hook.ChannelId)
|
|
pchan := Srv.Store.Channel().CheckPermissionsTo(c.Session.TeamId, hook.ChannelId, c.Session.UserId)
|
|
|
|
hook.UserId = c.Session.UserId
|
|
hook.TeamId = c.Session.TeamId
|
|
|
|
var channel *model.Channel
|
|
if result := <-cchan; result.Err != nil {
|
|
c.Err = result.Err
|
|
return
|
|
} else {
|
|
channel = result.Data.(*model.Channel)
|
|
}
|
|
|
|
if !c.HasPermissionsToChannel(pchan, "createIncomingHook") {
|
|
if channel.Type != model.CHANNEL_OPEN || channel.TeamId != c.Session.TeamId {
|
|
c.LogAudit("fail - bad channel permissions")
|
|
return
|
|
}
|
|
}
|
|
|
|
if result := <-Srv.Store.Webhook().SaveIncoming(hook); result.Err != nil {
|
|
c.Err = result.Err
|
|
return
|
|
} else {
|
|
c.LogAudit("success")
|
|
rhook := result.Data.(*model.IncomingWebhook)
|
|
w.Write([]byte(rhook.ToJson()))
|
|
}
|
|
}
|
|
|
|
func deleteIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
if !utils.Cfg.ServiceSettings.EnableIncomingWebhooks {
|
|
c.Err = model.NewLocAppError("deleteIncomingHook", "api.webhook.delete_incoming.disabled.app_errror", nil, "")
|
|
c.Err.StatusCode = http.StatusNotImplemented
|
|
return
|
|
}
|
|
|
|
if *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations {
|
|
if !(c.IsSystemAdmin() || c.IsTeamAdmin()) {
|
|
c.Err = model.NewLocAppError("deleteIncomingHook", "api.command.admin_only.app_error", nil, "")
|
|
c.Err.StatusCode = http.StatusForbidden
|
|
return
|
|
}
|
|
}
|
|
|
|
c.LogAudit("attempt")
|
|
|
|
props := model.MapFromJson(r.Body)
|
|
|
|
id := props["id"]
|
|
if len(id) == 0 {
|
|
c.SetInvalidParam("deleteIncomingHook", "id")
|
|
return
|
|
}
|
|
|
|
if result := <-Srv.Store.Webhook().GetIncoming(id); result.Err != nil {
|
|
c.Err = result.Err
|
|
return
|
|
} else {
|
|
if c.Session.UserId != result.Data.(*model.IncomingWebhook).UserId && !c.IsTeamAdmin() {
|
|
c.LogAudit("fail - inappropriate permissions")
|
|
c.Err = model.NewLocAppError("deleteIncomingHook", "api.webhook.delete_incoming.permissions.app_errror", nil, "user_id="+c.Session.UserId)
|
|
return
|
|
}
|
|
}
|
|
|
|
if err := (<-Srv.Store.Webhook().DeleteIncoming(id, model.GetMillis())).Err; err != nil {
|
|
c.Err = err
|
|
return
|
|
}
|
|
|
|
c.LogAudit("success")
|
|
w.Write([]byte(model.MapToJson(props)))
|
|
}
|
|
|
|
func getIncomingHooks(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
if !utils.Cfg.ServiceSettings.EnableIncomingWebhooks {
|
|
c.Err = model.NewLocAppError("getIncomingHooks", "api.webhook.get_incoming.disabled.app_error", nil, "")
|
|
c.Err.StatusCode = http.StatusNotImplemented
|
|
return
|
|
}
|
|
|
|
if *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations {
|
|
if !(c.IsSystemAdmin() || c.IsTeamAdmin()) {
|
|
c.Err = model.NewLocAppError("getIncomingHooks", "api.command.admin_only.app_error", nil, "")
|
|
c.Err.StatusCode = http.StatusForbidden
|
|
return
|
|
}
|
|
}
|
|
|
|
if result := <-Srv.Store.Webhook().GetIncomingByTeam(c.Session.TeamId); result.Err != nil {
|
|
c.Err = result.Err
|
|
return
|
|
} else {
|
|
hooks := result.Data.([]*model.IncomingWebhook)
|
|
w.Write([]byte(model.IncomingWebhookListToJson(hooks)))
|
|
}
|
|
}
|
|
|
|
func createOutgoingHook(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
if !utils.Cfg.ServiceSettings.EnableOutgoingWebhooks {
|
|
c.Err = model.NewLocAppError("createOutgoingHook", "api.webhook.create_outgoing.disabled.app_error", nil, "")
|
|
c.Err.StatusCode = http.StatusNotImplemented
|
|
return
|
|
}
|
|
|
|
if *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations {
|
|
if !(c.IsSystemAdmin() || c.IsTeamAdmin()) {
|
|
c.Err = model.NewLocAppError("createOutgoingHook", "api.command.admin_only.app_error", nil, "")
|
|
c.Err.StatusCode = http.StatusForbidden
|
|
return
|
|
}
|
|
}
|
|
|
|
c.LogAudit("attempt")
|
|
|
|
hook := model.OutgoingWebhookFromJson(r.Body)
|
|
|
|
if hook == nil {
|
|
c.SetInvalidParam("createOutgoingHook", "webhook")
|
|
return
|
|
}
|
|
|
|
hook.CreatorId = c.Session.UserId
|
|
hook.TeamId = c.Session.TeamId
|
|
|
|
if len(hook.ChannelId) != 0 {
|
|
cchan := Srv.Store.Channel().Get(hook.ChannelId)
|
|
pchan := Srv.Store.Channel().CheckPermissionsTo(c.Session.TeamId, hook.ChannelId, c.Session.UserId)
|
|
|
|
var channel *model.Channel
|
|
if result := <-cchan; result.Err != nil {
|
|
c.Err = result.Err
|
|
return
|
|
} else {
|
|
channel = result.Data.(*model.Channel)
|
|
}
|
|
|
|
if channel.Type != model.CHANNEL_OPEN {
|
|
c.LogAudit("fail - not open channel")
|
|
}
|
|
|
|
if !c.HasPermissionsToChannel(pchan, "createOutgoingHook") {
|
|
if channel.Type != model.CHANNEL_OPEN || channel.TeamId != c.Session.TeamId {
|
|
c.LogAudit("fail - bad channel permissions")
|
|
return
|
|
}
|
|
}
|
|
} else if len(hook.TriggerWords) == 0 {
|
|
c.Err = model.NewLocAppError("createOutgoingHook", "api.webhook.create_outgoing.triggers.app_error", nil, "")
|
|
return
|
|
}
|
|
|
|
if result := <-Srv.Store.Webhook().SaveOutgoing(hook); result.Err != nil {
|
|
c.Err = result.Err
|
|
return
|
|
} else {
|
|
c.LogAudit("success")
|
|
rhook := result.Data.(*model.OutgoingWebhook)
|
|
w.Write([]byte(rhook.ToJson()))
|
|
}
|
|
}
|
|
|
|
func getOutgoingHooks(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
if !utils.Cfg.ServiceSettings.EnableOutgoingWebhooks {
|
|
c.Err = model.NewLocAppError("getOutgoingHooks", "api.webhook.get_outgoing.disabled.app_error", nil, "")
|
|
c.Err.StatusCode = http.StatusNotImplemented
|
|
return
|
|
}
|
|
|
|
if *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations {
|
|
if !(c.IsSystemAdmin() || c.IsTeamAdmin()) {
|
|
c.Err = model.NewLocAppError("getOutgoingHooks", "api.command.admin_only.app_error", nil, "")
|
|
c.Err.StatusCode = http.StatusForbidden
|
|
return
|
|
}
|
|
}
|
|
|
|
if result := <-Srv.Store.Webhook().GetOutgoingByTeam(c.Session.TeamId); result.Err != nil {
|
|
c.Err = result.Err
|
|
return
|
|
} else {
|
|
hooks := result.Data.([]*model.OutgoingWebhook)
|
|
w.Write([]byte(model.OutgoingWebhookListToJson(hooks)))
|
|
}
|
|
}
|
|
|
|
func deleteOutgoingHook(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
if !utils.Cfg.ServiceSettings.EnableOutgoingWebhooks {
|
|
c.Err = model.NewLocAppError("deleteOutgoingHook", "api.webhook.delete_outgoing.disabled.app_error", nil, "")
|
|
c.Err.StatusCode = http.StatusNotImplemented
|
|
return
|
|
}
|
|
|
|
if *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations {
|
|
if !(c.IsSystemAdmin() || c.IsTeamAdmin()) {
|
|
c.Err = model.NewLocAppError("deleteOutgoingHook", "api.command.admin_only.app_error", nil, "")
|
|
c.Err.StatusCode = http.StatusForbidden
|
|
return
|
|
}
|
|
}
|
|
|
|
c.LogAudit("attempt")
|
|
|
|
props := model.MapFromJson(r.Body)
|
|
|
|
id := props["id"]
|
|
if len(id) == 0 {
|
|
c.SetInvalidParam("deleteIncomingHook", "id")
|
|
return
|
|
}
|
|
|
|
if result := <-Srv.Store.Webhook().GetOutgoing(id); result.Err != nil {
|
|
c.Err = result.Err
|
|
return
|
|
} else {
|
|
if c.Session.UserId != result.Data.(*model.OutgoingWebhook).CreatorId && !c.IsTeamAdmin() {
|
|
c.LogAudit("fail - inappropriate permissions")
|
|
c.Err = model.NewLocAppError("deleteOutgoingHook", "api.webhook.delete_outgoing.permissions.app_error", nil, "user_id="+c.Session.UserId)
|
|
return
|
|
}
|
|
}
|
|
|
|
if err := (<-Srv.Store.Webhook().DeleteOutgoing(id, model.GetMillis())).Err; err != nil {
|
|
c.Err = err
|
|
return
|
|
}
|
|
|
|
c.LogAudit("success")
|
|
w.Write([]byte(model.MapToJson(props)))
|
|
}
|
|
|
|
func regenOutgoingHookToken(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
if !utils.Cfg.ServiceSettings.EnableIncomingWebhooks {
|
|
c.Err = model.NewLocAppError("regenOutgoingHookToken", "api.webhook.regen_outgoing_token.disabled.app_error", nil, "")
|
|
c.Err.StatusCode = http.StatusNotImplemented
|
|
return
|
|
}
|
|
|
|
if *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations {
|
|
if !(c.IsSystemAdmin() || c.IsTeamAdmin()) {
|
|
c.Err = model.NewLocAppError("regenOutgoingHookToken", "api.command.admin_only.app_error", nil, "")
|
|
c.Err.StatusCode = http.StatusForbidden
|
|
return
|
|
}
|
|
}
|
|
|
|
c.LogAudit("attempt")
|
|
|
|
props := model.MapFromJson(r.Body)
|
|
|
|
id := props["id"]
|
|
if len(id) == 0 {
|
|
c.SetInvalidParam("regenOutgoingHookToken", "id")
|
|
return
|
|
}
|
|
|
|
var hook *model.OutgoingWebhook
|
|
if result := <-Srv.Store.Webhook().GetOutgoing(id); result.Err != nil {
|
|
c.Err = result.Err
|
|
return
|
|
} else {
|
|
hook = result.Data.(*model.OutgoingWebhook)
|
|
|
|
if c.Session.TeamId != hook.TeamId && c.Session.UserId != hook.CreatorId && !c.IsTeamAdmin() {
|
|
c.LogAudit("fail - inappropriate permissions")
|
|
c.Err = model.NewLocAppError("regenOutgoingHookToken", "api.webhook.regen_outgoing_token.permissions.app_error", nil, "user_id="+c.Session.UserId)
|
|
return
|
|
}
|
|
}
|
|
|
|
hook.Token = model.NewId()
|
|
|
|
if result := <-Srv.Store.Webhook().UpdateOutgoing(hook); result.Err != nil {
|
|
c.Err = result.Err
|
|
return
|
|
} else {
|
|
w.Write([]byte(result.Data.(*model.OutgoingWebhook).ToJson()))
|
|
}
|
|
}
|