From 147bb5476bbfe589f2d04206d7dc1e00017ee829 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 25 Aug 2013 20:50:13 -0700 Subject: [PATCH] added input validation to quality profiles --- .../Qualities/QualityProfileModule.cs | 15 +- .../Profile/EditQualityProfileTemplate.html | 20 +-- .../Quality/Profile/EditQualityProfileView.js | 129 +++++++++--------- 3 files changed, 85 insertions(+), 79 deletions(-) diff --git a/NzbDrone.Api/Qualities/QualityProfileModule.cs b/NzbDrone.Api/Qualities/QualityProfileModule.cs index b21a8fb85..fd8c6dbd0 100644 --- a/NzbDrone.Api/Qualities/QualityProfileModule.cs +++ b/NzbDrone.Api/Qualities/QualityProfileModule.cs @@ -1,20 +1,11 @@ using System.Collections.Generic; -using NzbDrone.Core.Datastore; using NzbDrone.Core.Qualities; using NzbDrone.Api.Mapping; using System.Linq; +using FluentValidation; namespace NzbDrone.Api.Qualities { - - public static class LazyLoadedExtensions - { - public static IEnumerable GetForeignKeys(this IEnumerable models) - { - return models.Select(c => c.Id).Distinct(); - } - } - public class QualityProfileModule : NzbDroneRestModule { private readonly QualityProfileService _qualityProfileService; @@ -24,6 +15,10 @@ public QualityProfileModule(QualityProfileService qualityProfileService) { _qualityProfileService = qualityProfileService; + SharedValidator.RuleFor(c => c.Name).NotEmpty(); + SharedValidator.RuleFor(c => c.Cutoff).NotNull(); + SharedValidator.RuleFor(c => c.Allowed).NotEmpty(); + GetResourceAll = GetAll; GetResourceById = GetById; diff --git a/UI/Settings/Quality/Profile/EditQualityProfileTemplate.html b/UI/Settings/Quality/Profile/EditQualityProfileTemplate.html index c66bf2abb..021a494d7 100644 --- a/UI/Settings/Quality/Profile/EditQualityProfileTemplate.html +++ b/UI/Settings/Quality/Profile/EditQualityProfileTemplate.html @@ -20,9 +20,9 @@
- {{#each allowed}} - + {{/each}} @@ -41,12 +41,16 @@
-

Allowed

- +
+
+

Allowed

+ +
+
diff --git a/UI/Settings/Quality/Profile/EditQualityProfileView.js b/UI/Settings/Quality/Profile/EditQualityProfileView.js index 9cfa23926..52c275144 100644 --- a/UI/Settings/Quality/Profile/EditQualityProfileView.js +++ b/UI/Settings/Quality/Profile/EditQualityProfileView.js @@ -1,74 +1,81 @@ 'use strict'; -define(['app', 'marionette', 'Mixins/AsModelBoundView'], function (App, Marionette, AsModelBoundView) { +define( + [ + 'app', + 'marionette', + 'Mixins/AsModelBoundView', + 'Mixins/AsValidatedView' + ], function (App, Marionette, AsModelBoundView, AsValidatedView) { - var view = Marionette.ItemView.extend({ - template: 'Settings/Quality/Profile/EditQualityProfileTemplate', + var view = Marionette.ItemView.extend({ + template: 'Settings/Quality/Profile/EditQualityProfileTemplate', - ui: { - cutoff: '.x-cutoff' - }, + ui: { + cutoff: '.x-cutoff' + }, - events: { - 'click .x-save' : '_saveQualityProfile', - 'dblclick .x-available-list': '_moveQuality', - 'dblclick .x-allowed-list' : '_moveQuality' - }, + events: { + 'click .x-save' : '_saveQualityProfile', + 'dblclick .x-available-list': '_moveQuality', + 'dblclick .x-allowed-list' : '_moveQuality' + }, - initialize: function (options) { - this.profileCollection = options.profileCollection; - }, + initialize: function (options) { + this.profileCollection = options.profileCollection; + }, - _moveQuality: function (event) { + _moveQuality: function (event) { - var quality; - var qualityId = event.target.value; - var availableCollection = new Backbone.Collection(this.model.get('available')); - availableCollection.comparator = function (model) { - return model.get('weight'); - }; + var quality; + var qualityId = event.target.value; + var availableCollection = new Backbone.Collection(this.model.get('available')); + availableCollection.comparator = function (model) { + return model.get('weight'); + }; - var allowedCollection = new Backbone.Collection(this.model.get('allowed')); - allowedCollection.comparator = function (model) { - return model.get('weight'); - }; + var allowedCollection = new Backbone.Collection(this.model.get('allowed')); + allowedCollection.comparator = function (model) { + return model.get('weight'); + }; - if (availableCollection.get(qualityId)) { - quality = availableCollection.get(qualityId); - availableCollection.remove(quality); - allowedCollection.add(quality); + if (availableCollection.get(qualityId)) { + quality = availableCollection.get(qualityId); + availableCollection.remove(quality); + allowedCollection.add(quality); + } + else if (allowedCollection.get(qualityId)) { + quality = allowedCollection.get(qualityId); + + allowedCollection.remove(quality); + availableCollection.add(quality); + } + else { + throw 'couldnt find quality id ' + qualityId; + } + + this.model.set('available', availableCollection.toJSON()); + this.model.set('allowed', allowedCollection.toJSON()); + + this.render(); + }, + + _saveQualityProfile: function () { + var self = this; + var cutoff = _.findWhere(this.model.get('allowed'), { id: parseInt(this.ui.cutoff.val())}); + this.model.set('cutoff', cutoff); + + var promise = this.model.save(); + + if (promise) { + promise.done(function () { + self.profileCollection.add(self.model, { merge: true }); + App.vent.trigger(App.Commands.CloseModalCommand); + }); + } } - else if (allowedCollection.get(qualityId)) { - quality = allowedCollection.get(qualityId); + }); - allowedCollection.remove(quality); - availableCollection.add(quality); - } - else { - throw 'couldnt find quality id ' + qualityId; - } + AsValidatedView.call(view); + return AsModelBoundView.call(view); - this.model.set('available', availableCollection.toJSON()); - this.model.set('allowed', allowedCollection.toJSON()); - - this.render(); - }, - - _saveQualityProfile: function () { - var self = this; - var cutoff = _.findWhere(this.model.get('allowed'), { id: parseInt(this.ui.cutoff.val())}); - this.model.set('cutoff', cutoff); - - var promise = this.model.save(); - - if (promise) { - promise.done(function () { - self.profileCollection.add(self.model, { merge: true }); - App.vent.trigger(App.Commands.CloseModalCommand); - }); - } - } }); - - return AsModelBoundView.call(view); - -});