diff options
| author | transtrike <transtrike@gmail.com> | 2020-12-12 18:14:08 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2020-12-12 18:14:08 +0200 |
| commit | 68836192e1138e2590cbde0c8110507b59ebeed1 (patch) | |
| tree | 1385c625d5c2b4991044817f7d906433f31f1fd5 | |
| parent | 64e02c270d2911e85ea82b601e2aa251400fbcd0 (diff) | |
| download | DevHive-68836192e1138e2590cbde0c8110507b59ebeed1.tar DevHive-68836192e1138e2590cbde0c8110507b59ebeed1.tar.gz DevHive-68836192e1138e2590cbde0c8110507b59ebeed1.zip | |
New ValidationString implementation; Simplified PictureProfile
| -rw-r--r-- | API/Controllers/UserController.cs | 1 | ||||
| -rw-r--r-- | API/Extensions/ConfigureJWT.cs | 12 | ||||
| -rw-r--r-- | API/Service/UserService.cs | 9 | ||||
| -rw-r--r-- | Data/Models/Classes/User.cs | 57 |
4 files changed, 33 insertions, 46 deletions
diff --git a/API/Controllers/UserController.cs b/API/Controllers/UserController.cs index 6cb1f54..8503922 100644 --- a/API/Controllers/UserController.cs +++ b/API/Controllers/UserController.cs @@ -6,7 +6,6 @@ using Microsoft.AspNetCore.Mvc; using Data.Models.DTOs; using Microsoft.AspNetCore.Authorization; using Data.Models.Options; -using Microsoft.Extensions.Configuration; namespace API.Controllers { diff --git a/API/Extensions/ConfigureJWT.cs b/API/Extensions/ConfigureJWT.cs index 1de2aa5..22ada98 100644 --- a/API/Extensions/ConfigureJWT.cs +++ b/API/Extensions/ConfigureJWT.cs @@ -12,11 +12,17 @@ namespace API.Extensions { public static void JWTConfiguration(this IServiceCollection services, IConfiguration configuration) { - services.AddSingleton<JWTOptions>( - new JWTOptions(configuration.GetSection("AppSettings").GetSection("Secret").Value)); + services.AddSingleton(new JWTOptions(configuration + .GetSection("AppSettings") + .GetSection("Secret") + .Value)); // Get key from appsettings.json - var key = Encoding.ASCII.GetBytes(configuration.GetSection("AppSettings").GetSection("Secret").Value); + var key = Encoding.ASCII.GetBytes(configuration + .GetSection("AppSettings") + .GetSection("Secret") + .Value); + // Setup Jwt Authentication services.AddAuthentication(x => { diff --git a/API/Service/UserService.cs b/API/Service/UserService.cs index 797a924..c57b4b7 100644 --- a/API/Service/UserService.cs +++ b/API/Service/UserService.cs @@ -34,6 +34,8 @@ namespace API.Service if (user == null) return new NotFoundObjectResult("User does not exist!"); + //TODO: Clean it + // Get key from appsettings.json var key = Encoding.ASCII.GetBytes(_jwtOptions.Secret); @@ -57,9 +59,9 @@ namespace API.Service var tokenString = tokenHandler.WriteToken(token); return new OkObjectResult(new - { - Token = tokenString - }); + { + Token = tokenString + }); } public async Task<IActionResult> RegisterUser(RegisterDTO registerDTO) @@ -80,6 +82,7 @@ namespace API.Service private string GeneratePasswordHash(string password) { + //TODO: Hash password return password; // TEMPORARY! } diff --git a/Data/Models/Classes/User.cs b/Data/Models/Classes/User.cs index 50ceb1e..a3baeff 100644 --- a/Data/Models/Classes/User.cs +++ b/Data/Models/Classes/User.cs @@ -3,6 +3,8 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using Microsoft.AspNetCore.Identity; using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; namespace Data.Models.Classes { @@ -11,55 +13,46 @@ namespace Data.Models.Classes { private string _firstName; private string _lastName; - private string _profilePicture; [Required] [Range(3, 50)] [Display(Name = "Username")] - public override string UserName + public override string UserName { get => base.UserName; - set + set { ValidateString("Username", 3, 50, value, true); base.UserName = value; } } - + [Required] [Range(3, 30)] - public string FirstName + public string FirstName { get => this._firstName; - set + set { ValidateString("FirstName", 3, 30, value, false); this._firstName = value; } } - + [Required] [Range(3, 30)] - public string LastName - { + public string LastName + { get => this._lastName; - set + set { ValidateString("LastName", 3, 30, value, false); this._lastName = value; } } - public string ProfilePicture - { - get => this._profilePicture; - set - { - ValidateURL(value); - this._profilePicture = value; - } - } - + public string ProfilePicture { get; set; } + public string Role { get; set; } // public List<User> Friends { get; set; } @@ -68,28 +61,14 @@ namespace Data.Models.Classes /// Throws an argument exception if the given value is not composed only of letters, and if specified, also of digits. /// Does nothing otherwise. /// </summary> - private static void ValidateString(string name, int minLength, int maxLength, string value, bool canBeDigit) + private static void ValidateString(string propertyName, int minLength, int maxLength, string value, bool canBeDigit) { if (value.Length < minLength || value.Length > maxLength) - throw new ArgumentException($"{name} length cannot be less than {minLength} and more than {maxLength}."); + throw new ArgumentException($"{propertyName} length cannot be less than {minLength} and more than {maxLength}."); - foreach (char character in value) // more efficient than Linq - { - if (!Char.IsLetter(character)) - if (!(Char.IsDigit(character) && canBeDigit)) - throw new ArgumentException($"{name} contains invalid characters."); - } - } - - /// <summary> - /// Throws an exception if the absolute url isn't valid. - /// Does nothing otherwise. - /// </summary> - private static void ValidateURL(string urlValue) - { - // Throws an error is URL is invalid - Uri validatedUri; - Uri.TryCreate(urlValue, UriKind.Absolute, out validatedUri); + foreach (char ch in value) + if (!Char.IsLetter(ch) && !(Char.IsDigit(ch) && canBeDigit)) + throw new ArgumentException($"{propertyName} contains invalid characters."); } } } |
