Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Unify all the code for user creation into a new service. #29916

Closed
wants to merge 5 commits into from

Conversation

pierre-lehnen-rc
Copy link
Contributor

@pierre-lehnen-rc pierre-lehnen-rc commented Jul 25, 2023

Proposed changes (including videos or screenshots)

Moves the user creation code from Meteor's Accounts.createUserAsync and Accounts.insertUserDoc to a new User service on rocket.chat, with only minimal changes to behavior.

Changes:

  • Accounts.createUserAsync would query the user collection to check if the specified username and email were not already in use - but this check was only valid in regular user registration because in many of the places we call this function (such as OAuth), the username and email were filled in through callbacks after this check is done. So I dropped this check completely as in one way or another we already rely on the indexes for those fields.
  • In some places we passed an attribute named globalRoles to the user param when creating users with Accounts.insertUserDoc. I moved this attribute to the options param instead, so that the user object is compatible with IUser.
  • Accounts.onCreateUser and Accounts.validateNewUser will no longer be called. We had a few references to those in the code base and I moved all of them into the User service as well, except for the OAuth validateNewUser function that I moved into the beforeCreateUser callback instead.
  • Any call to Accounts.insertUserDoc will be redirected to this new service automatically. OAuth login is still handled by Meteor code so it relies on this. I made no such fallbacks for Accounts.validateNewUser.
  • Includes a change made on PR regression: Server isn't adding agent required properties to users created/updated through admin UI #29925 to save the user's roles before running the afterCreateUser callback (If that PR is already merged then this is not an additional change)

Issue(s)

Steps to test or reproduce

Further comments

This adds a reference to a new meteor package, oauth-encryption. This package was previously used internally on Accounts.insertUserDoc to handle a special case in OAuth logins, where some data may have been encrypted and stored before the user's ID was generated. The insertUserDoc function decrypts that data and re-encrypts using the ID once it is available.

@changeset-bot
Copy link

changeset-bot bot commented Jul 25, 2023

⚠️ No Changeset found

Latest commit: 11121b4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@codecov
Copy link

codecov bot commented Jul 25, 2023

Codecov Report

Merging #29916 (11121b4) into develop (919d0b8) will increase coverage by 0.53%.
Report is 1 commits behind head on develop.
The diff coverage is n/a.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop   #29916      +/-   ##
===========================================
+ Coverage    47.94%   48.47%   +0.53%     
===========================================
  Files          696      697       +1     
  Lines        13904    13911       +7     
  Branches      2445     2446       +1     
===========================================
+ Hits          6666     6744      +78     
+ Misses        6867     6790      -77     
- Partials       371      377       +6     
Flag Coverage Δ
e2e 46.57% <ø> (+0.57%) ⬆️
unit 64.90% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

@pierre-lehnen-rc pierre-lehnen-rc marked this pull request as ready for review July 26, 2023 13:44
@pierre-lehnen-rc pierre-lehnen-rc requested review from a team as code owners July 26, 2023 13:44
apps/meteor/server/services/user/service.ts Show resolved Hide resolved
apps/meteor/server/services/user/service.ts Show resolved Hide resolved
const verified = settings.get<boolean>('Accounts_Verify_Email_For_External_Accounts');

for (const service of Object.values(user.services) as Record<string, any>[]) {
if (!user.name && typeof service === 'object' && ('name' in service || 'username' in service)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can maybe this be a type assertion function? no perf benefit but we'll have it "typed"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, yes. In this case the type this would assert to is not defined yet. I'll try to include its definition on the next PR.

apps/meteor/server/services/user/service.ts Show resolved Hide resolved
apps/meteor/server/services/user/service.ts Show resolved Hide resolved
apps/meteor/server/services/user/service.ts Show resolved Hide resolved
apps/meteor/server/services/user/service.ts Show resolved Hide resolved
apps/meteor/server/services/user/service.ts Show resolved Hide resolved
@pierre-lehnen-rc
Copy link
Contributor Author

As this first PR focuses on unifying the code while changing as little as possible, I've marked as resolved the suggestions related to code that was only moved from one file to another. I'm working on a second PR with the actual improvements over this code and I'll consider those suggestions there.

@pierre-lehnen-rc pierre-lehnen-rc marked this pull request as draft August 4, 2023 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants