last works on XFile Provider by Implememnting Tag Provider and also add Global Supports for ProvidedFor and References inside File Provider ...
This commit is contained in:
+495
-15
@@ -31,6 +31,7 @@ namespace xFileService.Providers
|
||||
{
|
||||
//
|
||||
#region Props ...
|
||||
public IXFileTagProvider TagProvider { get; }
|
||||
public IHubContext<XFileEntityHub> Hub { get; }
|
||||
public IXFileRepository FileRepository { get; }
|
||||
public IXStorageProvider StorageProvider { get; }
|
||||
@@ -41,6 +42,7 @@ namespace xFileService.Providers
|
||||
//
|
||||
#region Constructor ...
|
||||
public XFileProvider(
|
||||
IXFileTagProvider tagProvider,
|
||||
IXFileRepository fileRepository,
|
||||
IXStorageProvider storageProvider,
|
||||
IXIdentityProvider identityProvider,
|
||||
@@ -50,6 +52,7 @@ namespace xFileService.Providers
|
||||
{
|
||||
//
|
||||
Hub = hub;
|
||||
TagProvider = tagProvider;
|
||||
FileRepository = fileRepository;
|
||||
StorageProvider = storageProvider;
|
||||
IdentityProvider = identityProvider;
|
||||
@@ -155,6 +158,262 @@ namespace xFileService.Providers
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check Specified User Info Has Permissions for Specified File ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<bool> HasPermission(
|
||||
Guid id,
|
||||
XUserClaimsInfoDto userInfo = null
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = false;
|
||||
|
||||
//
|
||||
// Validate ...
|
||||
result = !id.IsNull() &&
|
||||
!id.IsDefaultGuid() &&
|
||||
!userInfo.IsNullOrDefault();
|
||||
if (!result)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Item Exists ...
|
||||
result = await IsExists(id);
|
||||
if (!result)
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Dto ...
|
||||
var dto = await Get(id);
|
||||
result = !dto.IsNullOrDefault();
|
||||
if (!result)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
result =
|
||||
dto.OwnerId == userInfo.UserId ||
|
||||
userInfo.Roles.Any(r => r.ToNormalString() == "admin");
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Tags ...
|
||||
/// <summary>
|
||||
/// Attach Tag to Specified File ...
|
||||
/// </summary>
|
||||
/// <param name="tag"></param>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task TagAttach(
|
||||
string tag,
|
||||
Guid id,
|
||||
XUserClaimsInfoDto userInfo = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid = !tag.IsNullOrEmpty() &&
|
||||
!id.IsNull() &&
|
||||
!id.IsDefaultGuid() &&
|
||||
!userInfo.IsNullOrDefault();
|
||||
if (isValid)
|
||||
{
|
||||
//
|
||||
// Check Permissions ...
|
||||
isValid = await HasPermission(
|
||||
id: id,
|
||||
userInfo: userInfo
|
||||
);
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
await TagProvider.Attach(tag, id);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Detach Tag fro Specified File ...
|
||||
/// </summary>
|
||||
/// <param name="tag"></param>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task TagDetach(
|
||||
string tag,
|
||||
Guid id,
|
||||
XUserClaimsInfoDto userInfo = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid = !tag.IsNullOrEmpty() &&
|
||||
!id.IsNull() &&
|
||||
!id.IsDefaultGuid();
|
||||
if (isValid)
|
||||
{
|
||||
//
|
||||
// Check Permissions ...
|
||||
isValid = await HasPermission(
|
||||
id: id,
|
||||
userInfo: userInfo
|
||||
);
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
await TagProvider.Detach(tag, id);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attach Tags for Specified File ...
|
||||
/// </summary>
|
||||
/// <param name="tags"></param>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task TagsAttach(
|
||||
IEnumerable<string> tags,
|
||||
Guid id,
|
||||
XUserClaimsInfoDto userInfo = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid = !tags.IsNull() &&
|
||||
tags.HasChild() &&
|
||||
!id.IsNull() &&
|
||||
!id.IsDefaultGuid();
|
||||
if (isValid)
|
||||
{
|
||||
//
|
||||
// Check Permissions ...
|
||||
isValid = await HasPermission(
|
||||
id: id,
|
||||
userInfo: userInfo
|
||||
);
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
foreach (var tag in tags)
|
||||
{
|
||||
await TagAttach(tag, id);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Detach Tags for Specified File ...
|
||||
/// </summary>
|
||||
/// <param name="tags"></param>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task TagsDetach(
|
||||
IEnumerable<string> tags,
|
||||
Guid id,
|
||||
XUserClaimsInfoDto userInfo = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid = !tags.IsNull() &&
|
||||
tags.HasChild() &&
|
||||
!id.IsNull() &&
|
||||
!id.IsDefaultGuid();
|
||||
if (isValid)
|
||||
{
|
||||
//
|
||||
// Check Permissions ...
|
||||
isValid = await HasPermission(
|
||||
id: id,
|
||||
userInfo: userInfo
|
||||
);
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
foreach (var tag in tags)
|
||||
{
|
||||
await TagDetach(tag, id);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Detach all Attached Tags for Specified File ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task TagsDetach(
|
||||
Guid id,
|
||||
XUserClaimsInfoDto userInfo = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid = !id.IsNull() && !id.IsDefaultGuid();
|
||||
if (isValid)
|
||||
{
|
||||
//
|
||||
// Check Permissions ...
|
||||
isValid = await HasPermission(
|
||||
id: id,
|
||||
userInfo: userInfo
|
||||
);
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
var tags = await TagProvider.RemoveTagsFor(id);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
@@ -478,6 +737,17 @@ namespace xFileService.Providers
|
||||
result.Add(model.Id);
|
||||
}
|
||||
|
||||
//
|
||||
// Remove Tags ...
|
||||
if (!result.IsNull() && result.HasChild())
|
||||
{
|
||||
//
|
||||
foreach (var id in result)
|
||||
{
|
||||
await TagsDetach(id);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
@@ -642,6 +912,206 @@ namespace xFileService.Providers
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Reference ...
|
||||
/// <summary>
|
||||
/// Get Reference Identifier for Specified Provider and Specified File ...
|
||||
/// </summary>
|
||||
/// <param name="providedFor"></param>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public string GetIdentifier(
|
||||
string providedFor,
|
||||
Guid id
|
||||
)
|
||||
{
|
||||
return $"{providedFor}_{id}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check Specified File has Refernce to Provider ...
|
||||
/// </summary>
|
||||
/// <param name="providedFor"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> IsProvidedFor(
|
||||
string providedFor,
|
||||
Guid id
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid =
|
||||
!providedFor.IsNullOrEmpty() &&
|
||||
!id.IsNull() &&
|
||||
!id.IsDefaultGuid();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Exists ...
|
||||
isValid = await IsExists(id);
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var dto = await Get(id);
|
||||
isValid = !dto.IsNullOrDefault();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var identifier = GetIdentifier(providedFor, id);
|
||||
var result =
|
||||
dto.References.IsNull() &&
|
||||
dto.References.HasChild() &&
|
||||
dto.References.Any(r => r.ToNormalString() == identifier.ToNormalString());
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add Specified Reference to Specified File ...
|
||||
/// </summary>
|
||||
/// <param name="providedFor"></param>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <returns></returns>
|
||||
public async Task AddReference(
|
||||
string providedFor,
|
||||
Guid id,
|
||||
XUserClaimsInfoDto userInfo = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid =
|
||||
!providedFor.IsNullOrEmpty() &&
|
||||
!id.IsNull() &&
|
||||
!id.IsDefaultGuid() &&
|
||||
!userInfo.IsNullOrDefault();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Has Reference or not ...
|
||||
isValid = await IsProvidedFor(providedFor, id);
|
||||
if (!isValid)
|
||||
{
|
||||
//
|
||||
// Check Permissions ...
|
||||
isValid = await HasPermission(
|
||||
id: id,
|
||||
userInfo: userInfo
|
||||
);
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var dto = await Get(id);
|
||||
isValid = !dto.IsNullOrDefault();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var identifier = GetIdentifier(providedFor, id);
|
||||
dto.References.Add(identifier);
|
||||
|
||||
//
|
||||
dto = await Update(
|
||||
id: dto.Id,
|
||||
item: dto,
|
||||
userInfo: userInfo
|
||||
);
|
||||
isValid = !dto.IsNullOrDefault();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove Specified Reference from Specified File ...
|
||||
/// </summary>
|
||||
/// <param name="providedFor"></param>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <returns></returns>
|
||||
public async Task RemoveReference(
|
||||
string providedFor,
|
||||
Guid id,
|
||||
XUserClaimsInfoDto userInfo = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid =
|
||||
!providedFor.IsNullOrEmpty() &&
|
||||
!id.IsNull() &&
|
||||
!id.IsDefaultGuid() &&
|
||||
!userInfo.IsNullOrDefault();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Has Reference or not ...
|
||||
isValid = await IsProvidedFor(providedFor, id);
|
||||
if (isValid)
|
||||
{
|
||||
//
|
||||
// Check Permissions ...
|
||||
isValid = await HasPermission(
|
||||
id: id,
|
||||
userInfo: userInfo
|
||||
);
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Dto ...
|
||||
var dto = await Get(id);
|
||||
isValid = !dto.IsNullOrDefault();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var identifier = GetIdentifier(providedFor, id);
|
||||
dto.References = dto.References
|
||||
.Where(r => r.ToNormalString() != identifier.ToNormalString())
|
||||
.ToList();
|
||||
dto = await Update(
|
||||
id: dto.Id,
|
||||
item: dto,
|
||||
userInfo: userInfo
|
||||
);
|
||||
isValid = !dto.IsNullOrDefault();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Data Model ...
|
||||
/// <summary>
|
||||
@@ -831,6 +1301,18 @@ namespace xFileService.Providers
|
||||
}
|
||||
|
||||
//
|
||||
// Check Permissions ...
|
||||
isValid = await HasPermission(
|
||||
id: id,
|
||||
userInfo: userInfo
|
||||
);
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Entity ...
|
||||
var entity = await FileRepository.GetAsync(id);
|
||||
if (entity.IsNullOrDefault())
|
||||
{
|
||||
@@ -845,9 +1327,11 @@ namespace xFileService.Providers
|
||||
{
|
||||
nameof(XFile.Id),
|
||||
nameof(XFile.Deleted),
|
||||
nameof(XFileDto.Owner)
|
||||
nameof(XFileDto.Owner),
|
||||
nameof(XFile.References),
|
||||
}
|
||||
);
|
||||
entity.References = item.References.ToListString();
|
||||
if (entity.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidData.Throw();
|
||||
@@ -861,16 +1345,7 @@ namespace xFileService.Providers
|
||||
}
|
||||
|
||||
//
|
||||
// Validate Action ...
|
||||
isValid =
|
||||
entity.OwnerId == userInfo.UserId ||
|
||||
userInfo.Roles.Any(r => r.ToNormalString() == "admin".ToNormalString());
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Converts to Dto ...
|
||||
var result = await ToDto(entity);
|
||||
|
||||
//
|
||||
@@ -915,10 +1390,11 @@ namespace xFileService.Providers
|
||||
}
|
||||
|
||||
//
|
||||
// Validate Action ...
|
||||
isValid =
|
||||
result.OwnerId == userInfo.UserId ||
|
||||
userInfo.Roles.Any(r => r.ToNormalString() == "admin".ToNormalString());
|
||||
// Check Permissions ...
|
||||
isValid = await HasPermission(
|
||||
id: id,
|
||||
userInfo: userInfo
|
||||
);
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
@@ -938,6 +1414,10 @@ namespace xFileService.Providers
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Removing Tags ...
|
||||
await TagProvider.RemoveTagsFor(result.Id);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user