From abde51b167f17301c25e32ae247e7909c0dc9367 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 29 Jan 2020 23:13:15 +0800 Subject: ... --- Timeline/Services/User.cs | 49 ++++++++++++++++++++++++++++++++++++ Timeline/Services/UserRoleConvert.cs | 44 ++++++++++++++++++++++++++++++++ Timeline/Services/UserService.cs | 1 - 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 Timeline/Services/User.cs create mode 100644 Timeline/Services/UserRoleConvert.cs (limited to 'Timeline/Services') 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 roles) + { + return roles.Contains(AdminRole); + } + + public static string ToString(IReadOnlyCollection 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; -- cgit v1.2.3