using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using xCommons.Extensions; using xExceptions.Constants; using xIdentityModels; using xIdentityModels.Extensions; using xIdentityModels.Models; using xIdentityModels.Navigations; using xIds.Extensions; namespace xIds.Providers { public partial class XIdentityManager { // #region Device Actions ... /// /// Check a Device Exists or Not /// /// an instance of XDevice /// a boolean value public async Task IsDeviceExistsAsync( XDevice device ) { return await DbContext.Devices .AnyAsync(d => d.IsSameAs(device)); } /// /// Add new Device /// /// an instance of XDevice /// an instance of XDevice public async Task AddDeviceAsync( XDevice device ) { // var isDeviceExists = await IsDeviceExistsAsync(device); if (isDeviceExists) { return null; } // await DbContext.Devices .AddAsync(device); await DbContext.SaveChangesAsync(); return device; } /// /// Get a Device /// /// an instance of XDevice /// an instance of XDevice public Task GetDeviceAsync( XDevice device ) { // return DbContext.Devices .FirstOrDefaultAsync(d => d.IsSameAs(device)); } /// /// Retrieve a List of User Related Devices /// /// user identifier /// check a user can log in in system or not /// check user is banned or not /// a collection of XDevice instances which related to user public async Task> GetUserDevices( string userSelectByParam, bool checkCanLoginPolicies = true, bool checkIsBanned = true ) { // // Validate ARgs ... ValidationProvider.NotEmpty(userSelectByParam); // // Validate And Retrieve User ... var user = await ValidateUserExistsAndRetrieve( userSelectByParam, containDetails: true, checkCanLoginPolicies: checkCanLoginPolicies, checkIsBanned: checkIsBanned ); // // Check User Devices ... if (!user.Devices.HasChild()) { return new List(); } // return user.Devices; } /// /// Determines a Device is Exists in User's Devices or not /// /// an instance of XUser /// an instance of XDevice /// a boolean value public bool IsDeviceRelateDToUser( XUser user, XDevice device ) { if (!user.Devices.HasChild()) { return false; } // var result = user.Devices.Any(d => d.IsSameAs(device)); // return result; } /// /// Determines a Device is Exists in User's Devices or not /// /// user identifier /// an instance of XDevice /// check a user can log in in system or not /// check user is banned or not /// a boolean value public async Task IsDeviceRelateDToUser( string userSelectByParam, XDevice device, bool checkCanLoginPolicies = true, bool checkIsBanned = true ) { // // Validate Args ... ValidationProvider.NotEmpty(userSelectByParam); ValidationProvider.NotNull(device); // var userDevices = await GetUserDevices( userSelectByParam, checkCanLoginPolicies: checkCanLoginPolicies, checkIsBanned: checkIsBanned ); if (!userDevices.HasChild()) { return false; } // var result = userDevices.Any(d => d.IsSameAs(device)); // return result; } /// /// Add a Device to a User Related Devices /// /// an instance of XUser /// an instance of XDevice /// a boolean value public async Task AddUserRelatedDevice( XUser user, XDevice device ) { // var isExists = user.Devices.Any(d => d.IsSameAs(device)); if (isExists) { return true; } // user.Devices.Add(device); try { var result = await UpdateUserAsync(user); return result.Succeeded; } catch { throw XException.ActionFailed.ToException(); } } /// /// Add a Device to a User /// /// user identifier /// an instance of XDevice /// check a user can log in in system or not /// check user is banned or not /// a boolean value public async Task AddUserRelatedDevice( string userSelectByParam, XDevice device, bool checkCanLoginPolicies = true, bool checkIsBanned = true ) { // // Validate Args ... ValidationProvider.NotEmpty(userSelectByParam); ValidationProvider.NotNull(device); // // Check Device Related ... var isDeviceRelatedToUser = await IsDeviceRelateDToUser( userSelectByParam, device, checkCanLoginPolicies: checkCanLoginPolicies, checkIsBanned: checkIsBanned); if (isDeviceRelatedToUser) { return true; } // // Retrieve User ... var user = await ValidateUserExistsAndRetrieve( userSelectByParam, containDetails: true, checkCanLoginPolicies: checkCanLoginPolicies, checkIsBanned: checkIsBanned ); // // Add User Device ... user.Devices.Add(device); // var result = await UpdateUserAsync( user, checkCanLoginPolicies: false, checkIsBanned: false ); // return result.Succeeded; } /// /// Remove a Device from a User Related Devices List /// /// an instance of XUser /// an instance of XDevice /// a boolean value public async Task RemoveUserRelatedDevice( XUser user, XDevice device ) { // var isRelated = IsDeviceRelateDToUser(user, device); if (!isRelated) { return false; } // var instanse = user.Devices .FirstOrDefault(d => d.IsSameAs(device)); // user.Devices.Remove(instanse); // var result = await UpdateUserAsync( user, checkCanLoginPolicies: false, checkIsBanned: false ); // return result.Succeeded; } /// /// Remove a Device from a User Devices /// /// user identifier /// an instance of XDevice /// check a user can log in in system or not /// check user is banned or not /// a boolean value public async Task RemoveUserRelatedDevice( string userSelectByParam, XDevice device, bool checkCanLoginPolicies = true, bool checkIsBanned = true ) { // // Validate Args ... ValidationProvider.NotEmpty(userSelectByParam); ValidationProvider.NotNull(device); // // Validate user exists and Retrieve it based on userselectbyparam ... var user = await ValidateUserExistsAndRetrieve( userSelectByParam, containDetails: true, checkCanLoginPolicies: checkCanLoginPolicies, checkIsBanned: checkIsBanned ); // return await RemoveUserRelatedDevice( user, device ); } #endregion // #region Banned Device Actions ... /// /// Check a Device is exists in Banned Devices or not /// /// an instance of XDevice /// a boolean value public async Task IsBannedDeviceExists( XDevice device ) { // var result = false; var bannedDeviceEnumerable = DbContext.BannedDevices.AsAsyncEnumerable(); await foreach (var bannedDevice in bannedDeviceEnumerable) { // if (bannedDevice.Device.IsSameAs(device)) { // result = true; break; } } // return result; } /// /// Find First Banned Device /// /// an instance of XDevice /// an instance of XDevice public async Task BannedDeviceFindOne( XDevice device ) { // var result = await DbContext.BannedDevices .FirstOrDefaultAsync(bd => bd.Device.IsSameAs(device) ); // return result; } /// /// Remove Banned Device /// /// an instance of XBannedDevice /// public async Task BannedDeviceRemove( XBannedDevice model ) { // var isExists = await IsBannedDeviceExists(model.Device); if (!isExists) { return; } // var item = await BannedDeviceFindOne(model.Device); if (item == null) { return; } // DbContext.BannedDevices.Remove(item); await DbContext.SaveChangesAsync(); } /// /// Check a Device is Banned or not /// /// an instance of XDevice /// a boolean value private async Task IsDeviceBanned( XDevice device ) { // var result = await IsBannedDeviceExists(device); return result; } /// /// Retrieve Specific Banned Device /// /// an instance of XDevice /// an instance of XBannedDevice private async Task GetBannedDevice( XDevice device ) { // var result = await BannedDeviceFindOne(device); return result; } /// /// Is a Banned Device passed Banning Time /// /// an instance of XBannedDevice /// a boolean value private bool IsDelayTimePassed( XBannedDevice bannedDevice ) { // // Validate Args ... if (bannedDevice == null || Configuration == null) { XException.InvalidArgs.Throw(); } // var isPassed = bannedDevice .IsDelayTimePassed(Configuration.BannedDeviceTimeout); // return isPassed; } /// /// Retrieve Passed Time from Banning Time of specific Device /// /// an instance of XBannedDevice /// an instance of DateTime which represent Passed Time private DateTime GetPassedTime( XBannedDevice bannedDevice ) { // var result = bannedDevice.BannedOn .AddSeconds(Configuration.BannedDeviceTimeout); // return result; } #endregion } }