-
Notifications
You must be signed in to change notification settings - Fork 0
feat: seed roles and update users with default role and status #20
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
base: main
Are you sure you want to change the base?
Conversation
WalkthroughA new database migration was introduced to update the schema and seed data. The migration changes the Changes
Sequence Diagram(s)sequenceDiagram
participant Migration
participant Database
Migration->>Database: Alter users.password_hash to varchar(255)
alt If "Admin" role does not exist
Migration->>Database: Insert "Admin" role into roles
end
alt If "User" role does not exist
Migration->>Database: Insert "User" role into roles
end
Migration->>Database: Update users with NULL role_id to User role's ID
Migration->>Database: Set all users with status=false to status=true
Poem
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/Api/Migrations/20250707040159_SeedRolesAndUpdateUsers.Designer.cs(1 hunks)src/Api/Migrations/20250707040159_SeedRolesAndUpdateUsers.cs(1 hunks)src/Api/Migrations/DatabaseContextModelSnapshot.cs(1 hunks)
🧰 Additional context used
🧠 Learnings (4)
📓 Common learnings
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#2
File: src/Core/Services/UserService.cs:41-50
Timestamp: 2025-03-11T06:44:34.378Z
Learning: Soft-delete for the User entity has been implemented in PR #11 (https://github.com/OsmosysSoftware/dotnet-foundation-v2/pull/11) instead of fully deleting users.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#15
File: src/Core/Repositories/RoleRepository.cs:34-43
Timestamp: 2025-04-22T10:36:43.985Z
Learning: Pagination parameter validation for RoleRepository.GetAllRolesAsync() has been handled in PR #16 (feat: use centralized error handling) rather than directly in the authentication/authorization PR.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#1
File: src/Api/Program.cs:33-35
Timestamp: 2025-03-11T04:19:59.613Z
Learning: The null checking for database connection strings in src/Api/Program.cs is intentionally handled in PR #2 (https://github.com/OsmosysSoftware/dotnet-foundation-v2/pull/2) rather than in the initial base setup PR.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#17
File: src/Core/Repositories/RoleRepository.cs:61-66
Timestamp: 2025-04-22T11:45:05.600Z
Learning: In the dotnet-foundation-v2 project, soft delete for Roles is implemented by setting Status=false in the service layer (RoleService.DeleteRoleAsync) before calling the repository method, which then updates the entity instead of removing it.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#15
File: src/Core/Repositories/RoleRepository.cs:34-43
Timestamp: 2025-04-22T10:36:43.985Z
Learning: Pagination parameter validation for RoleRepository.GetAllRolesAsync() has been implemented in PR #16 to handle negative or zero values by throwing ArgumentOutOfRangeException.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#5
File: src/Core/Services/RoleService.cs:35-46
Timestamp: 2025-03-02T12:30:35.072Z
Learning: The user plans to enhance error reporting in RoleService.cs in a future PR, specifically throwing exceptions instead of returning null in error cases for better debugging and user feedback.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#3
File: src/Core/Entities/Models/User.cs:24-25
Timestamp: 2025-03-01T10:26:17.104Z
Learning: BCrypt password hashes used in the application are approximately 60 characters in length and will always be well under 255 characters, regardless of the input password's length.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#8
File: src/Core/Services/UserService.cs:75-93
Timestamp: 2025-03-05T07:16:04.675Z
Learning: When creating or updating users in the application, validate that the assigned role actually exists in the database before proceeding. This prevents invalid foreign key references and ensures data integrity.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#3
File: src/Api/Migrations/20250220183440_InitialCreate.cs:0-0
Timestamp: 2025-03-01T10:38:51.061Z
Learning: For the dotnet-foundation-v2 project, the suggestion to add a unique constraint on the email field in migrations is outdated according to the user.
src/Api/Migrations/DatabaseContextModelSnapshot.cs (7)
Learnt from: sameer-s-b
PR: OsmosysSoftware/dotnet-foundation-v2#4
File: src/Api/Migrations/DatabaseContextModelSnapshot.cs:86-89
Timestamp: 2025-02-27T19:07:46.207Z
Learning: Migration files like DatabaseContextModelSnapshot.cs are automatically generated by Entity Framework Core and should not be manually edited.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#6
File: src/Core/Services/UserService.cs:58-76
Timestamp: 2025-03-04T04:19:32.712Z
Learning: The `UserUpdateDto` class does not include a password field in the .NET Foundation project, because password updates are not handled in the regular user update flow by design.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#6
File: src/Core/Services/UserService.cs:58-76
Timestamp: 2025-03-04T04:19:32.712Z
Learning: The `UserUpdateDto` class doesn't include a password field, meaning password updates aren't handled in the regular user update flow in the .NET Foundation project.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#2
File: src/Core/DataContext/DatabaseContext.cs:0-0
Timestamp: 2025-03-11T13:28:33.599Z
Learning: In the dotnet-foundation-v2 project, CreatedAt properties in entity models like User.cs and Role.cs are initialized with DateTime.UtcNow by default and have private setters, so they don't need to be set in the DatabaseContext.UpdateTimestamps() method.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#3
File: src/Core/Entities/Models/User.cs:24-25
Timestamp: 2025-03-01T10:26:17.104Z
Learning: BCrypt password hashes used in the application are approximately 60 characters in length and will always be well under 255 characters, regardless of the input password's length.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#3
File: src/Api/Migrations/20250220183440_InitialCreate.cs:0-0
Timestamp: 2025-03-01T10:38:51.061Z
Learning: For the dotnet-foundation-v2 project, the suggestion to add a unique constraint on the email field in migrations is outdated according to the user.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#8
File: src/Core/Services/UserService.cs:144-152
Timestamp: 2025-03-05T08:12:34.130Z
Learning: In the UserService class, the BCrypt cost/work factor should be parameterized and made configurable through application settings rather than using the default value (10). This can be done by:
1. Injecting IConfiguration in the UserService constructor
2. Reading the work factor from configuration (e.g., "PasswordSecurity:BCryptWorkFactor")
3. Using this value in the HashPassword method: BCrypt.Net.BCrypt.HashPassword(password, workFactor)
4. Adding the appropriate configuration in appsettings.json
This approach provides flexibility to adjust password hashing security requirements without recompiling the application.
src/Api/Migrations/20250707040159_SeedRolesAndUpdateUsers.cs (10)
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#3
File: src/Api/Migrations/20250220183440_InitialCreate.cs:0-0
Timestamp: 2025-03-01T10:38:51.061Z
Learning: For the dotnet-foundation-v2 project, the suggestion to add a unique constraint on the email field in migrations is outdated according to the user.
Learnt from: sameer-s-b
PR: OsmosysSoftware/dotnet-foundation-v2#4
File: src/Api/Migrations/DatabaseContextModelSnapshot.cs:86-89
Timestamp: 2025-02-27T19:07:46.207Z
Learning: Migration files like DatabaseContextModelSnapshot.cs are automatically generated by Entity Framework Core and should not be manually edited.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#2
File: src/Core/Services/RoleService.cs:52-61
Timestamp: 2025-03-01T05:13:45.108Z
Learning: The `DeleteRoleAsync` method in `RoleService` should check if the role has any associated users before deletion by implementing a call to `HasAssociatedUsersAsync` and throwing a `DependencyConflictException` if users exist. This will be implemented in a future PR.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#2
File: src/Core/DataContext/DatabaseContext.cs:0-0
Timestamp: 2025-03-11T13:28:33.599Z
Learning: In the dotnet-foundation-v2 project, CreatedAt properties in entity models like User.cs and Role.cs are initialized with DateTime.UtcNow by default and have private setters, so they don't need to be set in the DatabaseContext.UpdateTimestamps() method.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#3
File: src/Core/Repositories/RoleRepository.cs:47-51
Timestamp: 2025-03-01T11:09:57.066Z
Learning: Referential integrity for Role deletion is currently handled at the database level through DeleteBehaviour configuration in DatabaseContext, which prevents deletion of roles that have associated users. Application-level checks will be added in future PRs.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#2
File: src/Core/Repositories/RoleRepository.cs:42-46
Timestamp: 2025-03-01T05:05:05.702Z
Learning: The Role-User relationship in the DatabaseContext is configured with DeleteBehavior.Restrict, which already prevents deletion of roles that are in use by users at the database level.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#17
File: src/Core/Repositories/RoleRepository.cs:61-66
Timestamp: 2025-04-22T11:45:05.600Z
Learning: In the dotnet-foundation-v2 project, soft delete for Roles is implemented by setting Status=false in the service layer (RoleService.DeleteRoleAsync) before calling the repository method, which then updates the entity instead of removing it.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#8
File: src/Core/Services/UserService.cs:75-93
Timestamp: 2025-03-05T07:16:04.675Z
Learning: When creating or updating users in the application, validate that the assigned role actually exists in the database before proceeding. This prevents invalid foreign key references and ensures data integrity.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#2
File: src/Core/Services/UserService.cs:41-50
Timestamp: 2025-03-11T06:44:34.378Z
Learning: Soft-delete for the User entity has been implemented in PR #11 (https://github.com/OsmosysSoftware/dotnet-foundation-v2/pull/11) instead of fully deleting users.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#3
File: src/Api/Migrations/20250220183440_InitialCreate.cs:0-0
Timestamp: 2025-03-01T10:38:51.061Z
Learning: The users table in the dotnet-foundation-v2 project already has a unique constraint on the email field implemented in the InitialCreate migration with a unique index named "IX_users_email".
src/Api/Migrations/20250707040159_SeedRolesAndUpdateUsers.Designer.cs (6)
Learnt from: sameer-s-b
PR: OsmosysSoftware/dotnet-foundation-v2#4
File: src/Api/Migrations/DatabaseContextModelSnapshot.cs:86-89
Timestamp: 2025-02-27T19:07:46.207Z
Learning: Migration files like DatabaseContextModelSnapshot.cs are automatically generated by Entity Framework Core and should not be manually edited.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#2
File: src/Core/DataContext/DatabaseContext.cs:0-0
Timestamp: 2025-03-11T13:28:33.599Z
Learning: In the dotnet-foundation-v2 project, CreatedAt properties in entity models like User.cs and Role.cs are initialized with DateTime.UtcNow by default and have private setters, so they don't need to be set in the DatabaseContext.UpdateTimestamps() method.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#3
File: src/Core/Repositories/RoleRepository.cs:47-51
Timestamp: 2025-03-01T11:09:57.066Z
Learning: Referential integrity for Role deletion is currently handled at the database level through DeleteBehaviour configuration in DatabaseContext, which prevents deletion of roles that have associated users. Application-level checks will be added in future PRs.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#2
File: src/Core/Repositories/RoleRepository.cs:42-46
Timestamp: 2025-03-01T05:05:05.702Z
Learning: The Role-User relationship in the DatabaseContext is configured with DeleteBehavior.Restrict, which already prevents deletion of roles that are in use by users at the database level.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#2
File: src/Core/Services/RoleService.cs:52-61
Timestamp: 2025-03-01T05:13:45.108Z
Learning: The `DeleteRoleAsync` method in `RoleService` should check if the role has any associated users before deletion by implementing a call to `HasAssociatedUsersAsync` and throwing a `DependencyConflictException` if users exist. This will be implemented in a future PR.
Learnt from: osm-Jatin
PR: OsmosysSoftware/dotnet-foundation-v2#8
File: src/Core/Services/UserService.cs:75-93
Timestamp: 2025-03-05T07:16:04.675Z
Learning: When creating or updating users in the application, validate that the assigned role actually exists in the database before proceeding. This prevents invalid foreign key references and ensures data integrity.
🔇 Additional comments (3)
src/Api/Migrations/DatabaseContextModelSnapshot.cs (1)
88-92: LGTM - Auto-generated model snapshot update.The password hash column type change from
longtexttovarchar(255)with max length 255 is appropriate for BCrypt hashes and aligns with the migration changes. This file is auto-generated by Entity Framework Core and correctly reflects the updated schema.src/Api/Migrations/20250707040159_SeedRolesAndUpdateUsers.cs (1)
13-23: LGTM - Password hash column optimization.The change from
longtexttovarchar(255)is appropriate for BCrypt hashes and improves storage efficiency. The 255-character limit is well above the ~60 characters typically used by BCrypt.src/Api/Migrations/20250707040159_SeedRolesAndUpdateUsers.Designer.cs (1)
1-185: LGTM - Auto-generated migration designer file.The migration designer file correctly defines the target database schema model. The entity configurations, relationships, and constraints are properly set up, including the updated
PasswordHashproperty withvarchar(255)and the foreign key relationships withDeleteBehavior.Restrict.
Task ID
REST-1127
Description
password_hashcolumn datatype to varchar(255)