diff --git a/Extensions/XModelExtensions.cs b/Extensions/XModelExtensions.cs index 502b34d..5d55da1 100644 --- a/Extensions/XModelExtensions.cs +++ b/Extensions/XModelExtensions.cs @@ -1,49 +1,84 @@ 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 { +namespace xIdentityModels.Extensions +{ + public static class XModelExtensions + { /// /// /// Convert XIdentityUserDescriptor instance to XUser /// /// /// - public static XUser ToXUser (this XIdentityUserDescriptor source) { - return new 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) + 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) { + public static bool IsSameAs(this XUser source, XIdentityUserDescriptor dest) + { // - if (source.IsNull () || - dest.IsNull ()) { + if (source.IsNull() || + dest.IsNull()) + { return false; } // - var isSame = source.IsSameContent (dest, propertyWhiteList : new [] { + var isSame = source.IsSameContent(dest, propertyWhiteList: new[] { nameof (XIdentityUserDescriptor.FirstName), nameof (XIdentityUserDescriptor.LastName), nameof (XIdentityUserDescriptor.UserName), @@ -55,7 +90,7 @@ namespace xIdentityModels.Extensions { nameof (XIdentityUserDescriptor.IsBanned), nameof (XIdentityUserDescriptor.DateOfBirth) }, - propertyValueCheckers : new [] { + propertyValueCheckers: new[] { new KeyValuePair> ( nameof (XIdentityUserDescriptor.DateOfBirth), (user, descriptorUser) => { @@ -76,18 +111,20 @@ namespace xIdentityModels.Extensions { /// /// /// - public static bool IsSameAs (this XDevice source, XDevice dest) { + public static bool IsSameAs(this XDevice source, XDevice dest) + { // - if (source.IsNull () || - dest.IsNull ()) { + 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 () && + 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; // @@ -99,15 +136,18 @@ namespace xIdentityModels.Extensions { /// /// /// - public static XTokenResponse ToXTokenResponse (this XUserClaimsInfoDto source) { + public static XTokenResponse ToXTokenResponse(this XUserClaimsInfoDto source) + { // - if (source.IsNull () || - (source.AccessToken.IsNullOrEmpty () && source.RefreshToken.IsNullOrEmpty ())) { + if (source.IsNull() || + (source.AccessToken.IsNullOrEmpty() && source.RefreshToken.IsNullOrEmpty())) + { return null; } // - var result = new XTokenResponse { + var result = new XTokenResponse + { AccessToken = source.AccessToken, RefreshToken = source.RefreshToken, ExpiresAt = source.ExpiresAt @@ -116,5 +156,109 @@ namespace xIdentityModels.Extensions { // 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; + } } } \ No newline at end of file diff --git a/Extensions/xIdentityModelsExtensions.cs b/Extensions/xIdentityModelsExtensions.cs index 4efbc11..949ca55 100644 --- a/Extensions/xIdentityModelsExtensions.cs +++ b/Extensions/xIdentityModelsExtensions.cs @@ -1,3 +1,4 @@ +using System; using System.Linq; using xCommons.Extensions; using xIdentityModels.Dtos; @@ -5,8 +6,6 @@ using xIdentityModels.Models; using xIdentityModels.Constants; using System.Collections.Generic; using xIdentityModels.Configurations; -using Microsoft.OpenApi.Expressions; -using System; namespace xIdentityModels.Extensions {