|
|
@ -1,47 +1,54 @@ |
|
|
|
import { Request, Response, Router } from 'express'; |
|
|
|
import { Request, Response, Router } from 'express' |
|
|
|
import { OrgUserRoles } from '../../../enums/OrgUserRoles'; |
|
|
|
import { OrgUserRoles } from '../../../enums/OrgUserRoles' |
|
|
|
import ApiToken from '../../models/ApiToken'; |
|
|
|
import ApiToken from '../../models/ApiToken' |
|
|
|
import { Tele } from '../../utils/Tele'; |
|
|
|
import { Tele } from '../../utils/Tele' |
|
|
|
import { metaApiMetrics } from '../helpers/apiMetrics'; |
|
|
|
import { metaApiMetrics } from '../helpers/apiMetrics' |
|
|
|
import { NcError } from '../helpers/catchError'; |
|
|
|
import { NcError } from '../helpers/catchError' |
|
|
|
import getHandler from '../helpers/getHandler'; |
|
|
|
import getHandler from '../helpers/getHandler' |
|
|
|
import ncMetaAclMw from '../helpers/ncMetaAclMw'; |
|
|
|
import ncMetaAclMw from '../helpers/ncMetaAclMw' |
|
|
|
import { PagedResponseImpl } from '../helpers/PagedResponse'; |
|
|
|
import { PagedResponseImpl } from '../helpers/PagedResponse' |
|
|
|
import { apiTokenListEE } from './ee/orgTokenApis'; |
|
|
|
import { apiTokenListEE } from './ee/orgTokenApis' |
|
|
|
|
|
|
|
|
|
|
|
async function apiTokenList(req, res) { |
|
|
|
async function apiTokenList(req, res) { |
|
|
|
const fk_user_id = req.user.id; |
|
|
|
const fk_user_id = req.user.id |
|
|
|
|
|
|
|
let includeUnmappedToken = false |
|
|
|
|
|
|
|
if (req['user'].roles.includes(OrgUserRoles.SUPER)) { |
|
|
|
|
|
|
|
includeUnmappedToken = true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
res.json( |
|
|
|
res.json( |
|
|
|
new PagedResponseImpl( |
|
|
|
new PagedResponseImpl( |
|
|
|
await ApiToken.listWithCreatedBy({ ...req.query, fk_user_id }), |
|
|
|
await ApiToken.listWithCreatedBy({ ...req.query, fk_user_id, includeUnmappedToken }), |
|
|
|
{ |
|
|
|
{ |
|
|
|
...req.query, |
|
|
|
...req.query, |
|
|
|
count: await ApiToken.count(), |
|
|
|
count: await ApiToken.count({ |
|
|
|
} |
|
|
|
includeUnmappedToken, |
|
|
|
) |
|
|
|
fk_user_id, |
|
|
|
); |
|
|
|
}), |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function apiTokenCreate(req: Request, res: Response) { |
|
|
|
export async function apiTokenCreate(req: Request, res: Response) { |
|
|
|
Tele.emit('evt', { evt_type: 'org:apiToken:created' }); |
|
|
|
Tele.emit('evt', { evt_type: 'org:apiToken:created' }) |
|
|
|
res.json(await ApiToken.insert({ ...req.body, fk_user_id: req['user'].id })); |
|
|
|
res.json(await ApiToken.insert({ ...req.body, fk_user_id: req['user'].id })) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function apiTokenDelete(req: Request, res: Response) { |
|
|
|
export async function apiTokenDelete(req: Request, res: Response) { |
|
|
|
const fk_user_id = req['user'].id; |
|
|
|
const fk_user_id = req['user'].id |
|
|
|
const apiToken = await ApiToken.getByToken(req.params.apiTokenId); |
|
|
|
const apiToken = await ApiToken.getByToken(req.params.apiTokenId) |
|
|
|
if ( |
|
|
|
if ( |
|
|
|
!req['user'].roles.includes(OrgUserRoles.SUPER) && |
|
|
|
!req['user'].roles.includes(OrgUserRoles.SUPER) && |
|
|
|
apiToken.fk_user_id !== fk_user_id |
|
|
|
apiToken.fk_user_id !== fk_user_id |
|
|
|
) { |
|
|
|
) { |
|
|
|
NcError.notFound('Token not found'); |
|
|
|
NcError.notFound('Token not found') |
|
|
|
} |
|
|
|
} |
|
|
|
Tele.emit('evt', { evt_type: 'org:apiToken:deleted' }); |
|
|
|
Tele.emit('evt', { evt_type: 'org:apiToken:deleted' }) |
|
|
|
res.json(await ApiToken.delete(req.params.token)); |
|
|
|
res.json(await ApiToken.delete(req.params.token)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const router = Router({ mergeParams: true }); |
|
|
|
const router = Router({ mergeParams: true }) |
|
|
|
|
|
|
|
|
|
|
|
router.get( |
|
|
|
router.get( |
|
|
|
'/api/v1/tokens', |
|
|
|
'/api/v1/tokens', |
|
|
@ -49,22 +56,22 @@ router.get( |
|
|
|
ncMetaAclMw(getHandler(apiTokenList, apiTokenListEE), 'apiTokenList', { |
|
|
|
ncMetaAclMw(getHandler(apiTokenList, apiTokenListEE), 'apiTokenList', { |
|
|
|
// allowedRoles: [OrgUserRoles.SUPER],
|
|
|
|
// allowedRoles: [OrgUserRoles.SUPER],
|
|
|
|
blockApiTokenAccess: true, |
|
|
|
blockApiTokenAccess: true, |
|
|
|
}) |
|
|
|
}), |
|
|
|
); |
|
|
|
) |
|
|
|
router.post( |
|
|
|
router.post( |
|
|
|
'/api/v1/tokens', |
|
|
|
'/api/v1/tokens', |
|
|
|
metaApiMetrics, |
|
|
|
metaApiMetrics, |
|
|
|
ncMetaAclMw(apiTokenCreate, 'apiTokenCreate', { |
|
|
|
ncMetaAclMw(apiTokenCreate, 'apiTokenCreate', { |
|
|
|
// allowedRoles: [OrgUserRoles.SUPER],
|
|
|
|
// allowedRoles: [OrgUserRoles.SUPER],
|
|
|
|
blockApiTokenAccess: true, |
|
|
|
blockApiTokenAccess: true, |
|
|
|
}) |
|
|
|
}), |
|
|
|
); |
|
|
|
) |
|
|
|
router.delete( |
|
|
|
router.delete( |
|
|
|
'/api/v1/tokens/:token', |
|
|
|
'/api/v1/tokens/:token', |
|
|
|
metaApiMetrics, |
|
|
|
metaApiMetrics, |
|
|
|
ncMetaAclMw(apiTokenDelete, 'apiTokenDelete', { |
|
|
|
ncMetaAclMw(apiTokenDelete, 'apiTokenDelete', { |
|
|
|
// allowedRoles: [OrgUserRoles.SUPER],
|
|
|
|
// allowedRoles: [OrgUserRoles.SUPER],
|
|
|
|
blockApiTokenAccess: true, |
|
|
|
blockApiTokenAccess: true, |
|
|
|
}) |
|
|
|
}), |
|
|
|
); |
|
|
|
) |
|
|
|
export default router; |
|
|
|
export default router |
|
|
|