All files / bintra/controllers Admins.js

89.2% Statements 124/139
100% Branches 3/3
78.18% Functions 43/55
89.2% Lines 124/139

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396                  1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x             1x 6x 6x     6x   6x   3x 3x 3x 3x 3x     3x     3x     3x     3x     3x                 1x 1x   1x   1x                       1x 1x   1x   1x                       1x 1x   1x   1x                       1x 2x   2x   2x   2x                       1x 2x   2x   2x                       1x 2x   2x   2x                       1x 1x   1x   1x                       1x 2x   2x   1x     1x                 1x 3x   3x   3x 2x       1x                         1x 1x   1x   1x                       1x 2x   2x   1x 1x     1x                 1x 2x 2x 2x   2x   1x     1x                 1x 2x   2x   1x     1x                 1x 2x   2x   1x     1x                 1x 2x   2x 2x 2x 2x   2x 2x 2x         2x 2x   2x 2x     2x     2x   2x   2x 2x 2x 2x       2x 2x                               1x 1x   1x   1x                       1x 1x   1x   1x            
'use strict';
 
/**
 * API controller for admin methods mapping.
 * @module controllers/admins
 * @license MIT
 * @author Kai KRETSCHMANN <kai@kretschmann.consulting>
 */
 
const utils = require('../utils/writer.js');
const eventEmitter = require('../utils/eventer').em;
const PackagesService = require('../service/PackagesService');
const UsersService = require('../service/UsersService');
const mongoose = require('mongoose');
const auth = require('../utils/auth');
const fs = require('fs');
const path = require('path');
 
const log4js = require('log4js');
const logger = log4js.getLogger();
logger.level = process.env.LOGLEVEL || /* istanbul ignore next */ 'warn'; // LCOV_EXCL_LINE
const EVENTNAME = 'apihit';
 
/**
 * Receive login data and return JWT token.
 * @function loginPost
 * @public
 */
module.exports.loginPost = function loginPost (args, res, _next) {
  const username = args.body.username;
  const password = args.body.password;
  let response;
 
  eventEmitter.emit('posthit', 'login');
 
  UsersService.checkUser(username, password)
    .then(function (useritem) {
      logger.info(`User found: ${useritem._id}`);
      const myRole = useritem.role;
      logger.info(`User has role ${myRole}`);
      const tokenString = auth.issueToken(username, myRole);
      response = {
        token: tokenString
      };
      res.writeHead(200, {
        'Content-Type': 'application/json'
      });
      return res.end(JSON.stringify(response));
    })
    .catch(function () {
      response = {
        message: 'Error: Credentials incorrect'
      };
      res.writeHead(403, {
        'Content-Type': 'application/json'
      });
      return res.end(JSON.stringify(response));
    });
};
 
/**
 * List all packages and variations.
 * @function listUsers
 * @public
 */
module.exports.listUsers = function listUsers (req, res, _next) {
  eventEmitter.emit(EVENTNAME, req);
 
  UsersService.listUsers()
    .then(function (payload) {
      utils.writeJson(res, payload, 200);
    })
    .catch(function (err) {
      utils.writeJson(res, err.message, 500);
    });
};
 
/**
 * Get user data of named user.
 * @function getUser
 * @public
 */
module.exports.getUser = function getUser (req, res, _next, name) {
  eventEmitter.emit(EVENTNAME, req);
 
  UsersService.getUser(name)
    .then(function (payload) {
      utils.writeJson(res, payload, 200);
    })
    .catch(function (err) {
      utils.writeJson(res, err.message, 400);
    });
};
 
/**
 * List all packages and variations.
 * @function deleteUser
 * @public
 */
module.exports.deleteUser = function deleteUser (req, res, _next, id) {
  eventEmitter.emit(EVENTNAME, req);
 
  UsersService.deleteUser(id)
    .then(function (payload) {
      utils.writeJson(res, payload, 200);
    })
    .catch(function (err) {
      utils.writeJson(res, err.message, 400);
    });
};
 
/**
 * List all packages and variations.
 * @function putUserStatus
 * @public
 */
module.exports.putUserStatus = function putUserStatus (req, res, _next, status, id) {
  eventEmitter.emit(EVENTNAME, req);
 
  logger.info(`putUserStatus ${id}/${status}!`);
 
  UsersService.putUserStatus(id, status)
    .then(function (payload) {
      utils.writeJson(res, payload, 200);
    })
    .catch(function (err) {
      utils.writeJson(res, err.message, 400);
    });
};
 
/**
 * List all packages and variations.
 * @function listUser
 * @public
 */
module.exports.listUser = function listUser (req, res, _next, id) {
  eventEmitter.emit(EVENTNAME, req);
 
  UsersService.listUser(id)
    .then(function (payload) {
      utils.writeJson(res, payload, 200);
    })
    .catch(function (err) {
      utils.writeJson(res, err.message, 400);
    });
};
 
/**
 * List all black listed domains.
 * @function listDomains
 * @public
 */
module.exports.listDomains = function listDomains (req, res, _next) {
  eventEmitter.emit(EVENTNAME, req);
 
  UsersService.listDomains()
    .then(function (payload) {
      utils.writeJson(res, payload, 200);
    })
    .catch(function (err) {
      utils.writeJson(res, err.message, 400);
    });
};
 
/**
 * Add a black listed domains.
 * @function addDomain
 * @public
 */
module.exports.addDomain = function listDomains (req, res, _next, name) {
  eventEmitter.emit(EVENTNAME, req);
 
  UsersService.addDomain(name)
    .then(function (payload) {
      utils.writeJson(res, payload, 200);
    })
    .catch(function (err) {
      utils.writeJson(res, err.message, 400);
    });
};
 
