From eb132c2da278d1b241473fcffc99ea7972a707dc Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 16 Mar 2020 01:33:16 +0100 Subject: [PATCH] Allowing account editing --- ....cs => 20200316003155_Initial.Designer.cs} | 4 +- ...05_Intial.cs => 20200316003155_Initial.cs} | 2 +- Kyoo/Views/API/AccountAPI.cs | 50 ++++++++++++++++++- Kyoo/Views/WebClient | 2 +- Kyoo/appsettings.json | 1 + 5 files changed, 53 insertions(+), 6 deletions(-) rename Kyoo/Models/DatabaseMigrations/Internal/{20200307160105_Intial.Designer.cs => 20200316003155_Initial.Designer.cs} (99%) rename Kyoo/Models/DatabaseMigrations/Internal/{20200307160105_Intial.cs => 20200316003155_Initial.cs} (99%) diff --git a/Kyoo/Models/DatabaseMigrations/Internal/20200307160105_Intial.Designer.cs b/Kyoo/Models/DatabaseMigrations/Internal/20200316003155_Initial.Designer.cs similarity index 99% rename from Kyoo/Models/DatabaseMigrations/Internal/20200307160105_Intial.Designer.cs rename to Kyoo/Models/DatabaseMigrations/Internal/20200316003155_Initial.Designer.cs index 2edbf076..55d95689 100644 --- a/Kyoo/Models/DatabaseMigrations/Internal/20200307160105_Intial.Designer.cs +++ b/Kyoo/Models/DatabaseMigrations/Internal/20200316003155_Initial.Designer.cs @@ -9,8 +9,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Kyoo.Models.DatabaseMigrations.Internal { [DbContext(typeof(DatabaseContext))] - [Migration("20200307160105_Intial")] - partial class Intial + [Migration("20200316003155_Initial")] + partial class Initial { protected override void BuildTargetModel(ModelBuilder modelBuilder) { diff --git a/Kyoo/Models/DatabaseMigrations/Internal/20200307160105_Intial.cs b/Kyoo/Models/DatabaseMigrations/Internal/20200316003155_Initial.cs similarity index 99% rename from Kyoo/Models/DatabaseMigrations/Internal/20200307160105_Intial.cs rename to Kyoo/Models/DatabaseMigrations/Internal/20200316003155_Initial.cs index e662a5dd..40b2e894 100644 --- a/Kyoo/Models/DatabaseMigrations/Internal/20200307160105_Intial.cs +++ b/Kyoo/Models/DatabaseMigrations/Internal/20200316003155_Initial.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace Kyoo.Models.DatabaseMigrations.Internal { - public partial class Intial : Migration + public partial class Initial : Migration { protected override void Up(MigrationBuilder migrationBuilder) { diff --git a/Kyoo/Views/API/AccountAPI.cs b/Kyoo/Views/API/AccountAPI.cs index a6b907f4..3d9caf26 100644 --- a/Kyoo/Views/API/AccountAPI.cs +++ b/Kyoo/Views/API/AccountAPI.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; +using System.IO; using System.Security.Claims; using System.Threading.Tasks; -using IdentityServer4.Extensions; using IdentityServer4.Models; using IdentityServer4.Services; using Kyoo.Models; @@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; using SignInResult = Microsoft.AspNetCore.Identity.SignInResult; namespace Kyoo.Api @@ -28,17 +29,30 @@ namespace Kyoo.Api public bool StayLoggedIn; } + public class AccountData + { + [FromQuery(Name = "email")] + public string Email { get; set; } + [FromQuery(Name = "username")] + public string Username { get; set; } + [FromQuery(Name = "picture")] + public IFormFile Picture { get; set; } + } + [Route("api/[controller]")] [ApiController] public class AccountController : Controller, IProfileService { private readonly UserManager _userManager; private readonly SignInManager _signInManager; + private readonly string _picturePath; - public AccountController(UserManager userManager, SignInManager siginInManager) + public AccountController(UserManager userManager, SignInManager siginInManager, IConfiguration configuration) { _userManager = userManager; _signInManager = siginInManager; + _picturePath = configuration.GetValue("profilePicturePath"); + } [HttpPost("register")] @@ -83,6 +97,7 @@ namespace Kyoo.Api { new Claim("email", user.Email), new Claim("username", user.UserName), + new Claim("picture", $"api/account/picture/{user.UserName}") }; context.IssuedClaims.AddRange(claims); @@ -94,5 +109,36 @@ namespace Kyoo.Api User user = await _userManager.GetUserAsync(context.Subject); context.IsActive = user != null; } + + [HttpGet("picture/{username}")] + public async Task GetPicture(string username) + { + User user = await _userManager.FindByNameAsync(username); + if (user == null) + return BadRequest(); + return new PhysicalFileResult(Path.Combine(_picturePath, user.Id), "image/png"); + } + + [HttpPost("update")] + [Authorize] + public async Task Update([FromForm] AccountData data) + { + User user = await _userManager.GetUserAsync(HttpContext.User); + + if (!string.IsNullOrEmpty(data.Email)) + user.Email = data.Email; + if (!string.IsNullOrEmpty(data.Username)) + user.UserName = data.Username; + if (data.Picture?.Length > 0) + { + string path = Path.Combine(_picturePath, user.Id); + await using (FileStream file = System.IO.File.Create(path)) + { + await data.Picture.CopyToAsync(file); + } + } + await _userManager.UpdateAsync(user); + return Ok(); + } } } \ No newline at end of file diff --git a/Kyoo/Views/WebClient b/Kyoo/Views/WebClient index 6d892fd4..044132d4 160000 --- a/Kyoo/Views/WebClient +++ b/Kyoo/Views/WebClient @@ -1 +1 @@ -Subproject commit 6d892fd46e7aa56a7cb0bebd94d4404899382f2b +Subproject commit 044132d4bf052f61c99186258e9aaf6da571b4ca diff --git a/Kyoo/appsettings.json b/Kyoo/appsettings.json index feb1809f..c7aa5002 100644 --- a/Kyoo/appsettings.json +++ b/Kyoo/appsettings.json @@ -16,6 +16,7 @@ "transmuxTempPath": "/tmp/cached/kyoo/transmux", "transcodeTempPath": "/tmp/cached/kyoo/transcode", "peoplePath": "/tmp/people", + "profilePicturePath": "/tmp/users/", "plugins": "plugins/", "regex": "(\\/(?.*)\\/)?.*\\/(?.+?)(( S(?\\d+)E(?\\d+)| (?\\d+)))?\\.", }