-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
180 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.