Files
xSaherElmIds/Providers/XIdentityManager.cs
T
2026-03-22 14:08:25 +03:30

187 lines
6.2 KiB
C#

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<XUser> UserManager { get; }
public IXIdentityHelper IdentityHelper { get; }
public ILogger<IXIdentityManager> Logger { get; }
public IXMessageProvider MessageProvider { get; }
public IXStorageProvider StorageProvider { get; }
public SignInManager<XUser> SignInManager { get; }
public IXSecurityProvider SecurityProvider { get; }
public XIdentityConfiguration Configuration { get; }
public RoleManager<IdentityRole> RoleManager { get; }
public XValidationProvider ValidationProvider { get; }
public XDataServiceConfiguration DataConfiguration { get; }
public IXIdentityMessageProvider IdentityMessageProvider { get; }
public XIdentityResourceConfiguration IdentityResourceConfiguration { get; }
public XIdentityManager(
XIdentityDbContext dbContext,
UserManager<XUser> userManager,
IXIdentityHelper identityHelper,
ILogger<IXIdentityManager> logger,
IXMessageProvider messageProvider,
IXStorageProvider storageProvider,
SignInManager<XUser> signInManager,
IXSecurityProvider securityProvider,
XIdentityConfiguration configuration,
RoleManager<IdentityRole> 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 ...
/// <summary>
/// Retrieve User DB Set
/// </summary>
/// <param name="containDetails">specifies returned object contains all Navigation Properties or not, default is false</param>
/// <returns></returns>
public IQueryable<XUser> GetUsersDbSet(
bool containDetails = false
)
{
//
var dbSet = containDetails ? GetUsersFullDbSet() :
UserManager.Users;
return dbSet;
}
/// <summary>
/// Retrieve User Db Set Full Navigation Properties
/// </summary>
/// <param name="enableTracking"></param>
/// <returns></returns>
public IQueryable<XUser> 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 ...
/// <summary>
/// Get User SelectBy Param
/// </summary>
/// <param name="model"></param>
/// <param name="forceNotNull"></param>
/// <param name="excludes"></param>
/// <returns></returns>
public string GetUserSelectByParam(
XActionRequest model,
bool forceNotNull = true,
ICollection<XUserSelectBy> 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
}
}