From 85c00a53d0baf445b7bff4a579de4a481e862172 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Tue, 7 Feb 2017 13:06:43 +0100 Subject: [PATCH] (mvc) add group selector field type, requirement for https://github.com/opnsense/core/issues/1377 --- .../Base/FieldTypes/AuthGroupField.php | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/AuthGroupField.php diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/AuthGroupField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/AuthGroupField.php new file mode 100644 index 000000000..935dcf6b4 --- /dev/null +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/AuthGroupField.php @@ -0,0 +1,132 @@ +internalMultiSelect = true; + } else { + $this->internalMultiSelect = false; + } + } + + /** + * generate validation data (list of certificates) + */ + public function eventPostLoading() + { + if (empty(self::$internalOptionList)) { + $cnf = Config::getInstance()->object(); + if (isset($cnf->system->group)) { + foreach ($cnf->system->group as $group) { + self::$internalOptionList[(string)$group->gid] = (string)$group->name; + } + } + } + } + + /** + * get valid options, descriptions and selected value + * @return array + */ + public function getNodeData() + { + $result = array (); + // if certificate is not required, add empty option + if (!$this->internalIsRequired) { + $result[""] = array("value" => gettext("none"), "selected" => 0); + } + + $groups = explode(',', $this->internalValue); + foreach (self::$internalOptionList as $optKey => $optValue) { + if (in_array($optKey, $groups)) { + $selected = 1; + } else { + $selected = 0; + } + $result[$optKey] = array("value" => $optValue, "selected" => $selected); + } + + return $result; + } + + /** + * retrieve field validators for this field type + * @return array returns InclusionIn validator + */ + public function getValidators() + { + $validators = parent::getValidators(); + if ($this->internalValue != null) { + if ($this->internalMultiSelect) { + // field may contain more than one cert + $validators[] = new CsvListValidator(array('message' => $this->internalValidationMessage, + 'domain'=>array_keys(self::$internalOptionList))); + } else { + // single group selection + $validators[] = new InclusionIn(array('message' => $this->internalValidationMessage, + 'domain'=>array_keys(self::$internalOptionList))); + } + } + return $validators; + } +} \ No newline at end of file