Skip to content

feature/change-user-entity-add-dev-user - #16

Closed
gvaguirres wants to merge 6 commits into
mainfrom
feature/change-user-entity-add-dev-user
Closed

feature/change-user-entity-add-dev-user#16
gvaguirres wants to merge 6 commits into
mainfrom
feature/change-user-entity-add-dev-user

Conversation

@gvaguirres

@gvaguirres gvaguirres commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • Refactor
    • User profile restructured: separate fields for name, display name, and email; display name is now stored as a nullable field.
    • Registration and user lookup updated to use the new name field.
    • Authentication mapping adjusted to use the new name-based identifier.
  • Chores
    • Database schema updated to add email and first name columns to support the new profile structure.

@coderabbitai

coderabbitai Bot commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@gvaguirres has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 15 minutes and 20 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 15 minutes and 20 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0ce4e920-4042-4566-91cb-ae76e2f64801

📥 Commits

Reviewing files that changed from the base of the PR and between 072a720 and 95defb7.

📒 Files selected for processing (2)
  • src/main/java/backendlab/team4you/config/CustomAuthenticationSuccessHandler.java
  • src/main/java/backendlab/team4you/controller/SignupController.java
📝 Walkthrough

Walkthrough

User identity mapping changed from email-based to name-based: UserEntity renamed/added fields (name, displayName, email, firstName), constructors and accessors updated; controller, repository, service, and security lookup now use name; DB migration adds email and first_name columns.

Changes

Cohort / File(s) Summary
User entity & API mapping
src/main/java/backendlab/team4you/user/UserEntity.java
Renamed unique column emailname; added displayName, email, firstName; constructor signature changed to (Bytes id, String name, String displayName); getName() returns name; getDisplayName() is nullable; added setName() and setDisplayName().
Controller adjustment
src/main/java/backendlab/team4you/controller/RegistrationController.java
Local principal identifier renamed from emailname; user lookup switched from userService.findByEmail(...)userService.findByName(...).
Repository & Service additions
src/main/java/backendlab/team4you/repository/UserRepository.java, src/main/java/backendlab/team4you/user/UserService.java
Added Optional<UserEntity> findByName(String name) in repository and public UserEntity findByName(String name) in service to support name-based lookups.
Security mapping
src/main/java/backendlab/team4you/config/SecurityConfig.java
UserDetailsService now resolves users via userService.findByName(username) and sets principal username from user.getName() instead of email-based mapping.
Database migration
src/main/resources/db/migration/V7__add_column_email_and_firstname.sql
Adds email and first_name columns to user_entities with IF NOT EXISTS guards.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Controller as RegistrationController
    participant Service as UserService
    participant Repo as UserRepository
    participant DB as Database

    Client->>Controller: request (Principal with name)
    Controller->>Service: findByName(name)
    Service->>Repo: findByName(name)
    Repo->>DB: SELECT ... WHERE name=?
    DB-->>Repo: user row (or empty)
    Repo-->>Service: UserEntity (or empty)
    Service-->>Controller: UserEntity (or null)
    Controller-->>Client: render welcome / redirect
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~40 minutes

Possibly related PRs

  • feature/security-config #5 — Introduces same user entity constructor/signature and name/displayName mapping changes; strongly related.
  • feature/security-config #13 — Modifies UserEntity, SecurityConfig, and controller/repository/service lookups from email→name; strongly related.

Suggested reviewers

  • JohanHiths

Poem

🐰 I hopped through fields both old and new,
Swapped email for name and showed a view,
displayName whispers, firstName peeks in,
Rewired the lookup, let the welcome begin,
A little refactor — a rabbit's joyful spin! 🥕

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Title check ⚠️ Warning The title mentions 'add-dev-user' but the actual changes are about refactoring UserEntity to use 'name' instead of 'email' as the primary identifier, adding new fields, and updating multiple service layers. Revise the title to accurately reflect the main changes, such as 'Refactor UserEntity: replace email with name as primary identifier' or 'Update user authentication to use name-based lookup instead of email'.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/change-user-entity-add-dev-user

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (1)
src/main/java/backendlab/team4you/user/UserEntity.java (1)

