import { CONFIG, reloadConfig } from '../../initializers/config'
import { PluginManager } from '../../lib/plugins/plugin-manager'
import { getThemeOrDefault } from '../../lib/plugins/theme-utils'
+import { Hooks } from '@server/lib/plugins/hooks'
const configRouter = express.Router()
let serverCommit: string
async function getConfig (req: express.Request, res: express.Response) {
- const allowed = await isSignupAllowed()
+ const { allowed } = await Hooks.wrapPromiseFun(
+ isSignupAllowed,
+ {},
+ 'filter:api.user.signup.allowed.result'
+ )
+
const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip)
const defaultTheme = getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME)
const isCidr = require('is-cidr')
-async function isSignupAllowed () {
+async function isSignupAllowed (): Promise<{ allowed: boolean, errorMessage?: string }> {
if (CONFIG.SIGNUP.ENABLED === false) {
- return false
+ return { allowed: false }
}
// No limit and signup is enabled
if (CONFIG.SIGNUP.LIMIT === -1) {
- return true
+ return { allowed: true }
}
const totalUsers = await UserModel.countTotal()
- return totalUsers < CONFIG.SIGNUP.LIMIT
+ return { allowed: totalUsers < CONFIG.SIGNUP.LIMIT }
}
function isSignupAllowedForCurrentIP (ip: string) {
import { doesVideoExist } from '../../helpers/middlewares'
import { UserRole } from '../../../shared/models/users'
import { MUserDefault } from '@server/typings/models'
+import { Hooks } from '@server/lib/plugins/hooks'
+import { isLocalVideoAccepted } from '@server/lib/moderation'
const usersAddValidator = [
body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
const ensureUserRegistrationAllowed = [
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
- const allowed = await isSignupAllowed()
- if (allowed === false) {
+ const allowedParams = {
+ body: req.body
+ }
+
+ const allowedResult = await Hooks.wrapPromiseFun(
+ isSignupAllowed,
+ allowedParams,
+ 'filter:api.user.signup.allowed.result'
+ )
+
+ if (allowedResult.allowed === false) {
return res.status(403)
- .json({ error: 'User registration is not enabled or user limit is reached.' })
+ .json({ error: allowedResult.errorMessage || 'User registration is not enabled or user limit is reached.' })
}
return next()
return false
}
})
+
+ registerHook({
+ target: 'filter:api.user.signup.allowed.result',
+ handler: (result, params) => {
+ if (params && params.body.email.includes('jma')) {
+ return { allowed: false, errorMessage: 'No jma' }
+ }
+
+ return result
+ }
+ })
}
async function unregister () {
getVideoWithToken,
setDefaultVideoChannel,
waitJobs,
- doubleFollow
+ doubleFollow, getConfig, registerUser
} from '../../../shared/extra-utils'
import { VideoCommentThreadTree } from '../../../shared/models/videos/video-comment.model'
import { VideoDetails } from '../../../shared/models/videos'
import { getYoutubeVideoUrl, importVideo } from '../../../shared/extra-utils/videos/video-imports'
+import { ServerConfig } from '@shared/models'
const expect = chai.expect
})
})
+ describe('Should run filter:api.user.signup.allowed.result', function () {
+
+ it('Should run on config endpoint', async function () {
+ const res = await getConfig(servers[0].url)
+ expect((res.body as ServerConfig).signup.allowed).to.be.true
+ })
+
+ it('Should allow a signup', async function () {
+ await registerUser(servers[0].url, 'john', 'password')
+ })
+
+ it('Should not allow a signup', async function () {
+ const res = await registerUser(servers[0].url, 'jma', 'password', 403)
+
+ expect(res.body.error).to.equal('No jma')
+ })
+ })
+
after(async function () {
await cleanupTests(servers)
})
// Filter result used to check if we need to auto blacklist a video
// (fired when a local or remote video is created or updated)
- 'filter:video.auto-blacklist.result': true
+ 'filter:video.auto-blacklist.result': true,
+
+ // Filter result used to check if a user can register on the instance
+ 'filter:api.user.signup.allowed.result': true
}
export type ServerFilterHookName = keyof typeof serverFilterHookObject