Skip to content

Commit

Permalink
fix: 逻辑修正
Browse files Browse the repository at this point in the history
  • Loading branch information
afzw committed Dec 13, 2023
1 parent a75cbb6 commit 0f9a2c1
Show file tree
Hide file tree
Showing 20 changed files with 180 additions and 188 deletions.
14 changes: 14 additions & 0 deletions src/apis/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Request, Response, NextFunction } from 'express'

class AuthController {
/**
* 获取个人简介
*/
public static async getProfile(req: Request, res: Response, next: NextFunction) {
const user = req.user

return res.send({ user })
}
}

export { AuthController }
14 changes: 9 additions & 5 deletions src/apis/auth/auth.route.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import { LocalAuthController } from '@/apis/auth/local-auth.controller'
import { AuthController } from './auth.controller'

/** 公共路由 */
const AuthRoutes: App.Route[] = [
// 登录
const authRoutes: App.Route[] = [
{
path: '/login',
method: 'POST',
middlewares: [LocalAuthController.login],
permission: 'public'
},
// 注册
{
path: '/register',
method: 'POST',
middlewares: [LocalAuthController.register],
permission: 'public'
},
// 登出
{
path: '/logout',
method: 'POST',
middlewares: [LocalAuthController.register],
permission: 'public'
},
{
path: '/profile',
method: 'GET',
middlewares: [AuthController.getProfile],
permission: 'user'
}
]

export { AuthRoutes }
export { authRoutes }
11 changes: 4 additions & 7 deletions src/apis/auth/local-auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { NextFunction, Request, Response } from 'express'
import sessionInfoDao from '@/dao/sessionInfo.dao'
import { SessionInfoProps } from '@/entities/sessionInfo.model'
import { UserFilter, UserProps } from '@/entities/auth/user.model'
import UserStore from '@/business/user/user.store'
import { LocalAuthStore } from '@/business/auth/local/local-auth.store'
import { AppError } from '@/lib/error'
import { encryptStringUsingSH512, genRandom32BitsHexString, genUniqString, genPBK } from '@/lib/encryption/crypto'
import { genRandom32BitsHexString, genUniqString, genPBK } from '@/lib/encryption/crypto'
import { Types } from 'mongoose'

/** 用户登录信息 */
Expand Down Expand Up @@ -56,9 +55,7 @@ class LocalAuthController {
const [createSessionInfoErr] = await callAsync(sessionInfoDao.create(sessionInfoProps))
if (createSessionInfoErr) console.log(`记录sessionInfo失败 => ${createSessionInfoErr}`)

const profile = _.pick(user, UserStore.theProfileKeys())

return res.send(profile)
return res.sendStatus(200)
})
}

Expand All @@ -83,10 +80,10 @@ class LocalAuthController {
password,
salt
}
const [createUserErr, user] = await callAsync(UserDao.create(newUserProps))
const [createUserErr] = await callAsync(UserDao.create(newUserProps))
if (createUserErr) return next(new AppError({ message: `创建用户失败 => ${createUserErr} ` }))

return res.send(user)
return res.sendStatus(200)
}

public static async logout(req: Request<null, number, null>, res: Response, next: NextFunction) {
Expand Down
17 changes: 17 additions & 0 deletions src/lib/http/cookie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* 将cookie字符串转为Map
* @param {string} cookieStr - cookie字符串
* @returns {Map} cookie Map
*/
export function cookieStr2Map(cookieStr: string): Map<string, string> {
const cookie = new Map<string, string>()
cookieStr.split(';').forEach(item => {
if (!item) return
const kv = item.split('=')
const key = kv[0].trim()
const val = kv[1].trim()
cookie.set(key, val)
})

return cookie
}
3 changes: 0 additions & 3 deletions src/loaders/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { Redis } from 'ioredis'
import RedisStore from 'connect-redis'

import { config } from '@/_config/config'
import { permissionValidatorRegister } from '@/loaders/rbac/validator'
import { localSerialize, localDeserialize } from '@/loaders/auth/local-auth'

/**
Expand Down Expand Up @@ -58,8 +57,6 @@ function loadExpress(app: express.Express) {
/** passport 本地策略 */
passport.serializeUser(localSerialize)
passport.deserializeUser(localDeserialize)
/** 注册权限校验器 */
app.use(permissionValidatorRegister)
}

