lin-cms-test
Version:
The core library of Lin CMS, this package is just for test!
251 lines (250 loc) • 8.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const consola_1 = tslib_1.__importDefault(require("consola"));
const jwt_1 = require("./jwt");
const util_1 = require("./util");
const db_1 = require("./db");
const interface_1 = require("./interface");
const extend_1 = require("./extend");
const exception_1 = require("./exception");
const lodash_1 = require("lodash");
const loader_1 = require("./loader");
const redprint_1 = require("./redprint");
const passwordHash_1 = require("./passwordHash");
// tslint:disable-next-line:variable-name
exports.__version__ = "0.0.1";
// 存放meta路由信息
exports.routeMetaInfo = new Map();
// 当前文件路由是否架子
exports.disableLoading = Symbol("disableLoading");
// 初始化各种扩展,中间件
class Lin {
async initApp(app, mount, // 是否挂载插件路由,默认为true
synchronize, // 是否同步模型到数据库
userModel, groupModel, authModel) {
this.app = app;
util_1.assert(!!this.app, "app must not be null");
// 2. 默认扩展 json logger
this.applyDefaultExtends();
// 3. manager
userModel = userModel || exports.User;
groupModel = groupModel || exports.Group;
authModel = authModel || exports.Auth;
this.applyManager(userModel, groupModel, authModel);
// 4. db 同步到数据库默认为false,每次同步会很慢 todo 抽离
await this.applyDB(synchronize);
// 5. jwt
this.applyJwt();
// 6. 挂载默认路由
mount && this.mount();
}
applyJwt() {
const secret = this.app.context.config.getItem("secret");
jwt_1.jwt.initApp(this.app, secret);
}
async applyDB(synchronize, force) {
synchronize && db_1.db.sync({ force });
}
applyManager(userModel, groupModel, authModel) {
const manager = new Manager();
this.manager = manager;
const pluginPath = this.app.context.config.getItem("pluginPath");
manager.initApp(this.app, userModel, groupModel, authModel, pluginPath);
}
applyDefaultExtends() {
extend_1.json(this.app);
extend_1.logging(this.app);
}
mount() {
const pluginRp = new redprint_1.Redprint({ prefix: "/plugin" });
Object.values(this.manager.plugins).forEach(plugin => {
consola_1.default.info(`loading plugin: ${lodash_1.get(plugin, "name")}`);
const controllers = Object.values(lodash_1.get(plugin, "controllers"));
if (controllers.length > 1) {
controllers.forEach(cont => {
lodash_1.set(cont, "opts.prefix", `/${lodash_1.get(plugin, "name")}${lodash_1.get(cont, "opts.prefix")}`);
lodash_1.get(cont, "stack", []).forEach(ly => {
consola_1.default.info(`loading a route: /plugin/${lodash_1.get(plugin, "name")}${lodash_1.get(ly, "path")}`);
lodash_1.set(ly, "path", `/${lodash_1.get(plugin, "name")}${lodash_1.get(ly, "path")}`);
});
// cont.stack[0].path
pluginRp
.use(cont.routes())
.use(cont.allowedMethods());
});
}
else {
controllers.forEach(cont => {
lodash_1.get(cont, "stack", []).forEach(ly => {
consola_1.default.info(`loading a route: /plugin/${lodash_1.get(ly, "path")}`);
});
pluginRp
.use(cont.routes())
.use(cont.allowedMethods());
});
}
});
this.app.use(pluginRp.routes()).use(pluginRp.allowedMethods());
}
}
exports.Lin = Lin;
// 管理插件,数据模型
class Manager {
initApp(app, userModel, groupModel, authModel, pluginPath) {
app.context.manager = this;
this.userModel = userModel;
this.groupModel = groupModel;
this.authModel = authModel;
this.loader = new loader_1.Loader(pluginPath, app);
}
get plugins() {
return this.loader.plugins;
}
verify(nickname, password) {
return this.userModel.verify(nickname, password);
}
findUser(args) {
return this.userModel.findOne({ where: Object.assign({}, args) });
}
findGroup(args) {
return this.groupModel.findOne({ where: Object.assign({}, args) });
}
}
exports.Manager = Manager;
/**
* 权限系统中的User模型
*/
exports.User = db_1.db.define("lin_user", Object.assign({}, interface_1.UserInterface.attributes), lodash_1.merge({
tableName: "lin_user"
}, interface_1.UserInterface.options));
// @ts-ignore
exports.User.verify = async function (nickname, password) {
// @ts-ignore
// tslint:disable-next-line: await-promise
const user = await this.findOne({ where: { nickname } });
if (!user) {
throw new exception_1.NotFound({ msg: "用户不存在" });
}
// @ts-ignore
if (!user.checkPassword(password)) {
throw new exception_1.AuthFailed({ msg: "密码错误,请输入正确密码" });
}
return user;
};
// @ts-ignore
exports.User.prototype.checkPassword = function (raw) {
if (!this.password || this.password === "") {
return false;
}
return passwordHash_1.verify(raw, this.password);
};
// @ts-ignore
exports.User.prototype.softDelete = function () {
this.delete_time = new Date();
this.save();
};
// @ts-ignore
exports.User.prototype.toJSON = function () {
const origin = {
id: this.id,
nickname: this.nickname,
admin: this.admin,
active: this.active,
email: this.email,
group_id: this.groupId,
create_time: this.create_time
};
if (lodash_1.has(this, "auths")) {
return Object.assign({}, origin, { auths: lodash_1.get(this, "auths", []) });
}
else if (lodash_1.has(this, "groupName")) {
return Object.assign({}, origin, { group_name: lodash_1.get(this, "groupName", "") });
}
else {
return origin;
}
};
// @ts-ignore
exports.User.prototype.resetPassword = function (newPassword) {
// 注意,重置密码后记得提交至数据库
this.password = newPassword;
};
// @ts-ignore
exports.User.prototype.changePassword = function (oldPassword, newPassword) {
if (this.checkPassword(oldPassword)) {
this.password = newPassword;
return true;
}
return false;
};
/**
* 权限系统中的Group模型
*/
exports.Group = db_1.db.define("lin_group", Object.assign({}, interface_1.GroupInterface.attributes), lodash_1.merge({
tableName: "lin_group"
}, interface_1.GroupInterface.options));
// @ts-ignore
exports.Group.prototype.toJSON = function () {
let origin = {
id: this.id,
name: this.name,
info: this.info
};
return lodash_1.has(this, "auths")
? Object.assign({}, origin, { auths: lodash_1.get(this, "auths", []) }) : origin;
};
/**
* 权限系统中的Auth模型
*/
exports.Auth = db_1.db.define("lin_auth", Object.assign({}, interface_1.AuthInterface.attributes), lodash_1.merge({
tableName: "lin_auth"
}, interface_1.AuthInterface.options));
// @ts-ignore
exports.Auth.prototype.toJSON = function () {
return {
id: this.id,
group_id: this.group_id,
module: this.module,
auth: this.auth
};
};
exports.Log = db_1.db.define("lin_log", Object.assign({}, interface_1.LogInterface.attributes), lodash_1.merge({
tableName: "lin_log"
}, interface_1.LogInterface.options));
// @ts-ignore
exports.Log.createLog = function (args, commit) {
// if (args) {
// Object.keys(args).forEach(arg => {
// set(log, arg, get(args, arg));
// });
// }
const log = exports.Log.build(args);
// @ts-ignore
commit && log.save();
return log;
};
// @ts-ignore
exports.Log.prototype.toJSON = function () {
let origin = {
// @ts-ignore
id: this.id,
// @ts-ignore
message: this.message,
// @ts-ignore
time: this.time,
// @ts-ignore
user_id: this.user_id,
// @ts-ignore
user_name: this.user_name,
// @ts-ignore
status_code: this.status_code,
// @ts-ignore
method: this.method,
// @ts-ignore
path: this.path,
// @ts-ignore
authority: this.authority
};
return origin;
};