Skip to content

Commit 88508ef

Browse files
author
Vadim Belov
committed
Add base Postgres user entity and UserRole enum
Introduced BasePostgresUser class with identity, contact, preferences, permissions, and role properties, mapped for PostgreSQL with EF Core. Added UserRole enum for role-based access control. Updated project references for shared models and cleaned up unused usings.
1 parent a48d621 commit 88508ef

4 files changed

Lines changed: 145 additions & 3 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using EasyExtensions.EntityFrameworkCore.Abstractions;
2+
using EasyExtensions.Models.Enums;
3+
using Microsoft.EntityFrameworkCore;
4+
using System.ComponentModel.DataAnnotations.Schema;
5+
using System.Text;
6+
7+
namespace EasyExtensions.EntityFrameworkCore.Npgsql.Database
8+
{
9+
/// <summary>
10+
/// Represents a base user account with identity, contact, and profile information.
11+
/// </summary>
12+
/// <remarks>This class provides common user properties for authentication, authorization, and
13+
/// personalization scenarios. It is intended to be used as a base type for user entities in applications that
14+
/// require user management. The class enforces unique email addresses and supports user preferences, permissions,
15+
/// and role-based access control.</remarks>
16+
[Table("users")]
17+
[Index(nameof(Email), IsUnique = true)]
18+
public class BasePostgresUser : BaseEntity<Guid>
19+
{
20+
/// <summary>
21+
/// Gets the full name, consisting of the first and last name combined with a space.
22+
/// </summary>
23+
[NotMapped]
24+
public string FullName => GetFullName();
25+
26+
private string GetFullName()
27+
{
28+
var sb = new StringBuilder();
29+
sb.Append(FirstName);
30+
if (!string.IsNullOrWhiteSpace(MiddleName))
31+
{
32+
sb.Append(' ').Append(MiddleName);
33+
}
34+
sb.Append(' ').Append(LastName);
35+
return sb.ToString().Trim();
36+
}
37+
38+
/// <summary>
39+
/// Gets or sets the first name of the person.
40+
/// </summary>
41+
[Column("first_name", TypeName = "citext")]
42+
public string FirstName { get; set; } = null!;
43+
44+
/// <summary>
45+
/// Gets or sets the middle name of the person.
46+
/// </summary>
47+
[Column("middle_name", TypeName = "citext")]
48+
public string? MiddleName { get; set; } = null!;
49+
50+
/// <summary>
51+
/// Gets or sets the last name of the person.
52+
/// </summary>
53+
[Column("last_name", TypeName = "citext")]
54+
public string LastName { get; set; } = null!;
55+
56+
/// <summary>
57+
/// Gets or sets the avatar image data in WebP format.
58+
/// </summary>
59+
[Column("avatar_webp_bytes")]
60+
public byte[]? AvatarWebPBytes { get; set; }
61+
62+
/// <summary>
63+
/// Gets or sets the email address associated with the entity.
64+
/// </summary>
65+
[Column("email", TypeName = "citext")]
66+
public string Email { get; set; } = null!;
67+
68+
/// <summary>
69+
/// Gets or sets the phone number associated with the entity.
70+
/// </summary>
71+
[Column("phone_number")]
72+
public long? PhoneNumber { get; set; }
73+
74+
/// <summary>
75+
/// Gets or sets the collection of user preferences as key-value pairs.
76+
/// </summary>
77+
/// <remarks>Each entry in the dictionary represents a user preference, where the key is the
78+
/// preference name and the value is its corresponding setting. Preference names and values are case-sensitive.
79+
/// Modifying this collection updates the user's stored preferences.</remarks>
80+
[Column("preferences")]
81+
public Dictionary<string, string> Preferences { get; set; } = [];
82+
83+
/// <summary>
84+
/// Gets or sets the collection of permissions assigned to the entity.
85+
/// </summary>
86+
[Column("permissions")]
87+
public ICollection<string> Permissions { get; set; } = [];
88+
89+
/// <summary>
90+
/// Gets or sets a value indicating whether the entity is active.
91+
/// </summary>
92+
[Column("is_active")]
93+
public bool IsActive { get; set; } = true;
94+
95+
/// <summary>
96+
/// Gets or sets the password hash in PHC (Password Hashing Competition) string format.
97+
/// </summary>
98+
/// <remarks>The value is typically a string produced by a password hashing algorithm that follows
99+
/// the PHC string format, which includes information about the algorithm, parameters, salt, and hash. This
100+
/// property may be null if no password is set.</remarks>
101+
[Column("password_phc")]
102+
public string? PasswordPhc { get; set; }
103+
104+
/// <summary>
105+
/// Gets or sets the role assigned to the user.
106+
/// </summary>
107+
[Column("role")]
108+
public UserRole Role { get; set; }
109+
110+
/// <summary>
111+
/// Gets or sets the token used to authorize a password reset request for the user.
112+
/// </summary>
113+
[Column("reset_password_token", TypeName = "citext")]
114+
public string? ResetPasswordToken { get; set; }
115+
}
116+
}

Sources/EasyExtensions.EntityFrameworkCore.Npgsql/EasyExtensions.EntityFrameworkCore.Npgsql.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
3535
</ItemGroup>
3636
<ItemGroup>
37+
<ProjectReference Include="..\EasyExtensions.EntityFrameworkCore\EasyExtensions.EntityFrameworkCore.csproj" />
3738
<ProjectReference Include="..\EasyExtensions\EasyExtensions.csproj" />
3839
</ItemGroup>
3940
</Project>

Sources/EasyExtensions.EntityFrameworkCore.Npgsql/Providers/PostgresConnectionStringProvider.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
using EasyExtensions.EntityFrameworkCore.Npgsql.Builders;
33
using Microsoft.Extensions.Configuration;
44
using Npgsql;
5-
using System;
6-
using System.Collections.Generic;
7-
using System.Text;
85

96
namespace EasyExtensions.EntityFrameworkCore.Npgsql.Providers
107
{
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace EasyExtensions.Models.Enums
2+
{
3+
/// <summary>
4+
/// Specifies the set of roles that can be assigned to a user within the system.
5+
/// </summary>
6+
/// <remarks>Use this enumeration to define user permissions and access levels. The roles represent
7+
/// increasing levels of responsibility and access, from standard users to administrators. The meaning and usage of
8+
/// the 'Custom' role may vary depending on application-specific requirements.</remarks>
9+
public enum UserRole
10+
{
11+
/// <summary>
12+
/// Indicates that the value is unknown or not specified.
13+
/// </summary>
14+
/// <remarks>Use this value when the actual value cannot be determined or is not applicable. This
15+
/// is typically used as a default or placeholder value.</remarks>
16+
Unknown = 1,
17+
18+
/// <summary>
19+
/// Represents a standard user account with typical permissions and access rights.
20+
/// </summary>
21+
User = 2,
22+
23+
/// <summary>
24+
/// Specifies an administrator role with elevated permissions.
25+
/// </summary>
26+
Admin = 4,
27+
}
28+
}

0 commit comments

Comments
 (0)