more info from Me
This commit is contained in:
@ -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<ApplicationUser> _userManager;
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
|
||||
ApplicationDbContext _dbContext;
|
||||
private ILogger _logger;
|
||||
|
||||
public ApiAccountController(UserManager<ApplicationUser> userManager,
|
||||
SignInManager<ApplicationUser> signInManager, ILoggerFactory loggerFactory)
|
||||
SignInManager<ApplicationUser> signInManager, ILoggerFactory loggerFactory, ApplicationDbContext dbContext)
|
||||
{
|
||||
UserManager = userManager;
|
||||
_signInManager = signInManager;
|
||||
_logger = loggerFactory.CreateLogger("ApiAuth");
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public UserManager<ApplicationUser> 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);
|
||||
}
|
||||
|
||||
|
@ -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(),
|
||||
|
@ -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<string> emails,
|
||||
IEnumerable<string> 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; }
|
||||
/// <summary>
|
||||
/// Known as profile, could point to an avatar
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user