* MM-22212: Read non-moderated permissions from higher-scoped scheme. * MM-2212: Corrects test count in comment. * MM-22212: Adds godoc comment. * MM-2212: Switches to the channel roles check in a few more places. * MM-22212: Refactors and fixes. * MM-22212: Reverts change, no longer required. * MM-22212: Removes translation. * MM-22212: Un-comments merged new permission. * MM-22212: Un-comments merged new permission. * MM-22212: Performance tweak. * MM-22212: Fixes some fmting. * MM-22212: Add unit test for newly-added store methods. * MM-22212: Renames app method. * MM-22212: Re-uses existing function to find string in slice. * MM-22212: Keeps 'higher-scoped' terminology for consistency. * MM-22212: Refactors based on PR feedback. * MM-22212: Fix for some bad merging. * MM-22212: Renamed some things. * MM-22212: Use an 'else' instead of a 'continue' for readability. * MM-22212: Caches (*SqlRoleStore).ChannelRolesUnderTeamRole. * MM-22212: Adds mock to new cache store. * MM-22212: Adds missing open tracing app layer methods. * MM-22212: Adds migration to add moderated permissions to channel_admin if present on channel_user. * MM-22212: Migrates team schemes. Removes unused AppError. * MM-22212: Fix for for if. * MM-22212: Fixes iterator. * MM-22212: Updates open tracing generated methods. * MM-22212: Fix mocks. * MM-22212: Change migration key name. * MM-22212: Switched to data structure from other branch. * MM-22212: Fixes tests after adding 'use_channel_mentions' to the channel_admin role. * MM-22212: Adds tracking of channel moderation. * Revert "MM-22212: Adds tracking of channel moderation." This reverts commit 23689efa22c112e4ba37f6a212535dd7ebfb63db. * MM-22212: Switch some functions to methods and vice versa. * MM-22212: Fix for refactor bug not notifiying websocket about changed role. * MM-22212: Adds test for public/private 'manage_members' handling. * MM-22122 Fix manage channel members edge case for public and private channels (#14049) * MM-22212: Adds moderated permission to team_admin. * MM-22212: Updates migration. * MM-22212: Revert unnecessary update to default roles. * Add channel scheme updated event when channel scheme is deleted or created (#14057) * MM-22212: Adds newline. * MM-22212: Migration fix. * MM-22212: Fix for migration. * MM-22212: Test fix. Co-authored-by: Farhan Munshi <3207297+fm2munsh@users.noreply.github.com>
207 строки
4.8 KiB
Go
207 строки
4.8 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"net/http"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/mattermost/mattermost-server/v5/model"
|
|
"github.com/mattermost/mattermost-server/v5/utils"
|
|
)
|
|
|
|
func (a *App) GetRole(id string) (*model.Role, *model.AppError) {
|
|
return a.Srv().Store.Role().Get(id)
|
|
}
|
|
|
|
func (a *App) GetAllRoles() ([]*model.Role, *model.AppError) {
|
|
return a.Srv().Store.Role().GetAll()
|
|
}
|
|
|
|
func (a *App) GetRoleByName(name string) (*model.Role, *model.AppError) {
|
|
role, err := a.Srv().Store.Role().GetByName(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = a.mergeChannelHigherScopedPermissions([]*model.Role{role})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return role, nil
|
|
}
|
|
|
|
func (a *App) GetRolesByNames(names []string) ([]*model.Role, *model.AppError) {
|
|
roles, err := a.Srv().Store.Role().GetByNames(names)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
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 (a *App) 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 := a.Srv().Store.Role().ChannelHigherScopedPermissions(higherScopeNamesToQuery)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, role := range roles {
|
|
if role.SchemeManaged {
|
|
if higherScopedPermissions, ok := higherScopedPermissionsMap[role.Name]; ok {
|
|
role.MergeChannelHigherScopedPermissions(higherScopedPermissions)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
|
|
return a.Srv().Store.Role().Save(role)
|
|
|
|
}
|
|
|
|
func (a *App) UpdateRole(role *model.Role) (*model.Role, *model.AppError) {
|
|
savedRole, err := a.Srv().Store.Role().Save(role)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
builtInChannelRoles := []string{
|
|
model.CHANNEL_GUEST_ROLE_ID,
|
|
model.CHANNEL_USER_ROLE_ID,
|
|
model.CHANNEL_ADMIN_ROLE_ID,
|
|
}
|
|
|
|
builtInRolesMinusChannelRoles := utils.RemoveStringsFromSlice(model.BuiltInSchemeManagedRoleIDs, builtInChannelRoles...)
|
|
|
|
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) {
|
|
return a.Srv().Store.Role().AllChannelSchemeRoles()
|
|
}
|
|
} else {
|
|
roleRetrievalFunc = func() ([]*model.Role, *model.AppError) {
|
|
return a.Srv().Store.Role().ChannelRolesUnderTeamRole(savedRole.Name)
|
|
}
|
|
}
|
|
|
|
impactedRoles, err := roleRetrievalFunc()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
impactedRoles = append(impactedRoles, role)
|
|
|
|
err = a.mergeChannelHigherScopedPermissions(impactedRoles)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, ir := range impactedRoles {
|
|
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.WEBSOCKET_EVENT_ROLE_UPDATED, "", "", "", nil)
|
|
message.Add("role", role.ToJson())
|
|
|
|
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, " ")
|
|
}
|