Repository navigation
Expand file tree
/
Copy pathpermissions.js
More file actions
338 lines (298 loc) · 8.34 KB
/
Copy pathpermissions.js
File metadata and controls
338 lines (298 loc) · 8.34 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
const ObjectId = require('bson-objectid').default;
const logging = require('@tryghost/logging');
const errors = require('@tryghost/errors');
const tpl = require('@tryghost/tpl');
const { createTransactionalMigration, combineTransactionalMigrations } = require('./migrations');
const MIGRATION_USER = 1;
const messages = {
permissionRoleActionError:
'Cannot {action} permission({permission}) with role({role}) - {resource} does not exist',
};
/**
* @param {import('knex').Knex} connection
* @param {PermissionConfig} config
*/
async function addPermissionHelper(connection, config) {
const existingPermission = await connection('permissions')
.where({
name: config.name,
action_type: config.action,
object_type: config.object,
})
.first();
if (existingPermission) {
logging.warn(`Permission for ${config.action}:${config.object} already added`);
return;
}
logging.info(`Adding permission for ${config.action}:${config.object}`);
const date = connection.raw('CURRENT_TIMESTAMP');
const data = {
id: ObjectId().toHexString(),
name: config.name,
action_type: config.action,
object_type: config.object,
created_at: date,
updated_at: date,
};
if (await connection.schema.hasColumn('permissions', 'created_by')) {
data.created_by = MIGRATION_USER;
}
if (await connection.schema.hasColumn('permissions', 'updated_by')) {
data.updated_by = MIGRATION_USER;
}
await connection('permissions').insert(data);
}
/**
* @param {import('knex').Knex} connection
* @param {PermissionConfig} config
*/
async function removePermissionHelper(connection, config) {
const existingPermission = await connection('permissions')
.where({
name: config.name,
action_type: config.action,
object_type: config.object,
})
.first();
if (!existingPermission) {
logging.warn(`Permission for ${config.action}:${config.object} already removed`);
return;
}
logging.info(`Removing permission for ${config.action}:${config.object}`);
await connection('permissions')
.where({
action_type: config.action,
object_type: config.object,
})
.del();
}
/**
* Creates a migration which will add a permission to the database
*
* @param {PermissionConfig} config
*
* @returns {Migration}
*/
function addPermission(config) {
return createTransactionalMigration(
async function up(connection) {
await addPermissionHelper(connection, config);
},
async function down(connection) {
await removePermissionHelper(connection, config);
},
);
}
/**
* Creates a migration which will remove a permission from the database
*
* @param {PermissionConfig} config
*
* @returns {Migration}
*/
function removePermission(config) {
return createTransactionalMigration(
async function up(connection) {
await removePermissionHelper(connection, config);
},
async function down(connection) {
await addPermissionHelper(connection, config);
},
);
}
/**
* @param {import('knex').Knex} connection
* @param {PermissionRoleConfig} config
*/
async function addPermissionToRoleHelper(connection, config) {
const permission = await connection('permissions')
.where({
name: config.permission,
})
.first();
// Permission checks no longer read these tables, and databases created
// since fixtures stopped seeding permissions have no rows to link.
if (!permission) {
logging.warn(
`Adding permission(${config.permission}) to role(${config.role}) - Permission not found.`,
);
return;
}
const role = await connection('roles')
.where({
name: config.role,
})
.first();
if (!role) {
throw new errors.InternalServerError({
message: tpl(messages.permissionRoleActionError, {
action: 'add',
permission: config.permission,
role: config.role,
resource: 'role',
}),
});
}
const existingRelation = await connection('permissions_roles')
.where({
permission_id: permission.id,
role_id: role.id,
})
.first();
if (existingRelation) {
logging.warn(
`Adding permission(${config.permission}) to role(${config.role}) - already exists`,
);
return;
}
logging.info(`Adding permission(${config.permission}) to role(${config.role})`);
await connection('permissions_roles').insert({
id: ObjectId().toHexString(),
permission_id: permission.id,
role_id: role.id,
});
}
/**
* @param {import('knex').Knex} connection
* @param {PermissionRoleConfig} config
*/
async function removePermissionFromRoleHelper(connection, config) {
const permission = await connection('permissions')
.where({
name: config.permission,
})
.first();
if (!permission) {
logging.warn(
`Removing permission(${config.permission}) from role(${config.role}) - Permission not found.`,
);
return;
}
const role = await connection('roles')
.where({
name: config.role,
})
.first();
if (!role) {
logging.warn(
`Removing permission(${config.permission}) from role(${config.role}) - Role not found.`,
);
return;
}
const existingRelation = await connection('permissions_roles')
.where({
permission_id: permission.id,
role_id: role.id,
})
.first();
if (!existingRelation) {
logging.warn(
`Removing permission(${config.permission}) from role(${config.role}) - already removed`,
);
return;
}
logging.info(`Removing permission(${config.permission}) from role(${config.role})`);
await connection('permissions_roles')
.where({
permission_id: permission.id,
role_id: role.id,
})
.del();
}
/**
* Creates a migration which will link a permission to a role in the database
*
* @param {PermissionRoleConfig} config
*
* @returns {Migration}
*/
function addPermissionToRole(config) {
return createTransactionalMigration(
async function up(connection) {
await addPermissionToRoleHelper(connection, config);
},
async function down(connection) {
await removePermissionFromRoleHelper(connection, config);
},
);
}
/**
* Creates a migration which will remove the permission from roles
*
* @param {PermissionRoleConfig} config
*
* @returns {Migration}
*/
function removePermissionFromRole(config) {
return createTransactionalMigration(
async function up(connection) {
await removePermissionFromRoleHelper(connection, config);
},
async function down(connection) {
await addPermissionToRoleHelper(connection, config);
},
);
}
/**
* Creates a migration which will add a permission to the database, and then link it to roles
*
* @param {PermissionConfig} config
* @param {string[]} roles - A list of role names
*
* @returns {Migration}
*/
function addPermissionWithRoles(config, roles) {
return combineTransactionalMigrations(
addPermission(config),
...roles.map((role) => addPermissionToRole({ permission: config.name, role })),
);
}
/**
* Creates a migration which will remove permissions from roles, and then remove the permission
*
* @param {PermissionConfig} config
* @param {string[]} roles - A list of role names
*
* @returns {Migration}
*/
function createRemovePermissionMigration(config, roles) {
return combineTransactionalMigrations(
...roles.map((role) => removePermissionFromRole({ permission: config.name, role })),
removePermission(config),
);
}
module.exports = {
addPermission,
addPermissionToRole,
removePermissionFromRole,
addPermissionWithRoles,
createRemovePermissionMigration,
};
/**
* @typedef {Object} PermissionConfig
* @prop {string} config.name - The name of the permission
* @prop {string} config.action - The action_type of the permission
* @prop {string} config.object - The object_type of the permission
*/
/**
* @typedef {Object} PermissionRoleConfig
* @prop {string} config.permission - The name of the permission
* @prop {string} config.role - The role to assign the Permission to
*/
/**
* @typedef {Object} TransactionalMigrationFunctionOptions
*
* @prop {import('knex').Knex} transacting
*/
/**
* @typedef {(options: TransactionalMigrationFunctionOptions) => Promise<void>} TransactionalMigrationFunction
*/
/**
* @typedef {Object} Migration
*
* @prop {Object} config
* @prop {boolean} config.transaction
*
* @prop {TransactionalMigrationFunction} up
* @prop {TransactionalMigrationFunction} down
*/