27-31: Consider adding uniqueness and validation constraints for email.

The email field lacks a unique constraint. If users should have unique email addresses, consider adding unique = true to the column definition. Additionally, validation annotations like @Email could help ensure data integrity:

-    `@Column`(name = "email")
+    `@Column`(name = "email", unique = true)
+    `@Email`
     private String email;

If email is intentionally optional and non-unique, this can be ignored.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/java/backendlab/team4you/user/UserEntity.java` around lines 27 - 31,
The email field in UserEntity currently has only `@Column`(name = "email") and
should enforce uniqueness and basic validation: update the email field's
annotations to include unique = true on the `@Column` and add validation
annotations such as `@Email` and `@NotBlank/`@NotNull (from
javax.validation.constraints) to ensure format and presence; keep existing field
name email and the UserEntity class while adding the necessary imports for the
validation annotations and adjust nullable in `@Column` if you want to prevent
nulls at the DB level.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/main/java/backendlab/team4you/user/UserEntity.java`:
- Around line 51-55: The 3-arg UserEntity constructor (UserEntity(Bytes id,
String name, String displayName)) leaves email and firstName null when used by
SignupController; update either the constructor or the controller to ensure
email and firstName are set before persist: add email and firstName parameters
to the UserEntity constructor and assign them, or in SignupController (where new
UserEntity(id, username, displayName) is used) call setEmail(...) and
setFirstName(...) on the created UserEntity instance before calling setRole(...)
/ saving. Mirror the approach used by UserService/UserMapper to ensure
consistency.
- Around line 66-73: The authentication break is caused by UserEntity.getName()
returning the username while RegistrationController.welcome() calls
userService.findByEmail(principal.getName()); fix by adding a findByName(String
name) method to UserRepository and update RegistrationController.welcome() to
call userService.findByName(principal.getName()) (keep SignupController and
SignupRequest unchanged so the principal remains the username), ensuring all
references to findByEmail(...) in RegistrationController are replaced with
findByName(...).

In `@src/main/resources/db/migration/V7__add_column_email_and_firstname.sql`:
- Around line 1-3: The V7 migration adds email and first_name but doesn't
populate email, which will break authentication used by
SecurityConfig.userDetailsService() (which calls user.getEmail()); update the V7
migration (V7__add_column_email_and_firstname.sql) to include a data-migration
step that sets email for existing rows—preferably copying from the legacy name
column if present (e.g. UPDATE user_entities SET email = name WHERE email IS
NULL AND name IS NOT NULL) and otherwise set a deterministic placeholder (e.g.
UPDATE user_entities SET email = CONCAT('user+', id, '@example.com') WHERE email
IS NULL) so no existing user has NULL email after migration.

---

Nitpick comments:
In `@src/main/java/backendlab/team4you/user/UserEntity.java`:
- Around line 27-31: The email field in UserEntity currently has only
`@Column`(name = "email") and should enforce uniqueness and basic validation:
update the email field's annotations to include unique = true on the `@Column` and
add validation annotations such as `@Email` and `@NotBlank/`@NotNull (from
javax.validation.constraints) to ensure format and presence; keep existing field
name email and the UserEntity class while adding the necessary imports for the
validation annotations and adjust nullable in `@Column` if you want to prevent
nulls at the DB level.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3cbd6fb8-59b3-4629-b576-a026e88abb53

📥 Commits

Reviewing files that changed from the base of the PR and between 527d158 and 50fc9c8.

📒 Files selected for processing (2)
  • src/main/java/backendlab/team4you/user/UserEntity.java
  • src/main/resources/db/migration/V7__add_column_email_and_firstname.sql

