Fixed: Prevent setting series folder to drone factory, root folder or another series' folder

This commit is contained in:
Mark McDowall 2014-04-04 08:18:07 -07:00
parent ff865f56d0
commit 0951e0c74b
5 changed files with 76 additions and 3 deletions

View File

@ -32,7 +32,13 @@ namespace NzbDrone.Api.Series
public SeriesModule(ICommandExecutor commandExecutor,
ISeriesService seriesService,
ISeriesStatisticsService seriesStatisticsService,
IMapCoversToLocal coverMapper)
IMapCoversToLocal coverMapper,
RootFolderValidator rootFolderValidator,
PathExistsValidator pathExistsValidator,
SeriesPathValidator seriesPathValidator,
SeriesExistsValidator seriesExistsValidator,
DroneFactoryValidator droneFactoryValidator
)
: base(commandExecutor)
{
_commandExecutor = commandExecutor;
@ -48,11 +54,18 @@ namespace NzbDrone.Api.Series
SharedValidator.RuleFor(s => s.QualityProfileId).ValidId();
PutValidator.RuleFor(s => s.Path).IsValidPath();
PutValidator.RuleFor(s => s.Path)
.Cascade(CascadeMode.StopOnFirstFailure)
.IsValidPath()
.SetValidator(rootFolderValidator)
.SetValidator(pathExistsValidator)
.SetValidator(seriesPathValidator)
.SetValidator(droneFactoryValidator);
PostValidator.RuleFor(s => s.Path).IsValidPath().When(s => String.IsNullOrEmpty(s.RootFolderPath));
PostValidator.RuleFor(s => s.RootFolderPath).IsValidPath().When(s => String.IsNullOrEmpty(s.Path));
PostValidator.RuleFor(s => s.Title).NotEmpty();
PostValidator.RuleFor(s => s.TvdbId).GreaterThan(0).SetValidator(seriesExistsValidator);
}
private SeriesResource GetSeries(int id)

View File

@ -682,9 +682,11 @@
<Compile Include="Update\UpdatePackageProvider.cs" />
<Compile Include="Update\UpdatePackage.cs" />
<Compile Include="Update\UpdateCheckService.cs" />
<Compile Include="Validation\Paths\SeriesExistsValidator.cs" />
<Compile Include="Validation\Paths\RootFolderValidator.cs" />
<Compile Include="Validation\Paths\DroneFactoryValidator.cs" />
<Compile Include="Validation\Paths\PathValidator.cs" />
<Compile Include="Validation\Paths\RootFolderValidator.cs" />
<Compile Include="Validation\Paths\SeriesPathValidator.cs" />
<Compile Include="Validation\Paths\PathExistsValidator.cs" />
<Compile Include="Validation\FolderValidator.cs" />
<Compile Include="Validation\RuleBuilderExtensions.cs" />

View File

@ -0,0 +1,26 @@
using System;
using FluentValidation.Validators;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Validation.Paths
{
public class SeriesExistsValidator : PropertyValidator
{
private readonly ISeriesService _seriesService;
public SeriesExistsValidator(ISeriesService seriesService)
: base("This series has already been added")
{
_seriesService = seriesService;
}
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue == null) return true;
var tvdbId = Convert.ToInt32(context.PropertyValue.ToString());
return (!_seriesService.GetAllSeries().Exists(s => s.TvdbId == tvdbId));
}
}
}

View File

@ -0,0 +1,30 @@
using FluentValidation.Validators;
using NzbDrone.Common;
using NzbDrone.Core.Tv;
using Omu.ValueInjecter;
namespace NzbDrone.Core.Validation.Paths
{
public class SeriesPathValidator : PropertyValidator
{
private readonly ISeriesService _seriesService;
public SeriesPathValidator(ISeriesService seriesService)
: base("Path is already configured for another series")
{
_seriesService = seriesService;
}
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue == null) return true;
var series = new Series();
series.InjectFrom(context.ParentContext.InstanceToValidate);
if (series.Id == 0) return true;
return (!_seriesService.GetAllSeries().Exists(s => s.Path.PathEquals(context.PropertyValue.ToString()) && s.Id != series.Id));
}
}
}

View File

@ -21,6 +21,7 @@
{{overview}}
</div>
</div>
{{#unless existing}}
<div class="row labels">
{{#unless path}}
<div class="span4">Path</div>
@ -29,6 +30,7 @@
<div class="span1 starting-season starting-season-label">Starting Season</div>
<div class="span2">Quality Profile</div>
</div>
{{/unless}}
<div class="row">
<form class="form-inline">
{{#if existing}}