diff options
author | crupest <crupest@outlook.com> | 2019-05-16 21:57:56 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-05-16 21:57:56 +0800 |
commit | 3de4179449a209646e0e5a967d270f7fa0878c03 (patch) | |
tree | a71563526342e8cc4a2c99227fcd178ea6d1fbde /Timeline/Entities/UserUtility.cs | |
parent | 461a37e3f2058d98c69cf598c1cd34a1d405a11e (diff) | |
download | timeline-3de4179449a209646e0e5a967d270f7fa0878c03.tar.gz timeline-3de4179449a209646e0e5a967d270f7fa0878c03.tar.bz2 timeline-3de4179449a209646e0e5a967d270f7fa0878c03.zip |
Change roles in UserInfo into isadmin.
Diffstat (limited to 'Timeline/Entities/UserUtility.cs')
-rw-r--r-- | Timeline/Entities/UserUtility.cs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/Timeline/Entities/UserUtility.cs b/Timeline/Entities/UserUtility.cs new file mode 100644 index 00000000..9a272948 --- /dev/null +++ b/Timeline/Entities/UserUtility.cs @@ -0,0 +1,49 @@ +using System; +using System.Linq; +using Timeline.Entities; +using Timeline.Models; + +namespace Timeline.Entities +{ + public static class UserUtility + { + public const string UserRole = "user"; + public const string AdminRole = "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 UserInfo CreateUserInfo(User user) + { + if (user == null) + throw new ArgumentNullException(nameof(user)); + return new UserInfo(user.Name, RoleArrayToIsAdmin(RoleStringToRoleArray(user.RoleString))); + } + + } +} |