/application/modules/Core/models/Acl/Group/Service.php

https://github.com/ArchiCroc/Tower-Web-Platform · PHP · 326 lines · 185 code · 46 blank · 95 comment · 41 complexity · 348b28dee3386b763c8f97c7522ed27f MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright (c) 2011 Matthew Doll <mdoll at homenet.me>.
  4. *
  5. * This file is part of HomeNet.
  6. *
  7. * HomeNet is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * HomeNet is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with HomeNet. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * @package Core
  22. * @subpackage Acl_Group
  23. * @copyright Copyright (c) 2011 Matthew Doll <mdoll at homenet.me>.
  24. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
  25. */
  26. class Core_Model_Acl_Group_Service {
  27. /**
  28. * @var Core_Model_Acl_Group_MapperInterface
  29. */
  30. protected $_mapper;
  31. /**
  32. * @return Core_Model_Acl_Group_MapperInterface
  33. */
  34. public function getMapper() {
  35. if (empty($this->_mapper)) {
  36. $this->_mapper = new Core_Model_Acl_Group_MapperDbTable();
  37. }
  38. return $this->_mapper;
  39. }
  40. /**
  41. * @param Content_Model_Section_MapperInterface $mapper
  42. */
  43. public function setMapper(Core_Model_Acl_Group_MapperInterface $mapper) {
  44. $this->_mapper = $mapper;
  45. }
  46. /**
  47. * @param int $id Acl_Group Id
  48. * @return Core_Model_Acl_Group
  49. * @throws NotFoundException
  50. */
  51. public function getObjectById($id) {
  52. $result = $this->getMapper()->fetchObjectById($id);
  53. if (empty($result)) {
  54. throw new NotFoundException('Id: '.$id.' Not Found', 404);
  55. }
  56. return $result;
  57. }
  58. /**
  59. * @param type $group Group Id
  60. * @return Core_Model_Acl_Group[]
  61. */
  62. public function getObjectsByGroup($group) {
  63. if (!is_numeric($group)) {
  64. throw new InvalidArgumentException('Invalid Group');
  65. }
  66. return $this->getMapper()->fetchObjectsByGroup($group);
  67. }
  68. /**
  69. * @param array $groups Array of Group Ids
  70. * @return Core_Model_Acl_Group[]
  71. */
  72. public function getObjectsByGroups(array $groups) {
  73. if (count($groups) == 0) {
  74. throw new InvalidArgumentException('Groups array is empty');
  75. }
  76. $rows = $this->getMapper()->fetchObjectsByGroups($groups);
  77. $array = array();
  78. foreach ($rows as $row) {
  79. $array[$row->group][] = $row;
  80. }
  81. return $array;
  82. }
  83. /**
  84. * @param array $groups Array of Group Ids
  85. * @param string $module
  86. * @return Core_Model_Acl_Group[]
  87. */
  88. public function getObjectsByGroupsModule(array $groups, $module) {
  89. if (count($groups) == 0) {
  90. throw new InvalidArgumentException('Groups array is empty');
  91. }
  92. if (empty($module)) {
  93. throw new InvalidArgumentException('Invalid Module');
  94. }
  95. $rows = $this->getMapper()->fetchObjectsByGroupsModule($groups, $module);
  96. $array = array();
  97. foreach ($rows as $row) {
  98. $array[$row->group][] = $row;
  99. }
  100. return $array;
  101. }
  102. /**
  103. * @param array $groups Array of Group Ids
  104. * @param string $module
  105. * @param integer $collection
  106. * @return Core_Model_Acl_Group[]
  107. */
  108. public function getObjectsByGroupsModuleCollection(array $groups, $module, $collection) {
  109. if (count($groups) == 0) {
  110. throw new InvalidArgumentException('Groups array is empty');
  111. }
  112. if (empty($module)) {
  113. throw new InvalidArgumentException('Invalid Module');
  114. }
  115. if (empty($collection) || !is_numeric($collection)) {
  116. throw new InvalidArgumentException('Invalid Collection');
  117. }
  118. $rows = $this->getMapper()->fetchObjectsByGroupsModuleCollection($groups, $module, $collection);
  119. $array = array();
  120. foreach ($rows as $row) {
  121. $array[$row->group][] = $row;
  122. }
  123. return $array;
  124. }
  125. /**
  126. * @param array $groups Array of Group Ids
  127. * @param string $module
  128. * @param string $controller
  129. * @param integer $object
  130. * @return Core_Model_Acl_Group[]
  131. */
  132. public function getObjectsByGroupsModuleControllerObject(array $groups, $module, $controller, $object = null) {
  133. if (count($groups) == 0) {
  134. throw new InvalidArgumentException('Groups array is empty');
  135. }
  136. if (empty($module)) {
  137. throw new InvalidArgumentException('Invalid Module');
  138. }
  139. if (empty($controller)) {
  140. throw new InvalidArgumentException('Invalid Controller');
  141. }
  142. $rows = $this->getMapper()->fetchObjectsByGroupsModuleControllerObject($groups, $module, $controller, $object);
  143. $array = array();
  144. foreach ($rows as $row) {
  145. $array[$row->group][] = $row;
  146. }
  147. // if (empty($contents)) {
  148. // throw new Exception('Apikey not found', 404);
  149. // }
  150. return $array;
  151. }
  152. /**
  153. * @param array $groups
  154. * @param type $module
  155. * @param type $controller
  156. * @param integer[] $objects
  157. * @return Core_Model_Acl_Group[]
  158. */
  159. public function getObjectsByGroupsModuleControllerObjects(array $groups, $module, $controller, array $objects) {
  160. if (count($groups) == 0) {
  161. throw new InvalidArgumentException('Groups array is empty');
  162. }
  163. if (empty($module)) {
  164. throw new InvalidArgumentException('Invalid Module');
  165. }
  166. if (empty($controller)) {
  167. throw new InvalidArgumentException('Invalid Controller');
  168. }
  169. if (empty($objects)) {
  170. throw new InvalidArgumentException('Objects array is empty');
  171. }
  172. $rows = $this->getMapper()->fetchObjectsByGroupsModuleControllerObjects($groups, $module, $controller, $objects);
  173. $array = array();
  174. foreach ($rows as $row) {
  175. $array[$row->group][] = $row;
  176. }
  177. return $array;
  178. }
  179. public function allow($group, $module, $controller = null, $action = null, $collection = null, $obj = null){
  180. $object = new Core_Model_Acl_Group();
  181. $object->group = $group;
  182. $object->module = $module;
  183. $object->controller = $controller;
  184. $object->action = $action;
  185. $object->collection = $collection;
  186. $object->object = $obj;
  187. $object->permission = 1;
  188. return $this->create($object);
  189. }
  190. /**
  191. * @param Core_Model_Acl_Group_Interface|array $mixed
  192. * @return Core_Model_Acl_Group
  193. * @throws InvalidArgumentException
  194. */
  195. public function create($mixed) {
  196. if ($mixed instanceof Core_Model_Acl_Group_Interface) {
  197. $object = $mixed;
  198. } elseif (is_array($mixed)) {
  199. $object = new Core_Model_Acl_Group(array('data' => $mixed));
  200. } else {
  201. throw new InvalidArgumentException('Invalid Object');
  202. }
  203. return $this->getMapper()->save($object);
  204. }
  205. /**
  206. * @param Core_Model_Acl_Group_Interface|array $mixed
  207. * @return Core_Model_Acl_Group
  208. * @throws InvalidArgumentException
  209. */
  210. public function update($mixed) {
  211. if ($mixed instanceof Core_Model_Acl_Group_Interface) {
  212. $object = $mixed;
  213. } elseif (is_array($mixed)) {
  214. $object = new Core_Model_Acl_Group(array('data' => $mixed));
  215. } else {
  216. throw new InvalidArgumentException('Invalid Object');
  217. }
  218. return $this->getMapper()->save($object);
  219. }
  220. /**
  221. * @param Core_Model_Acl_Group_Interface|array|integer $mixed
  222. * @return boolean Success
  223. * @throws InvalidArgumentException
  224. */
  225. public function delete($mixed) {
  226. if (is_int($mixed)) {
  227. $object = new Core_Model_Acl_Group();
  228. $object->id = $mixed;
  229. } elseif ($mixed instanceof Core_Model_Acl_Group_Interface) {
  230. $object = $mixed;
  231. } elseif (is_array($mixed)) {
  232. $object = new Core_Model_Acl_Group(array('data' => $mixed));
  233. } else {
  234. throw new InvalidArgumentException('Invalid Object');
  235. }
  236. return $this->getMapper()->delete($object);
  237. }
  238. public function deleteByGroup($group) {
  239. return $this->getMapper()->deleteByGroup($group);
  240. }
  241. public function deleteByModule($module) {
  242. if(($module === null) || !is_string($module)){
  243. throw new InvalidArgumentException('Invalid Module');
  244. }
  245. return $this->getMapper()->deleteByModule($module);
  246. }
  247. public function deleteByModuleCollection($module, $collection) {
  248. if(($module === null) || !is_string($module)){
  249. throw new InvalidArgumentException('Invalid Module');
  250. }
  251. if(($collection === null) || !is_numeric($collection)){
  252. throw new InvalidArgumentException('Invalid Collection');
  253. }
  254. return $this->getMapper()->deleteByModuleCollection($module, $collection);
  255. }
  256. public function deleteByModuleControllerObject($module, $controller, $object) {
  257. if(($module === null) || !is_string($module)){
  258. throw new InvalidArgumentException('Invalid Module');
  259. }
  260. if(($controller === null) || !is_string($controller)){
  261. throw new InvalidArgumentException('Invalid Controller');
  262. }
  263. if(($object === null)){
  264. throw new InvalidArgumentException('Invalid Object');
  265. }
  266. return $this->getMapper()->deleteByModuleControllerObject($module, $controller, $object);
  267. }
  268. /**
  269. * Delete all data. Used for unit testing/Will not work in production
  270. *
  271. * @return boolean Success
  272. * @throws NotAllowedException
  273. */
  274. public function deleteAll() {
  275. if (APPLICATION_ENV == 'production') {
  276. throw new Exception("Not Allowed");
  277. }
  278. return $this->getMapper()->deleteAll();
  279. }
  280. }