From a12409ff5bb922ddab70a61ce5c9b7c5740c7441 Mon Sep 17 00:00:00 2001 From: liujiangyong Date: Fri, 13 Jan 2023 14:27:28 +0800 Subject: [PATCH] init org mng table structure --- api/app/lib/controllers/auth/index.js | 140 ++++++++-------------- api/app/lib/index.js | 78 ++---------- api/app/lib/models/department.js | 33 ++--- api/app/lib/models/resource.js | 22 ++-- api/app/lib/models/user.js | 84 +++++-------- api/app/lib/models/user_resource.js | 52 ++++++++ api/app/lib/models/user_token.js | 3 +- script/1.0.0/data/1.admin_user.sql | 1 + script/1.0.0/schema/1.init_inspection.sql | 75 ++++++++++++ 9 files changed, 239 insertions(+), 249 deletions(-) create mode 100644 api/app/lib/models/user_resource.js create mode 100644 script/1.0.0/data/1.admin_user.sql create mode 100644 script/1.0.0/schema/1.init_inspection.sql diff --git a/api/app/lib/controllers/auth/index.js b/api/app/lib/controllers/auth/index.js index ea6a868..a03c824 100644 --- a/api/app/lib/controllers/auth/index.js +++ b/api/app/lib/controllers/auth/index.js @@ -6,113 +6,79 @@ const moment = require('moment'); const uuid = require('uuid'); async function login(ctx, next) { - // const transaction = await ctx.fs.dc.orm.transaction(); try { + const transaction = await ctx.fs.dc.orm.transaction(); + const models = ctx.fs.dc.models; const params = ctx.request.body; + let password = Hex.stringify(MD5(params.password)); + const userRes = await models.User.findOne({ + where: { + username: params.username, + password: password, + delete: false, + enable: true + }, + attributes: { exclude: ['password'] }, + include: [{ + attributes: ["resourceId"], + model: models.UserResource + }] + }); - let userRes = null - if (params.username && params.password) { - const password = Hex.stringify(MD5(params.password)); - userRes = await models.User.findOne({ - attributes: { exclude: ['password'] }, - where: { - userName: params.username, - password: password, - del: false, - }, - include: [{ - model: models.UserDepartment, - include: [{ - model: models.Department, - attributes: ['id', 'name'], - include: [{ - model: models.Company, - attributes: ['id', 'name'], - }] - }] - }, { - model: models.UserPost, - include: [{ - model: models.Post, - attributes: ['id', 'name'], - }] - }, { - model: models.Role, - }] - }); - } else if (params.phone && params.code) { - const record = await models.PhoneValidateCode.findOne({ - where: { - phone: phone, - code: code - } - }); - if (!record) { - throw '验证码错误' - } else if (record.expired < new Date()) { - throw '验证码已失效' + if (!userRes) { + ctx.status = 400; + ctx.body = { + "message": "账号或密码错误" } - userRes = await models.User.findOne({ - attributes: { exclude: ['password'] }, - where: { - tel: phone, - del: false, - }, - include: [{ - model: models.UserDepartment, - include: [{ - model: models.Department, - attributes: ['id', 'name'], - include: [{ - model: models.Company, - attributes: ['id', 'name'], - }] - }] - }, { - model: models.UserPost, - include: [{ - model: models.Post, - attributes: ['id', 'name'], - }] - }, { - model: models.Role, - }] - }); } - if (userRes) { - if (!userRes.state) { - throw '用户已禁用' + if (userRes) + if (userRes && !userRes.enable) { + ctx.status = 400; + ctx.body = { message: "该用户已被禁用" } } else { const token = uuid.v4(); - const userInfo = { + let deptInfo = null; + if (userRes) { + const { departmentId } = userRes.dataValues; + deptInfo = await models.Department.findOne({ + where: { + id: departmentId + } + }) + } + + if (!userRes) { + ctx.status = 400; + ctx.body = { message: "暂无登录权限,请联系管理员" } + return; + } + + let userData = userRes.dataValues; + let userRslt = Object.assign(userData, { authorized: true, - ...userRes.dataValues, - token, - }; - // 记录token - const expiredDay = 7; - const expired = moment().add(expiredDay, 'day').format('YYYY-MM-DD HH:mm:ss') + token: token, + userResources: userRes ? userRes.userResources.map(r => r.resourceId) : [], + type: deptInfo ? deptInfo.type : '' + }); await models.UserToken.create({ token: token, - userInfo: userInfo, - expired: expired + userInfo: userRslt, + expired: moment().add(30, 'days').format() }); ctx.status = 200; - ctx.body = userInfo; + ctx.body = userRslt; } - } else { - throw '账号或密码错误' - } - // await transaction.commit(); + await transaction.commit(); } catch (error) { - // await transaction.rollback(); + await transaction.rollback(); + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { - message: typeof error == 'string' ? error : '登录失败' + "message": "登录失败" } } } diff --git a/api/app/lib/index.js b/api/app/lib/index.js index 6956253..b661b8a 100644 --- a/api/app/lib/index.js +++ b/api/app/lib/index.js @@ -53,77 +53,17 @@ module.exports.models = function (dc) { // dc = { orm: Sequelize对象, ORM: Seq require(`./models/${filename}`)(dc) }); - const { Camera,Company, Department, Post, RoleGroup, Role, RoleResource, User, UserDepartment, UserPost, Site, ProjectDisclosure, ProjectDisclosureFiles, Coordinate, ProblemReport, ProblemReportFile, Worker, WorkerAttendance, - RiskReport, Metting, HideDangerRectify, HideDangerRectifySites, HideDangerDispose + const { Camera, Company, Department, Post, RoleGroup, Role, RoleResource, User, UserDepartment, UserPost, Site, ProjectDisclosure, ProjectDisclosureFiles, Coordinate, ProblemReport, ProblemReportFile, Worker, WorkerAttendance, + RiskReport, Metting, HideDangerRectify, HideDangerRectifySites, HideDangerDispose, UserResource, Resource } = dc.models; - Metting.belongsTo(User, { foreignKey: 'submitUser', targetKey: 'id' }); - User.hasMany(Metting, { foreignKey: 'submitUser', sourceKey: 'id' }); + UserResource.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' }); + User.hasMany(UserResource, { foreignKey: 'userId', sourceKey: 'id' }); - Department.belongsTo(Company, { foreignKey: 'companyId', targetKey: 'id' }); - Company.hasMany(Department, { foreignKey: 'companyId', sourceKey: 'id' }); + UserResource.belongsTo(Resource, { foreignKey: 'resourceId', targetKey: 'code' }); + Resource.hasMany(UserResource, { foreignKey: 'resourceId', sourceKey: 'code' }); + Resource.hasMany(Resource, { foreignKey: 'parentResource', sourceKey: 'code' }); - Post.belongsTo(Company, { foreignKey: 'companyId', targetKey: 'id' }); - Company.hasMany(Post, { foreignKey: 'companyId', sourceKey: 'id' }); - Post.belongsTo(Department, { foreignKey: 'departmentId', targetKey: 'id' }); - Department.hasMany(Post, { foreignKey: 'departmentId', sourceKey: 'id' }); - - Role.belongsTo(RoleGroup, { foreignKey: 'roleGroupId', targetKey: 'id' }); - RoleGroup.hasMany(Role, { foreignKey: 'roleGroupId', sourceKey: 'id' }); - - RoleResource.belongsTo(Role, { foreignKey: 'roleId', targetKey: 'id' }); - Role.hasMany(RoleResource, { foreignKey: 'roleId', sourceKey: 'id' }); - - UserDepartment.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' }); - User.hasMany(UserDepartment, { foreignKey: 'userId', sourceKey: 'id' }); - UserDepartment.belongsTo(Department, { foreignKey: 'departmentId', targetKey: 'id' }); - Department.hasMany(UserDepartment, { foreignKey: 'departmentId', sourceKey: 'id' }); - - UserPost.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' }); - User.hasMany(UserPost, { foreignKey: 'userId', sourceKey: 'id' }); - UserPost.belongsTo(Post, { foreignKey: 'postId', targetKey: 'id' }); - Post.hasMany(UserPost, { foreignKey: 'postId', sourceKey: 'id' }); - User.belongsTo(Role, { foreignKey: 'roleId', targetKey: 'id' }); - Role.hasMany(User, { foreignKey: 'roleId', sourceKey: 'id' }); - - ProjectDisclosure.belongsTo(Site, { foreignKey: 'siteId', targetKey: 'id' }); - Site.hasMany(ProjectDisclosure, { foreignKey: 'siteId', sourceKey: 'id' }); - ProjectDisclosure.belongsTo(User, { foreignKey: 'submiter', targetKey: 'id' }); - User.hasMany(ProjectDisclosure, { foreignKey: 'submiter', sourceKey: 'id' }); - - ProjectDisclosureFiles.belongsTo(ProjectDisclosure, { foreignKey: 'projectDisclosureId', targetKey: 'id' }); - ProjectDisclosure.hasMany(ProjectDisclosureFiles, { foreignKey: 'projectDisclosureId', sourceKey: 'id' }); - - ProblemReport.belongsTo(User, { foreignKey: 'reporter', sourceKey: 'id' }); - ProblemReport.hasMany(ProblemReportFile, { foreignKey: 'reportId', sourceKey: 'id' }); - - ProblemReport.belongsTo(Site, { foreignKey: 'siteId', sourceKey: 'id' }); - Site.hasMany(ProblemReport, { foreignKey: 'siteId', sourceKey: 'id' }); - - Site.belongsTo(Company, { foreignKey: 'companyId', targetKey: 'id' }); - Company.hasMany(Site, { foreignKey: 'companyId', sourceKey: 'id' }); - - Coordinate.belongsTo(Site, { foreignKey: 'siteId', targetKey: 'id' }); - Site.hasMany(Coordinate, { foreignKey: 'siteId', sourceKey: 'id' }); - - WorkerAttendance.belongsTo(Worker, { foreignKey: 'workerId', targetKey: 'id' }); - Worker.hasMany(WorkerAttendance, { foreignKey: 'workerId', sourceKey: 'id' }); - - RiskReport.belongsTo(Site, { foreignKey: 'siteId', targetKey: 'id' }); - Site.hasMany(RiskReport, { foreignKey: 'siteId', sourceKey: 'id' }); - - HideDangerRectifySites.belongsTo(HideDangerRectify, { foreignKey: 'rectifyId', targetKey: 'id' }); - HideDangerRectify.hasMany(HideDangerRectifySites, { foreignKey: 'rectifyId', sourceKey: 'id' }); - - HideDangerRectifySites.belongsTo(Site, { foreignKey: 'siteId', targetKey: 'id' }); - Site.hasMany(HideDangerRectifySites, { foreignKey: 'siteId', sourceKey: 'id' }); - - HideDangerDispose.belongsTo(HideDangerRectifySites, { foreignKey: 'rectifySiteId', targetKey: 'id' }); - HideDangerRectifySites.hasMany(HideDangerDispose, { foreignKey: 'rectifySiteId', sourceKey: 'id' }); - - HideDangerDispose.belongsTo(User, { foreignKey: 'disposeUser', targetKey: 'id' }); - User.hasMany(HideDangerDispose, { foreignKey: 'disposeUser', sourceKey: 'id' }); - - Camera.belongsTo(Site, { foreignKey: 'siteId', targetKey: 'id' }); - Site.hasMany(Camera, { foreignKey: 'siteId', sourceKey: 'id' }); + User.belongsTo(Department, { foreignKey: 'departmentId', targetKey: 'id' }); + Department.hasMany(User, { foreignKey: 'departmentId', sourceKey: 'id' }); }; diff --git a/api/app/lib/models/department.js b/api/app/lib/models/department.js index 17e69f6..2657f84 100644 --- a/api/app/lib/models/department.js +++ b/api/app/lib/models/department.js @@ -24,35 +24,22 @@ module.exports = dc => { field: "name", autoIncrement: false }, - companyId: { + dependence: { type: DataTypes.INTEGER, - allowNull: false, - defaultValue: null, - comment: null, - primaryKey: false, - field: "company_id", - autoIncrement: false, - references: { - key: "id", - model: "tCompany" - } - }, - relateSites: { - type: DataTypes.ARRAY(DataTypes.INTEGER), allowNull: true, defaultValue: null, - comment: null, + comment: "上级部门/从属", primaryKey: false, - field: "relate_sites", + field: "dependence", autoIncrement: false }, - del: { - type: DataTypes.BOOLEAN, - allowNull: true, + type: { + type: DataTypes.INTEGER, + allowNull: false, defaultValue: null, - comment: null, + comment: "市1,区县2,乡镇3,村4", primaryKey: false, - field: "del", + field: "type", autoIncrement: false } }, { @@ -60,6 +47,10 @@ module.exports = dc => { comment: "", indexes: [] }); + dc.models.Department = Department; + + + return Department; }; \ No newline at end of file diff --git a/api/app/lib/models/resource.js b/api/app/lib/models/resource.js index db258e0..f8ab18c 100644 --- a/api/app/lib/models/resource.js +++ b/api/app/lib/models/resource.js @@ -5,24 +5,15 @@ module.exports = dc => { const DataTypes = dc.ORM; const sequelize = dc.orm; const Resource = sequelize.define("resource", { - id: { - type: DataTypes.INTEGER, - allowNull: false, - defaultValue: null, - comment: null, - primaryKey: true, - field: "id", - autoIncrement: true, - unique: "resource_id_uindex" - }, code: { type: DataTypes.STRING, allowNull: false, defaultValue: null, comment: null, - primaryKey: false, + primaryKey: true, field: "code", - autoIncrement: false + autoIncrement: false, + unique: "resource_code_uindex" }, name: { type: DataTypes.STRING, @@ -31,15 +22,16 @@ module.exports = dc => { comment: null, primaryKey: false, field: "name", - autoIncrement: false + autoIncrement: false, + unique: "resource_name_uindex" }, - parentCode: { + parentResource: { type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: null, primaryKey: false, - field: "parent_code", + field: "parent_resource", autoIncrement: false } }, { diff --git a/api/app/lib/models/user.js b/api/app/lib/models/user.js index 8f687a5..3ea7040 100644 --- a/api/app/lib/models/user.js +++ b/api/app/lib/models/user.js @@ -15,49 +15,40 @@ module.exports = dc => { autoIncrement: true, unique: "user_id_uindex" }, - photo: { - type: DataTypes.TEXT, - allowNull: true, - defaultValue: null, - comment: null, - primaryKey: false, - field: "photo", - autoIncrement: false - }, - userName: { + name: { type: DataTypes.STRING, allowNull: false, defaultValue: null, comment: null, primaryKey: false, - field: "user_name", + field: "name", autoIncrement: false }, - password: { + username: { type: DataTypes.STRING, allowNull: false, defaultValue: null, - comment: null, + comment: "用户名 账号", primaryKey: false, - field: "password", + field: "username", autoIncrement: false }, - displayName: { + password: { type: DataTypes.STRING, allowNull: false, defaultValue: null, comment: null, primaryKey: false, - field: "display_name", + field: "password", autoIncrement: false }, - tel: { - type: DataTypes.STRING, + departmentId: { + type: DataTypes.INTEGER, allowNull: false, defaultValue: null, - comment: null, + comment: "部门id", primaryKey: false, - field: "tel", + field: "department_id", autoIncrement: false }, email: { @@ -69,58 +60,40 @@ module.exports = dc => { field: "email", autoIncrement: false }, - gender: { - type: DataTypes.STRING, - allowNull: true, - defaultValue: null, - comment: null, - primaryKey: false, - field: "gender", - autoIncrement: false - }, - state: { + enable: { type: DataTypes.BOOLEAN, - allowNull: true, - defaultValue: null, - comment: null, - primaryKey: false, - field: "state", - autoIncrement: false - }, - roleId: { - type: DataTypes.INTEGER, - allowNull: true, + allowNull: false, defaultValue: null, - comment: null, + comment: "启用状态", primaryKey: false, - field: "role_id", + field: "enable", autoIncrement: false }, - relateSites: { - type: DataTypes.ARRAY(DataTypes.INTEGER), - allowNull: true, + delete: { + type: DataTypes.BOOLEAN, + allowNull: false, defaultValue: null, comment: null, primaryKey: false, - field: "relate_sites", + field: "delete", autoIncrement: false }, - del: { - type: DataTypes.BOOLEAN, - allowNull: true, + phone: { + type: DataTypes.STRING, + allowNull: false, defaultValue: null, - comment: null, + comment: "手机号(小程序使用手机号登录)", primaryKey: false, - field: "del", + field: "phone", autoIncrement: false }, - isSuper: { - type: DataTypes.BOOLEAN, + post: { + type: DataTypes.STRING, allowNull: true, defaultValue: null, - comment: null, + comment: "职位", primaryKey: false, - field: "is_super", + field: "post", autoIncrement: false } }, { @@ -128,6 +101,7 @@ module.exports = dc => { comment: "", indexes: [] }); + dc.models.User = User; return User; }; \ No newline at end of file diff --git a/api/app/lib/models/user_resource.js b/api/app/lib/models/user_resource.js new file mode 100644 index 0000000..dc8581b --- /dev/null +++ b/api/app/lib/models/user_resource.js @@ -0,0 +1,52 @@ +/* eslint-disable*/ +'use strict'; + +module.exports = dc => { + const DataTypes = dc.ORM; + const sequelize = dc.orm; + const UserResource = sequelize.define("userResource", { + id: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: null, + comment: null, + primaryKey: true, + field: "id", + autoIncrement: true, + unique: "post_resource_id_uindex" + }, + userId: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: null, + comment: null, + primaryKey: false, + field: "user_id", + autoIncrement: false, + references: { + key: "id", + model: "post" + } + }, + resourceId: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: null, + comment: null, + primaryKey: false, + field: "resource", + autoIncrement: false, + references: { + key: "code", + model: "resource" + } + } + }, { + tableName: "user_resource", + comment: "", + indexes: [] + }); + + dc.models.UserResource = UserResource; + return UserResource; +}; \ No newline at end of file diff --git a/api/app/lib/models/user_token.js b/api/app/lib/models/user_token.js index 569a0ce..3f2354e 100644 --- a/api/app/lib/models/user_token.js +++ b/api/app/lib/models/user_token.js @@ -1,5 +1,4 @@ /* eslint-disable*/ - 'use strict'; module.exports = dc => { @@ -7,7 +6,7 @@ module.exports = dc => { const sequelize = dc.orm; const UserToken = sequelize.define("userToken", { token: { - type: DataTypes.STRING, + type: DataTypes.UUIDV4, allowNull: false, defaultValue: null, comment: null, diff --git a/script/1.0.0/data/1.admin_user.sql b/script/1.0.0/data/1.admin_user.sql new file mode 100644 index 0000000..b779175 --- /dev/null +++ b/script/1.0.0/data/1.admin_user.sql @@ -0,0 +1 @@ +INSERT INTO "public"."user" VALUES (1, '管理员', 'SuperAdmin', 'e10adc3949ba59abbe56e057f20f883e', 1, NULL, 't', 'f', '123456789', NULL); diff --git a/script/1.0.0/schema/1.init_inspection.sql b/script/1.0.0/schema/1.init_inspection.sql new file mode 100644 index 0000000..b3b0c57 --- /dev/null +++ b/script/1.0.0/schema/1.init_inspection.sql @@ -0,0 +1,75 @@ +DROP SEQUENCE if EXISTS "public"."user_id_seq"; +CREATE SEQUENCE "public"."user_id_seq" +INCREMENT 1 +MINVALUE 1 +MAXVALUE 9223372036854775807 +START 1 +CACHE 1; +DROP TABLE IF EXISTS "public"."user"; +CREATE TABLE "public"."user" ( + "id" int4 NOT NULL DEFAULT nextval('user_id_seq'::regclass), + "name" varchar(64) COLLATE "pg_catalog"."default" NOT NULL, + "username" varchar(64) COLLATE "pg_catalog"."default" NOT NULL, + "password" varchar(512) COLLATE "pg_catalog"."default" NOT NULL, + "department_id" int4 NOT NULL, + "email" varchar(128) COLLATE "pg_catalog"."default", + "enable" bool NOT NULL DEFAULT true, + "delete" bool NOT NULL DEFAULT false, + "phone" varchar(20) COLLATE "pg_catalog"."default" NOT NULL, + "post" varchar(64) COLLATE "pg_catalog"."default" +); +COMMENT ON COLUMN "public"."user"."username" IS '用户名 账号'; +COMMENT ON COLUMN "public"."user"."department_id" IS '部门id'; +COMMENT ON COLUMN "public"."user"."enable" IS '启用状态'; +COMMENT ON COLUMN "public"."user"."phone" IS '手机号(小程序使用手机号登录)'; +COMMENT ON COLUMN "public"."user"."post" IS '职位'; + + +DROP TABLE IF EXISTS "public"."resource"; +CREATE TABLE "public"."resource" ( + "code" varchar(128) COLLATE "pg_catalog"."default" NOT NULL, + "name" varchar(128) COLLATE "pg_catalog"."default" NOT NULL, + "parent_resource" varchar(128) COLLATE "pg_catalog"."default" +); +COMMENT ON TABLE "public"."resource" IS '权限字典'; + + +DROP SEQUENCE if EXISTS "public"."user_resource_id_seq"; +CREATE SEQUENCE "public"."user_resource_id_seq" +INCREMENT 1 +MINVALUE 1 +MAXVALUE 9223372036854775807 +START 1 +CACHE 1; +DROP TABLE IF EXISTS "public"."user_resource"; +CREATE TABLE "public"."user_resource" ( + "id" int4 NOT NULL DEFAULT nextval('user_resource_id_seq'::regclass), + "user_id" int4 NOT NULL, + "resource" varchar(128) COLLATE "pg_catalog"."default" NOT NULL +); + + +DROP TABLE IF EXISTS "public"."user_token"; +CREATE TABLE "public"."user_token" ( + "token" uuid NOT NULL, + "user_info" jsonb NOT NULL, + "expired" timestamptz(6) NOT NULL +); + + +DROP SEQUENCE if EXISTS "public"."department_id_seq"; +CREATE SEQUENCE "public"."department_id_seq" +INCREMENT 1 +MINVALUE 1 +MAXVALUE 9223372036854775807 +START 1 +CACHE 1; +DROP TABLE IF EXISTS "public"."department"; +CREATE TABLE "public"."department" ( + "id" int4 NOT NULL DEFAULT nextval('department_id_seq'::regclass), + "name" varchar(128) COLLATE "pg_catalog"."default" NOT NULL, + "dependence" int4, + "type" int4 NOT NULL +); +COMMENT ON COLUMN "public"."department"."dependence" IS '上级部门/从属'; +-- COMMENT ON COLUMN "public"."department"."type" IS '市1,区县2,乡镇3,村4';