Files
xIdentityModels/Extensions/XIdentityExtensions.cs
2024-01-25 04:42:47 +03:30

105 lines
3.2 KiB
C#

using System.Collections.Generic;
using xCommons.Extensions;
using xExceptions.Constants;
using xIdentityModels.Constants;
using xIdentityModels.Models;
namespace xIdentityModels.Extensions {
public static class XIdentityExtensions {
public static XUserSelectBy GetUserSelectByType (
this string source,
bool forceUserSelectByParamNotEmpty = true,
bool forceUserSelectByTypeMustSpecified = false
) {
//
// Validate Args ...
if (source.IsNullOrEmpty () &&
forceUserSelectByParamNotEmpty) {
XException.InvalidArgs.Throw ();
}
//
var result = XUserSelectBy.NotSpecified;
var userSelectByParam = source.ToNormalString ();
if (userSelectByParam.IsValidEmail ()) {
//
// Find User by Email ...
result = XUserSelectBy.Email;
} else if (userSelectByParam.IsValidMobileNumber ()) {
//
// Find User by Mobile Number ...
result = XUserSelectBy.MobileNumber;
} else if (userSelectByParam.IsGuid ()) {
//
// Find User By it's ID ...
result = XUserSelectBy.ID;
} else {
//
// UserName ...
result = XUserSelectBy.Username;
}
//
// Validation Result ...
if (forceUserSelectByTypeMustSpecified &&
result == XUserSelectBy.NotSpecified) {
XException.InvalidData.Throw ();
}
//
return result;
}
public static string GetUserSelectByParam (
this XActionRequest source,
bool forceNotNull = true,
ICollection<XUserSelectBy> excludes = null
) {
//
// Validate Args ...
if (source.IsNull ()) {
XException.InvalidArgs.Throw ();
}
//
var id = source.UserId;
var userName = source.UserName;
var email = source.Email;
var phoneNumber = source.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;
}
}
}