* Posts * Add missing translation * Fix internal store marshaling * [MM-22051] Remove To/From JSON (Channels) (#18116) * Channels * Channel members * ChannelSearch * Channel categories, list, sidebar, stats, view * Fix conversions * [MM-22051] Remove To/From JSON (Users) (#18121) * User related structs * Fix return * Team related structures (#18127) * [MM-22051] Remove To/From JSON (Status, Bot, Reaction, Thread, FileInfo) (#18130) * Status * Bot * Reaction * Thread * FileInfo * Some fixes * Translations update from Weblate (#18143) * Translated using Weblate (German) Currently translated at 100.0% (2309 of 2309 strings) Translation: mattermost-languages-shipped/mattermost-server Translate-URL: https://translate.mattermost.com/projects/mattermost/mattermost-server_master/de/ * Translated using Weblate (Turkish) Currently translated at 100.0% (2309 of 2309 strings) Translation: mattermost-languages-shipped/mattermost-server Translate-URL: https://translate.mattermost.com/projects/mattermost/mattermost-server_master/tr/ * Translated using Weblate (Hungarian) Currently translated at 100.0% (2309 of 2309 strings) Translation: mattermost-languages-shipped/mattermost-server Translate-URL: https://translate.mattermost.com/projects/mattermost/mattermost-server_master/hu/ * Update translation files Updated by "Cleanup translation files" hook in Weblate. Translation: mattermost-languages-shipped/mattermost-server Translate-URL: https://translate.mattermost.com/projects/mattermost/mattermost-server_master/ Update translation files Updated by "Cleanup translation files" hook in Weblate. Translation: mattermost-languages-shipped/mattermost-server Translate-URL: https://translate.mattermost.com/projects/mattermost/mattermost-server_master/ Update translation files Updated by "Cleanup translation files" hook in Weblate. Translation: mattermost-languages-shipped/mattermost-server Translate-URL: https://translate.mattermost.com/projects/mattermost/mattermost-server_master/ Update translation files Updated by "Cleanup translation files" hook in Weblate. Translation: mattermost-languages-shipped/mattermost-server Translate-URL: https://translate.mattermost.com/projects/mattermost/mattermost-server_master/ * Translated using Weblate (German) Currently translated at 100.0% (2301 of 2301 strings) Translation: mattermost-languages-shipped/mattermost-server Translate-URL: https://translate.mattermost.com/projects/mattermost/mattermost-server_master/de/ * Translated using Weblate (Turkish) Currently translated at 100.0% (2301 of 2301 strings) Translation: mattermost-languages-shipped/mattermost-server Translate-URL: https://translate.mattermost.com/projects/mattermost/mattermost-server_master/tr/ * Translated using Weblate (Hungarian) Currently translated at 100.0% (2301 of 2301 strings) Translation: mattermost-languages-shipped/mattermost-server Translate-URL: https://translate.mattermost.com/projects/mattermost/mattermost-server_master/hu/ * Translated using Weblate (English (Australia)) Currently translated at 100.0% (2301 of 2301 strings) Translation: mattermost-languages-shipped/mattermost-server Translate-URL: https://translate.mattermost.com/projects/mattermost/mattermost-server_master/en_AU/ * Translated using Weblate (Bulgarian) Currently translated at 100.0% (2301 of 2301 strings) Translation: mattermost-languages-shipped/mattermost-server Translate-URL: https://translate.mattermost.com/projects/mattermost/mattermost-server_master/bg/ * Translated using Weblate (Japanese) Currently translated at 100.0% (2301 of 2301 strings) Translation: mattermost-languages-shipped/mattermost-server Translate-URL: https://translate.mattermost.com/projects/mattermost/mattermost-server_master/ja/ * Translated using Weblate (Chinese (Simplified)) Currently translated at 100.0% (2301 of 2301 strings) Translation: mattermost-languages-shipped/mattermost-server Translate-URL: https://translate.mattermost.com/projects/mattermost/mattermost-server_master/zh_Hans/ Co-authored-by: JtheBAB <srast@bioc.uzh.ch> Co-authored-by: Kaya Zeren <kayazeren@gmail.com> Co-authored-by: Tóth Csaba // Online ERP Hungary Kft <csaba.toth@online-erp.hu> Co-authored-by: Matthew Williams <Matthew.Williams@outlook.com.au> Co-authored-by: Nikolai Zahariev <nikolaiz@yahoo.com> Co-authored-by: kaakaa <stooner.hoe@gmail.com> Co-authored-by: aeomin <lin@aeomin.net> Co-authored-by: Weblate (bot) <hosted@weblate.org> Co-authored-by: JtheBAB <srast@bioc.uzh.ch> Co-authored-by: Kaya Zeren <kayazeren@gmail.com> Co-authored-by: Tóth Csaba // Online ERP Hungary Kft <csaba.toth@online-erp.hu> Co-authored-by: Matthew Williams <Matthew.Williams@outlook.com.au> Co-authored-by: Nikolai Zahariev <nikolaiz@yahoo.com> Co-authored-by: kaakaa <stooner.hoe@gmail.com> Co-authored-by: aeomin <lin@aeomin.net> * [MM-22051] Remove To/From JSON methods from model (#18138) * Scheme * Role * Session * Config * Status * Fix logic * Emoji * GuestsInvite * Group * Command * ClusterInfo * License * Job * System * Plugin * Command2 * IncomingWebhook * OutgoingWebhook * Fix tests * Update traslation * Some fixes * Add missing return * Simplify * Make Config.ToJSONFiltered() return []byte * Make Busy.ToJSON() return []byte * Include error in log * Split logic * [MM-22051] Remove To/From JSON (final) (#18150) * SwitchRequest * PluginEventData * Permalink * PushNotification * SuggestCommand * PluginsResponse * WebSocketMessage * RemoteCluster * SharedChannel * PluginStatuses * InitialLoad * ClusterDiscovery * ClusterStats * MfaSecret * GroupSyncable * SAML * WebSocketRequest * TypingRequest * SecurityBulletin * OAuthApp * IntegrationAction * DataRetention * Preference * FileInfoList * Compliance * Preferences * FileInfoSearchResults * TermsOfService * InstallMarketplacePluginRequest * GitLabUser * UploadSessions * Remove unused helpers * Fix tests * [MM-23280] Fix linting for ToJSON/FromJSON (#18153) * SwitchRequest * PluginEventData * Permalink * PushNotification * SuggestCommand * PluginsResponse * WebSocketMessage * RemoteCluster * SharedChannel * PluginStatuses * InitialLoad * ClusterDiscovery * ClusterStats * MfaSecret * GroupSyncable * SAML * WebSocketRequest * TypingRequest * SecurityBulletin * OAuthApp * IntegrationAction * DataRetention * Preference * FileInfoList * Compliance * Preferences * FileInfoSearchResults * TermsOfService * InstallMarketplacePluginRequest * GitLabUser * UploadSessions * Remove unused helpers * Fix tests * Fix linting for ToJSON/FromJSON * Fix conversions Co-authored-by: Weblate (bot) <hosted@weblate.org> Co-authored-by: JtheBAB <srast@bioc.uzh.ch> Co-authored-by: Kaya Zeren <kayazeren@gmail.com> Co-authored-by: Tóth Csaba // Online ERP Hungary Kft <csaba.toth@online-erp.hu> Co-authored-by: Matthew Williams <Matthew.Williams@outlook.com.au> Co-authored-by: Nikolai Zahariev <nikolaiz@yahoo.com> Co-authored-by: kaakaa <stooner.hoe@gmail.com> Co-authored-by: aeomin <lin@aeomin.net> Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
275 строки
7.7 KiB
Go
275 строки
7.7 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/mattermost/mattermost-server/v6/model"
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
|
"github.com/mattermost/mattermost-server/v6/store"
|
|
"github.com/mattermost/mattermost-server/v6/utils"
|
|
)
|
|
|
|
func (a *App) GetRole(id string) (*model.Role, *model.AppError) {
|
|
role, err := a.Srv().Store.Role().Get(id)
|
|
if err != nil {
|
|
var nfErr *store.ErrNotFound
|
|
switch {
|
|
case errors.As(err, &nfErr):
|
|
return nil, model.NewAppError("GetRole", "app.role.get.app_error", nil, nfErr.Error(), http.StatusNotFound)
|
|
default:
|
|
return nil, model.NewAppError("GetRole", "app.role.get.app_error", nil, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
appErr := a.Srv().mergeChannelHigherScopedPermissions([]*model.Role{role})
|
|
if appErr != nil {
|
|
return nil, appErr
|
|
}
|
|
|
|
return role, nil
|
|
}
|
|
|
|
func (s *Server) GetRoleByName(ctx context.Context, name string) (*model.Role, *model.AppError) {
|
|
role, nErr := s.Store.Role().GetByName(ctx, name)
|
|
if nErr != nil {
|
|
var nfErr *store.ErrNotFound
|
|
switch {
|
|
case errors.As(nErr, &nfErr):
|
|
return nil, model.NewAppError("GetRoleByName", "app.role.get_by_name.app_error", nil, nfErr.Error(), http.StatusNotFound)
|
|
default:
|
|
return nil, model.NewAppError("GetRoleByName", "app.role.get_by_name.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
err := s.mergeChannelHigherScopedPermissions([]*model.Role{role})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return role, nil
|
|
}
|
|
|
|
func (a *App) GetRoleByName(ctx context.Context, name string) (*model.Role, *model.AppError) {
|
|
return a.Srv().GetRoleByName(ctx, name)
|
|
}
|
|
|
|
func (a *App) GetRolesByNames(names []string) ([]*model.Role, *model.AppError) {
|
|
roles, nErr := a.Srv().Store.Role().GetByNames(names)
|
|
if nErr != nil {
|
|
return nil, model.NewAppError("GetRolesByNames", "app.role.get_by_names.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
|
}
|
|
|
|
err := a.mergeChannelHigherScopedPermissions(roles)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return roles, nil
|
|
}
|
|
|
|
// mergeChannelHigherScopedPermissions updates the permissions based on the role type, whether the permission is
|
|
// moderated, and the value of the permission on the higher-scoped scheme.
|
|
func (s *Server) mergeChannelHigherScopedPermissions(roles []*model.Role) *model.AppError {
|
|
var higherScopeNamesToQuery []string
|
|
|
|
for _, role := range roles {
|
|
if role.SchemeManaged {
|
|
higherScopeNamesToQuery = append(higherScopeNamesToQuery, role.Name)
|
|
}
|
|
}
|
|
|
|
if len(higherScopeNamesToQuery) == 0 {
|
|
return nil
|
|
}
|
|
|
|
higherScopedPermissionsMap, err := s.Store.Role().ChannelHigherScopedPermissions(higherScopeNamesToQuery)
|
|
if err != nil {
|
|
return model.NewAppError("mergeChannelHigherScopedPermissions", "app.role.get_by_names.app_error", nil, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
|
|
for _, role := range roles {
|
|
if role.SchemeManaged {
|
|
if higherScopedPermissions, ok := higherScopedPermissionsMap[role.Name]; ok {
|
|
role.MergeChannelHigherScopedPermissions(higherScopedPermissions)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// mergeChannelHigherScopedPermissions updates the permissions based on the role type, whether the permission is
|
|
// moderated, and the value of the permission on the higher-scoped scheme.
|
|
func (a *App) mergeChannelHigherScopedPermissions(roles []*model.Role) *model.AppError {
|
|
return a.Srv().mergeChannelHigherScopedPermissions(roles)
|
|
}
|
|
|
|
func (a *App) PatchRole(role *model.Role, patch *model.RolePatch) (*model.Role, *model.AppError) {
|
|
// If patch is a no-op then short-circuit the store.
|
|
if patch.Permissions != nil && reflect.DeepEqual(*patch.Permissions, role.Permissions) {
|
|
return role, nil
|
|
}
|
|
|
|
role.Patch(patch)
|
|
role, err := a.UpdateRole(role)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
a.sendUpdatedRoleEvent(role)
|
|
|
|
return role, err
|
|
}
|
|
|
|
func (a *App) CreateRole(role *model.Role) (*model.Role, *model.AppError) {
|
|
role.Id = ""
|
|
role.CreateAt = 0
|
|
role.UpdateAt = 0
|
|
role.DeleteAt = 0
|
|
role.BuiltIn = false
|
|
role.SchemeManaged = false
|
|
|
|
var err error
|
|
role, err = a.Srv().Store.Role().Save(role)
|
|
if err != nil {
|
|
var invErr *store.ErrInvalidInput
|
|
switch {
|
|
case errors.As(err, &invErr):
|
|
return nil, model.NewAppError("CreateRole", "app.role.save.invalid_role.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
|
default:
|
|
return nil, model.NewAppError("CreateRole", "app.role.save.insert.app_error", nil, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
return role, nil
|
|
}
|
|
|
|
func (a *App) UpdateRole(role *model.Role) (*model.Role, *model.AppError) {
|
|
savedRole, err := a.Srv().Store.Role().Save(role)
|
|
if err != nil {
|
|
var invErr *store.ErrInvalidInput
|
|
switch {
|
|
case errors.As(err, &invErr):
|
|
return nil, model.NewAppError("UpdateRole", "app.role.save.invalid_role.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
|
default:
|
|
return nil, model.NewAppError("UpdateRole", "app.role.save.insert.app_error", nil, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
builtInChannelRoles := []string{
|
|
model.ChannelGuestRoleId,
|
|
model.ChannelUserRoleId,
|
|
model.ChannelAdminRoleId,
|
|
}
|
|
|
|
builtInRolesMinusChannelRoles := append(utils.RemoveStringsFromSlice(model.BuiltInSchemeManagedRoleIDs, builtInChannelRoles...), model.NewSystemRoleIDs...)
|
|
|
|
if utils.StringInSlice(savedRole.Name, builtInRolesMinusChannelRoles) {
|
|
return savedRole, nil
|
|
}
|
|
|
|
var roleRetrievalFunc func() ([]*model.Role, *model.AppError)
|
|
|
|
if utils.StringInSlice(savedRole.Name, builtInChannelRoles) {
|
|
roleRetrievalFunc = func() ([]*model.Role, *model.AppError) {
|
|
roles, nErr := a.Srv().Store.Role().AllChannelSchemeRoles()
|
|
if nErr != nil {
|
|
return nil, model.NewAppError("UpdateRole", "app.role.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
|
}
|
|
|
|
return roles, nil
|
|
}
|
|
} else {
|
|
roleRetrievalFunc = func() ([]*model.Role, *model.AppError) {
|
|
roles, nErr := a.Srv().Store.Role().ChannelRolesUnderTeamRole(savedRole.Name)
|
|
if nErr != nil {
|
|
return nil, model.NewAppError("UpdateRole", "app.role.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
|
}
|
|
|
|
return roles, nil
|
|
}
|
|
}
|
|
|
|
impactedRoles, appErr := roleRetrievalFunc()
|
|
if appErr != nil {
|
|
return nil, appErr
|
|
}
|
|
impactedRoles = append(impactedRoles, role)
|
|
|
|
appErr = a.mergeChannelHigherScopedPermissions(impactedRoles)
|
|
if appErr != nil {
|
|
return nil, appErr
|
|
}
|
|
|
|
for _, ir := range impactedRoles {
|
|
if ir.Name != role.Name {
|
|
a.sendUpdatedRoleEvent(ir)
|
|
}
|
|
}
|
|
|
|
return savedRole, nil
|
|
}
|
|
|
|
func (a *App) CheckRolesExist(roleNames []string) *model.AppError {
|
|
roles, err := a.GetRolesByNames(roleNames)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, name := range roleNames {
|
|
nameFound := false
|
|
for _, role := range roles {
|
|
if name == role.Name {
|
|
nameFound = true
|
|
break
|
|
}
|
|
}
|
|
if !nameFound {
|
|
return model.NewAppError("CheckRolesExist", "app.role.check_roles_exist.role_not_found", nil, "role="+name, http.StatusBadRequest)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *App) sendUpdatedRoleEvent(role *model.Role) {
|
|
message := model.NewWebSocketEvent(model.WebsocketEventRoleUpdated, "", "", "", nil)
|
|
roleJSON, jsonErr := json.Marshal(role)
|
|
if jsonErr != nil {
|
|
mlog.Warn("Failed to encode role to JSON", mlog.Err(jsonErr))
|
|
}
|
|
message.Add("role", string(roleJSON))
|
|
|
|
a.Srv().Go(func() {
|
|
a.Publish(message)
|
|
})
|
|
}
|
|
|
|
func RemoveRoles(rolesToRemove []string, roles string) string {
|
|
roleList := strings.Fields(roles)
|
|
newRoles := make([]string, 0)
|
|
|
|
for _, role := range roleList {
|
|
shouldRemove := false
|
|
for _, roleToRemove := range rolesToRemove {
|
|
if role == roleToRemove {
|
|
shouldRemove = true
|
|
break
|
|
}
|
|
}
|
|
if !shouldRemove {
|
|
newRoles = append(newRoles, role)
|
|
}
|
|
}
|
|
|
|
return strings.Join(newRoles, " ")
|
|
}
|