aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Models
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-11-13 16:20:45 +0800
committerGitHub <noreply@github.com>2020-11-13 16:20:45 +0800
commit381cb7c64123c71899f549baa16bb610cc8b037f (patch)
treedfb4dd690704e887e3609265e6a652fe7ccea218 /BackEnd/Timeline/Models
parent5b78017e93450342c85a0e7f5ed16bbb6ae8422e (diff)
parentb635b4453756d9a33c173c9b9f2ae0ab7c830d3b (diff)
downloadtimeline-381cb7c64123c71899f549baa16bb610cc8b037f.tar.gz
timeline-381cb7c64123c71899f549baa16bb610cc8b037f.tar.bz2
timeline-381cb7c64123c71899f549baa16bb610cc8b037f.zip
Merge pull request #183 from crupest/auth
Refactor auth module to enable more flexiable permission control.
Diffstat (limited to 'BackEnd/Timeline/Models')
-rw-r--r--BackEnd/Timeline/Models/Converters/JsonDateTimeConverter.cs2
-rw-r--r--BackEnd/Timeline/Models/Http/Common.cs4
-rw-r--r--BackEnd/Timeline/Models/Http/UserController.cs21
-rw-r--r--BackEnd/Timeline/Models/Http/UserInfo.cs21
-rw-r--r--BackEnd/Timeline/Models/User.cs28
-rw-r--r--BackEnd/Timeline/Models/Validation/Validator.cs4
6 files changed, 42 insertions, 38 deletions
diff --git a/BackEnd/Timeline/Models/Converters/JsonDateTimeConverter.cs b/BackEnd/Timeline/Models/Converters/JsonDateTimeConverter.cs
index 94b5cab0..72a2908c 100644
--- a/BackEnd/Timeline/Models/Converters/JsonDateTimeConverter.cs
+++ b/BackEnd/Timeline/Models/Converters/JsonDateTimeConverter.cs
@@ -12,7 +12,7 @@ namespace Timeline.Models.Converters
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Debug.Assert(typeToConvert == typeof(DateTime));
- return DateTime.Parse(reader.GetString(), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
+ return DateTime.Parse(reader.GetString()!, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
diff --git a/BackEnd/Timeline/Models/Http/Common.cs b/BackEnd/Timeline/Models/Http/Common.cs
index 5fa22c9e..2101a1bb 100644
--- a/BackEnd/Timeline/Models/Http/Common.cs
+++ b/BackEnd/Timeline/Models/Http/Common.cs
@@ -94,13 +94,13 @@ namespace Timeline.Models.Http
public bool Delete { get; set; }
}
- /// <summary></summary>
+ /// <summary></summary>
public CommonDeleteResponse()
{
}
- /// <summary></summary>
+ /// <summary></summary>
public CommonDeleteResponse(int code, string message, bool delete)
: base(code, message, new ResponseData(delete))
{
diff --git a/BackEnd/Timeline/Models/Http/UserController.cs b/BackEnd/Timeline/Models/Http/UserController.cs
index 6bc5a66e..92a63874 100644
--- a/BackEnd/Timeline/Models/Http/UserController.cs
+++ b/BackEnd/Timeline/Models/Http/UserController.cs
@@ -2,6 +2,7 @@ using AutoMapper;
using System.ComponentModel.DataAnnotations;
using Timeline.Controllers;
using Timeline.Models.Validation;
+using Timeline.Services;
namespace Timeline.Models.Http
{
@@ -27,11 +28,6 @@ namespace Timeline.Models.Http
/// </summary>
[Nickname]
public string? Nickname { get; set; }
-
- /// <summary>
- /// Whether to be administrator. Null if not change. Need to be administrator.
- /// </summary>
- public bool? Administrator { get; set; }
}
/// <summary>
@@ -50,18 +46,6 @@ namespace Timeline.Models.Http
/// </summary>
[Required, MinLength(1)]
public string Password { get; set; } = default!;
-
- /// <summary>
- /// Whether the new user is administrator.
- /// </summary>
- [Required]
- public bool? Administrator { get; set; }
-
- /// <summary>
- /// Nickname of the new user.
- /// </summary>
- [Nickname]
- public string? Nickname { get; set; }
}
/// <summary>
@@ -86,8 +70,7 @@ namespace Timeline.Models.Http
{
public UserControllerAutoMapperProfile()
{
- CreateMap<UserPatchRequest, User>(MemberList.Source);
- CreateMap<CreateUserRequest, User>(MemberList.Source);
+ CreateMap<UserPatchRequest, ModifyUserParams>();
}
}
}
diff --git a/BackEnd/Timeline/Models/Http/UserInfo.cs b/BackEnd/Timeline/Models/Http/UserInfo.cs
index d92a12c4..26b04e90 100644
--- a/BackEnd/Timeline/Models/Http/UserInfo.cs
+++ b/BackEnd/Timeline/Models/Http/UserInfo.cs
@@ -2,7 +2,9 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
+using System.Collections.Generic;
using Timeline.Controllers;
+using Timeline.Services;
namespace Timeline.Models.Http
{
@@ -27,6 +29,12 @@ namespace Timeline.Models.Http
/// True if the user is a administrator.
/// </summary>
public bool? Administrator { get; set; } = default!;
+#pragma warning disable CA2227 // Collection properties should be read only
+ /// <summary>
+ /// The permissions of the user.
+ /// </summary>
+ public List<string> Permissions { get; set; } = default!;
+#pragma warning restore CA2227 // Collection properties should be read only
#pragma warning disable CA1707 // Identifiers should not contain underscores
/// <summary>
/// Related links.
@@ -54,6 +62,14 @@ namespace Timeline.Models.Http
public string Timeline { get; set; } = default!;
}
+ public class UserPermissionsValueConverter : ITypeConverter<UserPermissions, List<string>>
+ {
+ public List<string> Convert(UserPermissions source, List<string> destination, ResolutionContext context)
+ {
+ return source.ToStringList();
+ }
+ }
+
public class UserInfoLinksValueResolver : IValueResolver<User, UserInfo, UserInfoLinks>
{
private readonly IActionContextAccessor _actionContextAccessor;
@@ -84,7 +100,10 @@ namespace Timeline.Models.Http
{
public UserInfoAutoMapperProfile()
{
- CreateMap<User, UserInfo>().ForMember(u => u._links, opt => opt.MapFrom<UserInfoLinksValueResolver>());
+ CreateMap<UserPermissions, List<string>>()
+ .ConvertUsing<UserPermissionsValueConverter>();
+ CreateMap<User, UserInfo>()
+ .ForMember(u => u._links, opt => opt.MapFrom<UserInfoLinksValueResolver>());
}
}
}
diff --git a/BackEnd/Timeline/Models/User.cs b/BackEnd/Timeline/Models/User.cs
index f08a62db..1e90cd1d 100644
--- a/BackEnd/Timeline/Models/User.cs
+++ b/BackEnd/Timeline/Models/User.cs
@@ -1,21 +1,23 @@
using System;
+using Timeline.Services;
namespace Timeline.Models
{
- public class User
+ public record User
{
- public string? UniqueId { get; set; }
- public string? Username { get; set; }
- public string? Nickname { get; set; }
- public bool? Administrator { get; set; }
+ public long Id { get; set; }
+ public string UniqueId { get; set; } = default!;
- #region secret
- public long? Id { get; set; }
- public string? Password { get; set; }
- public long? Version { get; set; }
- public DateTime? UsernameChangeTime { get; set; }
- public DateTime? CreateTime { get; set; }
- public DateTime? LastModified { get; set; }
- #endregion secret
+ public string Username { get; set; } = default!;
+ public string Nickname { get; set; } = default!;
+
+ [Obsolete("Use permissions instead.")]
+ public bool Administrator { get; set; }
+ public UserPermissions Permissions { get; set; } = default!;
+
+ public DateTime UsernameChangeTime { get; set; }
+ public DateTime CreateTime { get; set; }
+ public DateTime LastModified { get; set; }
+ public long Version { get; set; }
}
}
diff --git a/BackEnd/Timeline/Models/Validation/Validator.cs b/BackEnd/Timeline/Models/Validation/Validator.cs
index aef7891c..b7e754d3 100644
--- a/BackEnd/Timeline/Models/Validation/Validator.cs
+++ b/BackEnd/Timeline/Models/Validation/Validator.cs
@@ -111,12 +111,12 @@ namespace Timeline.Models.Validation
}
}
- protected override ValidationResult IsValid(object value, ValidationContext validationContext)
+ protected override ValidationResult IsValid(object? value, ValidationContext validationContext)
{
var (result, message) = _validator.Validate(value);
if (result)
{
- return ValidationResult.Success;
+ return ValidationResult.Success!;
}
else
{