rename membership and adopt newtype pattern #304

Closed
opened 2025-07-08 08:45:56 -04:00 by AtHeartEngineer · 0 comments

Originally created by @stefan0xC on 12/22/2024

I've been working on improving Vaultwarden's legibility by renaming the UsersOrganizations to simply Membership and making it clearer when something is referring to the User and when to a Membership relation.

After doing that I was motivated to try my hands on using the newtype pattern so that the Rust type system can be used to check on compile time if we always call the right uuid because there are a lot of Strings that can be used interchangeably otherwise.

For example I noticed that we are passing the wrong id here: ed4ad67e73/src/api/core/organizations.rs (L910)

Because in contrast to CollectionUser the GroupUser::new actually expects a different id: ed4ad67e73/src/db/models/group.rs (L122) This was probably overlooked because it's not a major issue when a user is not correctly invited to a group and it only shows up as a small warning in the logs:

[2024-12-22 21:23:47.022][request][INFO] POST /api/organizations/152bd996-19f9-40ee-b991-e52333a7e719/users/invite
[2024-12-22 21:23:47.031][vaultwarden::db::models::group][WARN] User could not be found!
[2024-12-22 21:23:48.082][response][INFO] (send_invite) POST /api/organizations/<org_id>/users/invite => 200 OK
*Originally created by @stefan0xC on 12/22/2024* I've been working on improving Vaultwarden's legibility by renaming the `UsersOrganizations` to simply `Membership` and making it clearer when something is referring to the `User` and when to a `Membership` relation. After doing that I was motivated to try my hands on using the [newtype pattern](https://doc.rust-lang.org/book/ch19-04-advanced-types.html#using-the-newtype-pattern-for-type-safety-and-abstraction) so that the Rust type system can be used to check on compile time if we always call the right `uuid` because there are a lot of Strings that can be used interchangeably otherwise. For example I noticed that we are passing the wrong id here: https://github.com/dani-garcia/vaultwarden/blob/ed4ad67e732c213beaec78970cdb68e48bee3dc1/src/api/core/organizations.rs#L910 Because in contrast to `CollectionUser` the `GroupUser::new` actually expects a different id: https://github.com/dani-garcia/vaultwarden/blob/ed4ad67e732c213beaec78970cdb68e48bee3dc1/src/db/models/group.rs#L122 This was probably overlooked because it's not a major issue when a user is not correctly invited to a group and it only shows up as a small warning in the logs: ``` [2024-12-22 21:23:47.022][request][INFO] POST /api/organizations/152bd996-19f9-40ee-b991-e52333a7e719/users/invite [2024-12-22 21:23:47.031][vaultwarden::db::models::group][WARN] User could not be found! [2024-12-22 21:23:48.082][response][INFO] (send_invite) POST /api/organizations/<org_id>/users/invite => 200 OK ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/vaultwarden#304