ban.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. const Errors = require('../lib/errors')
  2. module.exports = (sequelize, DataTypes) => {
  3. let Ban = sequelize.define('Ban', {
  4. canCreatePosts: {
  5. type: DataTypes.BOOLEAN,
  6. defaultValue: true,
  7. validate: {
  8. isBoolean (val) {
  9. if(typeof val !== 'boolean') {
  10. throw new sequelize.ValidationError('canCreateThreads must be a boolean')
  11. }
  12. }
  13. }
  14. },
  15. canCreateThreads: {
  16. type: DataTypes.BOOLEAN,
  17. defaultValue: true,
  18. validate: {
  19. isBoolean (val) {
  20. if(typeof val !== 'boolean') {
  21. throw new sequelize.ValidationError('canCreateThreads must be a boolean')
  22. }
  23. }
  24. }
  25. },
  26. ipBanned: {
  27. type: DataTypes.BOOLEAN,
  28. defaultValue: false,
  29. validate: {
  30. isBoolean (val) {
  31. if(typeof val !== 'boolean') {
  32. throw new sequelize.ValidationError('ipBanned must be a boolean')
  33. }
  34. }
  35. }
  36. },
  37. message: {
  38. type: DataTypes.TEXT,
  39. validate: {
  40. isString (val) {
  41. if(typeof val !== 'string') {
  42. throw new sequelize.ValidationError('message must be a string')
  43. }
  44. },
  45. len: {
  46. args: [0, 1024],
  47. msg: 'message must be less than 1024 characters'
  48. }
  49. }
  50. }
  51. }, {
  52. classMethods: {
  53. associate (models) {
  54. Ban.belongsTo(models.User)
  55. },
  56. async getBanInstance (username) {
  57. let user = await sequelize.models.User.findOne({ where: { username } })
  58. let ban = await Ban.findOne({ where: { UserId: user.id } })
  59. return ban
  60. },
  61. async canCreatePosts (username) {
  62. let ban = await this.getBanInstance(username)
  63. if(ban && !ban.canCreatePosts) {
  64. throw Errors.sequelizeValidation(sequelize.Sequelize, {
  65. error: ban.message || 'You have been banned from posting'
  66. })
  67. } else {
  68. return false
  69. }
  70. },
  71. async canCreateThreads (username) {
  72. let ban = await this.getBanInstance(username)
  73. if(ban && !ban.canCreateThreads) {
  74. throw Errors.sequelizeValidation(sequelize.Sequelize, {
  75. error: ban.message || 'You have been banned from creating threads'
  76. })
  77. } else {
  78. return false
  79. }
  80. },
  81. async isIpBanned (ip, username) {
  82. let { User, Ip } = sequelize.models
  83. if(username) {
  84. let user = await User.findOne({ where: {
  85. username
  86. }})
  87. if(user && user.admin) return false
  88. }
  89. let users = await User.findAll({
  90. include: [{
  91. model: Ip,
  92. where: { ip }
  93. }]
  94. })
  95. if(!users.length) return false
  96. let ban = await Ban.findOne({ where: {
  97. UserId: {
  98. $in: users.map(u => u.id)
  99. },
  100. ipBanned: true
  101. } })
  102. if(ban) {
  103. throw Errors.sequelizeValidation(sequelize.Sequelize, {
  104. error: ban.message ||
  105. 'This IP has been banned from creating accounts or logging in'
  106. })
  107. } else {
  108. return false
  109. }
  110. }
  111. }
  112. })
  113. return Ban
  114. }