User Management
Relevant source files
The following files were used as context for generating this wiki page:
The User Management system in PeP5 is built upon ASP.NET Core Identity and provides a robust framework for handling user accounts, roles, and lifecycle. It is responsible for creating, retrieving, and managing users, with a clear distinction between different user types such as Students and Teachers. The system supports both individual user registration and bulk import functionalities, which are crucial for academic environments.
Core components include the ApplicationUser model which extends the base IdentityUser, the UserService which encapsulates all user-related business logic, and various ViewModels like RegisterViewModel for data transfer. The entire system is integrated with Entity Framework Core for data persistence, with ApplicationDbContext defining the schema and relationships.
Data Model
ApplicationUser Entity
The ApplicationUser class extends the default Microsoft.AspNetCore.Identity.IdentityUser to include properties specific to the PeP5 platform. This allows for storing additional information for both students and teachers.
Sources: Models/ApplicationUser.cs
// From: Models/ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; } = string.Empty;
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; } = string.Empty;
[Display(Name = "Full Name")]
public string FullName => $"{FirstName} {LastName}".Trim();
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public bool IsActive { get; set; } = true;
// Student-specific properties
public string? StudentId { get; set; }
// Teacher-specific properties
public string? Department { get; set; }
}
Sources: Models/ApplicationUser.cs:5-29
The following table details the custom properties added to the ApplicationUser entity.
| Property | Type | Description |
|---|---|---|
FirstName | string | The user's first name. Required. |
LastName | string | The user's last name. Required. |
FullName | string | A computed property that returns the user's full name. |
CreatedAt | DateTime | The UTC timestamp when the user account was created. |
IsActive | bool | A flag to indicate if the user account is active. Defaults to true. |
StudentId | string? | An optional identifier for students. |
Department | string? | An optional department name for teachers. |
Sources: Models/ApplicationUser.cs:7-29
Entity Relationship Diagram
The ApplicationUser is a central entity, linked to exams, exam attempts, and exam codes. A user can be a creator of an exam (typically a Teacher) or a participant in an exam attempt (a Student).
This diagram illustrates the relationships between ApplicationUser and other key entities in the database.
Sources: Data/ApplicationDbContext.cs:41-140, Migrations/ApplicationDbContextModelSnapshot.cs
User Roles
The system defines a static set of user roles to manage permissions and access control.
| Role | Constant | Description |
|---|---|---|
| Admin | UserRoles.Admin | Administrator with full system access. |
| Teacher | UserRoles.Teacher | A user who can create and manage exams. |
| Student | UserRoles.Student | A user who can take exams. |
Sources: Models/ApplicationUser.cs:31-36
Role assignment is handled during user creation within the UserService. For example, when CreateStudentAsync is called, the user is automatically assigned the Student role.
// From: Services/UserService.cs
if (result.Succeeded)
{
await _userManager.AddToRoleAsync(user, UserRoles.Student);
return user;
}
Sources: Services/UserService.cs:192-196
Core Service: UserService
The IUserService interface and its implementation, UserService, are the heart of the user management logic. This service abstracts the direct use of UserManager<ApplicationUser> and ApplicationDbContext for common user-related operations.
Sources: Services/UserService.cs:10-25
Key Methods
| Method | Description |
|---|---|
GetStudentsAsync() | Retrieves a list of all users with the "Student" role. |
GetTeachersAsync() | Retrieves a list of all users with the "Teacher" role. |
CreateStudentAsync(...) | Creates a new user, assigns the "Student" role. |
CreateTeacherAsync(...) | Creates a new user, assigns the "Teacher" role. |
ImportStudentsAsync(...) | Bulk creates student accounts from a list of data. |
ToggleUserStatusAsync(userId) | Toggles the IsActive flag for a given user. |
DeactivateUserAsync(userId) | Sets a user's IsActive flag to false. |
ActivateUserAsync(userId) | Sets a user's IsActive flag to true. |
SendWelcomeEmailAsync(...) | Sends a welcome email to a new user with their credentials. |
Sources: Services/UserService.cs:12-22
User Creation Flow
The following diagram shows the process for creating a new student user.
This flow illustrates how a RegisterViewModel is transformed into an ApplicationUser, created via UserManager, assigned a role, and returned.
Sources: Services/UserService.cs:181-200, ViewModels/ExamViewModels.cs:20-50
User Status Management
User accounts can be activated or deactivated, which controls their ability to log in and access the system. The ToggleUserStatusAsync method provides a simple way to flip the IsActive property on the ApplicationUser model.
The sequence diagram below shows the interaction for toggling a user's status.
Sources: Services/UserService.cs:81-96
Bulk Student Import
A key feature for administrators and teachers is the ability to import multiple students at once. The ImportStudentsAsync method in UserService handles this process.
Import Process Flow
The import process validates each student record before creation to prevent duplicates.
This diagram outlines the validation and creation steps for each student record during a bulk import.
Sources: Services/UserService.cs:98-179
Supporting Data Structures
The import functionality relies on two helper classes to manage data and results.
| Class | Property | Type | Description |
|---|---|---|---|
StudentImportData | FirstName | string | Student's first name. |
LastName | string | Student's last name. | |
Email | string | Student's email address. | |
StudentId | string | Student's unique ID. | |
ImportResult | StudentData | StudentImportData | The original data for the student. |
Success | bool | Indicates if the import for this student was successful. | |
ErrorMessage | string | Contains error details if the import failed. |
Sources: Services/UserService.cs:436-455
Summary
The User Management system is a foundational part of the PeP5 application, providing essential services for account creation, role management, and status control. By extending ASP.NET Core Identity and encapsulating logic within the UserService, the system offers a clean and maintainable architecture. Features like bulk student import and automated welcome emails are tailored to the needs of an educational platform, streamlining administrative tasks. The clear separation of roles ensures that students and teachers have appropriate access levels throughout the application.