User.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 AuditLogEntrySchema = new Schema({
  9. _id: false,
  10. info: { type: Object },
  11. initiatorId: { type: Schema.Types.ObjectId },
  12. ipAddress: { type: String },
  13. operation: { type: String },
  14. userId: { type: Schema.Types.ObjectId },
  15. timestamp: { type: Date },
  16. })
  17. const UserSchema = new Schema({
  18. email: { type: String, default: '', maxlength: MAX_EMAIL_LENGTH },
  19. emails: [
  20. {
  21. email: { type: String, default: '', maxlength: MAX_EMAIL_LENGTH },
  22. reversedHostname: { type: String, default: '' },
  23. createdAt: {
  24. type: Date,
  25. default() {
  26. return new Date()
  27. },
  28. },
  29. confirmedAt: { type: Date },
  30. samlProviderId: { type: String },
  31. affiliationUnchecked: { type: Boolean },
  32. reconfirmedAt: { type: Date },
  33. },
  34. ],
  35. first_name: { type: String, default: '' },
  36. last_name: { type: String, default: '' },
  37. role: { type: String, default: '' },
  38. institution: { type: String, default: '' },
  39. hashedPassword: String,
  40. isAdmin: { type: Boolean, default: false },
  41. staffAccess: {
  42. publisherMetrics: { type: Boolean, default: false },
  43. publisherManagement: { type: Boolean, default: false },
  44. institutionMetrics: { type: Boolean, default: false },
  45. institutionManagement: { type: Boolean, default: false },
  46. groupMetrics: { type: Boolean, default: false },
  47. groupManagement: { type: Boolean, default: false },
  48. adminMetrics: { type: Boolean, default: false },
  49. },
  50. signUpDate: {
  51. type: Date,
  52. default() {
  53. return new Date()
  54. },
  55. },
  56. loginEpoch: { type: Number },
  57. lastActive: { type: Date },
  58. lastFailedLogin: { type: Date },
  59. lastLoggedIn: { type: Date },
  60. lastLoginIp: { type: String, default: '' },
  61. loginCount: { type: Number, default: 0 },
  62. holdingAccount: { type: Boolean, default: false },
  63. ace: {
  64. mode: { type: String, default: 'none' },
  65. theme: { type: String, default: 'textmate' },
  66. overallTheme: { type: String, default: '' },
  67. fontSize: { type: Number, default: '12' },
  68. autoComplete: { type: Boolean, default: true },
  69. autoPairDelimiters: { type: Boolean, default: true },
  70. spellCheckLanguage: { type: String, default: 'en' },
  71. pdfViewer: { type: String, default: 'pdfjs' },
  72. syntaxValidation: { type: Boolean },
  73. fontFamily: { type: String },
  74. lineHeight: { type: String },
  75. },
  76. features: {
  77. collaborators: {
  78. type: Number,
  79. default: Settings.defaultFeatures.collaborators,
  80. },
  81. versioning: { type: Boolean, default: Settings.defaultFeatures.versioning },
  82. dropbox: { type: Boolean, default: Settings.defaultFeatures.dropbox },
  83. github: { type: Boolean, default: Settings.defaultFeatures.github },
  84. gitBridge: { type: Boolean, default: Settings.defaultFeatures.gitBridge },
  85. compileTimeout: {
  86. type: Number,
  87. default: Settings.defaultFeatures.compileTimeout,
  88. },
  89. compileGroup: {
  90. type: String,
  91. default: Settings.defaultFeatures.compileGroup,
  92. },
  93. templates: { type: Boolean, default: Settings.defaultFeatures.templates },
  94. references: { type: Boolean, default: Settings.defaultFeatures.references },
  95. trackChanges: {
  96. type: Boolean,
  97. default: Settings.defaultFeatures.trackChanges,
  98. },
  99. mendeley: { type: Boolean, default: Settings.defaultFeatures.mendeley },
  100. zotero: { type: Boolean, default: Settings.defaultFeatures.zotero },
  101. referencesSearch: {
  102. type: Boolean,
  103. default: Settings.defaultFeatures.referencesSearch,
  104. },
  105. symbolPalette: {
  106. type: Boolean,
  107. default: Settings.defaultFeatures.symbolPalette,
  108. },
  109. },
  110. featuresOverrides: [
  111. {
  112. createdAt: {
  113. type: Date,
  114. default() {
  115. return new Date()
  116. },
  117. },
  118. expiresAt: { type: Date },
  119. note: { type: String },
  120. features: {
  121. collaborators: { type: Number },
  122. versioning: { type: Boolean },
  123. dropbox: { type: Boolean },
  124. github: { type: Boolean },
  125. gitBridge: { type: Boolean },
  126. compileTimeout: { type: Number },
  127. compileGroup: { type: String },
  128. templates: { type: Boolean },
  129. trackChanges: { type: Boolean },
  130. mendeley: { type: Boolean },
  131. zotero: { type: Boolean },
  132. referencesSearch: { type: Boolean },
  133. symbolPalette: { type: Boolean },
  134. },
  135. },
  136. ],
  137. featuresUpdatedAt: { type: Date },
  138. featuresEpoch: {
  139. type: String,
  140. },
  141. // when auto-merged from SL and must-reconfirm is set, we may end up using
  142. // `sharelatexHashedPassword` to recover accounts...
  143. sharelatexHashedPassword: String,
  144. must_reconfirm: { type: Boolean, default: false },
  145. referal_id: {
  146. type: String,
  147. default() {
  148. return TokenGenerator.generateReferralId()
  149. },
  150. },
  151. refered_users: [{ type: ObjectId, ref: 'User' }],
  152. refered_user_count: { type: Number, default: 0 },
  153. refProviders: {
  154. // The actual values are managed by third-party-references.
  155. mendeley: Schema.Types.Mixed,
  156. zotero: Schema.Types.Mixed,
  157. },
  158. alphaProgram: { type: Boolean, default: false }, // experimental features
  159. betaProgram: { type: Boolean, default: false },
  160. overleaf: {
  161. id: { type: Number },
  162. accessToken: { type: String },
  163. refreshToken: { type: String },
  164. },
  165. awareOfV2: { type: Boolean, default: false },
  166. samlIdentifiers: { type: Array, default: [] },
  167. thirdPartyIdentifiers: { type: Array, default: [] },
  168. migratedAt: { type: Date },
  169. twoFactorAuthentication: {
  170. createdAt: { type: Date },
  171. enrolledAt: { type: Date },
  172. secret: { type: String },
  173. },
  174. onboardingEmailSentAt: { type: Date },
  175. auditLog: [AuditLogEntrySchema],
  176. splitTests: Schema.Types.Mixed,
  177. analyticsId: { type: String },
  178. })
  179. function formatSplitTestsSchema(next) {
  180. if (this.splitTests) {
  181. for (const splitTestKey of Object.keys(this.splitTests)) {
  182. for (const variantIndex in this.splitTests[splitTestKey]) {
  183. this.splitTests[splitTestKey][variantIndex].assignedAt = new Date(
  184. this.splitTests[splitTestKey][variantIndex].assignedAt
  185. )
  186. }
  187. }
  188. }
  189. next()
  190. }
  191. UserSchema.pre('save', formatSplitTestsSchema)
  192. exports.User = mongoose.model('User', UserSchema)
  193. exports.UserSchema = UserSchema