using System;
using xModels.Dtos;
using xCommons.Extensions;
using xIdentityModels.Dtos;
using xIdentityModels.Models;
using System.Collections.Generic;
using xIdentityModels.Descriptors;
using xIdentityModels.Navigations;
namespace xIdentityModels.Extensions
{
public static class XModelExtensions
{
///
/// /// Convert XIdentityUserDescriptor instance to XUser
///
///
///
public static XUser ToXUser(this XIdentityUserDescriptor source)
{
return new XUser
{
Id = source.Id,
FirstName = source.FirstName,
LastName = source.LastName,
UserName = source.UserName,
Email = source.Email,
EmailConfirmed = source.EmailConfirmed,
PhoneNumber = source.PhoneNumber,
PhoneNumberConfirmed = source.PhoneNumberConfirmed,
IsEnable = true,
IsBanned = false,
CreationDate = DateTime.UtcNow,
DateOfBirth = DateTime.Parse(source.DateOfBirth)
};
}
///
/// Converts a User to Person ...
///
///
///
public static XPersonDto ToXPerson(this XUser source)
{
//
XPersonDto result = null;
//
if (!source.IsNullOrDefault())
{
//
result = new XPersonDto
{
Avatar = source.Avatar,
Username = source.UserName,
Lastname = source.LastName,
Firstname = source.FirstName,
};
}
//
return result;
}
///
/// Check a User is Same as Descriptor User
///
///
///
///
public static bool IsSameAs(this XUser source, XIdentityUserDescriptor dest)
{
//
if (source.IsNull() ||
dest.IsNull())
{
return false;
}
//
var isSame = source.IsSameContent(dest, propertyWhiteList: new[] {
nameof (XIdentityUserDescriptor.FirstName),
nameof (XIdentityUserDescriptor.LastName),
nameof (XIdentityUserDescriptor.UserName),
nameof (XIdentityUserDescriptor.Email),
nameof (XIdentityUserDescriptor.EmailConfirmed),
nameof (XIdentityUserDescriptor.PhoneNumber),
nameof (XIdentityUserDescriptor.PhoneNumberConfirmed),
nameof (XIdentityUserDescriptor.IsEnable),
nameof (XIdentityUserDescriptor.IsBanned),
nameof (XIdentityUserDescriptor.DateOfBirth)
},
propertyValueCheckers: new[] {
new KeyValuePair> (
nameof (XIdentityUserDescriptor.DateOfBirth),
(user, descriptorUser) => {
//
var descriptorUserDateOfBirth = DateTime.Parse (descriptorUser.DateOfBirth);
return DateTime.Equals (user.DateOfBirth, descriptorUserDateOfBirth);
})
}
);
//
return isSame;
}
///
/// Check to XDevice instance to be same or not
///
///
///
///
public static bool IsSameAs(this XDevice source, XDevice dest)
{
//
if (source.IsNull() ||
dest.IsNull())
{
return false;
}
//
var isSame = source.Os.ToNormalString() == dest.Os.ToNormalString() &&
source.OsVersion.ToNormalString() == dest.OsVersion.ToNormalString() &&
source.Browser.ToNormalString() == dest.Browser.ToNormalString() &&
source.UserAgent.ToNormalString() == dest.UserAgent.ToNormalString() &&
source.DeviceType == dest.DeviceType;
//
return isSame;
}
///
/// convert UserClaimsInfo to TokenResponse ...
///
///
///
public static XTokenResponse ToXTokenResponse(this XUserClaimsInfoDto source)
{
//
if (source.IsNull() ||
(source.AccessToken.IsNullOrEmpty() && source.RefreshToken.IsNullOrEmpty()))
{
return null;
}
//
var result = new XTokenResponse
{
AccessToken = source.AccessToken,
RefreshToken = source.RefreshToken,
ExpiresAt = source.ExpiresAt
};
//
return result;
}
///
/// Validate Item for Add or Update ...
///
///
///
public static bool IsValidForAdd(this XApplication source)
{
//
var result =
!source.IsNullOrDefault() &&
!source.Name.IsNullOrEmpty();
//
return result;
}
///
/// Validate Item for Add or Update ...
///
///
///
public static bool IsValidForAdd(this XApplicationDto source)
{
//
var result =
!source.IsNullOrDefault() &&
!source.Name.IsNullOrEmpty();
//
return result;
}
///
/// Validate Item for Add or Update ...
///
///
///
public static bool IsValidForAdd(this XApiKey source)
{
//
var result =
!source.IsNullOrDefault() &&
!source.ApplicationId.IsValidIntId() &&
!source.KeyHash.IsNullOrEmpty();
//
return result;
}
///
/// Validate Item for Add or Update ...
///
///
///
public static bool IsValidForAdd(this XApiKeyDto source)
{
//
var result =
!source.IsNullOrDefault() &&
!source.ApplicationId.IsValidIntId() &&
!source.KeyHash.IsNullOrEmpty();
//
return result;
}
///
/// Validate Item for Add or Update ...
///
///
///
public static bool IsValidForAdd(this XApiKeyUsage source)
{
//
var result =
!source.IsNullOrDefault() &&
!source.ApiKeyId.IsDefaultGuid() &&
!source.ClientIP.IsNullOrEmpty() &&
!source.Endpoint.IsNullOrEmpty() &&
!source.HttpMethod.IsNullOrEmpty();
//
return result;
}
///
/// Validate Item for Add or Update ...
///
///
///
public static bool IsValidForAdd(this XApiKeyUsageDto source)
{
//
var result =
!source.IsNullOrDefault() &&
!source.ApiKeyId.IsDefaultGuid() &&
!source.ClientIP.IsNullOrEmpty() &&
!source.Endpoint.IsNullOrEmpty() &&
!source.HttpMethod.IsNullOrEmpty();
//
return result;
}
}
}