-
Notifications
You must be signed in to change notification settings - Fork 169
/
isAdmin.js
83 lines (74 loc) · 3.17 KB
/
isAdmin.js
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
'use strict';
const boom = require('@hapi/boom');
const joi = require('joi');
const schema = require('screwdriver-data-schema');
const isAdminPlugin = {
name: 'isAdmin',
async register(server) {
server.route({
method: 'GET',
path: '/isAdmin',
options: {
description: 'Check if a user is admin of a pipeline, event, or job',
notes: 'Returns true or false',
tags: ['api'],
auth: {
strategies: ['token'],
scope: ['user']
},
plugins: {
'hapi-swagger': {
security: [{ token: [] }]
}
},
handler: async (request, h) =>
Promise.resolve()
.then(() => {
const { pipelineId, eventId, jobId } = request.query;
if (eventId) {
const { eventFactory } = request.server.app;
return eventFactory.get(eventId).then(e => e.pipelineId);
}
if (jobId) {
const { jobFactory } = request.server.app;
return jobFactory.get(jobId).then(j => j.pipelineId);
}
return pipelineId;
})
.then(pid => {
const { pipelineFactory } = request.server.app;
const { userFactory } = request.server.app;
const { username } = request.auth.credentials;
const { scmContext } = request.auth.credentials;
return Promise.all([
pipelineFactory.get(pid),
userFactory.get({ username, scmContext })
]).then(([pipeline, user]) => {
if (!pipeline) {
throw boom.notFound(`Pipeline ${pid} does not exist`);
}
// ask the user for permissions on this repo
return user
.getPermissions(pipeline.scmUri)
.then(permissions => h.response(permissions.admin));
});
})
.catch(err => {
throw err;
}),
validate: {
query: joi
.object()
.keys({
pipelineId: schema.models.pipeline.base.extract('id'),
eventId: schema.models.event.base.extract('id'),
jobId: schema.models.job.base.extract('id')
})
.max(1)
.min(1)
}
}
});
}
};
module.exports = isAdminPlugin;