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 | f0a317cc511a4a7b04a701c32881d1e3331f3711 (patch) | |
tree | a71563526342e8cc4a2c99227fcd178ea6d1fbde /Timeline/Entities/UserInfo.cs | |
parent | 44485a4559d75c8798972ba502c104d7e801e9ba (diff) | |
download | timeline-f0a317cc511a4a7b04a701c32881d1e3331f3711.tar.gz timeline-f0a317cc511a4a7b04a701c32881d1e3331f3711.tar.bz2 timeline-f0a317cc511a4a7b04a701c32881d1e3331f3711.zip |
Change roles in UserInfo into isadmin.
Diffstat (limited to 'Timeline/Entities/UserInfo.cs')
-rw-r--r-- | Timeline/Entities/UserInfo.cs | 74 |
1 files changed, 4 insertions, 70 deletions
diff --git a/Timeline/Entities/UserInfo.cs b/Timeline/Entities/UserInfo.cs index c9bcde5b..bb56df9d 100644 --- a/Timeline/Entities/UserInfo.cs +++ b/Timeline/Entities/UserInfo.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using Timeline.Models; namespace Timeline.Entities { @@ -11,83 +10,18 @@ namespace Timeline.Entities { } - public UserInfo(string username, params string[] roles) + public UserInfo(string username, bool isAdmin) { Username = username; - Roles = roles; + IsAdmin = isAdmin; } - public static UserInfo Create(User user) - { - if (user == null) - throw new ArgumentNullException(nameof(user)); - return Create(user.Name, user.RoleString); - } - - public static UserInfo Create(string username, string roleString) => new UserInfo - { - Username = username, - Roles = RolesFromString(roleString) - }; - public string Username { get; set; } - public string[] Roles { get; set; } - - public static IEqualityComparer<UserInfo> EqualityComparer { get; } = new EqualityComparerImpl(); - public static IComparer<UserInfo> Comparer { get; } = Comparer<UserInfo>.Create(Compare); - - private static string[] RolesFromString(string roleString) - { - if (roleString == null) - return null; - return roleString.Split(',').Select(r => r.Trim()).ToArray(); - } - - private class EqualityComparerImpl : IEqualityComparer<UserInfo> - { - bool IEqualityComparer<UserInfo>.Equals(UserInfo x, UserInfo y) - { - return Compare(x, y) == 0; - } - - int IEqualityComparer<UserInfo>.GetHashCode(UserInfo obj) - { - return obj.Username.GetHashCode() ^ NormalizeRoles(obj.Roles).GetHashCode(); - } - } - - private static string NormalizeRoles(string[] rawRoles) - { - var roles = rawRoles.Where(r => !string.IsNullOrWhiteSpace(r)).Select(r => r.Trim()).ToList(); - roles.Sort(); - return string.Join(',', roles); - } - - public static int Compare(UserInfo left, UserInfo right) - { - if (left == null) - { - if (right == null) - return 0; - return -1; - } - - if (right == null) - return 1; - - var uc = string.Compare(left.Username, right.Username); - if (uc != 0) - return uc; - - var leftRoles = NormalizeRoles(left.Roles); - var rightRoles = NormalizeRoles(right.Roles); - - return string.Compare(leftRoles, rightRoles); - } + public bool IsAdmin { get; set; } public override string ToString() { - return $"Username: {Username} ; Roles: {Roles}"; + return $"Username: {Username} ; IsAdmin: {IsAdmin}"; } } } |