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

pull/6/head
Mark McDowall 11 years ago
parent ff865f56d0
commit 0951e0c74b

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

@ -682,9 +682,11 @@
<Compile Include="Update\UpdatePackageProvider.cs" /> <Compile Include="Update\UpdatePackageProvider.cs" />
<Compile Include="Update\UpdatePackage.cs" /> <Compile Include="Update\UpdatePackage.cs" />
<Compile Include="Update\UpdateCheckService.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\DroneFactoryValidator.cs" />
<Compile Include="Validation\Paths\PathValidator.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\Paths\PathExistsValidator.cs" />
<Compile Include="Validation\FolderValidator.cs" /> <Compile Include="Validation\FolderValidator.cs" />
<Compile Include="Validation\RuleBuilderExtensions.cs" /> <Compile Include="Validation\RuleBuilderExtensions.cs" />

@ -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));
}
}
}

@ -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));
}
}
}

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

Loading…
Cancel
Save