export default loadExpress
2 changes: 0 additions & 2 deletions src/loaders/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import express from 'express'
import loadMongoDB from './mongo'
import loadExpress from './express'
import initRbac from './rbac'
import loadRouters from './router'
import { executScripts } from './script'
import { makeLogDir } from './log'
Expand All @@ -21,7 +20,6 @@ async function initApp(app: express.Express, config: App.Config) {
loadRouters(app)

// 业务逻辑初始化
initRbac()
await executScripts(config.scriptDir)

// 定时任务
Expand Down
45 changes: 37 additions & 8 deletions src/loaders/rbac/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
import { __roles } from './roles'
/** 系统角色 */
enum SysRole {
/** 匿名用户(游客) */
ANON = 'anon',
/** 普通用户 */
USER = 'user',
/** 系统管理员 */
ADMIN = 'admin'
}

/** 系统权限 */
enum SysPerm {
PUBLIC = 'public',
OPEN = 'open',
ANON = 'anon',
USER = 'user',
ADMIN = 'admin'
}

/**
* 全局变量——系统角色
*/
const __rolesMap = new Map<string, App.Role>()
__rolesMap.set(SysRole.ANON, { permissions: [SysPerm.ANON] })
__rolesMap.set(SysRole.USER, { permissions: [SysPerm.USER] })
__rolesMap.set(SysRole.ADMIN, { permissions: [SysPerm.ADMIN, SysPerm.USER] })

/**
* 获取某角色的权限
*/
function getPermsByRole(role: string): string[] {
return __rolesMap.get(role).permissions
}

/**
* 【初始化】加载角色权限系统
* 判断
*/
async function loadRbac() {
// 每一个角色添加“公开”权限
for (const roleName in __roles) {
__roles[roleName]['permissions'].unshift('public')
}
function judgeRoleHasPerm(role: string, perm: string): boolean {
return __rolesMap.get(role).permissions.includes(perm)
}

export default loadRbac
export { SysRole, SysPerm, getPermsByRole, judgeRoleHasPerm }
20 changes: 0 additions & 20 deletions src/loaders/rbac/roles.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/loaders/rbac/validator.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/loaders/router/admin.route.ts

This file was deleted.

32 changes: 16 additions & 16 deletions src/loaders/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import express from 'express'

import registerRouter from '@/loaders/router/register'
import { registerRoutes } from '@/loaders/router/register'
import errorHandler from '../errorHandler'

import __adminRoutes from '@/loaders/router/admin.route'
import __moduleRoutes from '@/loaders/router/module.route'
import __publicRoutes from '@/loaders/router/public.route'

// 路由字典
export const routesMap = new Map()
import { __adminRoutes } from '@/loaders/router/routes/admin.route'
import { __userRoutes } from '@/loaders/router/routes/user.route'
import { __publicRoutes } from '@/loaders/router/routes/public.route'
import { __openRoutes } from '@/loaders/router/routes/open.route'

/**
* 初始化路由器
* 加载路由
*/
function loadRouters(app: express.Express) {
// 监听系统管理层级路由
const adminRouter = express.Router()
registerRouter(adminRouter, __adminRoutes, 'admin')
registerRoutes(adminRouter, __adminRoutes, 'admin')
app.use('/', adminRouter)

const userRouter = express.Router()
registerRoutes(userRouter, __userRoutes, 'user')
app.use('/', userRouter)

// 监听业务路由
const moduleRouter = express.Router()
registerRouter(moduleRouter, __moduleRoutes, 'user')
app.use('/', moduleRouter)
const openRouter = express.Router()
registerRoutes(openRouter, __openRoutes, 'open')
app.use('/', openRouter)

// 监听公开路由
const publicRouter = express.Router()
registerRouter(publicRouter, __publicRoutes, 'public')
registerRoutes(publicRouter, __publicRoutes, 'public')
app.use('/', publicRouter)

// 添加错误处理器
Expand Down
24 changes: 0 additions & 24 deletions src/loaders/router/limiter.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/loaders/router/module.route.ts

This file was deleted.

Loading

0 comments on commit 0f9a2c1

Please sign in to comment.