accountSettings.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. AccountSettings = new Mongo.Collection('accountSettings');
  2. AccountSettings.attachSchema(
  3. new SimpleSchema({
  4. _id: {
  5. type: String,
  6. },
  7. booleanValue: {
  8. type: Boolean,
  9. optional: true,
  10. },
  11. sort: {
  12. type: Number,
  13. decimal: true,
  14. },
  15. createdAt: {
  16. type: Date,
  17. optional: true,
  18. // eslint-disable-next-line consistent-return
  19. autoValue() {
  20. if (this.isInsert) {
  21. return new Date();
  22. } else if (this.isUpsert) {
  23. return { $setOnInsert: new Date() };
  24. } else {
  25. this.unset();
  26. }
  27. },
  28. },
  29. modifiedAt: {
  30. type: Date,
  31. denyUpdate: false,
  32. // eslint-disable-next-line consistent-return
  33. autoValue() {
  34. if (this.isInsert || this.isUpsert || this.isUpdate) {
  35. return new Date();
  36. } else {
  37. this.unset();
  38. }
  39. },
  40. },
  41. }),
  42. );
  43. AccountSettings.allow({
  44. update(userId) {
  45. const user = Users.findOne(userId);
  46. return user && user.isAdmin;
  47. },
  48. });
  49. if (Meteor.isServer) {
  50. Meteor.startup(() => {
  51. AccountSettings._collection.createIndex({ modifiedAt: -1 });
  52. AccountSettings.upsert(
  53. { _id: 'accounts-allowEmailChange' },
  54. {
  55. $setOnInsert: {
  56. booleanValue: false,
  57. sort: 0,
  58. },
  59. },
  60. );
  61. AccountSettings.upsert(
  62. { _id: 'accounts-allowUserNameChange' },
  63. {
  64. $setOnInsert: {
  65. booleanValue: false,
  66. sort: 1,
  67. },
  68. },
  69. );
  70. AccountSettings.upsert(
  71. { _id: 'accounts-allowUserDelete' },
  72. {
  73. $setOnInsert: {
  74. booleanValue: false,
  75. sort: 0,
  76. },
  77. },
  78. );
  79. });
  80. }
  81. AccountSettings.helpers({
  82. allowEmailChange() {
  83. return AccountSettings.findOne('accounts-allowEmailChange').booleanValue;
  84. },
  85. allowUserNameChange() {
  86. return AccountSettings.findOne('accounts-allowUserNameChange').booleanValue;
  87. },
  88. allowUserDelete() {
  89. return AccountSettings.findOne('accounts-allowUserDelete').booleanValue;
  90. },
  91. });
  92. export default AccountSettings;