This commit is contained in:
2016-08-03 23:28:34 +02:00
parent d39e6b4325
commit 01f36ddef6
2 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,144 @@
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Authorization;
using Yavsc.Models;
using Yavsc.Models.Account;
using Microsoft.AspNet.Mvc;
using Yavsc.ViewModels.Account;
using System.Security.Claims;
using Microsoft.Extensions.Logging;
using Yavsc.Models.Auth;
namespace Yavsc.WebApi.Controllers
{
[Authorize,Route("~/api/account")]
public class ApiAccountController : Controller
{
private UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private ILogger _logger;
public ApiAccountController(UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager, ILoggerFactory loggerFactory)
{
UserManager = userManager;
_signInManager = signInManager;
_logger = loggerFactory.CreateLogger("ApiAuth");
}
public UserManager<ApplicationUser> UserManager
{
get
{
return _userManager;
}
private set
{
_userManager = value;
}
}
// POST api/Account/ChangePassword
public async Task<IActionResult> ChangePassword(ChangePasswordBindingModel model)
{
if (!ModelState.IsValid)
{
return new BadRequestObjectResult(ModelState);
}
var user = await _userManager.FindByIdAsync(User.GetUserId());
if (user == null || !(await _userManager.IsEmailConfirmedAsync(user))) {
IdentityResult result = await UserManager.ChangePasswordAsync(user, model.OldPassword,
model.NewPassword);
if (!result.Succeeded)
{
AddErrors(result);
return new BadRequestObjectResult(ModelState);
}
}
return Ok();
}
// POST api/Account/SetPassword
public async Task<IActionResult> SetPassword(SetPasswordBindingModel model)
{
if (!ModelState.IsValid)
{
return new BadRequestObjectResult(ModelState);
}
var user = await _userManager.FindByIdAsync(User.GetUserId());
if (user == null || !(await _userManager.IsEmailConfirmedAsync(user))) {
IdentityResult result = await UserManager.AddPasswordAsync(user, model.NewPassword);
if (!result.Succeeded)
{
AddErrors (result);
return new BadRequestObjectResult(ModelState);
}
}
return Ok();
}
// POST api/Account/Register
[AllowAnonymous]
public async Task<IActionResult> Register(RegisterViewModel model)
{
if (!ModelState.IsValid)
{
return new BadRequestObjectResult(ModelState);
}
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if (!result.Succeeded)
{
AddErrors (result);
return new BadRequestObjectResult(ModelState);
}
await _signInManager.SignInAsync(user, isPersistent: false);
return Ok();
}
private void AddErrors(IdentityResult result)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
UserManager.Dispose();
}
base.Dispose(disposing);
}
[HttpGet("~/api/me")]
public async Task<IActionResult> Me ()
{
if (User==null)
return new BadRequestObjectResult(
new { error = "user not found" });
var uid = User.GetUserId();
if (uid == null)
return new BadRequestObjectResult(
new { error = "user not identified" });
var iduser = await UserManager.FindByIdAsync(uid);
var user = new Me(iduser.Id,iduser.UserName,
new string [] { iduser.Email },
await UserManager.GetRolesAsync(iduser),
null // TODO better (an avatar, or Web site url)
);
return Ok(user);
}
}
}

View File

@ -0,0 +1,64 @@
using System;
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Mvc;
using Microsoft.Extensions.Logging;
using Yavsc.Models;
using Yavsc.Models.Identity;
[Authorize, Route("~/api/gcm")]
public class GCMController : Controller
{
ILogger _logger;
ApplicationDbContext _context;
public GCMController(ApplicationDbContext context,
ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<GCMController>();
_context = context;
}
/// <summary>
/// This is not a method supporting user creation.
/// It only registers Google Clood Messaging id.
/// </summary>
/// <param name="declaration"></param>
/// <returns></returns>
[Authorize, HttpPost("register")]
public IActionResult Register(
[FromBody] GoogleCloudMobileDeclaration declaration)
{
var uid = User.GetUserId();
_logger.LogWarning($"Registering device with id:{declaration.DeviceId} for {uid}");
if (ModelState.IsValid)
{
var alreadyRegisteredDevice = _context.GCMDevices.FirstOrDefault(d => d.DeviceId == declaration.DeviceId);
var deviceAlreadyRegistered = (alreadyRegisteredDevice!=null);
if (deviceAlreadyRegistered)
{
// Override an exiting owner
alreadyRegisteredDevice.DeclarationDate = DateTime.Now;
alreadyRegisteredDevice.DeviceOwnerId = uid;
alreadyRegisteredDevice.GCMRegistrationId = declaration.GCMRegistrationId;
alreadyRegisteredDevice.Model = declaration.Model;
alreadyRegisteredDevice.Platform = declaration.Platform;
alreadyRegisteredDevice.Version = declaration.Version;
_context.Update(alreadyRegisteredDevice);
_context.SaveChanges();
}
else
{
declaration.DeclarationDate = DateTime.Now;
declaration.DeviceOwnerId = uid;
_context.GCMDevices.Add(declaration);
_context.SaveChanges();
}
return Json(new { IsAnUpdate = deviceAlreadyRegistered });
}
return new BadRequestObjectResult(ModelState);
}
}