yapi/server_dist/controllers/base.js

444 lines
16 KiB
JavaScript
Raw Normal View History

2017-07-05 17:47:51 +08:00
'use strict';
2017-07-10 11:56:53 +08:00
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
2017-07-05 17:47:51 +08:00
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
2017-07-10 11:56:53 +08:00
var _project = require('../models/project.js');
var _project2 = _interopRequireDefault(_project);
2017-07-11 12:12:43 +08:00
var _user = require('../models/user.js');
2017-07-05 17:47:51 +08:00
2017-07-11 12:12:43 +08:00
var _user2 = _interopRequireDefault(_user);
2017-07-10 11:11:35 +08:00
2017-08-08 14:45:19 +08:00
var _interface = require('../models/interface.js');
var _interface2 = _interopRequireDefault(_interface);
var _group = require('../models/group.js');
var _group2 = _interopRequireDefault(_group);
var _underscore = require('underscore');
var _underscore2 = _interopRequireDefault(_underscore);
2017-07-11 12:12:43 +08:00
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2017-07-05 17:47:51 +08:00
2017-07-11 12:12:43 +08:00
var jwt = require('jsonwebtoken');
2017-07-10 11:11:35 +08:00
2017-07-11 12:12:43 +08:00
var baseController = function () {
function baseController(ctx) {
2017-07-10 11:11:35 +08:00
(0, _classCallCheck3.default)(this, baseController);
2017-07-11 16:50:17 +08:00
2017-07-12 17:01:27 +08:00
this.ctx = ctx;
2017-07-11 16:50:17 +08:00
//网站上线后role对象key是不能修改的value可以修改
this.roles = {
admin: 'Admin',
member: '网站会员'
};
2017-07-05 17:47:51 +08:00
}
(0, _createClass3.default)(baseController, [{
2017-07-11 12:12:43 +08:00
key: 'init',
value: function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) {
2017-07-12 17:01:27 +08:00
var ignoreRouter;
2017-07-11 12:12:43 +08:00
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
this.$user = null;
2017-08-11 11:42:52 +08:00
ignoreRouter = ['/api/user/login_by_token', '/api/user/login', '/api/user/reg', '/api/user/status', '/api/user/logout'];
2017-07-11 12:12:43 +08:00
2017-07-12 17:01:27 +08:00
if (!(ignoreRouter.indexOf(ctx.path) > -1)) {
2017-08-11 10:36:00 +08:00
_context.next = 6;
2017-07-11 12:12:43 +08:00
break;
}
this.$auth = true;
2017-08-11 10:36:00 +08:00
_context.next = 8;
2017-07-11 12:12:43 +08:00
break;
2017-08-11 10:36:00 +08:00
case 6:
_context.next = 8;
2017-07-11 12:12:43 +08:00
return this.checkLogin(ctx);
2017-08-11 10:36:00 +08:00
case 8:
2017-07-11 12:12:43 +08:00
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function init(_x) {
return _ref.apply(this, arguments);
}
return init;
}()
}, {
2017-07-05 17:47:51 +08:00
key: 'getUid',
2017-07-27 14:58:32 +08:00
value: function getUid() {
return parseInt(this.$uid, 10);
2017-07-05 17:47:51 +08:00
}
2017-07-11 12:12:43 +08:00
}, {
key: 'checkLogin',
value: function () {
var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(ctx) {
2017-07-11 12:12:43 +08:00
var token, uid, userInst, result, decoded;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
token = ctx.cookies.get('_yapi_token');
uid = ctx.cookies.get('_yapi_uid');
_context2.prev = 2;
if (!(!token || !uid)) {
_context2.next = 5;
break;
}
return _context2.abrupt('return', false);
case 5:
userInst = _yapi2.default.getInst(_user2.default); //创建user实体
_context2.next = 8;
return userInst.findById(uid);
case 8:
result = _context2.sent;
decoded = jwt.verify(token, result.passsalt);
if (!(decoded.uid == uid)) {
2017-07-11 16:50:17 +08:00
_context2.next = 15;
2017-07-11 12:12:43 +08:00
break;
}
this.$uid = uid;
this.$auth = true;
this.$user = result;
return _context2.abrupt('return', true);
2017-07-11 16:50:17 +08:00
case 15:
2017-07-11 12:12:43 +08:00
return _context2.abrupt('return', false);
2017-07-11 16:50:17 +08:00
case 18:
_context2.prev = 18;
2017-07-11 12:12:43 +08:00
_context2.t0 = _context2['catch'](2);
return _context2.abrupt('return', false);
2017-07-11 16:50:17 +08:00
case 21:
2017-07-11 12:12:43 +08:00
case 'end':
return _context2.stop();
}
}
2017-07-11 16:50:17 +08:00
}, _callee2, this, [[2, 18]]);
2017-07-11 12:12:43 +08:00
}));
function checkLogin(_x2) {
return _ref2.apply(this, arguments);
}
return checkLogin;
}()
2017-08-10 21:20:57 +08:00
/**
*
* @param {*} ctx
*/
2017-07-05 17:47:51 +08:00
}, {
key: 'getLoginStatus',
2017-07-11 12:12:43 +08:00
value: function () {
var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(ctx) {
2017-07-27 18:00:43 +08:00
var result;
2017-07-11 12:12:43 +08:00
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return this.checkLogin(ctx);
case 2:
_context3.t0 = _context3.sent;
if (!(_context3.t0 === true)) {
2017-07-27 18:00:43 +08:00
_context3.next = 7;
2017-07-11 12:12:43 +08:00
break;
}
2017-08-10 21:20:57 +08:00
result = _yapi2.default.commons.fieldSelect(this.$user, ['_id', 'username', 'email', 'up_time', 'add_time', 'role', 'type']);
2017-07-11 12:12:43 +08:00
2017-07-27 18:00:43 +08:00
result.server_ip = _yapi2.default.WEBCONFIG.server_ip;
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(result));
case 7:
2017-08-15 12:08:59 +08:00
return _context3.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 40011, '请登录...'));
2017-07-11 12:12:43 +08:00
2017-07-27 18:00:43 +08:00
case 8:
2017-07-11 12:12:43 +08:00
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
function getLoginStatus(_x3) {
return _ref3.apply(this, arguments);
}
return getLoginStatus;
}()
2017-07-06 19:21:54 +08:00
}, {
key: 'getRole',
value: function getRole() {
2017-07-11 16:50:17 +08:00
return this.$user.role;
2017-07-06 19:21:54 +08:00
}
2017-08-11 17:49:47 +08:00
}, {
key: 'getUsername',
value: function getUsername() {
return this.$user.username;
}
2017-07-10 11:56:53 +08:00
}, {
2017-08-16 17:13:14 +08:00
key: 'getProjectRole',
2017-07-10 11:56:53 +08:00
value: function () {
var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(id, type) {
2017-08-08 14:45:19 +08:00
var _this = this;
var result, interfaceInst, interfaceData, projectInst, projectData, memberData, groupInst, groupData, groupMemberData;
2017-07-11 12:12:43 +08:00
return _regenerator2.default.wrap(function _callee4$(_context4) {
2017-07-10 11:56:53 +08:00
while (1) {
2017-07-11 12:12:43 +08:00
switch (_context4.prev = _context4.next) {
2017-07-10 11:56:53 +08:00
case 0:
2017-08-08 14:45:19 +08:00
result = {};
_context4.prev = 1;
2017-07-10 11:56:53 +08:00
if (!(this.getRole() === 'admin')) {
2017-08-08 14:45:19 +08:00
_context4.next = 4;
2017-07-10 11:56:53 +08:00
break;
}
2017-08-16 17:13:14 +08:00
return _context4.abrupt('return', 'admin');
2017-07-10 11:56:53 +08:00
2017-08-08 14:45:19 +08:00
case 4:
if (!(type === 'interface')) {
_context4.next = 14;
2017-07-10 11:56:53 +08:00
break;
}
2017-08-08 14:45:19 +08:00
interfaceInst = _yapi2.default.getInst(_interface2.default);
_context4.next = 8;
return interfaceInst.get(id);
2017-07-10 11:56:53 +08:00
2017-08-08 14:45:19 +08:00
case 8:
interfaceData = _context4.sent;
2017-07-10 11:56:53 +08:00
2017-08-08 14:45:19 +08:00
result.interfaceData = interfaceData;
2017-07-10 11:56:53 +08:00
2017-08-08 14:45:19 +08:00
if (!(interfaceData.uid === this.getUid())) {
_context4.next = 12;
2017-07-10 11:56:53 +08:00
break;
}
2017-08-16 17:13:14 +08:00
return _context4.abrupt('return', 'owner');
2017-07-10 11:56:53 +08:00
2017-08-08 14:45:19 +08:00
case 12:
type = 'project';
id = interfaceData.project_id;
2017-07-10 11:56:53 +08:00
2017-08-08 14:45:19 +08:00
case 14:
if (!(type === 'project')) {
2017-08-16 17:13:14 +08:00
_context4.next = 30;
2017-08-08 14:45:19 +08:00
break;
}
2017-07-10 11:56:53 +08:00
2017-08-08 14:45:19 +08:00
projectInst = _yapi2.default.getInst(_project2.default);
_context4.next = 18;
return projectInst.get(id);
2017-07-10 11:56:53 +08:00
2017-08-08 14:45:19 +08:00
case 18:
projectData = _context4.sent;
2017-07-10 11:56:53 +08:00
2017-08-08 14:45:19 +08:00
if (!(projectData.uid === this.getUid())) {
_context4.next = 21;
break;
}
2017-08-16 17:13:14 +08:00
return _context4.abrupt('return', 'owner');
2017-08-08 14:45:19 +08:00
case 21:
memberData = _underscore2.default.find(projectData.members, function (m) {
if (m.uid === _this.getUid()) {
return true;
}
});
if (!(memberData && memberData.role)) {
2017-08-16 17:13:14 +08:00
_context4.next = 28;
2017-07-10 11:56:53 +08:00
break;
}
2017-08-16 17:13:14 +08:00
if (!(memberData.role === 'owner')) {
2017-08-08 14:45:19 +08:00
_context4.next = 27;
2017-07-10 11:56:53 +08:00
break;
}
2017-08-16 17:13:14 +08:00
return _context4.abrupt('return', 'owner');
2017-07-10 11:56:53 +08:00
2017-08-08 14:45:19 +08:00
case 27:
2017-08-16 17:13:14 +08:00
return _context4.abrupt('return', 'dev');
case 28:
2017-08-08 14:45:19 +08:00
type = 'group';
id = projectData.group_id;
2017-07-10 11:56:53 +08:00
2017-08-16 17:13:14 +08:00
case 30:
2017-08-08 14:45:19 +08:00
if (!(type === 'group')) {
2017-08-16 17:13:14 +08:00
_context4.next = 42;
2017-08-08 14:45:19 +08:00
break;
}
groupInst = _yapi2.default.getInst(_group2.default);
2017-08-16 17:13:14 +08:00
_context4.next = 34;
2017-08-08 14:45:19 +08:00
return groupInst.get(id);
2017-08-16 17:13:14 +08:00
case 34:
2017-08-08 14:45:19 +08:00
groupData = _context4.sent;
groupMemberData = _underscore2.default.find(groupData.members, function (m) {
if (m.uid === _this.getUid()) {
return true;
}
});
2017-07-10 11:56:53 +08:00
2017-08-08 14:45:19 +08:00
if (!(groupMemberData && groupMemberData.role)) {
2017-08-16 17:13:14 +08:00
_context4.next = 42;
2017-07-10 11:56:53 +08:00
break;
}
2017-08-16 17:13:14 +08:00
if (!(groupMemberData.role === 'owner')) {
_context4.next = 41;
2017-08-08 14:45:19 +08:00
break;
}
2017-08-16 17:13:14 +08:00
return _context4.abrupt('return', 'owner');
2017-08-08 14:45:19 +08:00
2017-08-16 17:13:14 +08:00
case 41:
return _context4.abrupt('return', 'dev');
2017-07-10 11:56:53 +08:00
2017-08-16 17:13:14 +08:00
case 42:
return _context4.abrupt('return', 'member');
2017-08-08 14:45:19 +08:00
2017-08-16 17:13:14 +08:00
case 45:
_context4.prev = 45;
2017-08-08 14:45:19 +08:00
_context4.t0 = _context4['catch'](1);
_yapi2.default.commons.log(_context4.t0.message, 'error');
return _context4.abrupt('return', false);
2017-08-16 17:13:14 +08:00
case 49:
2017-07-10 11:56:53 +08:00
case 'end':
2017-08-08 14:45:19 +08:00
return _context4.stop();
2017-07-10 11:56:53 +08:00
}
}
2017-08-16 17:13:14 +08:00
}, _callee4, this, [[1, 45]]);
2017-07-10 11:56:53 +08:00
}));
2017-08-16 17:13:14 +08:00
function getProjectRole(_x4, _x5) {
2017-08-08 14:45:19 +08:00
return _ref4.apply(this, arguments);
2017-07-10 11:56:53 +08:00
}
2017-08-16 17:13:14 +08:00
return getProjectRole;
}()
/**
*
* @param {*} id type对应的id
* @param {*} type enum[interface, project, group]
* @param {*} action enum[ danger , edit ] danger只有owner或管理员才能操作,edit只要是dev或以上就能执行
*/
}, {
key: 'checkAuth',
value: function () {
var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(id, type, action) {
2017-08-16 17:13:14 +08:00
var role;
return _regenerator2.default.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_context5.next = 2;
return this.getProjectRole(id, type);
case 2:
role = _context5.sent;
if (!(action === 'danger')) {
_context5.next = 8;
break;
}
if (!(role === 'admin' || role === 'owner')) {
_context5.next = 6;
break;
}
return _context5.abrupt('return', true);
case 6:
_context5.next = 11;
break;
case 8:
if (!(action === 'edit')) {
_context5.next = 11;
break;
}
if (!(role === 'admin' || role === 'owner' || role === 'dev')) {
_context5.next = 11;
break;
}
return _context5.abrupt('return', true);
case 11:
return _context5.abrupt('return', false);
case 12:
case 'end':
return _context5.stop();
}
}
}, _callee5, this);
}));
function checkAuth(_x6, _x7, _x8) {
return _ref5.apply(this, arguments);
}
2017-08-08 14:45:19 +08:00
return checkAuth;
2017-07-10 11:56:53 +08:00
}()
2017-07-05 17:47:51 +08:00
}]);
return baseController;
}();
module.exports = baseController;