diff --git a/guacamole/src/main/webapp/app/list/types/FilterPattern.js b/guacamole/src/main/webapp/app/list/types/FilterPattern.js index 61192b84e..fa42e2d0b 100644 --- a/guacamole/src/main/webapp/app/list/types/FilterPattern.js +++ b/guacamole/src/main/webapp/app/list/types/FilterPattern.js @@ -28,6 +28,8 @@ angular.module('list').factory('FilterPattern', ['$injector', // Required types var FilterToken = $injector.get('FilterToken'); + var IPv4Network = $injector.get('IPv4Network'); + var IPv6Network = $injector.get('IPv6Network'); // Required services var $parse = $injector.get('$parse'); @@ -75,6 +77,99 @@ angular.module('list').factory('FilterPattern', ['$injector', getters.push($parse(expression)); }); + /** + * Determines whether the given object contains properties that match + * the given string, according to the provided getters. + * + * @param {Object} object + * The object to match against. + * + * @param {String} str + * The string to match. + * + * @returns {Boolean} + * true if the object matches the given string, false otherwise. + */ + var matchesString = function matchesString(object, str) { + + // For each defined getter + for (var i=0; i < getters.length; i++) { + + // Retrieve value of current getter + var value = getters[i](object); + + // If the value matches the pattern, the whole object matches + if (String(value).toLowerCase().indexOf(str) !== -1) + return true; + + } + + // No matches found + return false; + + }; + + /** + * Determines whether the given object contains properties that match + * the given IPv4 network, according to the provided getters. + * + * @param {Object} object + * The object to match against. + * + * @param {IPv4Network} network + * The IPv4 network to match. + * + * @returns {Boolean} + * true if the object matches the given network, false otherwise. + */ + var matchesIPv4 = function matchesIPv4(object, network) { + + // For each defined getter + for (var i=0; i < getters.length; i++) { + + // Test value against IPv4 network + var value = IPv4Network.parse(String(getters[i](object))); + if (value && network.contains(value)) + return true; + + } + + // No matches found + return false; + + }; + + /** + * Determines whether the given object contains properties that match + * the given IPv6 network, according to the provided getters. + * + * @param {Object} object + * The object to match against. + * + * @param {IPv6Network} network + * The IPv6 network to match. + * + * @returns {Boolean} + * true if the object matches the given network, false otherwise. + */ + var matchesIPv6 = function matchesIPv6(object, network) { + + // For each defined getter + for (var i=0; i < getters.length; i++) { + + // Test value against IPv6 network + var value = IPv6Network.parse(String(getters[i](object))); + if (value && network.contains(value)) + return true; + + } + + // No matches found + return false; + + }; + + /** * Determines whether the given object matches the given filter pattern * token. @@ -89,27 +184,29 @@ angular.module('list').factory('FilterPattern', ['$injector', * @returns {Boolean} * true if the object matches the token, false otherwise. */ - var matchesToken = function matchToken(object, token) { + var matchesToken = function matchesToken(object, token) { - // Only match against literals - if (token.type !== 'LITERAL') - return false; + // Match depending on token type + switch (token.type) { - // For each defined getter - for (var i=0; i < getters.length; i++) { + // Simple string literal + case 'LITERAL': + return matchesString(object, token.value); - // Retrieve value of current getter - var value = getters[i](object); + // IPv4 network address / subnet + case 'IPV4_NETWORK': + return matchesIPv4(object, token.value); - // If the value matches the pattern, the whole object matches - if (String(value).toLowerCase().indexOf(token.value) !== -1) - return true; + // IPv6 network address / subnet + case 'IPV6_NETWORK': + return matchesIPv6(object, token.value); + + // Unsupported token type + default: + return false; } - // No matches found - return false; - }; /**