Merge branch 'master' into feature/upgrade-docker-deployment-flow
commit
70e2faf992
@ -0,0 +1,15 @@
|
||||
import * as _ from 'lodash'
|
||||
import { ParameterizedContext } from 'koa'
|
||||
const inTestMode = process.env.TEST_MODE === 'true'
|
||||
|
||||
|
||||
export async function isLoggedIn(ctx: ParameterizedContext<any, any>, next: () => Promise<any>) {
|
||||
if (!inTestMode && (!ctx.session || ctx.session.id == undefined)) {
|
||||
ctx.body = {
|
||||
isOk: false,
|
||||
errMsg: 'need login',
|
||||
}
|
||||
} else {
|
||||
await next()
|
||||
}
|
||||
}
|
@ -1,15 +1,36 @@
|
||||
import OrganizationService from '../../service/organization'
|
||||
import RepositoryService from '../../service/repository'
|
||||
import { Module, Interface, Property } from '../../models'
|
||||
|
||||
export enum ACCESS_TYPE { ORGANIZATION, REPOSITORY, USER }
|
||||
export enum ACCESS_TYPE { ORGANIZATION, REPOSITORY, MODULE, INTERFACE, PROPERTY, USER, ADMIN }
|
||||
const inTestMode = process.env.TEST_MODE === 'true'
|
||||
|
||||
export class AccessUtils {
|
||||
public static async canUserAccess(accessType: ACCESS_TYPE, curUserId: number, entityId: number): Promise<boolean> {
|
||||
if (inTestMode) {
|
||||
return true
|
||||
}
|
||||
if (accessType === ACCESS_TYPE.ORGANIZATION) {
|
||||
return await OrganizationService.canUserAccessOrganization(curUserId, entityId)
|
||||
} else if (accessType === ACCESS_TYPE.REPOSITORY) {
|
||||
return await RepositoryService.canUserAccessRepository(curUserId, entityId)
|
||||
} else if (accessType === ACCESS_TYPE.MODULE) {
|
||||
const mod = await Module.findByPk(entityId)
|
||||
return await RepositoryService.canUserAccessRepository(curUserId, mod.repositoryId)
|
||||
} else if (accessType === ACCESS_TYPE.INTERFACE) {
|
||||
const itf = await Interface.findByPk(entityId)
|
||||
return await RepositoryService.canUserAccessRepository(curUserId, itf.repositoryId)
|
||||
} else if (accessType === ACCESS_TYPE.PROPERTY) {
|
||||
const p = await Property.findByPk(entityId)
|
||||
return await RepositoryService.canUserAccessRepository(curUserId, p.repositoryId)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
public static isAdmin(curUserId: number) {
|
||||
if (inTestMode) {
|
||||
return true
|
||||
}
|
||||
return curUserId === 1
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
import * as schedule from 'node-schedule'
|
||||
import { Interface } from '../models'
|
||||
import { Op } from 'sequelize'
|
||||
import { DATE_CONST } from '../routes/utils/const'
|
||||
|
||||
export async function startTask() {
|
||||
|
||||
console.log(`Starting task: locker check`)
|
||||
|
||||
/**
|
||||
* 每5分钟检查lock超时
|
||||
*/
|
||||
schedule.scheduleJob('*/5 * * * *', async () => {
|
||||
// tslint:disable-next-line: no-null-keyword
|
||||
const [num] = await Interface.update({ lockerId: null }, {
|
||||
where: {
|
||||
lockerId: {
|
||||
[Op.gt]: 0,
|
||||
},
|
||||
updatedAt: {
|
||||
[Op.lt]: new Date(Date.now() - DATE_CONST.DAY),
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
num > 0 && console.log(`cleared ${num} locks`)
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue