[MM-25648] api4: add user/bot convert endpoints (#14877)

* api4: add user/bot convert endpoints

* api4: add convert user/bot to local mode

* api4: fix linting issues

* api4/bot: reflect review comments

* api4: update convert user endpoint paths

* remove shadow decl

* fix translation problems
Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2020-07-17 10:00:43 +03:00
коммит произвёл GitHub
родитель cb89c53dcc
Коммит 6a4e3293f8
11 изменённых файлов: 258 добавлений и 0 удалений

Просмотреть файл

@@ -21,6 +21,7 @@ func (api *API) InitBot() {
api.BaseRoutes.Bots.Handle("", api.ApiSessionRequired(getBots)).Methods("GET")
api.BaseRoutes.Bot.Handle("/disable", api.ApiSessionRequired(disableBot)).Methods("POST")
api.BaseRoutes.Bot.Handle("/enable", api.ApiSessionRequired(enableBot)).Methods("POST")
api.BaseRoutes.Bot.Handle("/convert_to_user", api.ApiSessionRequired(convertBotToUser)).Methods("POST")
api.BaseRoutes.Bot.Handle("/assign/{user_id:[A-Za-z0-9]+}", api.ApiSessionRequired(assignBot)).Methods("POST")
api.BaseRoutes.Bot.Handle("/icon", api.ApiSessionRequiredTrustRequester(getBotIconImage)).Methods("GET")
@@ -378,3 +379,46 @@ func deleteBotIconImage(c *Context, w http.ResponseWriter, r *http.Request) {
ReturnStatusOK(w)
}
func convertBotToUser(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireBotUserId()
if c.Err != nil {
return
}
bot, err := c.App.GetBot(c.Params.BotUserId, false)
if err != nil {
c.Err = err
return
}
userPatch := model.UserPatchFromJson(r.Body)
if userPatch == nil || userPatch.Password == nil || *userPatch.Password == "" {
c.SetInvalidParam("userPatch")
return
}
systemAdmin, _ := strconv.ParseBool(r.URL.Query().Get("set_system_admin"))
auditRec := c.MakeAuditRecord("convertBotToUser", audit.Fail)
defer c.LogAuditRec(auditRec)
auditRec.AddMeta("bot", bot)
auditRec.AddMeta("userPatch", userPatch)
auditRec.AddMeta("set_system_admin", systemAdmin)
if c.Params.UserId != c.App.Session().UserId && !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
return
}
user, err := c.App.ConvertBotToUser(bot, userPatch, systemAdmin)
if err != nil {
c.Err = err
return
}
auditRec.Success()
auditRec.AddMeta("convertedTo", user)
w.Write([]byte(user.ToJson()))
}