322 lines
9.5 KiB
C#
322 lines
9.5 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using xCommons.Extensions;
|
|
using xDataService.Models;
|
|
using xExceptions.Constants;
|
|
using xFileService.Extensions;
|
|
using xFileService.Interfaces;
|
|
using xFileService.Interfaces.Dtos;
|
|
using xFileService.Models.Dtos;
|
|
using xFileService.Models.Entities;
|
|
using xFileService.Providers.Dtos;
|
|
using xIdentityModels.Models;
|
|
using xTagService.Providers;
|
|
|
|
namespace xFileService.Providers
|
|
{
|
|
public class XFileProvider : XBaseProvidedHasTag<XFile, XFileDto, Guid, XFileDtoHub>, IXFileProvider
|
|
{
|
|
public XFileProvider(
|
|
IXFileTagProvider tagProvider,
|
|
IXFileServiceProvider provider
|
|
) : base(
|
|
provider: provider,
|
|
tagProvider: tagProvider,
|
|
permittedRoles: new string[] { "admin" }
|
|
)
|
|
{ }
|
|
|
|
public override bool IsOwned(
|
|
XFile item,
|
|
XUserClaimsInfoDto userInfo
|
|
)
|
|
{
|
|
//
|
|
var result =
|
|
!item.IsNullOrDefault() &&
|
|
!userInfo.IsNullOrDefault() &&
|
|
!item.OwnerId.IsNullOrEmpty() &&
|
|
userInfo.UserId == item.OwnerId;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
public override bool IsOwned(
|
|
XFileDto item,
|
|
XUserClaimsInfoDto userInfo
|
|
)
|
|
{
|
|
//
|
|
var result =
|
|
!item.IsNullOrDefault() &&
|
|
!userInfo.IsNullOrDefault() &&
|
|
!item.OwnerId.IsNullOrEmpty() &&
|
|
userInfo.UserId == item.OwnerId;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
#region Referenced Actions ...
|
|
/// <summary>
|
|
/// Get Reference Identifier for Specified Provider and Specified Item ...
|
|
/// </summary>
|
|
/// <param name="providedFor"></param>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <returns></returns>
|
|
public string GetIdentifier<TKey>(
|
|
string providedFor,
|
|
TKey forProvidedId
|
|
)
|
|
{
|
|
//
|
|
var result = $"{providedFor}_{forProvidedId}";
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Specified Item has Refernce to Provider ...
|
|
/// </summary>
|
|
/// <param name="providedFor"></param>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <param name="id"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> IsProvidedFor<TKey>(
|
|
string providedFor,
|
|
TKey forProvidedId,
|
|
Guid id,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid =
|
|
!id.IsNull() &&
|
|
!id.IsDefaultGuid() &&
|
|
!forProvidedId.IsNull() &&
|
|
!providedFor.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Exists ...
|
|
isValid = await IsExists(
|
|
id: id,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
var item = await Get(
|
|
id: id,
|
|
includeBuilder: null,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
isValid = !item.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
var references = item.GetReferences();
|
|
var result =
|
|
references.IsNull() &&
|
|
references.HasChild() &&
|
|
references.Any(r =>
|
|
r.ProvidedFor == providedFor &&
|
|
r.ReferencedTo == $"{forProvidedId}"
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Specified Reference to Specified Item ...
|
|
/// </summary>
|
|
/// <param name="providedFor"></param>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <param name="id"></param>
|
|
/// <param name="forIndex"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task AddReference<TKey>(
|
|
string providedFor,
|
|
TKey forProvidedId,
|
|
Guid id,
|
|
int forIndex = 0,
|
|
XUserClaimsInfoDto userInfo = null,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid =
|
|
!providedFor.IsNullOrEmpty() &&
|
|
!id.IsNull() &&
|
|
!id.IsDefaultGuid() &&
|
|
!providedFor.IsNull() &&
|
|
!userInfo.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Has Reference or not ...
|
|
isValid = await IsProvidedFor(
|
|
id: id,
|
|
providedFor: providedFor,
|
|
forProvidedId: forProvidedId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
if (isValid)
|
|
{
|
|
//
|
|
// Prevent Moving Forward Since Reference Exists ...
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Check Permissions ...
|
|
var item = await Get(
|
|
id: id,
|
|
includeBuilder: null,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
isValid =
|
|
!item.IsNullOrDefault() &&
|
|
(IsOwned(item, userInfo) ||
|
|
HasPermision(userInfo));
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
var reference = new XReference<string>
|
|
{
|
|
Index = forIndex,
|
|
ProvidedFor = providedFor,
|
|
ReferencedTo = $"{forProvidedId}"
|
|
};
|
|
var references = item.GetReferences();
|
|
references.Add(reference);
|
|
item = item.UpdateReferences(references);
|
|
item = await Update(
|
|
id: id,
|
|
item: item,
|
|
userInfo: userInfo,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove Specified Reference from Specified Item ...
|
|
/// </summary>
|
|
/// <param name="providedFor"></param>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <param name="id"></param>
|
|
/// <param name="forIndex"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task RemoveReference<TKey>(
|
|
string providedFor,
|
|
TKey forProvidedId,
|
|
Guid id,
|
|
int forIndex = 0,
|
|
XUserClaimsInfoDto userInfo = null,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid =
|
|
!id.IsNull() &&
|
|
!id.IsDefaultGuid() &&
|
|
!forProvidedId.IsNull() &&
|
|
!userInfo.IsNullOrDefault() &&
|
|
!providedFor.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Has Reference or not ...
|
|
isValid = await IsProvidedFor(
|
|
id: id,
|
|
providedFor: providedFor,
|
|
forProvidedId: forProvidedId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
if (!isValid)
|
|
{
|
|
//
|
|
// Prevent Moving Forward Since Reference not Exists ...
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Check Permissions ...
|
|
var item = await Get(
|
|
id: id,
|
|
includeBuilder: null,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
isValid =
|
|
!item.IsNullOrDefault() &&
|
|
(IsOwned(item, userInfo) ||
|
|
HasPermision(userInfo));
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
var identifier = GetIdentifier(
|
|
providedFor: providedFor,
|
|
forProvidedId: forProvidedId
|
|
);
|
|
var references = item.GetReferences();
|
|
references = references
|
|
.Where(r =>
|
|
r.Index != forIndex &&
|
|
r.ProvidedFor != providedFor &&
|
|
r.ReferencedTo != $"{forProvidedId}"
|
|
)
|
|
.ToList();
|
|
item = item.UpdateReferences(references);
|
|
|
|
//
|
|
item = await Update(
|
|
item: item,
|
|
id: item.Id,
|
|
userInfo: userInfo,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
#endregion
|
|
}
|
|
} |