From 57151c879376374425adb04fb68dad2cf7930df8 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Mon, 5 Aug 2019 18:58:56 +0800 Subject: 3 things. 1. Exchange Models and Entities namespace. 2. Fix the bug that input with missing field leads to 500. 3. Write unit tests. --- Timeline/Authenticate/Attribute.cs | 2 +- Timeline/Authenticate/AuthHandler.cs | 3 +- Timeline/Controllers/TokenController.cs | 18 ++----- Timeline/Controllers/UserController.cs | 5 +- Timeline/Controllers/UserTestController.cs | 1 + Timeline/Entities/DatabaseContext.cs | 42 +++++++++++++++ Timeline/Entities/Http/Common.cs | 37 ------------- Timeline/Entities/Http/Token.cs | 26 ---------- Timeline/Entities/Http/User.cs | 20 -------- Timeline/Entities/PutResult.cs | 17 ------ Timeline/Entities/UserInfo.cs | 23 --------- Timeline/Entities/UserUtility.cs | 60 ---------------------- Timeline/Helpers/InvalidModelResponseFactory.cs | 25 +++++++++ .../20190412102517_InitCreate.Designer.cs | 2 +- .../20190412144150_AddAdminUser.Designer.cs | 2 +- ...0412153003_MakeColumnsInUserNotNull.Designer.cs | 2 +- .../20190719115321_Add-User-Version.Designer.cs | 2 +- .../Migrations/DatabaseContextModelSnapshot.cs | 2 +- Timeline/Models/DatabaseContext.cs | 42 --------------- Timeline/Models/Http/Common.cs | 51 ++++++++++++++++++ Timeline/Models/Http/Token.cs | 32 ++++++++++++ Timeline/Models/Http/User.cs | 26 ++++++++++ Timeline/Models/PutResult.cs | 17 ++++++ Timeline/Models/UserInfo.cs | 23 +++++++++ Timeline/Models/UserUtility.cs | 60 ++++++++++++++++++++++ Timeline/Services/UserService.cs | 2 +- Timeline/Startup.cs | 9 +++- 27 files changed, 299 insertions(+), 252 deletions(-) create mode 100644 Timeline/Entities/DatabaseContext.cs delete mode 100644 Timeline/Entities/Http/Common.cs delete mode 100644 Timeline/Entities/Http/Token.cs delete mode 100644 Timeline/Entities/Http/User.cs delete mode 100644 Timeline/Entities/PutResult.cs delete mode 100644 Timeline/Entities/UserInfo.cs delete mode 100644 Timeline/Entities/UserUtility.cs create mode 100644 Timeline/Helpers/InvalidModelResponseFactory.cs delete mode 100644 Timeline/Models/DatabaseContext.cs create mode 100644 Timeline/Models/Http/Common.cs create mode 100644 Timeline/Models/Http/Token.cs create mode 100644 Timeline/Models/Http/User.cs create mode 100644 Timeline/Models/PutResult.cs create mode 100644 Timeline/Models/UserInfo.cs create mode 100644 Timeline/Models/UserUtility.cs (limited to 'Timeline') diff --git a/Timeline/Authenticate/Attribute.cs b/Timeline/Authenticate/Attribute.cs index 50b2681d..645eb236 100644 --- a/Timeline/Authenticate/Attribute.cs +++ b/Timeline/Authenticate/Attribute.cs @@ -1,5 +1,5 @@ using Microsoft.AspNetCore.Authorization; -using Timeline.Models; +using Timeline.Entities; namespace Timeline.Authenticate { diff --git a/Timeline/Authenticate/AuthHandler.cs b/Timeline/Authenticate/AuthHandler.cs index 41cb11c6..41d05831 100644 --- a/Timeline/Authenticate/AuthHandler.cs +++ b/Timeline/Authenticate/AuthHandler.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Security.Claims; using System.Text.Encodings.Web; using System.Threading.Tasks; +using Timeline.Models; using Timeline.Services; namespace Timeline.Authenticate @@ -80,7 +81,7 @@ namespace Timeline.Authenticate var identity = new ClaimsIdentity(AuthConstants.Scheme); identity.AddClaim(new Claim(identity.NameClaimType, userInfo.Username, ClaimValueTypes.String)); - identity.AddClaims(Entities.UserUtility.IsAdminToRoleArray(userInfo.Administrator).Select(role => new Claim(identity.RoleClaimType, role, ClaimValueTypes.String))); + identity.AddClaims(UserUtility.IsAdminToRoleArray(userInfo.Administrator).Select(role => new Claim(identity.RoleClaimType, role, ClaimValueTypes.String))); var principal = new ClaimsPrincipal(); principal.AddIdentity(identity); diff --git a/Timeline/Controllers/TokenController.cs b/Timeline/Controllers/TokenController.cs index 549e227b..57407558 100644 --- a/Timeline/Controllers/TokenController.cs +++ b/Timeline/Controllers/TokenController.cs @@ -5,13 +5,14 @@ using Microsoft.IdentityModel.Tokens; using System; using System.Collections.Generic; using System.Threading.Tasks; -using Timeline.Entities.Http; +using Timeline.Models.Http; using Timeline.Services; using static Timeline.Helpers.MyLogHelper; namespace Timeline.Controllers { [Route("token")] + [ApiController] public class TokenController : Controller { private static class LoggingEventIds @@ -60,22 +61,9 @@ namespace Timeline.Controllers Pair("Expire Offset (in days)", request.ExpireOffset))); } - TimeSpan? expireOffset = null; - if (request.ExpireOffset != null) - { - if (request.ExpireOffset.Value <= 0.0) - { - const string message = "Expire time is not bigger than 0."; - var code = ErrorCodes.Create_BadExpireOffset; - LogFailure(message, code); - return BadRequest(new CommonResponse(code, message)); - } - expireOffset = TimeSpan.FromDays(request.ExpireOffset.Value); - } - try { - var expiredTime = expireOffset == null ? null : (DateTime?)(_clock.GetCurrentTime() + expireOffset.Value); + var expiredTime = request.ExpireOffset == null ? null : (DateTime?)(_clock.GetCurrentTime().AddDays(request.ExpireOffset.Value)); var result = await _userService.CreateToken(request.Username, request.Password, expiredTime); _logger.LogInformation(LoggingEventIds.CreateSucceeded, FormatLogMessage("Attemp to login succeeded.", Pair("Username", request.Username), diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index 2099690c..042a8107 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -4,13 +4,14 @@ using Microsoft.Extensions.Logging; using System; using System.Threading.Tasks; using Timeline.Authenticate; -using Timeline.Entities; -using Timeline.Entities.Http; +using Timeline.Models; +using Timeline.Models.Http; using Timeline.Services; using static Timeline.Helpers.MyLogHelper; namespace Timeline.Controllers { + [ApiController] public class UserController : Controller { private static class ErrorCodes diff --git a/Timeline/Controllers/UserTestController.cs b/Timeline/Controllers/UserTestController.cs index 21686b81..a81f42a8 100644 --- a/Timeline/Controllers/UserTestController.cs +++ b/Timeline/Controllers/UserTestController.cs @@ -5,6 +5,7 @@ using Timeline.Authenticate; namespace Timeline.Controllers { [Route("Test/User")] + [ApiController] public class UserTestController : Controller { [HttpGet("[action]")] diff --git a/Timeline/Entities/DatabaseContext.cs b/Timeline/Entities/DatabaseContext.cs new file mode 100644 index 00000000..9fe046ac --- /dev/null +++ b/Timeline/Entities/DatabaseContext.cs @@ -0,0 +1,42 @@ +using Microsoft.EntityFrameworkCore; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace Timeline.Entities +{ + public static class UserRoles + { + public const string Admin = "admin"; + public const string User = "user"; + } + + [Table("user")] + public class User + { + [Column("id"), Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public long Id { get; set; } + + [Column("name"), Required] + public string Name { get; set; } + + [Column("password"), Required] + public string EncryptedPassword { get; set; } + + [Column("roles"), Required] + public string RoleString { get; set; } + + [Column("version"), Required] + public long Version { get; set; } + } + + public class DatabaseContext : DbContext + { + public DatabaseContext(DbContextOptions options) + : base(options) + { + + } + + public DbSet Users { get; set; } + } +} diff --git a/Timeline/Entities/Http/Common.cs b/Timeline/Entities/Http/Common.cs deleted file mode 100644 index 3a45a0ae..00000000 --- a/Timeline/Entities/Http/Common.cs +++ /dev/null @@ -1,37 +0,0 @@ -namespace Timeline.Entities.Http -{ - public class CommonResponse - { - public CommonResponse() - { - - } - - public CommonResponse(int code, string message) - { - Code = 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 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 deleted file mode 100644 index 8a02ed2e..00000000 --- a/Timeline/Entities/Http/Token.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace Timeline.Entities.Http -{ - public class CreateTokenRequest - { - public string Username { get; set; } - public string Password { get; set; } - // in day - public double? ExpireOffset { get; set; } - } - - public class CreateTokenResponse - { - public string Token { get; set; } - public UserInfo User { get; set; } - } - - public class VerifyTokenRequest - { - public string Token { get; set; } - } - - public class VerifyTokenResponse - { - public UserInfo User { get; set; } - } -} diff --git a/Timeline/Entities/Http/User.cs b/Timeline/Entities/Http/User.cs deleted file mode 100644 index b5384778..00000000 --- a/Timeline/Entities/Http/User.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace Timeline.Entities.Http -{ - public class UserPutRequest - { - public string Password { get; set; } - public bool Administrator { get; set; } - } - - public class UserPatchRequest - { - public string Password { get; set; } - public bool? Administrator { get; set; } - } - - public class ChangePasswordRequest - { - public string OldPassword { get; set; } - public string NewPassword { get; set; } - } -} diff --git a/Timeline/Entities/PutResult.cs b/Timeline/Entities/PutResult.cs deleted file mode 100644 index 4ed48572..00000000 --- a/Timeline/Entities/PutResult.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Timeline.Entities -{ - /// - /// Represents the result of a "put" operation. - /// - public enum PutResult - { - /// - /// Indicates the item did not exist and now is created. - /// - Created, - /// - /// Indicates the item exists already and is modified. - /// - Modified - } -} diff --git a/Timeline/Entities/UserInfo.cs b/Timeline/Entities/UserInfo.cs deleted file mode 100644 index 414a8dfe..00000000 --- a/Timeline/Entities/UserInfo.cs +++ /dev/null @@ -1,23 +0,0 @@ -namespace Timeline.Entities -{ - public sealed class UserInfo - { - public UserInfo() - { - } - - public UserInfo(string username, bool administrator) - { - Username = username; - Administrator = administrator; - } - - public string Username { get; set; } - public bool Administrator { get; set; } - - public override string ToString() - { - return $"Username: {Username} ; Administrator: {Administrator}"; - } - } -} diff --git a/Timeline/Entities/UserUtility.cs b/Timeline/Entities/UserUtility.cs deleted file mode 100644 index 14cdb2d6..00000000 --- a/Timeline/Entities/UserUtility.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; -using System.Linq; -using Timeline.Models; -using Timeline.Services; - -namespace Timeline.Entities -{ - public static class UserUtility - { - 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 }; - - 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 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, RoleStringToIsAdmin(user.RoleString)); - } - - internal static UserCache CreateUserCache(User user) - { - if (user == null) - throw new ArgumentNullException(nameof(user)); - return new UserCache { Username = user.Name, Administrator = RoleStringToIsAdmin(user.RoleString), Version = user.Version }; - } - } -} diff --git a/Timeline/Helpers/InvalidModelResponseFactory.cs b/Timeline/Helpers/InvalidModelResponseFactory.cs new file mode 100644 index 00000000..e5c87d9e --- /dev/null +++ b/Timeline/Helpers/InvalidModelResponseFactory.cs @@ -0,0 +1,25 @@ +using Microsoft.AspNetCore.Mvc; +using System.Text; +using Timeline.Models.Http; + +namespace Timeline.Helpers +{ + public static class InvalidModelResponseFactory + { + public static IActionResult Factory(ActionContext context) + { + var modelState = context.ModelState; + + var messageBuilder = new StringBuilder(); + foreach (var model in modelState) + foreach (var error in model.Value.Errors) + { + messageBuilder.Append(model.Key); + messageBuilder.Append(" : "); + messageBuilder.AppendLine(error.ErrorMessage); + } + + return new BadRequestObjectResult(CommonResponse.InvalidModel(messageBuilder.ToString())); + } + } +} diff --git a/Timeline/Migrations/20190412102517_InitCreate.Designer.cs b/Timeline/Migrations/20190412102517_InitCreate.Designer.cs index c68183de..1e4a4115 100644 --- a/Timeline/Migrations/20190412102517_InitCreate.Designer.cs +++ b/Timeline/Migrations/20190412102517_InitCreate.Designer.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Timeline.Models; +using Timeline.Entities; namespace Timeline.Migrations { diff --git a/Timeline/Migrations/20190412144150_AddAdminUser.Designer.cs b/Timeline/Migrations/20190412144150_AddAdminUser.Designer.cs index 319c646a..12a6fb77 100644 --- a/Timeline/Migrations/20190412144150_AddAdminUser.Designer.cs +++ b/Timeline/Migrations/20190412144150_AddAdminUser.Designer.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Timeline.Models; +using Timeline.Entities; namespace Timeline.Migrations { diff --git a/Timeline/Migrations/20190412153003_MakeColumnsInUserNotNull.Designer.cs b/Timeline/Migrations/20190412153003_MakeColumnsInUserNotNull.Designer.cs index c1d1565f..a2644feb 100644 --- a/Timeline/Migrations/20190412153003_MakeColumnsInUserNotNull.Designer.cs +++ b/Timeline/Migrations/20190412153003_MakeColumnsInUserNotNull.Designer.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Timeline.Models; +using Timeline.Entities; namespace Timeline.Migrations { diff --git a/Timeline/Migrations/20190719115321_Add-User-Version.Designer.cs b/Timeline/Migrations/20190719115321_Add-User-Version.Designer.cs index 42eeeb40..7402b082 100644 --- a/Timeline/Migrations/20190719115321_Add-User-Version.Designer.cs +++ b/Timeline/Migrations/20190719115321_Add-User-Version.Designer.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Timeline.Models; +using Timeline.Entities; namespace Timeline.Migrations { diff --git a/Timeline/Migrations/DatabaseContextModelSnapshot.cs b/Timeline/Migrations/DatabaseContextModelSnapshot.cs index 7d244969..be8b3e9f 100644 --- a/Timeline/Migrations/DatabaseContextModelSnapshot.cs +++ b/Timeline/Migrations/DatabaseContextModelSnapshot.cs @@ -2,7 +2,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Timeline.Models; +using Timeline.Entities; namespace Timeline.Migrations { diff --git a/Timeline/Models/DatabaseContext.cs b/Timeline/Models/DatabaseContext.cs deleted file mode 100644 index afd5a333..00000000 --- a/Timeline/Models/DatabaseContext.cs +++ /dev/null @@ -1,42 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; - -namespace Timeline.Models -{ - public static class UserRoles - { - public const string Admin = "admin"; - public const string User = "user"; - } - - [Table("user")] - public class User - { - [Column("id"), Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] - public long Id { get; set; } - - [Column("name"), Required] - public string Name { get; set; } - - [Column("password"), Required] - public string EncryptedPassword { get; set; } - - [Column("roles"), Required] - public string RoleString { get; set; } - - [Column("version"), Required] - public long Version { get; set; } - } - - public class DatabaseContext : DbContext - { - public DatabaseContext(DbContextOptions options) - : base(options) - { - - } - - public DbSet Users { get; set; } - } -} diff --git a/Timeline/Models/Http/Common.cs b/Timeline/Models/Http/Common.cs new file mode 100644 index 00000000..74959088 --- /dev/null +++ b/Timeline/Models/Http/Common.cs @@ -0,0 +1,51 @@ +namespace Timeline.Models.Http +{ + public class CommonResponse + { + public static class ErrorCodes + { + /// + /// Used when the model is invaid. + /// For example a required field is null. + /// + public const int InvalidModel = -100; + } + + public static CommonResponse InvalidModel(string message) + { + return new CommonResponse(ErrorCodes.InvalidModel, message); + } + + public CommonResponse() + { + + } + + public CommonResponse(int code, string message) + { + Code = 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 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/Models/Http/Token.cs b/Timeline/Models/Http/Token.cs new file mode 100644 index 00000000..cef679ef --- /dev/null +++ b/Timeline/Models/Http/Token.cs @@ -0,0 +1,32 @@ +using System.ComponentModel.DataAnnotations; + +namespace Timeline.Models.Http +{ + public class CreateTokenRequest + { + [Required] + public string Username { get; set; } + [Required] + public string Password { get; set; } + // in days, optional + [Range(1, 365)] + public int? ExpireOffset { get; set; } + } + + public class CreateTokenResponse + { + public string Token { get; set; } + public UserInfo User { get; set; } + } + + public class VerifyTokenRequest + { + [Required] + public string Token { get; set; } + } + + public class VerifyTokenResponse + { + public UserInfo User { get; set; } + } +} diff --git a/Timeline/Models/Http/User.cs b/Timeline/Models/Http/User.cs new file mode 100644 index 00000000..1de7fae2 --- /dev/null +++ b/Timeline/Models/Http/User.cs @@ -0,0 +1,26 @@ +using System.ComponentModel.DataAnnotations; + +namespace Timeline.Models.Http +{ + public class UserPutRequest + { + [Required] + public string Password { get; set; } + [Required] + public bool Administrator { get; set; } + } + + public class UserPatchRequest + { + public string Password { get; set; } + public bool? Administrator { get; set; } + } + + public class ChangePasswordRequest + { + [Required] + public string OldPassword { get; set; } + [Required] + public string NewPassword { get; set; } + } +} diff --git a/Timeline/Models/PutResult.cs b/Timeline/Models/PutResult.cs new file mode 100644 index 00000000..f11ac138 --- /dev/null +++ b/Timeline/Models/PutResult.cs @@ -0,0 +1,17 @@ +namespace Timeline.Models +{ + /// + /// Represents the result of a "put" operation. + /// + public enum PutResult + { + /// + /// Indicates the item did not exist and now is created. + /// + Created, + /// + /// Indicates the item exists already and is modified. + /// + Modified + } +} diff --git a/Timeline/Models/UserInfo.cs b/Timeline/Models/UserInfo.cs new file mode 100644 index 00000000..b5cb1e7f --- /dev/null +++ b/Timeline/Models/UserInfo.cs @@ -0,0 +1,23 @@ +namespace Timeline.Models +{ + public sealed class UserInfo + { + public UserInfo() + { + } + + public UserInfo(string username, bool administrator) + { + Username = username; + Administrator = administrator; + } + + public string Username { get; set; } + public bool Administrator { get; set; } + + public override string ToString() + { + return $"Username: {Username} ; Administrator: {Administrator}"; + } + } +} diff --git a/Timeline/Models/UserUtility.cs b/Timeline/Models/UserUtility.cs new file mode 100644 index 00000000..711e321a --- /dev/null +++ b/Timeline/Models/UserUtility.cs @@ -0,0 +1,60 @@ +using System; +using System.Linq; +using Timeline.Entities; +using Timeline.Services; + +namespace Timeline.Models +{ + public static class UserUtility + { + 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 }; + + 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 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, RoleStringToIsAdmin(user.RoleString)); + } + + internal static UserCache CreateUserCache(User user) + { + if (user == null) + throw new ArgumentNullException(nameof(user)); + return new UserCache { Username = user.Name, Administrator = RoleStringToIsAdmin(user.RoleString), Version = user.Version }; + } + } +} diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index 7fe7a2b6..28218612 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -6,8 +6,8 @@ using System.Linq; using System.Threading.Tasks; using Timeline.Entities; using Timeline.Models; -using static Timeline.Entities.UserUtility; using static Timeline.Helpers.MyLogHelper; +using static Timeline.Models.UserUtility; namespace Timeline.Services { diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 242e816d..e6a8f96f 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -8,7 +8,8 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Timeline.Authenticate; using Timeline.Configs; -using Timeline.Models; +using Timeline.Entities; +using Timeline.Helpers; using Timeline.Services; namespace Timeline @@ -29,7 +30,11 @@ namespace Timeline // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); + services.AddMvc() + .ConfigureApiBehaviorOptions(options =>{ + options.InvalidModelStateResponseFactory = InvalidModelResponseFactory.Factory; + }) + .SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddCors(options => { -- cgit v1.2.3