PageRenderTime 31ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/app/protected/modules/zurmo/utils/security/PoliciesFormUtil.php

https://bitbucket.org/zurmo/zurmo/
PHP | 229 lines | 118 code | 10 blank | 101 comment | 9 complexity | 7e315365622e25c6f2a25ae8ca0b6e99 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-2-Clause
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2015 Zurmo Inc.
  5. *
  6. * Zurmo is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * Zurmo is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
  24. * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
  25. *
  26. * The interactive user interfaces in original and modified versions
  27. * of this program must display Appropriate Legal Notices, as required under
  28. * Section 5 of the GNU Affero General Public License version 3.
  29. *
  30. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  31. * these Appropriate Legal Notices must retain the display of the Zurmo
  32. * logo and Zurmo copyright notice. If the display of the logo is not reasonably
  33. * feasible for technical reasons, the Appropriate Legal Notices must display the words
  34. * "Copyright Zurmo Inc. 2015. All rights reserved".
  35. ********************************************************************************/
  36. /**
  37. * Helper class to make a PoliciesForm
  38. * and populate the data attribute.
  39. */
  40. class PoliciesFormUtil
  41. {
  42. /**
  43. * @param $data - combined array of all policies
  44. * and existing policies on a permitable. Organized by module.
  45. * Example below:
  46. * @code
  47. <?php
  48. $data = array(
  49. 'UsersModule' => array(
  50. 'POLICY_ENFORCE_STRONG_PASSWORDS' => array(
  51. 'displayName' => UsersModule::POLICY_ENFORCE_STRONG_PASSWORDS,
  52. 'explicit' => Policy::YES,
  53. 'inherited' => null,
  54. ),
  55. 'POLICY_MINIMUM_PASSWORD_LENGTH' => array(
  56. 'displayName' => UsersModule::POLICY_MINIMUM_PASSWORD_LENGTH,
  57. 'explicit' => null,
  58. 'inherited' => null,
  59. ),
  60. 'POLICY_MINIMUM_USERNAME_LENGTH' => array(
  61. 'displayName' => UsersModule::POLICY_MINIMUM_USERNAME_LENGTH,
  62. 'explicit' => null,
  63. 'inherited' => null,
  64. ),
  65. 'POLICY_PASSWORD_EXPIRES' => array(
  66. 'displayName' => UsersModule::POLICY_PASSWORD_EXPIRES,
  67. 'explicit' => null,
  68. 'inherited' => Policy::YES,
  69. ),
  70. 'POLICY_PASSWORD_EXPIRY_DAYS' => array(
  71. 'displayName' => UsersModule::POLICY_PASSWORD_EXPIRY_DAYS,
  72. 'explicit' => null,
  73. 'inherited' => 15,
  74. ),
  75. ),
  76. );
  77. ?>
  78. * @endcode
  79. */
  80. public static function makeFormFromPoliciesData($data)
  81. {
  82. assert('is_array($data)');
  83. $form = new PoliciesForm();
  84. $form->data = $data;
  85. return $form;
  86. }
  87. /**
  88. * Set permitable policies from post
  89. * @return boolean - true on success
  90. */
  91. public static function setPoliciesFromCastedPost(array $validatedAndCastedPostData, $permitable)
  92. {
  93. assert('$permitable instanceof Permitable');
  94. assert('$permitable->id > 0');
  95. foreach ($validatedAndCastedPostData as $concatenatedIndex => $value)
  96. {
  97. $moduleClassName = self::getModuleClassNameFromPostConcatenatedIndexString($concatenatedIndex);
  98. $policy = self::getPolicyFromPostConcatenatedIndexString($concatenatedIndex);
  99. $saved = self::AddorRemoveSpecificPolicy(
  100. $moduleClassName,
  101. $permitable,
  102. $policy,
  103. $value);
  104. if (!$saved)
  105. {
  106. return false;
  107. }
  108. }
  109. return true;
  110. }
  111. /**
  112. * @return $moduleClassName string
  113. */
  114. protected static function getModuleClassNameFromPostConcatenatedIndexString($string)
  115. {
  116. assert('is_string($string)');
  117. $nameParts = explode(FormModelUtil::DELIMITER, $string);
  118. list($moduleClassName, $policy) = $nameParts;
  119. return $moduleClassName;
  120. }
  121. /**
  122. * @return policy integer
  123. */
  124. protected static function getPolicyFromPostConcatenatedIndexString($string)
  125. {
  126. assert('is_string($string)');
  127. $nameParts = explode(FormModelUtil::DELIMITER, $string);
  128. list($moduleClassName, $policy) = $nameParts;
  129. return constant($moduleClassName . '::' . $policy);
  130. }
  131. /**
  132. * @return policy id string
  133. */
  134. protected static function getPolicyIdFromPostConcatenatedIndexString($string)
  135. {
  136. assert('is_string($string)');
  137. $nameParts = explode(FormModelUtil::DELIMITER, $string);
  138. list($moduleClassName, $policyId) = $nameParts;
  139. return $policyId;
  140. }
  141. /**
  142. * @return type string
  143. */
  144. protected static function getTypeFromPostConcatenatedIndexString($string)
  145. {
  146. assert('is_string($string)');
  147. $nameParts = explode(FormModelUtil::DELIMITER, $string);
  148. list($moduleClassName, $policy, $type) = $nameParts;
  149. return $type;
  150. }
  151. protected static function AddorRemoveSpecificPolicy($moduleClassName, $permitable, $policy, $value)
  152. {
  153. assert('is_string($moduleClassName)');
  154. assert('$permitable instanceof Permitable');
  155. assert('$permitable->id > 0');
  156. assert('is_string($policy)');
  157. assert('is_int($value) || $value == null || $value == ""');
  158. if (!empty($value))
  159. {
  160. $permitable->setPolicy ($moduleClassName, $policy, $value);
  161. }
  162. else
  163. {
  164. $permitable->removePolicy($moduleClassName, $policy);
  165. }
  166. $saved = $permitable->save();
  167. return $saved;
  168. }
  169. /**
  170. * @param PoliciesForm $form
  171. * @param array $validatedAndCastedPostData
  172. * @return PoliciesForm
  173. * @throws NotSupportedException
  174. */
  175. public static function loadFormFromCastedPost(PoliciesForm $form, array $validatedAndCastedPostData)
  176. {
  177. $delimiter = FormModelUtil::DELIMITER;
  178. foreach ($validatedAndCastedPostData as $concatenatedIndex => $value)
  179. {
  180. $concatenatedIndex = $form::resolveNameForDelimiterSplit($concatenatedIndex, $delimiter);
  181. $moduleClassName = self::getModuleClassNameFromPostConcatenatedIndexString($concatenatedIndex);
  182. $policyId = self::getPolicyIdFromPostConcatenatedIndexString($concatenatedIndex);
  183. $type = self::getTypeFromPostConcatenatedIndexString($concatenatedIndex);
  184. if ($value == '')
  185. {
  186. $value = null;
  187. }
  188. if ($type == 'helper')
  189. {
  190. $form->data[$moduleClassName][$policyId]['helper'] = $value;
  191. }
  192. elseif ($type == null)
  193. {
  194. $form->data[$moduleClassName][$policyId]['explicit'] = $value;
  195. }
  196. else
  197. {
  198. throw new NotSupportedException();
  199. }
  200. }
  201. return $form;
  202. }
  203. /**
  204. * Used to properly type cast incoming POST data
  205. */
  206. public static function typeCastPostData($postData)
  207. {
  208. assert('is_array($postData)');
  209. foreach ($postData as $concatenatedIndex => $value)
  210. {
  211. if ($value != '')
  212. {
  213. $postData[$concatenatedIndex] = intval($value);
  214. }
  215. }
  216. return $postData;
  217. }
  218. }
  219. ?>