User.js 6.6 KB

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