/application/migrations/030200/Privilege.php

https://bitbucket.org/khuongduybui/openfisma · PHP · 191 lines · 156 code · 10 blank · 25 comment · 4 complexity · c421e8e57684bc349666f2911561ec71 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright (c) 2013 Endeavor Systems, Inc.
  4. *
  5. * This file is part of OpenFISMA.
  6. *
  7. * OpenFISMA is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
  9. * version.
  10. *
  11. * OpenFISMA is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  12. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with OpenFISMA. If not, see
  16. * {@link http://www.gnu.org/licenses/}.
  17. */
  18. /**
  19. * @author Andrew Reeves <andrew.reeves@endeavorsystems.com>
  20. * @copyright (c) Endeavor Systems, Inc. 2013 {@link http://www.endeavorsystems.com}
  21. * @license http://www.openfisma.org/content/license GPLv3
  22. * @package Migration
  23. */
  24. class Application_Migration_030200_Privilege extends Fisma_Migration_Abstract
  25. {
  26. protected function _updatePrivileges()
  27. {
  28. $this->getHelper()->exec(
  29. 'DELETE FROM role_privilege WHERE privilegeid IN (SELECT id FROM privilege WHERE resource = ?)',
  30. array('poc')
  31. );
  32. $this->getHelper()->exec(
  33. 'DELETE FROM privilege WHERE resource = ?',
  34. array('poc')
  35. );
  36. $this->getHelper()->exec(
  37. 'DELETE FROM role_privilege WHERE privilegeid IN (SELECT id FROM privilege WHERE action = ?)',
  38. array('manage_poc_list')
  39. );
  40. $this->getHelper()->exec(
  41. 'DELETE FROM privilege WHERE action = ?',
  42. array('manage_poc_list')
  43. );
  44. $this->getHelper()->insert('privilege', array('resource' => 'incident', 'action' => 'comment'));
  45. $this->getHelper()->insert('privilege', array('resource' => 'incident', 'action' => 'delete'));
  46. $this->getHelper()->insert(
  47. 'privilege',
  48. array('resource' => 'incident', 'action' => 'manage_response_strategies')
  49. );
  50. $findingUpdate = $this->getHelper()->insert('privilege', array('resource' => 'finding', 'action' => 'update'));
  51. $this->getHelper()->exec(
  52. 'INSERT into role_privilege (roleid, privilegeid) (' .
  53. 'SELECT DISTINCT roleid, ' . $findingUpdate . ' from role_privilege WHERE privilegeid IN (' .
  54. 'SELECT id FROM privilege WHERE resource = ? AND (' .
  55. 'action like ? OR action like ?' .
  56. ')' .
  57. ')' .
  58. ');',
  59. array('finding', 'update_%', 'upload_evidence')
  60. );
  61. $this->getHelper()->dropForeignKeys('evaluation', 'evaluation_privilegeid_privilege_id');
  62. $this->getHelper()->exec(
  63. 'DELETE FROM role_privilege WHERE privilegeid IN (' .
  64. 'SELECT id FROM privilege WHERE resource = ? AND (' .
  65. 'action like ? OR action like ? OR action like ? OR action like ?' .
  66. ')' .
  67. ');',
  68. array('finding', 'update_%', 'upload_evidence', 'mitigation_%', 'evidence_%')
  69. );
  70. $this->getHelper()->exec(
  71. 'DELETE FROM privilege WHERE resource = ? AND (' .
  72. 'action like ? OR action like ? OR action like ? OR action like ?' .
  73. ')',
  74. array('finding', 'update_%', 'upload_evidence', 'mitigation_%', 'evidence_%')
  75. );
  76. $workflowManage =
  77. $this->getHelper()->insert('privilege', array('resource' => 'workflow', 'action' => 'manage'));
  78. $this->getHelper()->exec(
  79. 'INSERT into role_privilege (roleid, privilegeid) (' .
  80. 'SELECT DISTINCT roleid, ' . $workflowManage . ' from role_privilege WHERE privilegeid IN (' .
  81. 'SELECT id FROM privilege WHERE resource = ? AND action like ?' .
  82. ')' .
  83. ');',
  84. array('evaluation', 'create')
  85. );
  86. $this->getHelper()->exec(
  87. 'DELETE FROM role_privilege WHERE privilegeid IN (SELECT id FROM privilege WHERE resource = ?)',
  88. array('evaluation')
  89. );
  90. $this->getHelper()->exec(
  91. 'DELETE FROM privilege WHERE resource = ?',
  92. array('evaluation')
  93. );
  94. $this->getHelper()->exec(
  95. 'DELETE FROM role_privilege WHERE privilegeid IN (SELECT id FROM privilege WHERE resource = ?)',
  96. array('vulnerability_resolution')
  97. );
  98. $this->getHelper()->exec(
  99. 'DELETE FROM privilege WHERE resource = ?',
  100. array('vulnerability_resolution')
  101. );
  102. }
  103. protected function _updateRoles()
  104. {
  105. $now = self::now();
  106. $this->getHelper()->exec(
  107. 'UPDATE role SET name = ? WHERE nickname = ?',
  108. array('Administrator', 'ADMIN')
  109. );
  110. if (count($this->getHelper()->query('SELECT id FROM role WHERE name = ?', array('Administrator'))) === 0) {
  111. $this->getHelper()->insert(
  112. 'role',
  113. array(
  114. 'name' => 'Administrator',
  115. 'nickname' => 'ADMIN',
  116. 'modifiedts' => $now,
  117. 'createdts' => $now
  118. )
  119. );
  120. }
  121. if (count($this->getHelper()->query('SELECT id FROM role WHERE name = ?', array('Power User'))) === 0) {
  122. $this->getHelper()->insert(
  123. 'role',
  124. array(
  125. 'name' => 'User',
  126. 'nickname' => 'USER',
  127. 'modifiedts' => $now,
  128. 'createdts' => $now
  129. )
  130. );
  131. }
  132. if (count($this->getHelper()->query('SELECT id FROM role WHERE name = ?', array('Viewer'))) === 0) {
  133. $this->getHelper()->insert(
  134. 'role',
  135. array(
  136. 'name' => 'Viewer',
  137. 'nickname' => 'VIEWER',
  138. 'modifiedts' => $now,
  139. 'createdts' => $now
  140. )
  141. );
  142. }
  143. }
  144. protected function _assignPrivileges()
  145. {
  146. $builtinRoles = array('Administrator', 'Power User', 'Viewer');
  147. $inExpr = 'IN (' . implode(',', array_fill(0, count($builtinRoles), '?')) . ')';
  148. $this->getHelper()->exec(
  149. 'DELETE FROM role_privilege '
  150. . 'WHERE roleid IN ('
  151. . ' SELECT id FROM role WHERE name ' . $inExpr
  152. . ')',
  153. $builtinRoles
  154. );
  155. $fh = fopen(dirname(__FILE__) . '/builtin-roles.csv', 'r');
  156. while ($row = fgetcsv($fh)) {
  157. $this->getHelper()->exec(
  158. 'INSERT INTO role_privilege '
  159. . 'SELECT r.id, p.id '
  160. . 'FROM role r, privilege p '
  161. . 'WHERE r.name = ? AND p.resource = ? AND p.action = ?',
  162. $row
  163. );
  164. }
  165. fclose($fh);
  166. }
  167. /**
  168. * Migrate.
  169. */
  170. public function migrate()
  171. {
  172. $this->_updatePrivileges();
  173. $this->_updateRoles();
  174. $this->_assignPrivileges();
  175. $this->getHelper()->update(
  176. 'privilege',
  177. array('action' => 'manage_environments'),
  178. array('resource' => 'asset', 'action' => 'manage_service_tags')
  179. );
  180. }
  181. }