forked from screwdriver-cd/screwdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisAdmin.js
81 lines (71 loc) · 2.88 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
'use strict';
const boom = require('boom');
const joi = require('joi');
const schema = require('screwdriver-data-schema');
exports.register = (server, options, next) => {
server.route({
method: 'GET',
path: '/isAdmin',
config: {
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: (request, reply) =>
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 => reply(permissions.admin));
}
);
})
.catch(err => reply(boom.boomify(err))),
validate: {
query: joi
.object()
.keys({
pipelineId: joi.reach(schema.models.pipeline.base, 'id'),
eventId: joi.reach(schema.models.event.base, 'id'),
jobId: joi.reach(schema.models.job.base, 'id')
})
.max(1)
.min(1)
}
}
});
next();
};
exports.register.attributes = {
name: 'isAdmin'
};