mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat(roles, visibility): refactor user roles to use flags and introduce visibility enum
This commit is contained in:
@@ -19,7 +19,8 @@ namespace SharpbinV3.Server.Services
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public string DisplayName { get; set; } = string.Empty;
|
||||
public bool TotpEnabled { get; set; }
|
||||
public int[] Roles { get; set; } = [];
|
||||
public Role Roles { get; set; }
|
||||
public bool IsBanned { get; set; }
|
||||
public DateTime Expires { get; set; }
|
||||
}
|
||||
|
||||
@@ -54,7 +55,7 @@ namespace SharpbinV3.Server.Services
|
||||
};
|
||||
|
||||
if (_authSettings.First_User_Admin && !await _db.Users.AnyAsync())
|
||||
user.Roles = [0, 1, 255];
|
||||
user.Roles = Role.User | Role.Admin;
|
||||
|
||||
_db.Users.Add(user);
|
||||
await _db.SaveChangesAsync();
|
||||
@@ -73,19 +74,11 @@ namespace SharpbinV3.Server.Services
|
||||
new("username", user.Username),
|
||||
new("displayname", user.DisplayName ?? ""),
|
||||
new("totp_enabled", totpEnabled.ToString()),
|
||||
new("roles", ((int)user.Roles).ToString()),
|
||||
new("is_banned", user.IsBanned.ToString()),
|
||||
new(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
|
||||
};
|
||||
|
||||
if (user.Roles != null)
|
||||
{
|
||||
foreach (var role in user.Roles)
|
||||
claims.Add(new Claim(ClaimTypes.Role, role.ToString()));
|
||||
}
|
||||
else
|
||||
{
|
||||
claims.Add(new Claim(ClaimTypes.Role, "0"));
|
||||
}
|
||||
|
||||
var descriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(claims),
|
||||
|
||||
@@ -4,6 +4,7 @@ using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SharpbinV3.Server.Data;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
using SharpbinV3.Server.Data.Enums;
|
||||
using SharpbinV3.Server.Extensions;
|
||||
using SharpbinV3.Server.Settings;
|
||||
|
||||
@@ -23,7 +24,7 @@ namespace SharpbinV3.Server.Services
|
||||
string content,
|
||||
string title,
|
||||
string syntax,
|
||||
int visibility,
|
||||
Visibility visibility,
|
||||
long expiresAt,
|
||||
bool shouldCompress
|
||||
)
|
||||
@@ -198,9 +199,9 @@ namespace SharpbinV3.Server.Services
|
||||
return title.Length <= _pasteSettings.MaxTitleLength;
|
||||
}
|
||||
|
||||
public bool ValidateVisibility(int visibility)
|
||||
public bool ValidateVisibility(Visibility visibility)
|
||||
{
|
||||
return visibility >= 0 && visibility <= 2;
|
||||
return Enum.IsDefined(typeof(Visibility), visibility);
|
||||
}
|
||||
|
||||
public bool ValidateExpiresAt(long expiresAt)
|
||||
|
||||
Reference in New Issue
Block a user