forked from mattermost/mattermost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permissions.go
245 lines (200 loc) · 6.25 KB
/
permissions.go
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"bufio"
"encoding/json"
"fmt"
"io"
"github.com/mattermost/mattermost-server/model"
"github.com/pkg/errors"
)
const permissionsExportBatchSize = 100
const systemSchemeName = "00000000-0000-0000-0000-000000000000" // Prevents collisions with user-created schemes.
func (a *App) ResetPermissionsSystem() *model.AppError {
// Reset all Teams to not have a scheme.
if result := <-a.Srv.Store.Team().ResetAllTeamSchemes(); result.Err != nil {
return result.Err
}
// Reset all Channels to not have a scheme.
if result := <-a.Srv.Store.Channel().ResetAllChannelSchemes(); result.Err != nil {
return result.Err
}
// Reset all Custom Role assignments to Users.
if result := <-a.Srv.Store.User().ClearAllCustomRoleAssignments(); result.Err != nil {
return result.Err
}
// Reset all Custom Role assignments to TeamMembers.
if result := <-a.Srv.Store.Team().ClearAllCustomRoleAssignments(); result.Err != nil {
return result.Err
}
// Reset all Custom Role assignments to ChannelMembers.
if result := <-a.Srv.Store.Channel().ClearAllCustomRoleAssignments(); result.Err != nil {
return result.Err
}
// Purge all schemes from the database.
if result := <-a.Srv.Store.Scheme().PermanentDeleteAll(); result.Err != nil {
return result.Err
}
// Purge all roles from the database.
if result := <-a.Srv.Store.Role().PermanentDeleteAll(); result.Err != nil {
return result.Err
}
// Remove the "System" table entry that marks the advanced permissions migration as done.
if result := <-a.Srv.Store.System().PermanentDeleteByName(ADVANCED_PERMISSIONS_MIGRATION_KEY); result.Err != nil {
return result.Err
}
// Now that the permissions system has been reset, re-run the migration to reinitialise it.
a.DoAdvancedPermissionsMigration()
a.DoEmojisPermissionsMigration()
return nil
}
func (a *App) ExportPermissions(w io.Writer) error {
next := a.SchemesIterator(permissionsExportBatchSize)
var schemeBatch []*model.Scheme
for schemeBatch = next(); len(schemeBatch) > 0; schemeBatch = next() {
for _, scheme := range schemeBatch {
roleNames := []string{
scheme.DefaultTeamAdminRole,
scheme.DefaultTeamUserRole,
scheme.DefaultChannelAdminRole,
scheme.DefaultChannelUserRole,
}
roles := []*model.Role{}
for _, roleName := range roleNames {
if len(roleName) == 0 {
continue
}
role, err := a.GetRoleByName(roleName)
if err != nil {
return err
}
roles = append(roles, role)
}
schemeExport, err := json.Marshal(&model.SchemeConveyor{
Name: scheme.Name,
DisplayName: scheme.DisplayName,
Description: scheme.Description,
Scope: scheme.Scope,
TeamAdmin: scheme.DefaultTeamAdminRole,
TeamUser: scheme.DefaultTeamUserRole,
ChannelAdmin: scheme.DefaultChannelAdminRole,
ChannelUser: scheme.DefaultChannelUserRole,
Roles: roles,
})
if err != nil {
return err
}
schemeExport = append(schemeExport, []byte("\n")...)
_, err = w.Write(schemeExport)
if err != nil {
return err
}
}
}
defaultRoleNames := []string{}
for _, dr := range model.MakeDefaultRoles() {
defaultRoleNames = append(defaultRoleNames, dr.Name)
}
roles, appErr := a.GetRolesByNames(defaultRoleNames)
if appErr != nil {
return errors.New(appErr.Message)
}
schemeExport, err := json.Marshal(&model.SchemeConveyor{
Name: systemSchemeName,
Roles: roles,
})
if err != nil {
return err
}
schemeExport = append(schemeExport, []byte("\n")...)
_, err = w.Write(schemeExport)
return err
}
func (a *App) ImportPermissions(jsonl io.Reader) error {
createdSchemeIDs := []string{}
scanner := bufio.NewScanner(jsonl)
for scanner.Scan() {
var schemeConveyor *model.SchemeConveyor
err := json.Unmarshal(scanner.Bytes(), &schemeConveyor)
if err != nil {
rollback(a, createdSchemeIDs)
return err
}
if schemeConveyor.Name == systemSchemeName {
for _, roleIn := range schemeConveyor.Roles {
dbRole, err := a.GetRoleByName(roleIn.Name)
if err != nil {
rollback(a, createdSchemeIDs)
return errors.New(err.Message)
}
_, err = a.PatchRole(dbRole, &model.RolePatch{
Permissions: &roleIn.Permissions,
})
if err != nil {
rollback(a, createdSchemeIDs)
return err
}
}
continue
}
// Create the new Scheme. The new Roles are created automatically.
var appErr *model.AppError
schemeCreated, appErr := a.CreateScheme(schemeConveyor.Scheme())
if appErr != nil {
rollback(a, createdSchemeIDs)
return errors.New(appErr.Message)
}
createdSchemeIDs = append(createdSchemeIDs, schemeCreated.Id)
schemeIn := schemeConveyor.Scheme()
roleNameTuples := [][]string{
{schemeCreated.DefaultTeamAdminRole, schemeIn.DefaultTeamAdminRole},
{schemeCreated.DefaultTeamUserRole, schemeIn.DefaultTeamUserRole},
{schemeCreated.DefaultChannelAdminRole, schemeIn.DefaultChannelAdminRole},
{schemeCreated.DefaultChannelUserRole, schemeIn.DefaultChannelUserRole},
}
for _, roleNameTuple := range roleNameTuples {
if len(roleNameTuple[0]) == 0 || len(roleNameTuple[1]) == 0 {
continue
}
err = updateRole(a, schemeConveyor, roleNameTuple[0], roleNameTuple[1])
if err != nil {
// Delete the new Schemes. The new Roles are deleted automatically.
rollback(a, createdSchemeIDs)
return err
}
}
}
if err := scanner.Err(); err != nil {
rollback(a, createdSchemeIDs)
return err
}
return nil
}
func rollback(a *App, createdSchemeIDs []string) {
for _, schemeID := range createdSchemeIDs {
a.DeleteScheme(schemeID)
}
}
func updateRole(a *App, sc *model.SchemeConveyor, roleCreatedName, defaultRoleName string) error {
var err *model.AppError
roleCreated, err := a.GetRoleByName(roleCreatedName)
if err != nil {
return errors.New(err.Message)
}
var roleIn *model.Role
for _, role := range sc.Roles {
if role.Name == defaultRoleName {
roleIn = role
break
}
}
roleCreated.DisplayName = roleIn.DisplayName
roleCreated.Description = roleIn.Description
roleCreated.Permissions = roleIn.Permissions
_, err = a.UpdateRole(roleCreated)
if err != nil {
return errors.New(fmt.Sprintf("%v: %v\n", err.Message, err.DetailedError))
}
return nil
}