aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Entities
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-07-27 21:47:14 +0800
committerGitHub <noreply@github.com>2019-07-27 21:47:14 +0800
commitdc8269f1034d155e74ea4807d44c755fb83a7630 (patch)
tree572a2ae5c65c484718b3bfda68fd8babc56fe6f2 /Timeline/Entities
parentf0a317cc511a4a7b04a701c32881d1e3331f3711 (diff)
parentcccaf4cfdc1b932882768975f409763c22ed1ee1 (diff)
downloadtimeline-dc8269f1034d155e74ea4807d44c755fb83a7630.tar.gz
timeline-dc8269f1034d155e74ea4807d44c755fb83a7630.tar.bz2
timeline-dc8269f1034d155e74ea4807d44c755fb83a7630.zip
Merge pull request #25 from crupest/auth
Refactor a lot, especially authentication.
Diffstat (limited to 'Timeline/Entities')
-rw-r--r--Timeline/Entities/Http/Common.cs38
-rw-r--r--Timeline/Entities/Http/Token.cs6
-rw-r--r--Timeline/Entities/Http/User.cs40
-rw-r--r--Timeline/Entities/PutResult.cs17
-rw-r--r--Timeline/Entities/UserInfo.cs4
-rw-r--r--Timeline/Entities/UserUtility.cs19
6 files changed, 57 insertions, 67 deletions
diff --git a/Timeline/Entities/Http/Common.cs b/Timeline/Entities/Http/Common.cs
index 9575e6fa..3a45a0ae 100644
--- a/Timeline/Entities/Http/Common.cs
+++ b/Timeline/Entities/Http/Common.cs
@@ -1,29 +1,37 @@
namespace Timeline.Entities.Http
{
- public class ReturnCodeMessageResponse
+ public class CommonResponse
{
- public ReturnCodeMessageResponse()
+ public CommonResponse()
{
}
- public ReturnCodeMessageResponse(int code)
- {
- ReturnCode = code;
- }
-
- public ReturnCodeMessageResponse(string message)
+ public CommonResponse(int code, string message)
{
+ Code = code;
Message = message;
}
- public ReturnCodeMessageResponse(int code, string message)
- {
- ReturnCode = code;
- Message = message;
- }
+ public int Code { get; set; }
+ public string Message { get; set; }
+ }
+
+ public static class CommonPutResponse
+ {
+ public const int CreatedCode = 0;
+ public const int ModifiedCode = 1;
+
+ public static CommonResponse Created { get; } = new CommonResponse(CreatedCode, "A new item is created.");
+ public static CommonResponse Modified { get; } = new CommonResponse(ModifiedCode, "An existent item is modified.");
+ }
+
+ public static class CommonDeleteResponse
+ {
+ public const int DeletedCode = 0;
+ public const int NotExistsCode = 1;
- public int? ReturnCode { get; set; } = null;
- public string Message { get; set; } = null;
+ public static CommonResponse Deleted { get; } = new CommonResponse(DeletedCode, "An existent item is deleted.");
+ public static CommonResponse NotExists { get; } = new CommonResponse(NotExistsCode, "The item does not exist.");
}
}
diff --git a/Timeline/Entities/Http/Token.cs b/Timeline/Entities/Http/Token.cs
index 45ee0fc5..aeb9fbf2 100644
--- a/Timeline/Entities/Http/Token.cs
+++ b/Timeline/Entities/Http/Token.cs
@@ -8,9 +8,8 @@
public class CreateTokenResponse
{
- public bool Success { get; set; }
public string Token { get; set; }
- public UserInfo UserInfo { get; set; }
+ public UserInfo User { get; set; }
}
public class VerifyTokenRequest
@@ -20,7 +19,6 @@
public class VerifyTokenResponse
{
- public bool IsValid { get; set; }
- public UserInfo UserInfo { get; set; }
+ public UserInfo User { get; set; }
}
}
diff --git a/Timeline/Entities/Http/User.cs b/Timeline/Entities/Http/User.cs
index db3d5071..91423c7b 100644
--- a/Timeline/Entities/Http/User.cs
+++ b/Timeline/Entities/Http/User.cs
@@ -12,49 +12,9 @@
public bool? IsAdmin { 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/PutResult.cs b/Timeline/Entities/PutResult.cs
new file mode 100644
index 00000000..4ed48572
--- /dev/null
+++ b/Timeline/Entities/PutResult.cs
@@ -0,0 +1,17 @@
+namespace Timeline.Entities
+{
+ /// <summary>
+ /// Represents the result of a "put" operation.
+ /// </summary>
+ public enum PutResult
+ {
+ /// <summary>
+ /// Indicates the item did not exist and now is created.
+ /// </summary>
+ Created,
+ /// <summary>
+ /// Indicates the item exists already and is modified.
+ /// </summary>
+ Modified
+ }
+}
diff --git a/Timeline/Entities/UserInfo.cs b/Timeline/Entities/UserInfo.cs
index bb56df9d..9a82c991 100644
--- a/Timeline/Entities/UserInfo.cs
+++ b/Timeline/Entities/UserInfo.cs
@@ -1,7 +1,3 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-
namespace Timeline.Entities
{
public sealed class UserInfo
diff --git a/Timeline/Entities/UserUtility.cs b/Timeline/Entities/UserUtility.cs
index 9a272948..cbbd391c 100644
--- a/Timeline/Entities/UserUtility.cs
+++ b/Timeline/Entities/UserUtility.cs
@@ -1,14 +1,14 @@
using System;
using System.Linq;
-using Timeline.Entities;
using Timeline.Models;
+using Timeline.Services;
namespace Timeline.Entities
{
public static class UserUtility
{
- public const string UserRole = "user";
- public const string AdminRole = "admin";
+ public const string UserRole = UserRoles.User;
+ public const string AdminRole = UserRoles.Admin;
public static string[] UserRoleArray { get; } = new string[] { UserRole };
public static string[] AdminRoleArray { get; } = new string[] { UserRole, AdminRole };
@@ -38,12 +38,23 @@ namespace Timeline.Entities
return RoleArrayToRoleString(IsAdminToRoleArray(isAdmin));
}
+ public static bool RoleStringToIsAdmin(string roleString)
+ {
+ return RoleArrayToIsAdmin(RoleStringToRoleArray(roleString));
+ }
+
public static UserInfo CreateUserInfo(User user)
{
if (user == null)
throw new ArgumentNullException(nameof(user));
- return new UserInfo(user.Name, RoleArrayToIsAdmin(RoleStringToRoleArray(user.RoleString)));
+ return new UserInfo(user.Name, RoleStringToIsAdmin(user.RoleString));
}
+ internal static UserCache CreateUserCache(User user)
+ {
+ if (user == null)
+ throw new ArgumentNullException(nameof(user));
+ return new UserCache { Username = user.Name, IsAdmin = RoleStringToIsAdmin(user.RoleString), Version = user.Version };
+ }
}
}