XYZ-37: Advanced Permissions Phase 1 Backend. (#8159)
* XYZ-13: Update Permission and Role structs to new design. * XYZ-10: Role store. * XYZ-9/XYZ-44: Roles API endpoints and WebSocket message. * XYZ-8: Switch server permissions checks to store backed roles. * XYZ-58: Proper validation of roles where required. * XYZ-11/XYZ-55: Migration to store backed roles from policy config. * XYZ-37: Update unit tests to work with database roles. * XYZ-56: Remove the "guest" role. * Changes to SetDefaultRolesFromConfig. * Short-circuit the store if nothing has changed. * Address first round of review comments. * Address second round of review comments.
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
1c7f25773a
Коммит
e1cd646135
88
app/role.go
88
app/role.go
@@ -1,19 +1,91 @@
|
||||
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
||||
// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package app
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/utils"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (a *App) Role(id string) *model.Role {
|
||||
return a.roles[id]
|
||||
func (a *App) GetRole(id string) (*model.Role, *model.AppError) {
|
||||
if result := <-a.Srv.Store.Role().Get(id); result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.(*model.Role), nil
|
||||
}
|
||||
}
|
||||
|
||||
// Updates the roles based on the app config and the global license check. You may need to invoke
|
||||
// this when license changes are made.
|
||||
func (a *App) SetDefaultRolesBasedOnConfig() {
|
||||
a.roles = utils.DefaultRolesBasedOnConfig(a.Config())
|
||||
func (a *App) GetRoleByName(name string) (*model.Role, *model.AppError) {
|
||||
if result := <-a.Srv.Store.Role().GetByName(name); result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.(*model.Role), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) GetRolesByNames(names []string) ([]*model.Role, *model.AppError) {
|
||||
if result := <-a.Srv.Store.Role().GetByNames(names); result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.([]*model.Role), 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) UpdateRole(role *model.Role) (*model.Role, *model.AppError) {
|
||||
if result := <-a.Srv.Store.Role().Save(role); result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
a.sendUpdatedRoleEvent(role)
|
||||
|
||||
return role, 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.Go(func() {
|
||||
a.Publish(message)
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user