1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"net/http"
"reflect"
"strings"
"github.com/mattermost/mattermost-server/model"
)
func (a *App) GetRole(id string) (*model.Role, *model.AppError) {
result := <-a.Srv.Store.Role().Get(id)
if result.Err != nil {
return nil, result.Err
}
return result.Data.(*model.Role), nil
}
func (a *App) GetRoleByName(name string) (*model.Role, *model.AppError) {
result := <-a.Srv.Store.Role().GetByName(name)
if result.Err != nil {
return nil, result.Err
}
return result.Data.(*model.Role), nil
}
func (a *App) GetRolesByNames(names []string) ([]*model.Role, *model.AppError) {
result := <-a.Srv.Store.Role().GetByNames(names)
if result.Err != nil {
return nil, result.Err
}
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) 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
result := <-a.Srv.Store.Role().Save(role)
if result.Err != nil {
return nil, result.Err
}
return result.Data.(*model.Role), nil
}
func (a *App) UpdateRole(role *model.Role) (*model.Role, *model.AppError) {
result := <-a.Srv.Store.Role().Save(role)
if result.Err != nil {
return nil, result.Err
}
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)
})
}
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, " ")
}
|