{"record":{"id":"656f9149df1430cb","repo":"RocketChat/Rocket.Chat","slug":"error-user-not-found-656f91","errorCode":"error-user-not-found","errorMessage":"User not found","messagePattern":"User not found","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/meteor-methods/auth/addUserToRole.ts","lineNumber":46,"sourceCode":"\t\t\tmethod: 'authorization:addUserToRole',\n\t\t});\n\t}\n\n\tif (role._id === 'admin' && !(await hasPermissionAsync(userId, 'assign-admin-role'))) {\n\t\tthrow new Meteor.Error('error-action-not-allowed', 'Assigning admin is not allowed', {\n\t\t\tmethod: 'authorization:addUserToRole',\n\t\t\taction: 'Assign_admin',\n\t\t});\n\t}\n\n\tconst user = await Users.findOneByUsernameIgnoringCase(username, {\n\t\tprojection: {\n\t\t\t_id: 1,\n\t\t},\n\t});\n\n\tif (!user?._id) {\n\t\tthrow new Meteor.Error('error-user-not-found', 'User not found', {\n\t\t\tmethod: 'authorization:addUserToRole',\n\t\t});\n\t}\n\n\t// verify if user can be added to given scope\n\tif (scope && !(await Roles.canAddUserToRole(user._id, role._id, scope))) {\n\t\tthrow new Meteor.Error('error-invalid-user', 'User is not part of given room', {\n\t\t\tmethod: 'authorization:addUserToRole',\n\t\t});\n\t}\n\n\tconst add = await addUserRolesAsync(user._id, [role._id], scope);\n\n\tif (settings.get('UI_DisplayRoles')) {\n\t\tvoid api.broadcast('user.roleUpdate', {\n\t\t\ttype: 'added',\n\t\t\t_id: role._id,\n\t\t\tu: {","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/meteor-methods/auth/addUserToRole.ts#L28-L64","documentation":"addUserToRole() resolves the target by username case-insensitively with Users.findOneByUsernameIgnoringCase; no matching user document throws error-user-not-found. Only _id is projected — the user must already exist, there is no auto-invite behavior on this path.","triggerScenarios":"authorization:addUserToRole / roles.addUserToRole with a username that does not exist, has a typo, or refers to a deleted user; usernames taken from a stale admin picker; calling immediately after signup before the record is visible (replication lag in scaled setups).","commonSituations":"Typo'd usernames; deleted or deactivated users still listed in UI; federated/app users stored under different usernames than expected.","solutions":["Verify the username first (GET /api/v1/users.info?username=...) and correct it.","Refresh the user picker — the user may have been deleted or renamed.","If invoked right after signup, retry once the user record is confirmed to exist."],"exampleFix":"// before: typo'd username -> error-user-not-found\nMeteor.call('authorization:addUserToRole', roleId, 'jonh.doe');\n\n// after: verify then call with the exact username\nconst info = await getUserByUsername('john.doe'); // GET /api/v1/users.info?username=john.doe\nif (!info) throw new Error('no such user');\nMeteor.call('authorization:addUserToRole', roleId, info.user.username);","handlingStrategy":"validation","validationCode":"// Verify the target user exists before assigning a role\nconst res = await fetch(`/api/v1/users.info?username=${encodeURIComponent(username)}`, {\n  headers: { 'X-Auth-Token': token, 'X-User-Id': uid },\n});\nif (!res.ok) throw new Error(`no user named ${username}`);","typeGuard":null,"tryCatchPattern":"try {\n  await Meteor.callAsync('authorization:addUserToRole', roleId, username, scope);\n} catch (err: any) {\n  if (err?.error === 'error-user-not-found' && err?.details?.method === 'authorization:addUserToRole') {\n    // stale picker or typo: re-resolve the username, then retry once\n    const fresh = await resolveUsername(username);\n    return fresh ? Meteor.callAsync('authorization:addUserToRole', roleId, fresh, scope) : undefined;\n  }\n  throw err;\n}","preventionTips":["Resolve usernames through users.info before role assignment.","Refresh user pickers after user deletions/renames.","In scaled deployments, confirm the user record exists before acting immediately after signup."],"tags":["users","not-found","roles"],"backgroundTag":"user-not-found","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}