private void ValidateDependency(IPackageDetails package, PackageDependency dependency) { SemanticVersion currentVersion; lock (_syncObj) { if (!_apiPackages.TryGetValue(dependency.Id, out currentVersion)) { return; } } var versionSpec = dependency.VersionSpec; var minVersion = versionSpec.MinVersion; if (minVersion != null) { if (versionSpec.IsMinInclusive && currentVersion < minVersion) { package.ValidationContext.Add(BusinessRuleValidationResult.CreateErrorWithTag(string.Format(_languageService.GetString("NuGetExplorer_ApiPackageRegistry_Validation_Error_Message_Pattern_1"), package.Id, dependency.Id, dependency.VersionSpec.MinVersion, currentVersion), ValidationTags.Api)); } if (!versionSpec.IsMinInclusive && currentVersion <= minVersion) { package.ValidationContext.Add(BusinessRuleValidationResult.CreateErrorWithTag(string.Format(_languageService.GetString("NuGetExplorer_ApiPackageRegistry_Validation_Error_Message_Pattern_2"), package.Id, dependency.Id, dependency.VersionSpec.MinVersion, currentVersion), ValidationTags.Api)); } } var maxVersion = versionSpec.MaxVersion; if (maxVersion == null) { return; } if (versionSpec.IsMaxInclusive && currentVersion > maxVersion) { package.ValidationContext.Add(BusinessRuleValidationResult.CreateErrorWithTag(string.Format(_languageService.GetString("NuGetExplorer_ApiPackageRegistry_Validation_Error_Message_Pattern_3"), package.Id, dependency.Id, dependency.VersionSpec.MaxVersion, currentVersion), ValidationTags.Api)); } if (!versionSpec.IsMaxInclusive && currentVersion >= maxVersion) { package.ValidationContext.Add(BusinessRuleValidationResult.CreateErrorWithTag(string.Format(_languageService.GetString("NuGetExplorer_ApiPackageRegistry_Validation_Error_Message_Pattern_4"), package.Id, dependency.Id, dependency.VersionSpec.MaxVersion, currentVersion), ValidationTags.Api)); } }
protected override Task InitializeAsync() { var context = new ValidationContext(); var result1 = BusinessRuleValidationResult.CreateErrorWithTag("Error1 message", "A"); var result2 = BusinessRuleValidationResult.CreateWarningWithTag("Warning1 message", "B"); var result3 = FieldValidationResult.CreateWarningWithTag("Property1", "Warning2 message", "C"); var tag = new { Name = "A", Line = (int?)2 }; var result4 = BusinessRuleValidationResult.CreateErrorWithTag("Error2 message with object tag", tag); var result5 = BusinessRuleValidationResult.CreateErrorWithTag("Error3 message", "B"); var result6 = BusinessRuleValidationResult.CreateError("Error3 message"); var result7 = BusinessRuleValidationResult.CreateError("Error4 message"); var result8 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning3 message", new { Name = "A", Line = 1 }); var result9 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning4 message", new { Name = "A", Line = 2 }); var result10 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning5 message", new { Name = "A", Line = 3, ColumnName = "ColA" }); var result11 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning6 message", new { Name = "A", Line = 20, ColumnIndex = 2 }); var result12 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning7 message", new { Name = "A", Line = 12, ColumnName = "ColC", ColumnIndex = 3 }); var result13 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning8 message", new { Name = "A", Line = 10 }); var result14 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning9 message", new { Name = "A", Line = 24 }); context.Add(result1); context.Add(result2); context.Add(result3); context.Add(result4); context.Add(result5); context.Add(result6); context.Add(result7); context.Add(result8); context.Add(result9); context.Add(result10); context.Add(result11); context.Add(result12); context.Add(result13); context.Add(result14); ValidationContext = context; return(base.InitializeAsync()); }
protected override Task InitializeAsync() { var context = new ValidationContext(); var result1 = BusinessRuleValidationResult.CreateErrorWithTag("Error1 message", "A"); var result2 = BusinessRuleValidationResult.CreateWarningWithTag("Warning1 message", "B"); var result3 = FieldValidationResult.CreateWarningWithTag("Property1", "Warning2 message", "C"); var tag = new { Name = "A", Line = new Nullable <int>(2) }; var result4 = BusinessRuleValidationResult.CreateErrorWithTag("Error2 message with object tag", tag); var result5 = BusinessRuleValidationResult.CreateErrorWithTag("Error3 message", "B"); var result6 = BusinessRuleValidationResult.CreateError("Error3 message"); var result7 = BusinessRuleValidationResult.CreateError("Error4 message"); var result8 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning3 message", new { Name = "A", Line = new Nullable <int>(1) }); var result9 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning4 message", new { Name = "A", Line = new Nullable <int>(2) }); var result10 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning5 message", new { Name = "A", Line = new Nullable <int>(3) }); var result11 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning6 message", new { Name = "A", Line = new Nullable <int>(20) }); var result12 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning7 message", new { Name = "A", Line = new Nullable <int>(12) }); var result13 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning8 message", new { Name = "A", Line = new Nullable <int>(10) }); var result14 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning9 message", new { Name = "A", Line = new Nullable <int>(24) }); context.AddBusinessRuleValidationResult(result1); context.AddBusinessRuleValidationResult(result2); context.AddFieldValidationResult(result3); context.AddBusinessRuleValidationResult(result4); context.AddBusinessRuleValidationResult(result5); context.AddBusinessRuleValidationResult(result6); context.AddBusinessRuleValidationResult(result7); context.AddFieldValidationResult(result8); context.AddFieldValidationResult(result9); context.AddFieldValidationResult(result10); context.AddFieldValidationResult(result11); context.AddFieldValidationResult(result12); context.AddFieldValidationResult(result13); context.AddFieldValidationResult(result14); ValidationContext = context; return(base.InitializeAsync()); }
/// <summary> /// Validates the license. /// </summary> /// <param name="license">The license key the user has given to be validated.</param> /// <returns>The validation context containing all the validation results.</returns> public IValidationContext ValidateLicense(string license) { Argument.IsNotNullOrWhitespace(() => license); var validationContext = new ValidationContext(); Log.Info("Validating license"); try { var licenseObject = License.Load(license); var failureList = licenseObject.Validate() .Signature(_applicationIdService.ApplicationId) .AssertValidLicense().ToList(); if (failureList.Count > 0) { foreach (var failure in failureList) { var businessRuleValidationResult = BusinessRuleValidationResult.CreateErrorWithTag(failure.Message, failure.HowToResolve); validationContext.AddBusinessRuleValidationResult(businessRuleValidationResult); } } var licenseAttributes = licenseObject.AdditionalAttributes; if (licenseAttributes != null) { foreach (var licenseAttribute in licenseAttributes.GetAll()) { if (string.Equals(licenseAttribute.Key, LicenseElements.MachineId)) { Log.Debug("Validating license using machine ID"); var machineLicenseValidationContext = _machineLicenseValidationService.Validate(licenseAttribute.Value); validationContext.SynchronizeWithContext(machineLicenseValidationContext, true); if (machineLicenseValidationContext.HasErrors) { Log.Error("The license can only run on machine with ID '{0}'", licenseAttribute.Value); } } // TODO: add additional attribute checks here } } // Also validate the xml, very important for expiration date and version var xmlValidationContext = ValidateXml(license); validationContext.SynchronizeWithContext(xmlValidationContext, true); } catch (Exception ex) { Log.Error(ex, "An error occurred while loading the license"); validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError("An unknown error occurred while loading the license, please contact support")); } finally { if (validationContext.GetErrors().Count > 0) { Log.Warning("License is not valid:"); Log.Indent(); foreach (var error in validationContext.GetErrors()) { Log.Warning("- {0}\n{1}", error.Message, error.Tag as string); } Log.Unindent(); } else { Log.Info("License is valid"); } } return(validationContext); }