diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-09 21:48:12 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-09 21:48:12 +0800 |
commit | d97185e6152a327f6ef3b1873bfd86f1a3aac3a1 (patch) | |
tree | f1cf455b758e8bf0265d4db0e42a404e9877b321 /Timeline/Models/UserUtility.cs | |
parent | 29bd71cd93b03248254f341aff9252374abc74ec (diff) | |
parent | c964cbbb6c8f228bd4c3943025c6107ac4e42a13 (diff) | |
download | timeline-d97185e6152a327f6ef3b1873bfd86f1a3aac3a1.tar.gz timeline-d97185e6152a327f6ef3b1873bfd86f1a3aac3a1.tar.bz2 timeline-d97185e6152a327f6ef3b1873bfd86f1a3aac3a1.zip |
Merge pull request #38 from crupest/null-request-field
Do 3 things.
Diffstat (limited to 'Timeline/Models/UserUtility.cs')
-rw-r--r-- | Timeline/Models/UserUtility.cs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/Timeline/Models/UserUtility.cs b/Timeline/Models/UserUtility.cs new file mode 100644 index 00000000..405987b5 --- /dev/null +++ b/Timeline/Models/UserUtility.cs @@ -0,0 +1,60 @@ +using System;
+using System.Linq;
+using Timeline.Entities;
+using Timeline.Services;
+
+namespace Timeline.Models
+{
+ public static class UserUtility
+ {
+ public const string UserRole = UserRoles.User;
+ public const string AdminRole = UserRoles.Admin;
+
+ public static string[] UserRoleArray { get; } = new string[] { UserRole };
+ public static string[] AdminRoleArray { get; } = new string[] { UserRole, AdminRole };
+
+ public static string[] IsAdminToRoleArray(bool isAdmin)
+ {
+ return isAdmin ? AdminRoleArray : UserRoleArray;
+ }
+
+ public static bool RoleArrayToIsAdmin(string[] roles)
+ {
+ return roles.Contains(AdminRole);
+ }
+
+ public static string[] RoleStringToRoleArray(string roleString)
+ {
+ return roleString.Split(',').ToArray();
+ }
+
+ public static string RoleArrayToRoleString(string[] roles)
+ {
+ return string.Join(',', roles);
+ }
+
+ public static string IsAdminToRoleString(bool isAdmin)
+ {
+ return RoleArrayToRoleString(IsAdminToRoleArray(isAdmin));
+ }
+
+ public static bool RoleStringToIsAdmin(string roleString)
+ {
+ return RoleArrayToIsAdmin(RoleStringToRoleArray(roleString));
+ }
+
+ public static UserInfo CreateUserInfo(User user)
+ {
+ if (user == null)
+ throw new ArgumentNullException(nameof(user));
+ return new UserInfo(user.Name, RoleStringToIsAdmin(user.RoleString));
+ }
+
+ internal static UserCache CreateUserCache(User user)
+ {
+ if (user == null)
+ throw new ArgumentNullException(nameof(user));
+ return new UserCache { Username = user.Name, Administrator = RoleStringToIsAdmin(user.RoleString), Version = user.Version };
+ }
+ }
+}
|