lockedUsers.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. // Method to find locked users and release them if needed
  3. Meteor.methods({
  4. getLockedUsers() {
  5. // Check if user has admin rights
  6. const userId = Meteor.userId();
  7. if (!userId) {
  8. throw new Meteor.Error('error-invalid-user', 'Invalid user');
  9. }
  10. const user = ReactiveCache.getUser(userId);
  11. if (!user || !user.isAdmin) {
  12. throw new Meteor.Error('error-not-allowed', 'Not allowed');
  13. }
  14. // Current time to check against unlockTime
  15. const currentTime = Number(new Date());
  16. // Find users that are locked (known users)
  17. const lockedUsers = Meteor.users.find(
  18. {
  19. 'services.accounts-lockout.unlockTime': {
  20. $gt: currentTime,
  21. }
  22. },
  23. {
  24. fields: {
  25. _id: 1,
  26. username: 1,
  27. emails: 1,
  28. 'services.accounts-lockout.unlockTime': 1,
  29. 'services.accounts-lockout.failedAttempts': 1
  30. }
  31. }
  32. ).fetch();
  33. // Format the results for the UI
  34. return lockedUsers.map(user => {
  35. const email = user.emails && user.emails.length > 0 ? user.emails[0].address : 'No email';
  36. const remainingLockTime = Math.round((user.services['accounts-lockout'].unlockTime - currentTime) / 1000);
  37. return {
  38. _id: user._id,
  39. username: user.username || 'No username',
  40. email,
  41. failedAttempts: user.services['accounts-lockout'].failedAttempts || 0,
  42. unlockTime: user.services['accounts-lockout'].unlockTime,
  43. remainingLockTime // in seconds
  44. };
  45. });
  46. },
  47. unlockUser(userId) {
  48. // Check if user has admin rights
  49. const adminId = Meteor.userId();
  50. if (!adminId) {
  51. throw new Meteor.Error('error-invalid-user', 'Invalid user');
  52. }
  53. const admin = ReactiveCache.getUser(adminId);
  54. if (!admin || !admin.isAdmin) {
  55. throw new Meteor.Error('error-not-allowed', 'Not allowed');
  56. }
  57. // Make sure the user to unlock exists
  58. const userToUnlock = Meteor.users.findOne(userId);
  59. if (!userToUnlock) {
  60. throw new Meteor.Error('error-user-not-found', 'User not found');
  61. }
  62. // Unlock the user
  63. Meteor.users.update(
  64. { _id: userId },
  65. {
  66. $unset: {
  67. 'services.accounts-lockout': 1
  68. }
  69. }
  70. );
  71. return true;
  72. },
  73. unlockAllUsers() {
  74. // Check if user has admin rights
  75. const adminId = Meteor.userId();
  76. if (!adminId) {
  77. throw new Meteor.Error('error-invalid-user', 'Invalid user');
  78. }
  79. const admin = ReactiveCache.getUser(adminId);
  80. if (!admin || !admin.isAdmin) {
  81. throw new Meteor.Error('error-not-allowed', 'Not allowed');
  82. }
  83. // Unlock all users
  84. Meteor.users.update(
  85. { 'services.accounts-lockout.unlockTime': { $exists: true } },
  86. {
  87. $unset: {
  88. 'services.accounts-lockout': 1
  89. }
  90. },
  91. { multi: true }
  92. );
  93. return true;
  94. }
  95. });