aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Entities/UserInfo.cs
blob: c9bcde5b327c86b4202e0de9554c1ad71db7e2b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Linq;
using Timeline.Models;

namespace Timeline.Entities
{
    public sealed class UserInfo
    {
        public UserInfo()
        {
        }

        public UserInfo(string username, params string[] roles)
        {
            Username = username;
            Roles = roles;
        }

        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 override string ToString()
        {
            return $"Username: {Username} ; Roles: {Roles}";
        }
    }
}