Comment on lines +51 to 55
public UserEntity(Bytes id, String name, String displayName) {
this.id = id != null ? id.toBase64UrlString() : null;
this.email = email;
this.firstName = firstName;
this.name = name;
this.displayName = displayName;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Check if email/firstName are set anywhere after UserEntity construction

echo "=== SignupController full context ==="
rg -n -A20 'new UserEntity\(' --type java

echo "=== Check for setEmail calls ==="
rg -n -C3 '\.setEmail\(' --type java

echo "=== Check for setFirstName calls ==="
rg -n -C3 '\.setFirstName\(' --type java

Repository: ithsjava25/project-backend-team4you

Length of output: 10214


SignupController does not set email or firstName when creating users via the 3-arg constructor.

In SignupController.java:72-76, the constructor call new UserEntity(id, username, displayName) leaves email and firstName null at the time of save. Only setRole() is called afterward.

This differs from UserService and UserMapper, which use the no-arg constructor followed by explicit setEmail() and setFirstName() calls. Either add these fields as constructor parameters, or ensure setters are called before persisting the user entity.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/java/backendlab/team4you/user/UserEntity.java` around lines 51 - 55,
The 3-arg UserEntity constructor (UserEntity(Bytes id, String name, String
displayName)) leaves email and firstName null when used by SignupController;
update either the constructor or the controller to ensure email and firstName
are set before persist: add email and firstName parameters to the UserEntity
constructor and assign them, or in SignupController (where new UserEntity(id,
username, displayName) is used) call setEmail(...) and setFirstName(...) on the
created UserEntity instance before calling setRole(...) / saving. Mirror the
approach used by UserService/UserMapper to ensure consistency.

Comment thread src/main/java/backendlab/team4you/user/UserEntity.java
Comment on lines +1 to +3
ALTER TABLE user_entities
ADD COLUMN IF NOT EXISTS email VARCHAR(255),
ADD COLUMN IF NOT EXISTS first_name VARCHAR(255);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Check if there are usages of findByEmail or getEmail that expect non-null values

# Search for findByEmail usages
echo "=== findByEmail usages ==="
rg -n -C3 'findByEmail'

# Search for getEmail usages
echo "=== getEmail usages ==="
rg -n -C3 '\.getEmail\s*\('

Repository: ithsjava25/project-backend-team4you

Length of output: 6316


Missing data migration to populate email column for existing users will block their authentication.

V7 adds email and first_name columns to the user_entities table without a data migration. All code paths relying on email (particularly SecurityConfig.userDetailsService() line 77, which uses user.getEmail() as the authentication username) will fail for existing users with NULL email values.

Add a data migration to either:

  1. Populate email from the previous name column (if available from V5), or
  2. Set a default/placeholder email for existing users

Without this, existing users cannot authenticate after this migration is applied.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/resources/db/migration/V7__add_column_email_and_firstname.sql`
around lines 1 - 3, The V7 migration adds email and first_name but doesn't
populate email, which will break authentication used by
SecurityConfig.userDetailsService() (which calls user.getEmail()); update the V7
migration (V7__add_column_email_and_firstname.sql) to include a data-migration
step that sets email for existing rows—preferably copying from the legacy name
column if present (e.g. UPDATE user_entities SET email = name WHERE email IS
NULL AND name IS NOT NULL) and otherwise set a deterministic placeholder (e.g.
UPDATE user_entities SET email = CONCAT('user+', id, '@example.com') WHERE email
IS NULL) so no existing user has NULL email after migration.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/main/java/backendlab/team4you/controller/RegistrationController.java`:
- Around line 71-72: Principal.getName() returns the user's email (per
SecurityConfig.userDetailsService), so calling userService.findByName(name) is
semantically incorrect; update RegistrationController to call
userService.findByEmail(principal.getName()) instead (or, alternatively, change
SecurityConfig.userDetailsService to use user.getName() as the username); prefer
switching the lookup in RegistrationController from findByName to findByEmail to
match CustomAuthenticationSuccessHandler and the rest of the codebase.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c932cdcf-9336-4933-8088-50e8d3edec3b

📥 Commits

Reviewing files that changed from the base of the PR and between 50fc9c8 and 8c4da1e.

📒 Files selected for processing (3)
  • src/main/java/backendlab/team4you/controller/RegistrationController.java
  • src/main/java/backendlab/team4you/repository/UserRepository.java
  • src/main/java/backendlab/team4you/user/UserService.java
✅ Files skipped from review due to trivial changes (1)
  • src/main/java/backendlab/team4you/repository/UserRepository.java

Comment on lines +71 to +72
String name = principal.getName();
UserEntity user = userService.findByName(name);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify how SecurityConfig sets up the Principal username
ast-grep --pattern $'UserDetailsService userDetailsService($$$) {
  $$$
}'

# Check what SecurityConfig uses for the username
rg -n -A10 "UserDetailsService" --type java

Repository: ithsjava25/project-backend-team4you

Length of output: 2782


🏁 Script executed:

# Get the context around lines 71-72 in RegistrationController
sed -n '60,85p' src/main/java/backendlab/team4you/controller/RegistrationController.java

Repository: ithsjava25/project-backend-team4you

Length of output: 711


🏁 Script executed:

# Check if UserService has findByName() method
rg -n "findByName|findByEmail" src/main/java/backendlab/team4you/user/UserService.java -A2

Repository: ithsjava25/project-backend-team4you

Length of output: 493


🏁 Script executed:

# Check for any other usages of findByName or findByEmail in the codebase
rg -n "findByName|findByEmail" --type java -B2 -A2

Repository: ithsjava25/project-backend-team4you

Length of output: 4233


Semantic mismatch: Principal.getName() returns email, not the name field.

According to SecurityConfig.java (line 77), the UserDetailsService bean builds the security User with username=user.getEmail(). This means Principal.getName() returns the email address, not the entity's name field.

Calling userService.findByName(name) with an email value will fail to find users unless the name and email columns happen to contain identical values. This is inconsistent with the rest of the codebase, which correctly uses findByEmail() (e.g., SecurityConfig line 71, CustomAuthenticationSuccessHandler line 35).

Either:

  1. Revert to using findByEmail() (since Principal provides the email), or
  2. Update SecurityConfig.userDetailsService() to use user.getName() as the username
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/java/backendlab/team4you/controller/RegistrationController.java`
around lines 71 - 72, Principal.getName() returns the user's email (per
SecurityConfig.userDetailsService), so calling userService.findByName(name) is
semantically incorrect; update RegistrationController to call
userService.findByEmail(principal.getName()) instead (or, alternatively, change
SecurityConfig.userDetailsService to use user.getName() as the username); prefer
switching the lookup in RegistrationController from findByName to findByEmail to
match CustomAuthenticationSuccessHandler and the rest of the codebase.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/main/java/backendlab/team4you/config/SecurityConfig.java`:
- Around line 71-77: The success handler still looks up users by email which
breaks the new name-based principal; update CustomAuthenticationSuccessHandler
(the method handling authentication success that currently calls
userService.findByEmail(username)) to use
userService.findByName(authentication.getName()) (or equivalent variable)
instead, handle a null return the same way you did in SecurityConfig (throw or
fallback), and keep the existing routing logic (checking for WebAuthn
credentials) so users with name-based principals are routed to /webauthn-check
when appropriate.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d0a2047a-321e-4130-b2ce-83d1fe04cc83

📥 Commits

Reviewing files that changed from the base of the PR and between 8c4da1e and 072a720.

📒 Files selected for processing (1)
  • src/main/java/backendlab/team4you/config/SecurityConfig.java

Comment thread src/main/java/backendlab/team4you/config/SecurityConfig.java
@gvaguirres gvaguirres closed this Apr 9, 2026
@gvaguirres
gvaguirres deleted the feature/change-user-entity-add-dev-user branch April 9, 2026 15:10
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.

1 participant