]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
f0f5567b | 3 | const checkErrors = require('./utils').checkErrors |
441b66f8 | 4 | const constants = require('../../initializers/constants') |
b09ce645 | 5 | const db = require('../../initializers/database') |
f0f5567b C |
6 | const friends = require('../../lib/friends') |
7 | const logger = require('../../helpers/logger') | |
441b66f8 | 8 | const utils = require('../../helpers/utils') |
9f10b292 | 9 | |
fc51fde0 | 10 | const validatorsPod = { |
c4403b29 C |
11 | makeFriends, |
12 | podsAdd | |
9f10b292 C |
13 | } |
14 | ||
15 | function makeFriends (req, res, next) { | |
441b66f8 C |
16 | // Force https if the administrator wants to make friends |
17 | if (utils.isTestInstance() === false && constants.CONFIG.WEBSERVER.SCHEME === 'http') { | |
18 | return res.status(400).send('Cannot make friends with a non HTTPS webserver.') | |
19 | } | |
20 | ||
49abbbbe | 21 | req.checkBody('hosts', 'Should have an array of unique hosts').isEachUniqueHostValid() |
1e2564d3 C |
22 | |
23 | logger.debug('Checking makeFriends parameters', { parameters: req.body }) | |
24 | ||
d57d6f26 C |
25 | checkErrors(req, res, function () { |
26 | friends.hasFriends(function (err, hasFriends) { | |
27 | if (err) { | |
28 | logger.error('Cannot know if we have friends.', { error: err }) | |
29 | res.sendStatus(500) | |
30 | } | |
31 | ||
32 | if (hasFriends === true) { | |
33 | // We need to quit our friends before make new ones | |
b09ce645 | 34 | return res.sendStatus(409) |
d57d6f26 | 35 | } |
b09ce645 C |
36 | |
37 | return next() | |
d57d6f26 | 38 | }) |
9f10b292 C |
39 | }) |
40 | } | |
41 | ||
42 | function podsAdd (req, res, next) { | |
4793c343 C |
43 | req.checkBody('host', 'Should have a host').isHostValid() |
44 | req.checkBody('email', 'Should have an email').isEmail() | |
528a9efa | 45 | req.checkBody('publicKey', 'Should have a public key').notEmpty() |
b09ce645 | 46 | logger.debug('Checking podsAdd parameters', { parameters: req.body }) |
528a9efa | 47 | |
b09ce645 C |
48 | checkErrors(req, res, function () { |
49 | db.Pod.loadByHost(req.body.host, function (err, pod) { | |
50 | if (err) { | |
51 | logger.error('Cannot load pod by host.', { error: err }) | |
52 | res.sendStatus(500) | |
53 | } | |
9f10b292 | 54 | |
b09ce645 C |
55 | // Pod with this host already exists |
56 | if (pod) { | |
57 | return res.sendStatus(409) | |
58 | } | |
9f10b292 | 59 | |
b09ce645 C |
60 | return next() |
61 | }) | |
62 | }) | |
9f10b292 C |
63 | } |
64 | ||
65 | // --------------------------------------------------------------------------- | |
66 | ||
fc51fde0 | 67 | module.exports = validatorsPod |