using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using xCommons.Extensions; using xCommons.Providers; using xExceptions.Constants; using xIdentityModels; using xIdentityModels.Configurations; using xIdentityModels.Constants; using xIdentityModels.Models; using xMessageService.Interfaces; using xStorageService.Interfaces; using xIds.Configurations; using xIds.DbContext; using xIds.Interfaces; namespace xIds.Providers { public partial class XIdentityManager : IXIdentityManager { // private JsonSerializerSettings jsonSerializerSettings; // public XIdentityDbContext DbContext { get; } public UserManager UserManager { get; } public IXIdentityHelper IdentityHelper { get; } public ILogger Logger { get; } public IXMessageProvider MessageProvider { get; } public IXStorageProvider StorageProvider { get; } public SignInManager SignInManager { get; } public IXSecurityProvider SecurityProvider { get; } public XIdentityConfiguration Configuration { get; } public RoleManager RoleManager { get; } public XValidationProvider ValidationProvider { get; } public XDataServiceConfiguration DataConfiguration { get; } public IXIdentityMessageProvider IdentityMessageProvider { get; } public XIdentityResourceConfiguration IdentityResourceConfiguration { get; } public XIdentityManager( XIdentityDbContext dbContext, UserManager userManager, IXIdentityHelper identityHelper, ILogger logger, IXMessageProvider messageProvider, IXStorageProvider storageProvider, SignInManager signInManager, IXSecurityProvider securityProvider, XIdentityConfiguration configuration, RoleManager roleManager, XValidationProvider validationProvider, XDataServiceConfiguration dataConfiguration, IXIdentityMessageProvider identityMessageProvider, XIdentityResourceConfiguration identityResourceConfiguration ) { // Logger = logger; DbContext = dbContext; RoleManager = roleManager; UserManager = userManager; SignInManager = signInManager; SecurityProvider = securityProvider; Configuration = configuration; IdentityHelper = identityHelper; MessageProvider = messageProvider; StorageProvider = storageProvider; DataConfiguration = dataConfiguration; ValidationProvider = validationProvider; IdentityMessageProvider = identityMessageProvider; IdentityResourceConfiguration = identityResourceConfiguration; // this.jsonSerializerSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; } // #region Class Getter (s) Methods ... /// /// Retrieve User DB Set /// /// specifies returned object contains all Navigation Properties or not, default is false /// public IQueryable GetUsersDbSet( bool containDetails = false ) { // var dbSet = containDetails ? GetUsersFullDbSet() : UserManager.Users; return dbSet; } /// /// Retrieve User Db Set Full Navigation Properties /// /// /// public IQueryable GetUsersFullDbSet(bool enableTracking = false) { // var dbSet = UserManager.Users .Include(u => u.Roles) .Include(u => u.Devices) .Include(u => u.Followers) .Include(u => u.Followings) .Include(u => u.Avatars); // return dbSet.AsSplitQuery(); } #endregion // #region Tools ... /// /// Get User SelectBy Param /// /// /// /// /// public string GetUserSelectByParam( XActionRequest model, bool forceNotNull = true, ICollection excludes = null ) { // // Validate Args ... ValidationProvider.NotNull(model); // var id = model.UserId; var userName = model.UserName; var email = model.Email; var phoneNumber = model.MobileNumber; // if (excludes != null) { // if (excludes.Contains(XUserSelectBy.ID)) { id = null; } if (excludes.Contains(XUserSelectBy.Email)) { email = null; } if (excludes.Contains(XUserSelectBy.MobileNumber)) { phoneNumber = null; } if (excludes.Contains(XUserSelectBy.Username)) { userName = null; } } var selectByParam = id.IsNullOrEmpty() ? userName.IsNullOrEmpty() ? email.IsNullOrEmpty() ? phoneNumber.IsNullOrEmpty() ? null : phoneNumber : email : userName : id; // // Check result ... if (forceNotNull && selectByParam.IsNullOrEmpty()) { XException.InvalidData.Throw(); } // return selectByParam; } #endregion } }