Started adding async calls for upgrading to Meteor 3.0. Numerous other fixes.

This commit is contained in:
2025-07-02 11:18:09 -07:00
parent e1216741d6
commit 2e99ad007c
32 changed files with 549 additions and 373 deletions

View File

@@ -9,12 +9,13 @@ if(Meteor.isServer) {
});
Meteor.methods({
"insertUser": function(user) {
"insertUser": function(user, roles) {
check(user, {
username: String,
emails: [{address: String, verified: Match.Maybe(Boolean)}],
roles: [String]
//roles: [String]
});
check(roles, [String])
//Verify the currently logged in user has authority to manage users.
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_MANAGE])) {
@@ -33,7 +34,7 @@ if(Meteor.isServer) {
}
else throw new Meteor.Error(403, "Not authorized to add users.");
},
"updateUser": function(user) {
"updateUser": async function(user, roles) {
check(user, {
_id: String,
username: String,
@@ -41,15 +42,36 @@ if(Meteor.isServer) {
address: String,
verified: Boolean
}],
roles: [String]
//roles: [String]
});
check(roles, [String])
//Verify the currently logged in user has authority to manage users.
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_MANAGE])) {
//Verify the user name isn't already used with a different ID.
if(Meteor.collections.Users.findOne({username: user.username, _id: {$ne: user._id}}) == undefined) {
//Update the user. Note: I am using direct mongo modification, versus attempting to go through the Accounts and Roles objects. This could cause problems in the future if these packages change their data structures.
Meteor.collections.Users.update(user._id, {$set: {username: user.username, emails: user.emails, roles: user.roles}});
await Meteor.collections.Users.updateAsync(user._id, {$set: {username: user.username, emails: user.emails/*, roles: user.roles*/}});
let currentRoles = await Roles.getRolesForUserAsync(user._id)
//console.log(currentRoles)
//console.log(roles)
//TODO: Figure out which roles to add, and which roles to remove.
// Add roles not in the database already.
//console.log("Adding the user " + user._id + " to the following roles: ")
//for(let next of roles) console.log(next)
let rolesToAdd = roles.filter(x => !currentRoles.includes(x))
let rolesToRemove = currentRoles.filter(x => !roles.includes(x))
console.log('Roles to remove: ' + rolesToRemove)
console.log('Roles to add: ' + rolesToAdd)
if(rolesToAdd.length) Roles.addUsersToRoles([user._id], rolesToAdd)
if(rolesToRemove.length) Roles.removeUsersFromRoles([user._id], rolesToRemove)
}
else {
throw new Meteor.Error(400, "User name already exists.");
@@ -57,10 +79,10 @@ if(Meteor.isServer) {
}
else throw new Meteor.Error(403, "Not authorized to update users.");
},
"deleteUser": function(id) {
"deleteUser": async function(id) {
check(id, String);
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_MANAGE])) {
Meteor.collections.Users.remove(id);
await Meteor.collections.Users.removeAsync(id);
}
else throw new Meteor.Error(403, "Not authorized to remove users.");
}