PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/application/models/IncidentTable.php

https://bitbucket.org/openfisma-ondemand/openfisma
PHP | 396 lines | 321 code | 10 blank | 65 comment | 3 complexity | 66fc665b1c4192aa933db31cb054a833 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, GPL-3.0, Apache-2.0, EPL-1.0
  1. <?php
  2. /**
  3. * Copyright (c) 2008 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. * IncidentTable
  20. *
  21. * @uses Fisma_Doctrine_Table
  22. * @package Models
  23. * @copyright (c) Endeavor Systems, Inc. 2009 {@link http://www.endeavorsystems.com}
  24. * @author Josh Boyd <joshua.boyd@endeavorsystems.com>
  25. * @license http://www.openfisma.org/content/license GPLv3
  26. */
  27. class IncidentTable extends Fisma_Doctrine_Table implements Fisma_Search_Searchable
  28. {
  29. protected $_customLogicalNames = array(
  30. 'createdTs' => 'Created',
  31. 'modifiedTs' => 'Updated'
  32. );
  33. /**
  34. * Returns a query which matches all of the current user's viewable incidents
  35. *
  36. * @param User $user
  37. * @param Fisma_Zend_Acl $acl Optional, defaults to $user->acl()
  38. * @return Doctrine_Query
  39. */
  40. public function getUserIncidentQuery(User $user, Fisma_Zend_Acl $acl = null)
  41. {
  42. $incidentQuery = Doctrine_Query::create()
  43. ->from('Incident i');
  44. /*
  45. * A user can read *all* incidents if he has the "incident read" privilege. Otherwise, he is only allowed to
  46. * view those incidents for which he is an actor or an observer.
  47. */
  48. $acl = (isset($acl)) ? $acl : $user->acl();
  49. if (!$acl->hasPrivilegeForClass('read', 'Incident')) {
  50. $incidentQuery->leftJoin('i.Users u')
  51. ->where('u.id = ?', $user->id);
  52. }
  53. return $incidentQuery;
  54. }
  55. /**
  56. * Implement the interface for Searchable
  57. */
  58. public function getSearchableFields()
  59. {
  60. return array (
  61. 'id' => array(
  62. 'initiallyVisible' => true,
  63. 'label' => 'ID',
  64. 'sortable' => true,
  65. 'type' => 'integer',
  66. 'formatter' => 'Fisma.TableFormat.recordLink',
  67. 'formatterParameters' => array(
  68. 'prefix' => '/incident/view/id/'
  69. )
  70. ),
  71. 'incidentDate' => array(
  72. 'initiallyVisible' => false,
  73. 'sortable' => true,
  74. 'type' => 'date',
  75. 'formatter' => 'Fisma.TableFormat.formatDate',
  76. 'timezoneAbbrField' => 'incidentTimezone'
  77. ),
  78. 'incidentTime' => array(
  79. 'initiallyVisible' => false,
  80. 'sortable' => true,
  81. 'type' => 'datetime',
  82. 'formatter' => 'Fisma.TableFormat.formatTime',
  83. 'timezoneAbbrField' => 'incidentTimezone'
  84. ),
  85. 'reportTs' => array(
  86. 'initiallyVisible' => true,
  87. 'sortable' => true,
  88. 'type' => 'datetime',
  89. 'formatter' => 'datetime',
  90. 'timezoneAbbrField' => 'reportTz'
  91. ),
  92. 'reporter' => array(
  93. 'initiallyVisible' => true,
  94. 'label' => 'Reporter',
  95. 'join' => array(
  96. 'model' => 'User',
  97. 'relation' => 'ReportingUser',
  98. 'field' => 'displayName'
  99. ),
  100. 'sortable' => true,
  101. 'type' => 'text'
  102. ),
  103. 'status' => array(
  104. 'enumValues' => $this->getEnumValues('status'),
  105. 'initiallyVisible' => false,
  106. 'sortable' => true,
  107. 'type' => 'enum'
  108. ),
  109. 'pocUser' => array(
  110. 'initiallyVisible' => true,
  111. 'label' => 'Incident_Point_of_Contact',
  112. 'join' => array(
  113. 'model' => 'User',
  114. 'relation' => 'PointOfContact',
  115. 'field' => 'displayName'
  116. ),
  117. 'sortable' => true,
  118. 'type' => 'text'
  119. ),
  120. 'organization' => array(
  121. 'initiallyVisible' => true,
  122. 'label' => 'Organization/System',
  123. 'join' => array(
  124. 'model' => 'Organization',
  125. 'relation' => 'Organization',
  126. 'field' => 'nickname'
  127. ),
  128. 'sortable' => true,
  129. 'type' => 'text'
  130. ),
  131. 'parentOrganization' => array(
  132. 'initiallyVisible' => false,
  133. 'label' => 'Parent Organization/System',
  134. 'join' => array(
  135. 'model' => 'Organization',
  136. 'relation' => 'ParentOrganization',
  137. 'field' => 'nickname'
  138. ),
  139. 'sortable' => true,
  140. 'type' => 'text'
  141. ),
  142. 'additionalInfo' => array(
  143. 'initiallyVisible' => false,
  144. 'sortable' => true,
  145. 'type' => 'text'
  146. ),
  147. 'source' => array(
  148. 'initiallyVisible' => false,
  149. 'type' => 'text',
  150. 'sortable' => true
  151. ),
  152. 'severityLevel' => array(
  153. 'initiallyVisible' => true,
  154. 'type' => 'text',
  155. 'sortable' => true
  156. ),
  157. 'impact' => array(
  158. 'initiallyVisible' => false,
  159. 'sortable' => true,
  160. 'type' => 'text'
  161. ),
  162. 'category' => array(
  163. 'initiallyVisible' => false,
  164. 'label' => 'Category',
  165. 'join' => array(
  166. 'model' => 'IrCategory',
  167. 'relation' => 'Category.Category',
  168. 'field' => 'category'
  169. ),
  170. 'sortable' => true,
  171. 'type' => 'text'
  172. ),
  173. 'categoryName' => array(
  174. 'initiallyVisible' => false,
  175. 'label' => 'Category Name',
  176. 'join' => array(
  177. 'model' => 'IrCategory',
  178. 'relation' => 'Category.Category',
  179. 'field' => 'name'
  180. ),
  181. 'sortable' => true,
  182. 'type' => 'text'
  183. ),
  184. 'subcategory' => array(
  185. 'initiallyVisible' => false,
  186. 'label' => 'Subcategory',
  187. 'join' => array(
  188. 'model' => 'IrSubCategory',
  189. 'relation' => 'Category',
  190. 'field' => 'name'
  191. ),
  192. 'sortable' => true,
  193. 'type' => 'text'
  194. ),
  195. 'currentWorkflowName' => array(
  196. 'initiallyVisible' => false,
  197. 'label' => 'Workflow',
  198. 'sortable' => true,
  199. 'type' => 'text'
  200. ),
  201. 'modifiedTs' => array(
  202. 'initiallyVisible' => false,
  203. 'sortable' => true,
  204. 'type' => 'datetime',
  205. 'formatter' => 'datetime'
  206. ),
  207. 'piiInvolved' => array(
  208. 'enumValues' => $this->getEnumValues('piiInvolved'),
  209. 'initiallyVisible' => true,
  210. 'sortable' => true,
  211. 'type' => 'enum'
  212. ),
  213. 'piiAdditional' => array(
  214. 'initiallyVisible' => false,
  215. 'sortable' => false,
  216. 'type' => 'text'
  217. ),
  218. 'piiMobileMedia' => array(
  219. 'initiallyVisible' => false,
  220. 'sortable' => true,
  221. 'enumValues' => $this->getEnumValues('piiMobileMedia'),
  222. 'type' => 'enum'
  223. ),
  224. 'piiMobileMediaType' => array(
  225. 'initiallyVisible' => false,
  226. 'sortable' => true,
  227. 'type' => 'text'
  228. ),
  229. 'piiEncrypted' => array(
  230. 'initiallyVisible' => false,
  231. 'sortable' => true,
  232. 'enumValues' => $this->getEnumValues('piiEncrypted'),
  233. 'type' => 'enum'
  234. ),
  235. 'piiAuthoritiesContacted' => array(
  236. 'initiallyVisible' => false,
  237. 'sortable' => true,
  238. 'enumValues' => $this->getEnumValues('piiAuthoritiesContacted'),
  239. 'type' => 'enum'
  240. ),
  241. 'piiPoliceReport' => array(
  242. 'initiallyVisible' => false,
  243. 'sortable' => true,
  244. 'enumValues' => $this->getEnumValues('piiPoliceReport'),
  245. 'type' => 'enum'
  246. ),
  247. 'piiIndividualsCount' => array(
  248. 'initiallyVisible' => false,
  249. 'type' => 'integer'
  250. ),
  251. 'piiIndividualsNotified' => array(
  252. 'initiallyVisible' => false,
  253. 'sortable' => true,
  254. 'enumValues' => $this->getEnumValues('piiIndividualsNotified'),
  255. 'type' => 'enum'
  256. ),
  257. 'piiShipment' => array(
  258. 'initiallyVisible' => false,
  259. 'sortable' => true,
  260. 'enumValues' => $this->getEnumValues('piiShipment'),
  261. 'type' => 'enum'
  262. ),
  263. 'piiShipmentSenderContacted' => array(
  264. 'initiallyVisible' => false,
  265. 'sortable' => true,
  266. 'enumValues' => $this->getEnumValues('piiShipmentSenderContacted'),
  267. 'type' => 'enum'
  268. ),
  269. 'piiShipmentSenderCompany' => array(
  270. 'initiallyVisible' => false,
  271. 'sortable' => true,
  272. 'type' => 'text'
  273. ),
  274. 'piiShipmentTimeline' => array(
  275. 'initiallyVisible' => false,
  276. 'sortable' => false,
  277. 'type' => 'text'
  278. ),
  279. 'piiShipmentTrackingNumbers' => array(
  280. 'initiallyVisible' => false,
  281. 'sortable' => true,
  282. 'type' => 'text'
  283. ),
  284. 'hostAdditional' => array(
  285. 'initiallyVisible' => false,
  286. 'label' => 'Asset Description',
  287. 'sortable' => false,
  288. 'type' => 'text'
  289. ),
  290. 'actionsTaken' => array(
  291. 'initiallyVisible' => false,
  292. 'sortable' => false,
  293. 'type' => 'text'
  294. ),
  295. 'jsonComments' => array(
  296. 'initiallyVisible' => false,
  297. 'label' => 'Comments',
  298. 'sortable' => false,
  299. 'type' => 'text',
  300. 'formatter' => 'Fisma.TableFormat.formatComments'
  301. ),
  302. 'closedTs' => array(
  303. 'initiallyVisible' => false,
  304. 'sortable' => true,
  305. 'type' => 'datetime',
  306. 'formatter' => 'datetime'
  307. ),
  308. 'reportingUserId' => array(
  309. 'initiallyVisible' => false,
  310. 'type' => 'integer',
  311. 'hidden' => true
  312. ),
  313. 'reportTz' => array(
  314. 'initiallyVisible' => false,
  315. 'type' => 'text',
  316. 'hidden' => true,
  317. 'sortable' => false
  318. ),
  319. 'incidentTimezone' => array(
  320. 'initiallyVisible' => false,
  321. 'type' => 'text',
  322. 'hidden' => true,
  323. 'sortable' => false
  324. )
  325. );
  326. }
  327. /**
  328. * Return a list of fields which are used for access control
  329. *
  330. * @return array
  331. */
  332. public function getAclFields()
  333. {
  334. if (CurrentUser::getInstance()->acl()->hasPrivilegeForClass('read', 'Incident')) {
  335. // If the user has the privilege to view all incidents, then no ACL is required.
  336. return array();
  337. } else {
  338. // Otherwise use the IrIncidentUser join table to determine access rights
  339. return array('id' => 'IncidentTable::getIncidentIds');
  340. }
  341. }
  342. /**
  343. * Provide ID list for ACL filter
  344. *
  345. * @return array
  346. * @deprecated pending on the removal of executions from model classes
  347. */
  348. static function getIncidentIds($incidentAccessQuery = null)
  349. {
  350. $incidentAccessQuery = (isset($incidentAccessQuery)) ? $incidentAccessQuery : self::getIncidentIdsQuery();
  351. $results = $incidentAccessQuery->execute();
  352. $incidentIds = array_keys($results);
  353. return $incidentIds;
  354. }
  355. /**
  356. * Build the query for getIncidentIds
  357. *
  358. * @return Doctrine_Query
  359. */
  360. static function getIncidentIdsQuery()
  361. {
  362. $currentUser = CurrentUser::getInstance();
  363. $incidentAccessQuery = Doctrine_Query::create()
  364. ->select('incidentId')
  365. ->from('IrIncidentUser INDEXBY incidentId')
  366. ->where('userId = ?', $currentUser->id)
  367. ->setHydrationMode(Doctrine::HYDRATE_ARRAY);
  368. return $incidentAccessQuery;
  369. }
  370. /**
  371. * Return the query to fetch one attachment (if any) from an incident
  372. *
  373. * @param int $incidentId The id of the Incident to get
  374. * @param int $attachmentId The id of the Attachment to get
  375. *
  376. * @return Doctrine_Query
  377. */
  378. public static function getAttachmentQuery($incidentId, $attachmentId)
  379. {
  380. return Doctrine_Query::create()
  381. ->from('Incident i')
  382. ->leftJoin('i.Attachments a')
  383. ->where('i.id = ?', $incidentId)
  384. ->andWhere('a.id = ?', $attachmentId);
  385. }
  386. }