diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-05 18:58:56 +0800 |
---|---|---|
committer | 杨宇千 <crupest@outlook.com> | 2019-08-05 18:58:56 +0800 |
commit | dc1ab11cea249f4ca967f86b115147a63f7c93a5 (patch) | |
tree | b5f26eabcfa547e76dd7e70ffe0763f3fadf0ad5 /Timeline/Models/UserUtility.cs | |
parent | 5f5458518df0475fa36af754cae52eb31eb92fa3 (diff) | |
download | timeline-dc1ab11cea249f4ca967f86b115147a63f7c93a5.tar.gz timeline-dc1ab11cea249f4ca967f86b115147a63f7c93a5.tar.bz2 timeline-dc1ab11cea249f4ca967f86b115147a63f7c93a5.zip |
3 things.
1. Exchange Models and Entities namespace.
2. Fix the bug that input with missing field leads to 500.
3. Write unit tests.
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..711e321a --- /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 }; + } + } +} |