aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Models/UserConvert.cs
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-10-24 20:15:58 +0800
committerGitHub <noreply@github.com>2019-10-24 20:15:58 +0800
commit7305358a88ffc87f51f7b78deb4f07ef99120beb (patch)
tree7ca5010a06829cc5fadea1ea17ae72d082fc344c /Timeline/Models/UserConvert.cs
parent297d0c9029360f1d5334ed843b9b299356740ec1 (diff)
parenta0f3cd7599a48c14fb5492fb1c6e2dbd0a82fb45 (diff)
downloadtimeline-7305358a88ffc87f51f7b78deb4f07ef99120beb.tar.gz
timeline-7305358a88ffc87f51f7b78deb4f07ef99120beb.tar.bz2
timeline-7305358a88ffc87f51f7b78deb4f07ef99120beb.zip
Merge pull request #50 from crupest/refactor
Refactor : A Huge Step
Diffstat (limited to 'Timeline/Models/UserConvert.cs')
-rw-r--r--Timeline/Models/UserConvert.cs67
1 files changed, 67 insertions, 0 deletions
diff --git a/Timeline/Models/UserConvert.cs b/Timeline/Models/UserConvert.cs
new file mode 100644
index 00000000..5b132421
--- /dev/null
+++ b/Timeline/Models/UserConvert.cs
@@ -0,0 +1,67 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Timeline.Entities;
+using Timeline.Services;
+
+namespace Timeline.Models
+{
+ public static class UserConvert
+ {
+ public static UserInfo CreateUserInfo(User user)
+ {
+ if (user == null)
+ throw new ArgumentNullException(nameof(user));
+ return new UserInfo(user.Name, UserRoleConvert.ToBool(user.RoleString));
+ }
+
+ internal static UserCache CreateUserCache(User user)
+ {
+ if (user == null)
+ throw new ArgumentNullException(nameof(user));
+ return new UserCache
+ {
+ Username = user.Name,
+ Administrator = UserRoleConvert.ToBool(user.RoleString),
+ Version = user.Version
+ };
+ }
+ }
+
+ [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);
+ }
+ }
+}