last ...
This commit is contained in:
@@ -6,6 +6,7 @@ using xIdentityModels.Models;
|
||||
using xIdentityModels.Constants;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityModels.Configurations;
|
||||
using xExceptions.Constants;
|
||||
|
||||
namespace xIdentityModels.Extensions
|
||||
{
|
||||
@@ -173,6 +174,217 @@ namespace xIdentityModels.Extensions
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract Top XApiKey Scope inside an Enumerable ...
|
||||
/// </summary>
|
||||
/// <param name="scopes"></param>
|
||||
/// <returns></returns>
|
||||
public static XApiKeyScope GetTopXApiKeyScope(
|
||||
this IEnumerable<XApiKeyScope> scopes
|
||||
)
|
||||
{
|
||||
//
|
||||
// Check Scopes Has Child ...
|
||||
if (!scopes.HasChild())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var hasAdmin = scopes
|
||||
.Any(s =>
|
||||
s.GetStringValue().ToNormalString() == XApiKeyScope.Admin.GetStringValue().ToNormalString());
|
||||
var hasRead = scopes
|
||||
.Any(s =>
|
||||
s.GetStringValue().ToNormalString() == XApiKeyScope.Read.GetStringValue().ToNormalString());
|
||||
var hasWrite = scopes
|
||||
.Any(s =>
|
||||
s.GetStringValue().ToNormalString() == XApiKeyScope.Write.GetStringValue().ToNormalString());
|
||||
var hasManage =
|
||||
(hasRead && hasWrite) ||
|
||||
scopes
|
||||
.Any(s =>
|
||||
s.GetStringValue().ToNormalString() == XApiKeyScope.Manage.GetStringValue().ToNormalString());
|
||||
|
||||
//
|
||||
// Admin ...
|
||||
if (hasAdmin)
|
||||
{
|
||||
return XApiKeyScope.Admin;
|
||||
}
|
||||
else if (hasManage)
|
||||
{
|
||||
return XApiKeyScope.Manage;
|
||||
}
|
||||
else if (hasWrite)
|
||||
{
|
||||
return XApiKeyScope.Write;
|
||||
}
|
||||
else
|
||||
{
|
||||
return XApiKeyScope.Read;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check Source ApiKey Scope Has Access to Dest ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="dest"></param>
|
||||
/// <returns></returns>
|
||||
public static bool HasAccess(
|
||||
this XApiKeyScope source,
|
||||
XApiKeyScope dest
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = source == dest;
|
||||
if (result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
var alls = Enumerable.Empty<XApiKeyScope>();
|
||||
switch (source)
|
||||
{
|
||||
//
|
||||
// Admin ...
|
||||
case XApiKeyScope.Admin:
|
||||
//
|
||||
alls = [
|
||||
XApiKeyScope.Admin,
|
||||
XApiKeyScope.Manage,
|
||||
XApiKeyScope.Write,
|
||||
XApiKeyScope.Read
|
||||
];
|
||||
|
||||
//
|
||||
result = alls.Contains(dest);
|
||||
break;
|
||||
|
||||
//
|
||||
// Manage ...
|
||||
case XApiKeyScope.Manage:
|
||||
//
|
||||
alls = [
|
||||
XApiKeyScope.Manage,
|
||||
XApiKeyScope.Write,
|
||||
XApiKeyScope.Read
|
||||
];
|
||||
|
||||
//
|
||||
result = alls.Contains(dest);
|
||||
break;
|
||||
|
||||
//
|
||||
// Write ...
|
||||
case XApiKeyScope.Write:
|
||||
//
|
||||
alls = [XApiKeyScope.Write];
|
||||
|
||||
//
|
||||
result = alls.Contains(dest);
|
||||
break;
|
||||
|
||||
//
|
||||
// Read ...
|
||||
case XApiKeyScope.Read:
|
||||
//
|
||||
alls = [XApiKeyScope.Read];
|
||||
|
||||
//
|
||||
result = alls.Contains(dest);
|
||||
break;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check Allowed Scopes passed Based on Required Scopes ...
|
||||
/// </summary>
|
||||
/// <param name="allowedScopes"></param>
|
||||
/// <param name="requiredScopes"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsScopePassed(
|
||||
this IEnumerable<string> allowedScopes,
|
||||
IEnumerable<string> requiredScopes
|
||||
)
|
||||
{
|
||||
//
|
||||
// if Allowed Scopes is Empty, this means access to
|
||||
// All Scopes are Valid ...
|
||||
var result = !allowedScopes.HasChild();
|
||||
if (result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Make Sure we Have at least one Required Scope for Validation ...
|
||||
result = requiredScopes.HasChild();
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Converts each Allowed or Required Scopes to XApiKeyScope ...
|
||||
var alloweds = allowedScopes.Select(asc =>
|
||||
{
|
||||
//
|
||||
if (asc.ToNormalString() == XApiKeyScope.Admin.GetStringValue().ToNormalString())
|
||||
{
|
||||
return XApiKeyScope.Admin;
|
||||
}
|
||||
else if (asc.ToNormalString() == XApiKeyScope.Manage.GetStringValue().ToNormalString())
|
||||
{
|
||||
return XApiKeyScope.Manage;
|
||||
}
|
||||
else if (asc.ToNormalString() == XApiKeyScope.Write.GetStringValue().ToNormalString())
|
||||
{
|
||||
return XApiKeyScope.Write;
|
||||
}
|
||||
else
|
||||
{
|
||||
return XApiKeyScope.Read;
|
||||
}
|
||||
});
|
||||
var requireds = requiredScopes.Select(asc =>
|
||||
{
|
||||
//
|
||||
if (asc.ToNormalString() == XApiKeyScope.Admin.GetStringValue().ToNormalString())
|
||||
{
|
||||
return XApiKeyScope.Admin;
|
||||
}
|
||||
else if (asc.ToNormalString() == XApiKeyScope.Manage.GetStringValue().ToNormalString())
|
||||
{
|
||||
return XApiKeyScope.Manage;
|
||||
}
|
||||
else if (asc.ToNormalString() == XApiKeyScope.Write.GetStringValue().ToNormalString())
|
||||
{
|
||||
return XApiKeyScope.Write;
|
||||
}
|
||||
else
|
||||
{
|
||||
return XApiKeyScope.Read;
|
||||
}
|
||||
});
|
||||
|
||||
//
|
||||
// Retrieve top Allowed ApiKey Scope ...
|
||||
var topAllowed = alloweds.GetTopXApiKeyScope();
|
||||
var topRequired = requireds.GetTopXApiKeyScope();
|
||||
|
||||
//
|
||||
result = topAllowed.HasAccess(topRequired);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check Specified User Contains Specified UserSelectByParam or not ...
|
||||
/// </summary>
|
||||
@@ -205,7 +417,7 @@ namespace xIdentityModels.Extensions
|
||||
public static bool IsRevoked(this XApiKey source)
|
||||
{
|
||||
//
|
||||
var result =
|
||||
var result =
|
||||
!source.IsNullOrDefault() &&
|
||||
source.RevokedAt.HasValue;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user