From 7279ac292a8379a70ad0491893091e3cf2febe4a Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 15 Jan 2018 14:09:19 +0100 Subject: [PATCH] more info from Me --- Yavsc/ApiControllers/AccountController.cs | 25 ++++++++++---- Yavsc/Models/Relationship/Location.cs | 2 +- Yavsc/ViewModels/Account/Me.cs | 40 ++++++++++++++--------- 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/Yavsc/ApiControllers/AccountController.cs b/Yavsc/ApiControllers/AccountController.cs index ae792609..b18806e8 100644 --- a/Yavsc/ApiControllers/AccountController.cs +++ b/Yavsc/ApiControllers/AccountController.cs @@ -13,6 +13,8 @@ namespace Yavsc.WebApi.Controllers using Models.Auth; using Yavsc.Helpers; using System.Linq; + using Microsoft.Data.Entity; + using Microsoft.AspNet.Identity.EntityFramework; [Authorize(),Route("~/api/account")] public class ApiAccountController : Controller @@ -21,14 +23,16 @@ namespace Yavsc.WebApi.Controllers private UserManager _userManager; private readonly SignInManager _signInManager; + ApplicationDbContext _dbContext; private ILogger _logger; public ApiAccountController(UserManager userManager, - SignInManager signInManager, ILoggerFactory loggerFactory) + SignInManager signInManager, ILoggerFactory loggerFactory, ApplicationDbContext dbContext) { UserManager = userManager; _signInManager = signInManager; _logger = loggerFactory.CreateLogger("ApiAuth"); + _dbContext = dbContext; } public UserManager UserManager @@ -131,13 +135,20 @@ namespace Yavsc.WebApi.Controllers new { error = "user not found" }); var uid = User.GetUserId(); - var iduser = await UserManager.FindByIdAsync(uid); + var userData = await _dbContext.Users + .Include(u=>u.PostalAddress) + .Include(u=>u.AccountBalance) + .Include(u=>u.Roles) + .FirstAsync(u=>u.Id == uid); + + var user = new Me(userData); + + var userRoles = _dbContext.UserRoles.Where(u=>u.UserId == uid).ToArray(); + + IdentityRole [] roles = _dbContext.Roles.Where(r=>userRoles.Any(ur=>ur.RoleId==r.Id)).ToArray(); + + user.Roles = roles.Select(r=>r.Name).ToArray(); - var user = new Me(iduser.Id,iduser.UserName, - new string [] { iduser.Email }, - await UserManager.GetRolesAsync(iduser), - iduser.Avatar, iduser.PostalAddress?.Address - ); return Ok(user); } diff --git a/Yavsc/Models/Relationship/Location.cs b/Yavsc/Models/Relationship/Location.cs index 19005cb2..6afa336e 100644 --- a/Yavsc/Models/Relationship/Location.cs +++ b/Yavsc/Models/Relationship/Location.cs @@ -27,7 +27,7 @@ namespace Yavsc.Models.Relationship } - public class Location : Position { + public class Location : Position, ILocation { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long Id { get; set; } [Required(), diff --git a/Yavsc/ViewModels/Account/Me.cs b/Yavsc/ViewModels/Account/Me.cs index d314d7b5..20e23481 100644 --- a/Yavsc/ViewModels/Account/Me.cs +++ b/Yavsc/ViewModels/Account/Me.cs @@ -1,33 +1,43 @@ +using System; using System.Collections.Generic; using System.Linq; namespace Yavsc.Models.Auth { - public class Me { - public Me(string useruserid, - string username, - IEnumerable emails, - IEnumerable roles, - string avatar, - string address) + public class Me : IApplicationUser { + public Me(ApplicationUser user) { - Id = useruserid; - UserName = username; - EMails = emails.ToArray(); - Roles = roles.ToArray(); - Avatar = avatar; - Address = address; + Id = user.Id; + UserName = user.UserName; + EMail = user.Email; + Avatar = user.Avatar; + PostalAddress = user.PostalAddress; + DedicatedGoogleCalendar = user.DedicatedGoogleCalendar; } public string Id { get; set; } public string UserName { get; set; } - public string[] EMails { get; set; } + public string EMail { get; set; } public string[] Roles { get; set; } /// /// Known as profile, could point to an avatar /// /// public string Avatar { get; set; } - public string Address { get; set; } + + public IAccountBalance AccountBalance + { + get; set; + } + + public string DedicatedGoogleCalendar + { + get; set; + } + + public ILocation PostalAddress + { + get; set; + } } } \ No newline at end of file