* 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.
217 строки
5.6 KiB
Go
217 строки
5.6 KiB
Go
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
l4g "github.com/alecthomas/log4go"
|
|
"github.com/mattermost/mattermost-server/model"
|
|
)
|
|
|
|
func (a *App) SessionHasPermissionTo(session model.Session, permission *model.Permission) bool {
|
|
if !a.RolesGrantPermission(session.GetUserRoles(), permission.Id) {
|
|
a.ClearSessionCacheForUser(session.UserId)
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
/// DO NOT USE: LEGACY
|
|
func (a *App) SessionHasPermissionToTeam(session model.Session, teamId string, permission *model.Permission) bool {
|
|
if teamId == "" {
|
|
return false
|
|
}
|
|
|
|
teamMember := session.GetTeamByTeamId(teamId)
|
|
if teamMember != nil {
|
|
if a.RolesGrantPermission(teamMember.GetRoles(), permission.Id) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return a.RolesGrantPermission(session.GetUserRoles(), permission.Id)
|
|
}
|
|
|
|
func (a *App) SessionHasPermissionToChannel(session model.Session, channelId string, permission *model.Permission) bool {
|
|
if channelId == "" {
|
|
return false
|
|
}
|
|
|
|
cmc := a.Srv.Store.Channel().GetAllChannelMembersForUser(session.UserId, true)
|
|
|
|
var channelRoles []string
|
|
if cmcresult := <-cmc; cmcresult.Err == nil {
|
|
ids := cmcresult.Data.(map[string]string)
|
|
if roles, ok := ids[channelId]; ok {
|
|
channelRoles = strings.Fields(roles)
|
|
if a.RolesGrantPermission(channelRoles, permission.Id) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
channel, err := a.GetChannel(channelId)
|
|
if err == nil && channel.TeamId != "" {
|
|
return a.SessionHasPermissionToTeam(session, channel.TeamId, permission)
|
|
} else if err != nil && err.StatusCode == http.StatusNotFound {
|
|
return false
|
|
}
|
|
|
|
return a.SessionHasPermissionTo(session, permission)
|
|
}
|
|
|
|
func (a *App) SessionHasPermissionToChannelByPost(session model.Session, postId string, permission *model.Permission) bool {
|
|
var channelMember *model.ChannelMember
|
|
if result := <-a.Srv.Store.Channel().GetMemberForPost(postId, session.UserId); result.Err == nil {
|
|
channelMember = result.Data.(*model.ChannelMember)
|
|
|
|
if a.RolesGrantPermission(channelMember.GetRoles(), permission.Id) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
if result := <-a.Srv.Store.Channel().GetForPost(postId); result.Err == nil {
|
|
channel := result.Data.(*model.Channel)
|
|
return a.SessionHasPermissionToTeam(session, channel.TeamId, permission)
|
|
}
|
|
|
|
return a.SessionHasPermissionTo(session, permission)
|
|
}
|
|
|
|
func (a *App) SessionHasPermissionToUser(session model.Session, userId string) bool {
|
|
if userId == "" {
|
|
return false
|
|
}
|
|
|
|
if session.UserId == userId {
|
|
return true
|
|
}
|
|
|
|
if a.SessionHasPermissionTo(session, model.PERMISSION_EDIT_OTHER_USERS) {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (a *App) SessionHasPermissionToPost(session model.Session, postId string, permission *model.Permission) bool {
|
|
post, err := a.GetSinglePost(postId)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
if post.UserId == session.UserId {
|
|
return true
|
|
}
|
|
|
|
return a.SessionHasPermissionToChannel(session, post.ChannelId, permission)
|
|
}
|
|
|
|
func (a *App) HasPermissionTo(askingUserId string, permission *model.Permission) bool {
|
|
user, err := a.GetUser(askingUserId)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
roles := user.GetRoles()
|
|
|
|
return a.RolesGrantPermission(roles, permission.Id)
|
|
}
|
|
|
|
func (a *App) HasPermissionToTeam(askingUserId string, teamId string, permission *model.Permission) bool {
|
|
if teamId == "" || askingUserId == "" {
|
|
return false
|
|
}
|
|
|
|
teamMember, err := a.GetTeamMember(teamId, askingUserId)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
roles := teamMember.GetRoles()
|
|
|
|
if a.RolesGrantPermission(roles, permission.Id) {
|
|
return true
|
|
}
|
|
|
|
return a.HasPermissionTo(askingUserId, permission)
|
|
}
|
|
|
|
func (a *App) HasPermissionToChannel(askingUserId string, channelId string, permission *model.Permission) bool {
|
|
if channelId == "" || askingUserId == "" {
|
|
return false
|
|
}
|
|
|
|
channelMember, err := a.GetChannelMember(channelId, askingUserId)
|
|
if err == nil {
|
|
roles := channelMember.GetRoles()
|
|
if a.RolesGrantPermission(roles, permission.Id) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
var channel *model.Channel
|
|
channel, err = a.GetChannel(channelId)
|
|
if err == nil {
|
|
return a.HasPermissionToTeam(askingUserId, channel.TeamId, permission)
|
|
}
|
|
|
|
return a.HasPermissionTo(askingUserId, permission)
|
|
}
|
|
|
|
func (a *App) HasPermissionToChannelByPost(askingUserId string, postId string, permission *model.Permission) bool {
|
|
var channelMember *model.ChannelMember
|
|
if result := <-a.Srv.Store.Channel().GetMemberForPost(postId, askingUserId); result.Err == nil {
|
|
channelMember = result.Data.(*model.ChannelMember)
|
|
|
|
if a.RolesGrantPermission(channelMember.GetRoles(), permission.Id) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
if result := <-a.Srv.Store.Channel().GetForPost(postId); result.Err == nil {
|
|
channel := result.Data.(*model.Channel)
|
|
return a.HasPermissionToTeam(askingUserId, channel.TeamId, permission)
|
|
}
|
|
|
|
return a.HasPermissionTo(askingUserId, permission)
|
|
}
|
|
|
|
func (a *App) HasPermissionToUser(askingUserId string, userId string) bool {
|
|
if askingUserId == userId {
|
|
return true
|
|
}
|
|
|
|
if a.HasPermissionTo(askingUserId, model.PERMISSION_EDIT_OTHER_USERS) {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (a *App) RolesGrantPermission(roleNames []string, permissionId string) bool {
|
|
roles, err := a.GetRolesByNames(roleNames)
|
|
if err != nil {
|
|
// This should only happen if something is very broken. We can't realistically
|
|
// recover the situation, so deny permission and log an error.
|
|
l4g.Error("Failed to get roles from database with role names: " + strings.Join(roleNames, ","))
|
|
l4g.Error(err)
|
|
return false
|
|
}
|
|
|
|
for _, role := range roles {
|
|
permissions := role.Permissions
|
|
for _, permission := range permissions {
|
|
if permission == permissionId {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|