roadie/RoadieApi/Controllers/EntityControllerBase.cs

48 lines
1.7 KiB
C#
Raw Normal View History

2018-11-06 15:55:31 -06:00
using Mapster;
using Microsoft.AspNet.OData;
2018-11-11 14:45:44 -06:00
using Microsoft.AspNetCore.Identity;
2018-11-02 16:04:49 -05:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Roadie.Library.Caching;
2018-11-05 20:41:51 -06:00
using Roadie.Library.Configuration;
2018-11-11 14:45:44 -06:00
using Roadie.Library.Identity;
using System.Threading.Tasks;
using models = Roadie.Library.Models.Users;
2018-11-02 16:04:49 -05:00
namespace Roadie.Api.Controllers
{
public abstract class EntityControllerBase : ODataController
2018-11-02 16:04:49 -05:00
{
protected ILogger _logger;
private models.User _currentUser = null;
2018-11-11 14:45:44 -06:00
protected ICacheManager CacheManager { get; }
protected IConfiguration Configuration { get; }
protected IRoadieSettings RoadieSettings { get; }
protected UserManager<ApplicationUser> UserManager { get; }
2018-11-04 18:08:37 -06:00
2018-11-11 14:45:44 -06:00
public EntityControllerBase(ICacheManager cacheManager, IConfiguration configuration, UserManager<ApplicationUser> userManager)
2018-11-02 16:04:49 -05:00
{
2018-11-11 14:45:44 -06:00
this.CacheManager = cacheManager;
this.Configuration = configuration;
this.RoadieSettings = new RoadieSettings();
this.Configuration.GetSection("RoadieSettings").Bind(this.RoadieSettings);
this.UserManager = userManager;
}
2018-11-06 15:55:31 -06:00
2018-11-11 14:45:44 -06:00
protected async Task<models.User> CurrentUserModel()
{
if (this._currentUser == null)
2018-11-11 14:45:44 -06:00
{
if (this.User.Identity.IsAuthenticated)
2018-11-11 14:45:44 -06:00
{
var user = await this.UserManager.GetUserAsync(User);
this._currentUser = user.Adapt<models.User>();
this._currentUser.IsAdmin = User.IsInRole("Admin");
this._currentUser.IsEditor = User.IsInRole("Editor");
}
}
return this._currentUser;
2018-11-02 16:04:49 -05:00
}
}
}