aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Entities
diff options
context:
space:
mode:
Diffstat (limited to 'Timeline/Entities')
-rw-r--r--Timeline/Entities/Http/Common.cs29
-rw-r--r--Timeline/Entities/Http/Token.cs (renamed from Timeline/Entities/User.cs)17
-rw-r--r--Timeline/Entities/Http/User.cs54
-rw-r--r--Timeline/Entities/UserInfo.cs77
4 files changed, 156 insertions, 21 deletions
diff --git a/Timeline/Entities/Http/Common.cs b/Timeline/Entities/Http/Common.cs
new file mode 100644
index 00000000..9575e6fa
--- /dev/null
+++ b/Timeline/Entities/Http/Common.cs
@@ -0,0 +1,29 @@
+namespace Timeline.Entities.Http
+{
+ public class ReturnCodeMessageResponse
+ {
+ public ReturnCodeMessageResponse()
+ {
+
+ }
+
+ public ReturnCodeMessageResponse(int code)
+ {
+ ReturnCode = code;
+ }
+
+ public ReturnCodeMessageResponse(string message)
+ {
+ Message = message;
+ }
+
+ public ReturnCodeMessageResponse(int code, string message)
+ {
+ ReturnCode = code;
+ Message = message;
+ }
+
+ public int? ReturnCode { get; set; } = null;
+ public string Message { get; set; } = null;
+ }
+}
diff --git a/Timeline/Entities/User.cs b/Timeline/Entities/Http/Token.cs
index b5664bb0..45ee0fc5 100644
--- a/Timeline/Entities/User.cs
+++ b/Timeline/Entities/Http/Token.cs
@@ -1,4 +1,4 @@
-namespace Timeline.Entities
+namespace Timeline.Entities.Http
{
public class CreateTokenRequest
{
@@ -23,19 +23,4 @@
public bool IsValid { get; set; }
public UserInfo UserInfo { get; set; }
}
-
- public class CreateUserRequest
- {
- public string Username { get; set; }
- public string Password { get; set; }
- public string[] Roles { get; set; }
- }
-
- public class CreateUserResponse
- {
- public const int SuccessCode = 0;
- public const int AlreadyExistsCode = 1;
-
- public int ReturnCode { get; set; }
- }
}
diff --git a/Timeline/Entities/Http/User.cs b/Timeline/Entities/Http/User.cs
new file mode 100644
index 00000000..31cafaa3
--- /dev/null
+++ b/Timeline/Entities/Http/User.cs
@@ -0,0 +1,54 @@
+namespace Timeline.Entities.Http
+{
+ public class UserModifyRequest
+ {
+ public string Password { get; set; }
+ public string[] Roles { get; set; }
+ }
+
+ public static class UserPutResponse
+ {
+ public const int CreatedCode = 0;
+ public const int ModifiedCode = 1;
+
+ public static ReturnCodeMessageResponse Created { get; } = new ReturnCodeMessageResponse(CreatedCode, "A new user is created.");
+ public static ReturnCodeMessageResponse Modified { get; } = new ReturnCodeMessageResponse(ModifiedCode, "A existing user is modified.");
+ }
+
+ public static class UserDeleteResponse
+ {
+ public const int DeletedCode = 0;
+ public const int NotExistsCode = 1;
+
+ public static ReturnCodeMessageResponse Deleted { get; } = new ReturnCodeMessageResponse(DeletedCode, "A existing user is deleted.");
+ public static ReturnCodeMessageResponse NotExists { get; } = new ReturnCodeMessageResponse(NotExistsCode, "User with given name does not exists.");
+ }
+
+ public class ChangePasswordRequest
+ {
+ public string OldPassword { get; set; }
+ public string NewPassword { get; set; }
+ }
+
+ public static class ChangePasswordResponse
+ {
+ public const int SuccessCode = 0;
+ public const int BadOldPasswordCode = 1;
+ public const int NotExistsCode = 2;
+
+ public static ReturnCodeMessageResponse Success { get; } = new ReturnCodeMessageResponse(SuccessCode, "Success to change password.");
+ public static ReturnCodeMessageResponse BadOldPassword { get; } = new ReturnCodeMessageResponse(BadOldPasswordCode, "Old password is wrong.");
+ public static ReturnCodeMessageResponse NotExists { get; } = new ReturnCodeMessageResponse(NotExistsCode, "Username does not exists, please update token.");
+ }
+
+ public static class PutAvatarResponse
+ {
+ public const int SuccessCode = 0;
+ public const int ForbiddenCode = 1;
+ public const int NotExistsCode = 2;
+
+ public static ReturnCodeMessageResponse Success {get;} = new ReturnCodeMessageResponse(SuccessCode, "Success to upload avatar.");
+ public static ReturnCodeMessageResponse Forbidden {get;} = new ReturnCodeMessageResponse(ForbiddenCode, "You are not allowed to upload the user's avatar.");
+ public static ReturnCodeMessageResponse NotExists {get;} = new ReturnCodeMessageResponse(NotExistsCode, "The username does not exists. If you are a user, try update your token.");
+ }
+}
diff --git a/Timeline/Entities/UserInfo.cs b/Timeline/Entities/UserInfo.cs
index d9c5acad..c9bcde5b 100644
--- a/Timeline/Entities/UserInfo.cs
+++ b/Timeline/Entities/UserInfo.cs
@@ -1,26 +1,93 @@
using System;
+using System.Collections.Generic;
using System.Linq;
using Timeline.Models;
namespace Timeline.Entities
{
- public class UserInfo
+ public sealed class UserInfo
{
public UserInfo()
{
+ }
+ public UserInfo(string username, params string[] roles)
+ {
+ Username = username;
+ Roles = roles;
}
- public UserInfo(User user)
+ public static UserInfo Create(User user)
{
if (user == null)
throw new ArgumentNullException(nameof(user));
-
- Username = user.Name;
- Roles = user.RoleString.Split(',').Select(s => s.Trim()).ToArray();
+ 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}";
+ }
}
}