/**
 * Delete a black listed domains.
 * @function deleteDomain
 * @public
 */
module.exports.deleteDomain = function deleteDomain (req, res, _next, name) {
  eventEmitter.emit(EVENTNAME, req);
 
  UsersService.deleteDomain(name)
    .then(function (payload) {
      utils.writeText(res, payload, 200);
    })
    .catch(function (err) {
      utils.writeText(res, err.message, err.code);
    });
};
 
/**
 * Retreive a given domain.
 * @function checkDomain
 * @public
 */
module.exports.checkDomain = function checkDomain (req, res, _next, name) {
  eventEmitter.emit(EVENTNAME, req);
 
  UsersService.checkDomain(name)
    .then(function (payload) {
      if (payload == null) {
        utils.writeJson(res, {
          message: 'Not found'
        }, 404);
      } else {
        utils.writeJson(res, payload, 200);
      }
    })
    .catch(function (err) {
      utils.writeJson(res, err.message, 400);
    });
};
 
/**
 * Create a user.
 * @function createUser
 * @public
 */
module.exports.createUser = function createUser (req, res, _next, user) {
  eventEmitter.emit(EVENTNAME, req);
 
  UsersService.createUser(user)
    .then(function (payload) {
      utils.writeJson(res, payload, 200);
    })
    .catch(function (err) {
      utils.writeJson(res, err.message, 400);
    });
};
 
/**
 * List all packages and variations but filter.
 * @function searchPackages
 * @public
 */
module.exports.searchPackages = function searchPackages (req, res, _next, jsearch) {
  eventEmitter.emit(EVENTNAME, req);
 
  PackagesService.searchPackages(jsearch)
    .then(function (payload) {
      logger.error('Found search result');
      utils.writeJson(res, payload, 200);
    })
    .catch(function (err) {
      utils.writeText(res, err.message, err.code);
    });
};
 
/**
 * Update user data via patch.
 * @function patchUser
 * @public
 */
module.exports.patchUser = function patchUser (req, res, _next, jpatch, id) {
  logger.info('In PATCH controller');
  eventEmitter.emit(EVENTNAME, req);
  logger.info(req.headers['content-type']);
 
  UsersService.patchUser(id, jpatch)
    .then(function (payload) {
      utils.writeJson(res, payload, 200);
    })
    .catch(function (err) {
      utils.writeJson(res, err.message, 400);
    });
};
 
/**
 * Delete package with given package data.
 * @function deletePackage
 * @public
 */
module.exports.deletePackage = function deletePackage (req, res, _next, packageName, packageVersion, packageArch, packageFamily, packageHash) {
  eventEmitter.emit(EVENTNAME, req);
 
  PackagesService.deletePackage(packageName, packageVersion, packageArch, packageFamily, packageHash)
    .then(function (payload) {
      utils.writeText(res, payload, 200);
    })
    .catch(function (err) {
      utils.writeText(res, err.message, err.code);
    });
};
 
/**
 * Delete package with given package id.
 * @function deletePackageById
 * @public
 */
module.exports.deletePackageById = function deletePackageById (req, res, _next, id) {
  eventEmitter.emit(EVENTNAME, req);
 
  PackagesService.deletePackageById(id)
    .then(function (payload) {
      utils.writeText(res, payload, 200);
    })
    .catch(function (err) {
      utils.writeText(res, err.message, err.code);
    });
};
 
/**
 * Get all system version names.
 * @function getVersions
 * @public
 */
module.exports.getVersions = function getVersions (req, res, _next) {
  eventEmitter.emit(EVENTNAME, req);
 
  const jdata = process.versions;
  const json = require('../package.json');
  const gitrevFilename = path.join(__dirname, '../.gitrevision');
  let gitrevision = '';
 
  try {
    fs.accessSync(gitrevFilename, fs.constants.R_OK);
    gitrevision = fs.readFileSync(gitrevFilename, 'utf8');
  } catch (err) {
    logger.error(`gitrevision file not found at: ${gitrevFilename}`);
  }
 
  jdata.bintra = json.version;
  logger.debug(`bintra version found: ${jdata.bintra}`);
 
  jdata.gitrevision = gitrevision.trim();
  logger.debug(`git revision found: ${jdata.gitrevision}`);
 
  // get DB version
  const admin = new mongoose.mongo.Admin(mongoose.connection.db);
  let myinfo;
  async function doinfo () {
    myinfo = await admin.buildInfo();
  }
  doinfo()
    .then(() => {
      logger.debug(`mongo version found: ${myinfo.version}`);
      jdata.mongodb = myinfo.version;
      const payload = JSON.stringify(jdata);
      res.writeHead(200, {
        'Content-Type': 'application/json'
      });
 
      logger.info('return');
      return res.end(payload);
    })
    .catch(err => {
      logger.error('We have a probleme here: ' + err)
      res.writeHead(500, {
        'Content-Type': 'text/plain'
      });
      return res.end();
    });
};
 
/**
 * Get count per creator.
 * @function getCountPerCreator
 * @public
 **/
module.exports.getCountPerCreator = function getCountPerCreator (req, res, _next) {
  eventEmitter.emit(EVENTNAME, req);
 
  PackagesService.countPerCreator()
    .then(function (payload) {
      utils.writeJson(res, payload, 200);
    })
    .catch(function (err) {
      utils.writeJson(res, err.message, 400);
    });
};
 
/**
 * List authentication log.
 * @function getLogauth
 * @public
 */
module.exports.getLogauth = function getLogauth (req, res, _next) {
  eventEmitter.emit(EVENTNAME, req);
 
  UsersService.getLogauth()
    .then(function (payload) {
      utils.writeJson(res, payload, 200);
    })
    .catch(function (err) {
      utils.writeJson(res, err.message, 400);
    });
};