User.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. lastActive: { type: Date },
  57. lastLoggedIn: { type: Date },
  58. lastLoginIp: { type: String, default: '' },
  59. loginCount: { type: Number, default: 0 },
  60. holdingAccount: { type: Boolean, default: false },
  61. ace: {
  62. mode: { type: String, default: 'none' },
  63. theme: { type: String, default: 'textmate' },
  64. overallTheme: { type: String, default: '' },
  65. fontSize: { type: Number, default: '12' },
  66. autoComplete: { type: Boolean, default: true },
  67. autoPairDelimiters: { type: Boolean, default: true },
  68. spellCheckLanguage: { type: String, default: 'en' },
  69. pdfViewer: { type: String, default: 'pdfjs' },
  70. syntaxValidation: { type: Boolean },
  71. fontFamily: { type: String },
  72. lineHeight: { type: String },
  73. },
  74. features: {
  75. collaborators: {
  76. type: Number,
  77. default: Settings.defaultFeatures.collaborators,
  78. },
  79. versioning: { type: Boolean, default: Settings.defaultFeatures.versioning },
  80. dropbox: { type: Boolean, default: Settings.defaultFeatures.dropbox },
  81. github: { type: Boolean, default: Settings.defaultFeatures.github },
  82. gitBridge: { type: Boolean, default: Settings.defaultFeatures.gitBridge },
  83. compileTimeout: {
  84. type: Number,
  85. default: Settings.defaultFeatures.compileTimeout,
  86. },
  87. compileGroup: {
  88. type: String,
  89. default: Settings.defaultFeatures.compileGroup,
  90. },
  91. templates: { type: Boolean, default: Settings.defaultFeatures.templates },
  92. references: { type: Boolean, default: Settings.defaultFeatures.references },
  93. trackChanges: {
  94. type: Boolean,
  95. default: Settings.defaultFeatures.trackChanges,
  96. },
  97. mendeley: { type: Boolean, default: Settings.defaultFeatures.mendeley },
  98. zotero: { type: Boolean, default: Settings.defaultFeatures.zotero },
  99. referencesSearch: {
  100. type: Boolean,
  101. default: Settings.defaultFeatures.referencesSearch,
  102. },
  103. symbolPalette: {
  104. type: Boolean,
  105. default: Settings.defaultFeatures.symbolPalette,
  106. },
  107. },
  108. featuresOverrides: [
  109. {
  110. createdAt: {
  111. type: Date,
  112. default() {
  113. return new Date()
  114. },
  115. },
  116. expiresAt: { type: Date },
  117. note: { type: String },
  118. features: {
  119. collaborators: { type: Number },
  120. versioning: { type: Boolean },
  121. dropbox: { type: Boolean },
  122. github: { type: Boolean },
  123. gitBridge: { type: Boolean },
  124. compileTimeout: { type: Number },
  125. compileGroup: { type: String },
  126. templates: { type: Boolean },
  127. trackChanges: { type: Boolean },
  128. mendeley: { type: Boolean },
  129. zotero: { type: Boolean },
  130. referencesSearch: { type: Boolean },
  131. symbolPalette: { type: Boolean },
  132. },
  133. },
  134. ],
  135. featuresUpdatedAt: { type: Date },
  136. featuresEpoch: {
  137. type: String,
  138. },
  139. // when auto-merged from SL and must-reconfirm is set, we may end up using
  140. // `sharelatexHashedPassword` to recover accounts...
  141. sharelatexHashedPassword: String,
  142. must_reconfirm: { type: Boolean, default: false },
  143. referal_id: {
  144. type: String,
  145. default() {
  146. return TokenGenerator.generateReferralId()
  147. },
  148. },
  149. refered_users: [{ type: ObjectId, ref: 'User' }],
  150. refered_user_count: { type: Number, default: 0 },
  151. refProviders: {
  152. // The actual values are managed by third-party-references.
  153. mendeley: Schema.Types.Mixed,
  154. zotero: Schema.Types.Mixed,
  155. },
  156. alphaProgram: { type: Boolean, default: false }, // experimental features
  157. betaProgram: { type: Boolean, default: false },
  158. overleaf: {
  159. id: { type: Number },
  160. accessToken: { type: String },
  161. refreshToken: { type: String },
  162. },
  163. awareOfV2: { type: Boolean, default: false },
  164. samlIdentifiers: { type: Array, default: [] },
  165. thirdPartyIdentifiers: { type: Array, default: [] },
  166. migratedAt: { type: Date },
  167. twoFactorAuthentication: {
  168. createdAt: { type: Date },
  169. enrolledAt: { type: Date },
  170. secret: { type: String },
  171. },
  172. onboardingEmailSentAt: { type: Date },
  173. auditLog: [AuditLogEntrySchema],
  174. splitTests: Schema.Types.Mixed,
  175. analyticsId: { type: String },
  176. })
  177. function formatSplitTestsSchema(next) {
  178. if (this.splitTests) {
  179. for (const splitTestKey of Object.keys(this.splitTests)) {
  180. for (const variantIndex in this.splitTests[splitTestKey]) {
  181. this.splitTests[splitTestKey][variantIndex].assignedAt = new Date(
  182. this.splitTests[splitTestKey][variantIndex].assignedAt
  183. )
  184. }
  185. }
  186. }
  187. next()
  188. }
  189. UserSchema.pre('save', formatSplitTestsSchema)
  190. exports.User = mongoose.model('User', UserSchema)
  191. exports.UserSchema = UserSchema