2020-08-07 12:36:34 +10:00
const Errors = require ( '../lib/errors' )
module . exports = ( sequelize , DataTypes ) => {
let Ban = sequelize . define ( 'Ban' , {
2020-08-11 00:49:26 +10:00
ReadOnlyMode : {
2020-08-07 12:36:34 +10:00
type : DataTypes . BOOLEAN ,
2020-08-14 21:12:56 +10:00
defaultValue : false ,
2020-08-07 12:36:34 +10:00
validate : {
isBoolean ( val ) {
if ( typeof val !== 'boolean' ) {
2020-08-11 00:49:26 +10:00
throw new sequelize . ValidationError ( 'ReadOnlyMode must be a boolean' )
2020-08-07 12:36:34 +10:00
}
}
}
} ,
ipBanned : {
type : DataTypes . BOOLEAN ,
defaultValue : false ,
validate : {
isBoolean ( val ) {
if ( typeof val !== 'boolean' ) {
throw new sequelize . ValidationError ( 'ipBanned must be a boolean' )
}
}
}
} ,
2020-09-16 03:56:30 +10:00
DisableLogin : {
type : DataTypes . BOOLEAN ,
defaultValue : false ,
validate : {
isBoolean ( val ) {
if ( typeof val !== 'boolean' ) {
throw new sequelize . ValidationError ( 'DisableLogin must be a boolean' )
}
}
}
} ,
2020-08-07 12:36:34 +10:00
message : {
type : DataTypes . TEXT ,
validate : {
isString ( val ) {
if ( typeof val !== 'string' ) {
throw new sequelize . ValidationError ( 'message must be a string' )
}
} ,
len : {
args : [ 0 , 1024 ] ,
msg : 'message must be less than 1024 characters'
}
}
}
2020-11-22 17:48:11 +11:00
} )
2020-08-07 12:36:34 +10:00
2020-11-22 17:48:11 +11:00
Ban . associate = function ( models ) {
Ban . belongsTo ( models . User )
}
Ban . getBanInstance = function ( userid ) {
let ban = Ban . findOne ( { where : { UserId : userid } } )
2020-08-07 12:36:34 +10:00
2020-11-22 17:48:11 +11:00
return ban
}
Ban . isIpBanned = function ( ip , username ) {
let { User , Ip } = sequelize . models
2020-08-07 12:36:34 +10:00
2020-11-22 17:48:11 +11:00
if ( username ) {
let user = User . findOne ( { where : {
username
} } )
if ( user && user . admin ) return false
}
2020-08-07 12:36:34 +10:00
2020-11-22 17:48:11 +11:00
let users = User . findAll ( {
include : [ {
model : Ip ,
where : { ip }
} ]
} )
if ( ! users . length ) return false
2020-08-07 12:36:34 +10:00
2020-11-22 17:48:11 +11:00
let ban = Ban . findOne ( { where : {
UserId : {
$in : users . map ( u => u . id )
} ,
ipBanned : true
} } )
2020-08-07 12:36:34 +10:00
2020-11-22 17:48:11 +11:00
if ( ban ) {
throw Errors . sequelizeValidation ( sequelize . Sequelize , {
error : ban . message ||
'This IP has been banned from creating accounts or logging in'
} )
} else {
return false
2020-08-07 12:36:34 +10:00
}
2020-11-22 17:48:11 +11:00
}
Ban . ReadOnlyMode = function ( username ) {
let ban = this . getBanInstance ( username )
2020-08-07 12:36:34 +10:00
2020-11-22 17:48:11 +11:00
if ( ban && ban . ReadOnlyMode ) {
throw Errors . sequelizeValidation ( sequelize . Sequelize , {
error : 'You have been set into Read Only mode by an Administrator (aka you\'re banned), Reason: "' + ban . message + '" If you feel like this ban is false, visit /banned' || 'You have been set into Read Only mode by an Administrator. If you would like to object to this ban, visit /banned'
} )
} else {
return false
}
}
2020-08-07 12:36:34 +10:00
return Ban
}