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 | 38ff45fcc0b58a95ad52ba43a8be4ff466694269 (patch) | |
tree | f1cf455b758e8bf0265d4db0e42a404e9877b321 /Timeline/Models/UserUtility.cs | |
parent | e283a3e745bad05a55c572646d7b20fbaaeb522d (diff) | |
parent | ea8397bafe24b5c9ab814891eb3a293a07ca217e (diff) | |
download | timeline-38ff45fcc0b58a95ad52ba43a8be4ff466694269.tar.gz timeline-38ff45fcc0b58a95ad52ba43a8be4ff466694269.tar.bz2 timeline-38ff45fcc0b58a95ad52ba43a8be4ff466694269.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 };
+ }
+ }
+}
|