[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
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
cb89c53dcc
Коммит
6a4e3293f8
@@ -61,6 +61,8 @@ type AppIface interface {
|
||||
ChannelMembersMinusGroupMembers(channelID string, groupIDs []string, page, perPage int) ([]*model.UserWithGroups, int64, *model.AppError)
|
||||
// ClientConfigWithComputed gets the configuration in a format suitable for sending to the client.
|
||||
ClientConfigWithComputed() map[string]string
|
||||
// ConvertBotToUser converts a bot to user.
|
||||
ConvertBotToUser(bot *model.Bot, userPatch *model.UserPatch, sysadmin bool) (*model.User, *model.AppError)
|
||||
// ConvertUserToBot converts a user to bot.
|
||||
ConvertUserToBot(user *model.User) (*model.Bot, *model.AppError)
|
||||
// CreateBot creates the given bot and corresponding user.
|
||||
|
||||
@@ -1362,6 +1362,28 @@ func (a *OpenTracingAppLayer) Config() *model.Config {
|
||||
return resultVar0
|
||||
}
|
||||
|
||||
func (a *OpenTracingAppLayer) ConvertBotToUser(bot *model.Bot, userPatch *model.UserPatch, sysadmin bool) (*model.User, *model.AppError) {
|
||||
origCtx := a.ctx
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.ConvertBotToUser")
|
||||
|
||||
a.ctx = newCtx
|
||||
a.app.Srv().Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
a.app.Srv().Store.SetContext(origCtx)
|
||||
a.ctx = origCtx
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
resultVar0, resultVar1 := a.app.ConvertBotToUser(bot, userPatch, sysadmin)
|
||||
|
||||
if resultVar1 != nil {
|
||||
span.LogFields(spanlog.Error(resultVar1))
|
||||
ext.Error.Set(span, true)
|
||||
}
|
||||
|
||||
return resultVar0, resultVar1
|
||||
}
|
||||
|
||||
func (a *OpenTracingAppLayer) ConvertUserToBot(user *model.User) (*model.Bot, *model.AppError) {
|
||||
origCtx := a.ctx
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.ConvertUserToBot")
|
||||
|
||||
37
app/user.go
37
app/user.go
@@ -2140,3 +2140,40 @@ func (a *App) invalidateUserCacheAndPublish(userId string) {
|
||||
func (a *App) GetKnownUsers(userID string) ([]string, *model.AppError) {
|
||||
return a.Srv().Store.User().GetKnownUsers(userID)
|
||||
}
|
||||
|
||||
// ConvertBotToUser converts a bot to user.
|
||||
func (a *App) ConvertBotToUser(bot *model.Bot, userPatch *model.UserPatch, sysadmin bool) (*model.User, *model.AppError) {
|
||||
user, err := a.Srv().Store.User().Get(bot.UserId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if sysadmin && !user.IsInRole(model.SYSTEM_ADMIN_ROLE_ID) {
|
||||
_, err = a.UpdateUserRoles(
|
||||
user.Id,
|
||||
fmt.Sprintf("%s %s", user.Roles, model.SYSTEM_ADMIN_ROLE_ID),
|
||||
false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
user.Patch(userPatch)
|
||||
|
||||
user, err = a.UpdateUser(user, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = a.UpdatePassword(user, *userPatch.Password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
appErr := a.Srv().Store.Bot().PermanentDelete(bot.UserId)
|
||||
if appErr != nil {
|
||||
return nil, model.NewAppError("ConvertBotToUser", "", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user