accountSettings.js 2.1 KB

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