Skip to content

post_helper

Francesco Molitierno edited this page Sep 5, 2019 · 1 revision

Api List of /usr/lib/lua/web/post_helper.lua

Here you can see some useful funcution

function M.handleQuery(mapParams, mapValidation)
--- Method to store the POST parameters sent by the UI in UCI (SAVE action)
-- @function [parent=#post_helper] handleQuery
-- @param #table mapParams key/string dictionary containing for each form control's name, the associated path
--                      this should be an exact path since we're going to write
--                      if you need to READ partial paths, please do so after this function has run
-- @param #table mapValidation key/function dictionary containing for each form control's name, the associated
--                      validation function. The validation function should return (err, msg). If err
--                      is nil, then msg should contain an error message, otherwise err should be true
-- @return #table,#table it returns a dictionary containing for each input name, the retrieved value from UCI
--          and another dictionary containing for each failed validation the help message

function M.mergeTables(content, toadd)
--- Merge two tables. Take the content of toadd and put it in content overwriting any existing element
-- @function [parent=#post_helper] mergeTables
-- @param #table content the main table
-- @param #table toadd the table to add to the main table
-- @return nothing but content is updated

function M.secondsToTime(time)
-- Construct an time string from the number of seconds
-- @param #number time
-- @return #string number of seconds

function M.secondsToTimeShort(time)
-- Construct an time string from the number of seconds
-- @param #number time
-- @return #string number of seconds

function M.handleTableQuery(columns, options, filter, defaultObject, mapValidation)
--- Method to handle queries generated by standard UI tables
-- @function [parent=#post_helper] handleTableQuery
-- @param #table columns array describing each column of the table
-- @param #table options table containing options for the table
--               canEdit - bool indicating if we should allow editing a line
--               canAddDelete - bool indicating if we should allow adding / removing lines
--               canApply - bool indicating if we should allow restart related module after uci changes
--               editing - int - index of the currently edited element (-1 if new element, 0 if not editing)
--               minEntries - int - minimum number of entries in the table (will prevent delete under this number)
--               maxEntries - int - maximum number of entries in the table (will prevent adding above this number)
--               tableid - string - id of the table
--               basepath - string - base path for parameters in transformer
--               stateid - string - token used to detect if changes happened since the page was displayed
--               errmsg - string - global error message (for the table) to display
--               sorted - string or function - sorted method for table data
-- @param filter function that accepts a line data as the input and returns true if it should be included
--               or false otherwise
-- @param #table defaultObject table or nil (transformer param name => value)
--               1) table that is merged with the data gathered from the form before being written
--               2) nil just use the data gathered from the form without change
-- @param #table mapValidation for each input name maps to a function that returns true if the value is "valid"
--               or returns false if the value is invalid
-- @return #table, #table

function M.validateNonEmptyString(value)
-- @function [parent=#post_helper] validateNonEmptyString
-- @param value
-- @return #boolean, #string

function M.validateStringIsIP(value)
-- @function [parent=#post_helper] validateStringIsIP
-- @param value
-- @return #boolean, #string

function M.validateStringIsMAC(value)
-- This function is used to validate MAC address format and reserved MAC address.
-- the [value] a valid MAC address format
-- the [value] is not the reserved MAC address based on the RFC7042
-- don't allow if mac address is in Mulicast or Unicast or IPv6 mulicast or PPP ranges.
-- @param value
-- @return true or nil+error message

function M.validateStringIsDomainName(value)
-- Check whether the received 'value' has the syntax of a domain name [RFC 1123]
-- @function [parent=#post_helper] validateStringIsDomainName
-- @param value
-- @return #boolean, #string

function M.validateBoolean(value)
-- @function [parent=#post_helper] validateBoolean
-- @param value
-- @return #boolean, #string

function M.validateStringIsPort(value,data,key)
-- Strips leading zeroes and whitespaces from port number and Validates it
-- If valid, Saves the port number to the corresponding input field in GUI
-- @function [parent=#post_helper] validateStringIsPort
-- @param value
-- @return #boolean, #string

function M.validateStringIsPortRange(value,data,key)
-- Strips leading zeroes and whitespaces from port numbers and Validates the port range
-- If valid, Saves the port range to the corresponding input field in GUI
-- @function [parent=#post_helper] validateStringIsPortRange
-- @param value
-- @return #boolean, #string

function M.validatePositiveNum(value)
-- @function [parent=#post_helper] validatePositiveNum
-- @param value
-- -- @return #boolean, #string

function M.getValidateNumberInRange(min, max)
-- Return a function that can be used to validate if the input is a number between min and max (inclusive)
-- If min is nil or max is nil, it won't check for it
-- @function [parent=#post_helper] getValidateNumberInRange
-- @param #number min
-- @param #number max
-- @return #boolean, #string

function M.getValidateWholeNumber(value)
-- Return a function that can be used to validate if the input is a whole number
-- @function [parent=#post_helper] getValidateWholeNumber
-- @param #number value
-- @return #boolean, #string

function M.validateRegExpire (value)
-- @function [parent=#post_helper] validateRegExpire
-- @param value
-- @param min
-- @param max
-- --@return #boolean, #string

function M.getValidateInCheckboxgroup(enum)
-- Return a function that can be used to validate if the given value/array is part of the choices
-- It also does some processing on the data to normalize it for use.
-- If only one checkbox is selected, then we don't get an array -> make an array of one element
-- @function [parent=#post_helper] getValidateInCheckboxgroup
-- @param #table enum array of entries of a select input
-- @return #boolean, #string

function M.getValidateCheckboxSwitch()
-- Return a function that can be used to validate if the checkbox for switch is checked or not,
-- If the checkbox is checked, then the corresponding value in the post array is set "1", otherwise "0"
-- @function [parent=#post_helper] getValidateInCheckboxgroup
-- @return #boolean, #string

function M.getValidateInEnumSelect(enum)
-- Return a function that can be used to validate if the given value is part of the choices
-- @function [parent=#post_helper] getValidateInEnumSelect
-- @param #table enum array of entries of a select input
-- @return #boolean, #string

function M.getValidateStringLength(length)
-- Return a function that can be used to validate if a string's length is greater or equal to length
-- @function [parent=#post_helper] getValidateStringLength
-- @param #number length minimum length of the string
-- @return #boolean, #string

function M.getValidateStringLengthInRange(minl, maxl)
-- Return a function that can be used to validate if the string length is between l1 and l2 (included)
-- @function [parent=#post_helper] getValidateStringLengthInRange
-- @param #number minl minimum length of the string
-- @param #number maxl maximum length of the string
-- @return #boolean, #string

function M.getValidationIfPropInList(func, prop, values)
-- Return a validation function that will be enabled only if a given property is present and otherwise return true
-- @function [parent=#post_helper] getValidationIfPropInList
-- @param validation function (prototype is of type (value, object)
-- @param #string prop name of the property used as a trigger
-- @param #table values array of values that should trigger the behavior
-- @return validation function

function M.getValidationIfCheckboxSwitchPropInList(func, prop, values)
-- Return a validation function that will be enabled only if a given checkboxswitch property is present and otherwise return true
-- @function [parent=#post_helper] getValidationIfPropInList
-- @param validation function (prototype is of type (value, object)
-- @param #string prop name of the property used as a trigger
-- @param #table values array of values that should trigger the behavior
-- @return validation function

function M.validateStringIsLeaseTime(value)
-- @function [parent=#post_helper] validateStringIsLeaseTime
-- @param value
-- @return #boolean, #string

function M.getValidationPassword(additionalvalid)
-- The object of this function is to not modify the password when we receive the predefined
-- dummy value ######**. If we do, we remove it from the post data so that this parameter
-- won't be written to transformer. You can pass an additional validation function that will
-- be called on a "modified" value to add for instance password strength check
-- @function [parent=#post_helper] getValidationPassword
-- @param #function additionalvalid
-- @return #function

function M.getOptionalValidation(additionalvalid)
-- This function will allow to only apply validation if the value is non empty / nil
-- This is useful in the case of an optional parameter that has to nonetheless follow
-- a certain format
-- @function [parent=#post_helper] getOptionalValidation
-- @param #function additionalvalid
-- @return #function

function M.getConditionalValidation(condition, istrue, isfalse)
-- This function will apply different validation function based on the outcome of the condition function
-- Helpful when needing to apply different validations / operations based on other elements
-- @function [parent=#post_helper] getConditionalValidation
-- @param #function condition the test function to decide which validation function to apply (uses the same prototype as validation function)
-- @param #function istrue validation function to apply if condition returns true (uses always true if not a function)
-- @param #function isfalse validation function to apply if condition returns false (uses always true if not a function)
-- @return #function

function M.getAndValidation(valid1, valid2)
-- This function uses 2 validation functions and will only return true
-- if both return true.
-- @function [parent=#post_helper] getAndValidation
-- @param #function valid1
-- @param #function valid2
-- @return #boolean, #string

function M.getOrValidation(valid1, valid2)
-- This function uses 2 validation functions and will only return true
-- if one of them returns true.
-- @function [parent=#post_helper] getAndValidation
-- @param #function valid1
-- @param #function valid2
-- @return #boolean, #string

function M.validatePSK(psk)
--- This function validates a WPA/WPA2 PSK key
-- It must be between 8 and 63 characters long and those characters must be ASCII printable (32-126)
-- or 64 hexa decimal values (0-9,a-f,A-F)
-- @param #string psk the PSK key to validate
-- @return #boolean, #string

local function validatePin8(pin)
--- Following the Wifi certificationw we need to check if the pin with 8 digits the last digit is the
-- the checksum of the others
-- @param #number the PIN code value
Note this is local (copy or make public)

function M.validateWPSPIN(value)
--- valide WPS pin code. Must be 4-8 digits (can have a space or - in the middle)
-- @param #string value the PIN code that was entered

function M.validateWEP(value)
--- check for WEP keys
-- 5,10,13 and 26 characters are allowed for the WEP key
-- 5 and 13 can contain ASCII characters
-- 10 and 26 can only contain Hexadecimal values
-- @param #string value the WEP key
-- @return #boolean, #string

function M.validateIPv4Netmask(value)
--- Check that the given value is a valid IPv4 netmask.
-- In particular it will check that the netmask falls in the
-- range of /8 and /30 (both inclusive) which is what makes
-- sense for DHCP pool configuration.
-- @param #string value The netmask in dotted decimal notation.
-- @treturn boolean True if it's a valid subnet mask.
-- @treturn number The number of bits in the host part of the subnet mask.
-- @error Error message.

function M.getValidateStringIsIPv4InNetwork(gw, nm)
--- This function returns a validator that will check that the provided value is an IPv4 in the same network
-- as the network based on the GW IP + Netmask
-- @param #string gw the gateway IP@ on the considered network
-- @param #string nm the netmask to use
-- @return true or nil+error message

function M.getValidateStringIsDeviceIPv4(gw, nm)
--- This function returns a validator that will check that the provided value is an IPv4 in the same network
-- as the network based on the GW IP + Netmask except the GW and forbidden IPs (broadcast & network identifier)
-- @param #string gw the gateway IP@ on the considered network
-- @param #string nm the netmask to use
-- @return true or nil+error message

function M.validateStringIsIPv6(value)
--- This function returns a validator that will check that the provided value is an IPv6 address
-- @param #string value the IPv6 Address
-- @return true or nil+error message

function M.isBroadcastAddress(ip, subnetMask)
-- validate the given ip/subnet is broadcast address or not

function M.advancedIPValidation(value, object, key)
--- This function is used for Local Device IP validation and NTP server validation. It is validating that:
-- if object["localdevmask"] is a valid netmask
-- the [value] a valid IPv4 address,
-- the [value] is not the broadcast or network address based on the network mask
-- don't allow if ip is in the CLASS A IP range 0.0.0.0/2, except the private 10.0.0.0/24 range
-- the [value] is not in the multicast range 224.0.0.0/4
-- the [value] is not in the limited broadcast destination address 255.255.255.255/32
-- @return true or nil+error message

function M.isPublicIP(value)
--- Check whether the given IPv4 address is a public address.
-- @string value The IP address to be validated.
-- @return true or nil+error message

function M.DNSIPValidation(ipaddr, dnsData)
--- is the given ip address valid as a DNS server IP in the given context
-- @param @tstring ipaddr the IP address
-- @param @ttable dnsData the localdev mask
-- @return true or nil+error message

function M.isWANIP(ipAddress, all_intfs)
--- Check whether the given IP address is in any WAN interface subnet range
-- @tstring ipAddress the IP address
-- @ttable all_intfs the all interfaces
-- @treturn true and interface name if the given IP address is in any WAN subnet range otherwise nil

function M.cidr2mask(cidr)
---This function converts a CIDR(Classless Inter-domain routing) notation to a subnet mask. Eg, convert 24 to 255.255.255.0
-- @param #string 'cidr' The CIDR notation number. Eg: "24"
-- @return #string network mask or nil+error message. Eg: "255.255.255.0"

function M.validateIPAndSubnet(ipTypeV4orV6)
-- This function returns a validator that check that the given value is valid IPv4 subnet or IPv6 subnet
-- @param #number ipTypeV4orV6 is IP address type. Eg:'4' denotes for IPv4 address type and '6' denotes for IPv6 address type
-- @param #string value User specified data entered for IPv4 or IPv6
-- @return true or nil+error message

function M.validateURL(url,proto)
-- validate the given url is valid or not

function M.validateQTN(value)
--Validate the given ip/mac is Quantenna.

function M.isNetworkAddress(ipAddress, subnetMask)
-- validate the given ip/subnet is network address or not

function M.getDefaultSubnetMask(ipAddress)
--- This function is used to get default subnet mask.
-- if user enters the [ipAddress] without subnet mask, get default mask.
-- the [ipAddress] is in the Class A IP range 10.0.0.0 to 127.255.255.255, then return default mask "8"
-- the [ipAddress] is in the Class B IP range 128.0.0.0 to 191.255.255.255, then return default mask "16"
-- the [ipAddress] is in the Class C IP range 192.0.0.0 to 223.255.255.255, then return default mask "24"
-- @return #default subnet mask, nil+error message

function M.getPossibleHostsInSubnet(subnetmask)
-- Calculate the number of effective hosts possible in the network with the given subnet mask.
-- @string subnetmask The subnet mask, in dotted-decimal notation.
-- @treturn number The number of effective hosts possible in the network with the given subnet mask.
-- @error Error message.

function M.staticLeaseIPValidation(value, object)
-- Validate the given IP address is not in the broadcast, multicast, loopback, reserved, gatewayip, network.
-- @string object The localdevIP and localdevmask.
-- @string value The IPv4 address.
-- @treturn true For valid IP address.
-- @error Error message.

function M.getRandomKey()
--Generate random key for new rule
--@return 16 digit random key.

function M.validateSSID(value)
--validates the SSID if both pattern matches then only the user able to apply changes
--if any of the pattern fails then it will show the corresponding Error messages

function M.populateAttenuation(attenuation, direction)
--- Formats the attenuation values for ADSL in the format "20.2 dB" (OR) "8.7 dB",
--- VDSL in the format "DS0 20.2 dB, DS1 53.6 dB, DS2 N/A (OR) US0 8.0 dB, US1 N/A, US2 41.6 dB"
-- @string attenuation the attenuation value
-- @string direction upstream or downstream direction
-- @treturn string the formatted string of attenuation values for VDSL/ADSL

function M.isUpgradeAllowed(upgradefw, userRole)
--- Is upgrade allowed or not
-- @param upgradefw upgradefw config value
-- @param userRole Role of the user
-- @return true if upgrade is allowed
-- @return nil if upgrade is not allowed

function M.isSpaceInString(value)
-- Is space allowed or not
-- @param value is a input string
-- @return true if no space in input string
-- @return nil, error message if space in input string