aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-01-29 23:13:15 +0800
committercrupest <crupest@outlook.com>2020-01-29 23:13:15 +0800
commitdd0097af5c4ccbe25a1faca2286d729c93fd4116 (patch)
treec9131a7b95fffd64bf2c26527d7f62fbdefa7e2c /Timeline/Services
parent401a0c86054711bf5ebdce7d7717c9b59bffc2fa (diff)
downloadtimeline-dd0097af5c4ccbe25a1faca2286d729c93fd4116.tar.gz
timeline-dd0097af5c4ccbe25a1faca2286d729c93fd4116.tar.bz2
timeline-dd0097af5c4ccbe25a1faca2286d729c93fd4116.zip
...
Diffstat (limited to 'Timeline/Services')
-rw-r--r--Timeline/Services/User.cs49
-rw-r--r--Timeline/Services/UserRoleConvert.cs44
-rw-r--r--Timeline/Services/UserService.cs1
3 files changed, 93 insertions, 1 deletions
diff --git a/Timeline/Services/User.cs b/Timeline/Services/User.cs
new file mode 100644
index 00000000..f63a374e
--- /dev/null
+++ b/Timeline/Services/User.cs
@@ -0,0 +1,49 @@
+using Microsoft.AspNetCore.Mvc;
+using System;
+using Timeline.Controllers;
+
+namespace Timeline.Services
+{
+ public class User
+ {
+ public string? Username { get; set; }
+ public string? Nickname { get; set; }
+ public string? AvatarUrl { get; set; }
+
+ #region adminsecret
+ public bool? Administrator { get; set; }
+ #endregion adminsecret
+
+ #region secret
+ public long? Id { get; set; }
+ public string? Password { get; set; }
+ public long? Version { get; set; }
+ #endregion secret
+ }
+
+ public static class UserExtensions
+ {
+ public static User EraseSecretAndFinalFill(this User user, IUrlHelper urlHelper, bool adminstrator)
+ {
+ if (user == null)
+ throw new ArgumentNullException(nameof(user));
+
+ var result = new User
+ {
+ Username = user.Username,
+ Nickname = user.Nickname,
+ AvatarUrl = urlHelper.ActionLink(action: nameof(UserAvatarController.Get), controller: nameof(UserAvatarController), values: new
+ {
+ user.Username
+ })
+ };
+
+ if (adminstrator)
+ {
+ result.Administrator = user.Administrator;
+ }
+
+ return result;
+ }
+ }
+}
diff --git a/Timeline/Services/UserRoleConvert.cs b/Timeline/Services/UserRoleConvert.cs
new file mode 100644
index 00000000..4fa4a7b8
--- /dev/null
+++ b/Timeline/Services/UserRoleConvert.cs
@@ -0,0 +1,44 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Timeline.Entities;
+
+namespace Timeline.Services
+{
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "No need.")]
+ public static class UserRoleConvert
+ {
+ public const string UserRole = UserRoles.User;
+ public const string AdminRole = UserRoles.Admin;
+
+ public static string[] ToArray(bool administrator)
+ {
+ return administrator ? new string[] { UserRole, AdminRole } : new string[] { UserRole };
+ }
+
+ public static string[] ToArray(string s)
+ {
+ return s.Split(',').ToArray();
+ }
+
+ public static bool ToBool(IReadOnlyCollection<string> roles)
+ {
+ return roles.Contains(AdminRole);
+ }
+
+ public static string ToString(IReadOnlyCollection<string> roles)
+ {
+ return string.Join(',', roles);
+ }
+
+ public static string ToString(bool administrator)
+ {
+ return administrator ? UserRole + "," + AdminRole : UserRole;
+ }
+
+ public static bool ToBool(string s)
+ {
+ return s.Contains("admin", StringComparison.InvariantCulture);
+ }
+ }
+}
diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs
index 616e70ba..ff2306c5 100644
--- a/Timeline/Services/UserService.cs
+++ b/Timeline/Services/UserService.cs
@@ -6,7 +6,6 @@ using System.Linq;
using System.Threading.Tasks;
using Timeline.Entities;
using Timeline.Helpers;
-using Timeline.Models;
using Timeline.Models.Validation;
using static Timeline.Resources.Services.UserService;