aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Entities
diff options
context:
space:
mode:
Diffstat (limited to 'Timeline/Entities')
-rw-r--r--Timeline/Entities/Http/User.cs16
-rw-r--r--Timeline/Entities/UserInfo.cs74
-rw-r--r--Timeline/Entities/UserUtility.cs49
3 files changed, 64 insertions, 75 deletions
diff --git a/Timeline/Entities/Http/User.cs b/Timeline/Entities/Http/User.cs
index 31cafaa3..db3d5071 100644
--- a/Timeline/Entities/Http/User.cs
+++ b/Timeline/Entities/Http/User.cs
@@ -1,9 +1,15 @@
namespace Timeline.Entities.Http
{
- public class UserModifyRequest
+ public class UserPutRequest
{
public string Password { get; set; }
- public string[] Roles { get; set; }
+ public bool IsAdmin { get; set; }
+ }
+
+ public class UserPatchRequest
+ {
+ public string Password { get; set; }
+ public bool? IsAdmin { get; set; }
}
public static class UserPutResponse
@@ -47,8 +53,8 @@
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.");
+ 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 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}";
}
}
}
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)));
+ }
+
+ }
+}