User.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. const Settings = require('@overleaf/settings')
  2. const mongoose = require('../infrastructure/Mongoose')
  3. const TokenGenerator = require('../Features/TokenGenerator/TokenGenerator')
  4. const { Schema } = mongoose
  5. const { ObjectId } = Schema
  6. // See https://stackoverflow.com/questions/386294/what-is-the-maximum-length-of-a-valid-email-address/574698#574698
  7. const MAX_EMAIL_LENGTH = 254
  8. const UserSchema = new Schema(
  9. {
  10. email: { type: String, default: '', maxlength: MAX_EMAIL_LENGTH },
  11. emails: [
  12. {
  13. email: { type: String, default: '', maxlength: MAX_EMAIL_LENGTH },
  14. reversedHostname: { type: String, default: '' },
  15. createdAt: {
  16. type: Date,
  17. default() {
  18. return new Date()
  19. },
  20. },
  21. confirmedAt: { type: Date },
  22. samlProviderId: { type: String },
  23. affiliationUnchecked: { type: Boolean },
  24. reconfirmedAt: { type: Date },
  25. },
  26. ],
  27. first_name: { type: String, default: '' },
  28. last_name: { type: String, default: '' },
  29. role: { type: String, default: '' },
  30. institution: { type: String, default: '' },
  31. hashedPassword: String,
  32. enrollment: {
  33. sso: [
  34. {
  35. groupId: {
  36. type: ObjectId,
  37. ref: 'Subscription',
  38. },
  39. linkedAt: Date,
  40. primary: { type: Boolean, default: false },
  41. },
  42. ],
  43. managedBy: {
  44. type: ObjectId,
  45. ref: 'Subscription',
  46. },
  47. enrolledAt: { type: Date },
  48. },
  49. isAdmin: { type: Boolean, default: false },
  50. staffAccess: {
  51. publisherMetrics: { type: Boolean, default: false },
  52. publisherManagement: { type: Boolean, default: false },
  53. institutionMetrics: { type: Boolean, default: false },
  54. institutionManagement: { type: Boolean, default: false },
  55. groupMetrics: { type: Boolean, default: false },
  56. groupManagement: { type: Boolean, default: false },
  57. adminMetrics: { type: Boolean, default: false },
  58. splitTestMetrics: { type: Boolean, default: false },
  59. splitTestManagement: { type: Boolean, default: false },
  60. },
  61. signUpDate: {
  62. type: Date,
  63. default() {
  64. return new Date()
  65. },
  66. },
  67. loginEpoch: { type: Number },
  68. lastActive: { type: Date },
  69. lastFailedLogin: { type: Date },
  70. lastLoggedIn: { type: Date },
  71. lastLoginIp: { type: String, default: '' },
  72. lastPrimaryEmailCheck: { type: Date },
  73. lastTrial: { type: Date },
  74. loginCount: { type: Number, default: 0 },
  75. holdingAccount: { type: Boolean, default: false },
  76. ace: {
  77. mode: { type: String, default: 'none' },
  78. theme: { type: String, default: 'textmate' },
  79. overallTheme: { type: String, default: '' },
  80. fontSize: { type: Number, default: '12' },
  81. autoComplete: { type: Boolean, default: true },
  82. autoPairDelimiters: { type: Boolean, default: true },
  83. spellCheckLanguage: { type: String, default: 'en' },
  84. pdfViewer: { type: String, default: 'pdfjs' },
  85. syntaxValidation: { type: Boolean },
  86. fontFamily: { type: String },
  87. lineHeight: { type: String },
  88. mathPreview: { type: Boolean, default: true },
  89. },
  90. features: {
  91. collaborators: {
  92. type: Number,
  93. default: Settings.defaultFeatures.collaborators,
  94. },
  95. versioning: {
  96. type: Boolean,
  97. default: Settings.defaultFeatures.versioning,
  98. },
  99. dropbox: { type: Boolean, default: Settings.defaultFeatures.dropbox },
  100. github: { type: Boolean, default: Settings.defaultFeatures.github },
  101. gitBridge: { type: Boolean, default: Settings.defaultFeatures.gitBridge },
  102. compileTimeout: {
  103. type: Number,
  104. default: Settings.defaultFeatures.compileTimeout,
  105. },
  106. compileGroup: {
  107. type: String,
  108. default: Settings.defaultFeatures.compileGroup,
  109. },
  110. references: {
  111. type: Boolean,
  112. default: Settings.defaultFeatures.references,
  113. },
  114. trackChanges: {
  115. type: Boolean,
  116. default: Settings.defaultFeatures.trackChanges,
  117. },
  118. mendeley: { type: Boolean, default: Settings.defaultFeatures.mendeley },
  119. zotero: { type: Boolean, default: Settings.defaultFeatures.zotero },
  120. referencesSearch: {
  121. type: Boolean,
  122. default: Settings.defaultFeatures.referencesSearch,
  123. },
  124. symbolPalette: {
  125. type: Boolean,
  126. default: Settings.defaultFeatures.symbolPalette,
  127. },
  128. // labs feature, which shouldnt have a default as we havent decided pricing model yet
  129. aiErrorAssistant: {
  130. type: Boolean,
  131. },
  132. },
  133. featuresOverrides: [
  134. {
  135. createdAt: {
  136. type: Date,
  137. default() {
  138. return new Date()
  139. },
  140. },
  141. expiresAt: { type: Date },
  142. note: { type: String },
  143. features: {
  144. aiErrorAssistant: { type: Boolean },
  145. collaborators: { type: Number },
  146. versioning: { type: Boolean },
  147. dropbox: { type: Boolean },
  148. github: { type: Boolean },
  149. gitBridge: { type: Boolean },
  150. compileTimeout: { type: Number },
  151. compileGroup: { type: String },
  152. templates: { type: Boolean },
  153. trackChanges: { type: Boolean },
  154. mendeley: { type: Boolean },
  155. zotero: { type: Boolean },
  156. referencesSearch: { type: Boolean },
  157. symbolPalette: { type: Boolean },
  158. compileAssistant: { type: Boolean },
  159. },
  160. },
  161. ],
  162. featuresUpdatedAt: { type: Date },
  163. featuresEpoch: {
  164. type: String,
  165. },
  166. must_reconfirm: { type: Boolean, default: false },
  167. referal_id: {
  168. type: String,
  169. default() {
  170. return TokenGenerator.generateReferralId()
  171. },
  172. },
  173. refered_users: [{ type: ObjectId, ref: 'User' }],
  174. refered_user_count: { type: Number, default: 0 },
  175. refProviders: {
  176. // The actual values are managed by third-party-references.
  177. mendeley: Schema.Types.Mixed,
  178. zotero: Schema.Types.Mixed,
  179. },
  180. writefull: {
  181. enabled: { type: Boolean, default: null },
  182. },
  183. alphaProgram: { type: Boolean, default: false }, // experimental features
  184. betaProgram: { type: Boolean, default: false },
  185. labsProgram: { type: Boolean, default: false },
  186. overleaf: {
  187. id: { type: Number },
  188. accessToken: { type: String },
  189. refreshToken: { type: String },
  190. },
  191. awareOfV2: { type: Boolean, default: false },
  192. samlIdentifiers: { type: Array, default: [] },
  193. thirdPartyIdentifiers: { type: Array, default: [] },
  194. migratedAt: { type: Date },
  195. twoFactorAuthentication: {
  196. createdAt: { type: Date },
  197. enrolledAt: { type: Date },
  198. secretEncrypted: { type: String },
  199. },
  200. onboardingEmailSentAt: { type: Date },
  201. splitTests: Schema.Types.Mixed,
  202. analyticsId: { type: String },
  203. completedTutorials: Schema.Types.Mixed,
  204. suspended: { type: Boolean },
  205. },
  206. { minimize: false }
  207. )
  208. function formatSplitTestsSchema(next) {
  209. if (this.splitTests) {
  210. for (const splitTestKey of Object.keys(this.splitTests)) {
  211. for (const variantIndex in this.splitTests[splitTestKey]) {
  212. this.splitTests[splitTestKey][variantIndex].assignedAt = new Date(
  213. this.splitTests[splitTestKey][variantIndex].assignedAt
  214. )
  215. }
  216. }
  217. }
  218. next()
  219. }
  220. UserSchema.pre('save', formatSplitTestsSchema)
  221. exports.User = mongoose.model('User', UserSchema)
  222. exports.UserSchema = UserSchema