/backend/modules/groups/engine/model.php

https://github.com/DoFken/forkcms · PHP · 384 lines · 205 code · 33 blank · 146 comment · 5 complexity · d8d678e15df7690347ea619b1cfeffbf MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Fork CMS.
  4. *
  5. * For the full copyright and license information, please view the license
  6. * file that was distributed with this source code.
  7. */
  8. /**
  9. * In this file we store all generic functions that we will be using in the groups module.
  10. *
  11. * @author Jeroen Van den Bossche <jeroenvandenbossche@netlash.com>
  12. */
  13. class BackendGroupsModel
  14. {
  15. const QRY_BROWSE =
  16. 'SELECT g.id, g.name, COUNT(u.id) AS num_users
  17. FROM groups AS g
  18. LEFT OUTER JOIN users_groups AS ug ON g.id = ug.group_id
  19. LEFT OUTER JOIN users AS u ON u.id = ug.user_id
  20. GROUP BY g.id';
  21. const QRY_ACTIVE_USERS =
  22. 'SELECT u.id, u.email
  23. FROM users AS u
  24. INNER JOIN users_groups AS ug ON u.id = ug.user_id
  25. WHERE ug.group_id = ? AND u.deleted = ?';
  26. /**
  27. * Add action permissions
  28. *
  29. * @param array $actionPermissions
  30. */
  31. public static function addActionPermissions($actionPermissions)
  32. {
  33. foreach((array) $actionPermissions as $permission)
  34. {
  35. if(!self::existsActionPermission($permission))
  36. {
  37. BackendModel::getDB(true)->insert('groups_rights_actions', $permission);
  38. }
  39. }
  40. }
  41. /**
  42. * Add module permissions
  43. *
  44. * @param array $modulePermissions
  45. */
  46. public static function addModulePermissions($modulePermissions)
  47. {
  48. foreach((array) $modulePermissions as $permission)
  49. {
  50. if(!self::existsModulePermission($permission))
  51. {
  52. BackendModel::getDB(true)->insert('groups_rights_modules', $permission);
  53. }
  54. }
  55. }
  56. /**
  57. * Check if a group already exists
  58. *
  59. * @param string $name
  60. * @return bool
  61. */
  62. public static function alreadyExists($name)
  63. {
  64. return (bool) BackendModel::getDB()->getVar(
  65. 'SELECT i.*
  66. FROM groups AS i
  67. WHERE i.name = ?',
  68. array((string) $name)
  69. );
  70. }
  71. /**
  72. * Delete a group
  73. *
  74. * @param int $id The id of the group to delete.
  75. */
  76. public static function delete($id)
  77. {
  78. BackendModel::getDB(true)->delete('groups', 'id = ?', array((int) $id));
  79. }
  80. /**
  81. * Delete action permissions
  82. *
  83. * @param array $actionPermissions The action permissions to delete.
  84. */
  85. public static function deleteActionPermissions($actionPermissions)
  86. {
  87. foreach((array) $actionPermissions as $permission)
  88. {
  89. if(self::existsActionPermission($permission))
  90. {
  91. BackendModel::getDB(true)->delete(
  92. 'groups_rights_actions',
  93. 'group_id = ? AND module = ? AND action = ?',
  94. array($permission['group_id'], $permission['module'], $permission['action'])
  95. );
  96. }
  97. }
  98. }
  99. /**
  100. * Delete module permissions
  101. *
  102. * @param array $modulePermissions The module permissions to delete.
  103. */
  104. public static function deleteModulePermissions($modulePermissions)
  105. {
  106. foreach((array) $modulePermissions as $permission)
  107. {
  108. if(self::existsModulePermission($permission))
  109. {
  110. BackendModel::getDB(true)->delete(
  111. 'groups_rights_modules',
  112. 'group_id = ? AND module = ?',
  113. array($permission['group_id'], $permission['module'])
  114. );
  115. }
  116. }
  117. }
  118. /**
  119. * Delete a user's multiple groups
  120. *
  121. * @param int $userId The id of the user.
  122. */
  123. public static function deleteMultipleGroups($userId)
  124. {
  125. BackendModel::getDB(true)->delete('users_groups', 'user_id = ?', array($userId));
  126. }
  127. /**
  128. * Check if a group already exists
  129. *
  130. * @param int $id The id to check upon.
  131. * @return bool
  132. */
  133. public static function exists($id)
  134. {
  135. return (bool) BackendModel::getDB()->getVar(
  136. 'SELECT i.*
  137. FROM groups AS i
  138. WHERE i.id = ?',
  139. array((int) $id)
  140. );
  141. }
  142. /**
  143. * Check if a action permission exists
  144. *
  145. * @param array $permission The permission to check upon.
  146. * @return bool
  147. */
  148. public static function existsActionPermission($permission)
  149. {
  150. return (bool) BackendModel::getDB()->getVar(
  151. 'SELECT i.*
  152. FROM groups_rights_actions AS i
  153. WHERE i.module = ? AND i.group_id = ? AND i.action = ?',
  154. array($permission['module'], $permission['group_id'], $permission['action'])
  155. );
  156. }
  157. /**
  158. * Check if a module permission exists
  159. *
  160. * @param array $permission The permission to check upon.
  161. * @return bool
  162. */
  163. public static function existsModulePermission($permission)
  164. {
  165. return (bool) BackendModel::getDB()->getVar(
  166. 'SELECT i.*
  167. FROM groups_rights_modules AS i
  168. WHERE i.module = ? AND i.group_id = ?',
  169. array($permission['module'], $permission['group_id'])
  170. );
  171. }
  172. /**
  173. * Get a group
  174. *
  175. * @param int $id The id of the group to fetch.
  176. * @return array
  177. */
  178. public static function get($id)
  179. {
  180. return (array) BackendModel::getDB()->getRecord(
  181. 'SELECT i.*
  182. FROM groups AS i
  183. WHERE i.id = ?',
  184. array((int) $id)
  185. );
  186. }
  187. /**
  188. * Get group action permissions
  189. *
  190. * @param int $id The id of the group.
  191. * @return array
  192. */
  193. public static function getActionPermissions($id)
  194. {
  195. return (array) BackendModel::getDB()->getRecords(
  196. 'SELECT i.module, i.action
  197. FROM groups_rights_actions AS i
  198. WHERE i.group_id = ?',
  199. array((int) $id)
  200. );
  201. }
  202. /**
  203. * Get all groups
  204. *
  205. * @return array
  206. */
  207. public static function getAll()
  208. {
  209. return (array) BackendModel::getDB()->getRecords(
  210. 'SELECT i.id AS value, i.name AS label FROM groups AS i'
  211. );
  212. }
  213. /**
  214. * Get all groups of one user
  215. *
  216. * @param int $id
  217. * @return array
  218. */
  219. public static function getGroupsByUser($id)
  220. {
  221. return (array) BackendModel::getDB()->getRecords(
  222. 'SELECT i.id, i.name
  223. FROM groups AS i
  224. INNER JOIN users_groups AS ug ON i.id = ug.group_id
  225. WHERE ug.user_id = ?',
  226. array((int) $id)
  227. );
  228. }
  229. /**
  230. * Get group module permissions
  231. *
  232. * @param int $id The id of the group.
  233. * @return array
  234. */
  235. public static function getModulePermissions($id)
  236. {
  237. return (array) BackendModel::getDB()->getRecords(
  238. 'SELECT i.*
  239. FROM groups_rights_modules AS i
  240. WHERE i.group_id = ?',
  241. array((int) $id)
  242. );
  243. }
  244. /**
  245. * Get a group setting
  246. *
  247. * @param int $groupId The id of the group of the setting.
  248. * @param string $name The name of the setting to fetch.
  249. * @return array
  250. */
  251. public static function getSetting($groupId, $name)
  252. {
  253. $setting = (array) BackendModel::getDB()->getRecord(
  254. 'SELECT i.*
  255. FROM groups_settings AS i
  256. WHERE i.group_id = ? AND i.name = ?',
  257. array((int) $groupId, (string) $name)
  258. );
  259. if(isset($setting['value']))
  260. {
  261. return unserialize($setting['value']);
  262. }
  263. }
  264. /**
  265. * Get all users in a group
  266. *
  267. * @param int $groupId The id of the group.
  268. * @return array
  269. */
  270. public static function getUsers($groupId)
  271. {
  272. return (array) BackendModel::getDB()->getRecords(
  273. 'SELECT i.*
  274. FROM users AS i
  275. INNER JOIN users_groups AS ug ON i.id = ug.user_id
  276. WHERE ug.group_id = ? AND i.deleted = ? AND i.active = ?',
  277. array((int) $groupId, 'N', 'Y')
  278. );
  279. }
  280. /**
  281. * Insert a group and a setting
  282. *
  283. * @param array $group The group to insert.
  284. * @param array $setting The setting to insert.
  285. */
  286. public static function insert($group, $setting)
  287. {
  288. // insert group
  289. $id = BackendModel::getDB(true)->insert('groups', $group);
  290. // build setting
  291. $setting['group_id'] = $id;
  292. // insert setting
  293. self::insertSetting($setting);
  294. // return the id
  295. return $id;
  296. }
  297. /**
  298. * Insert a user's multiple groups
  299. *
  300. * @param int $userId The id of the user.
  301. * @param array $groups The groups.
  302. */
  303. public static function insertMultipleGroups($userId, array $groups)
  304. {
  305. $userId = (int) $userId;
  306. $groups = (array) $groups;
  307. // delete all previous user groups
  308. self::deleteMultipleGroups($userId);
  309. // loop through groups
  310. foreach($groups as $group)
  311. {
  312. // add user id
  313. $item['user_id'] = $userId;
  314. $item['group_id'] = $group;
  315. // insert item
  316. BackendModel::getDB(true)->insert('users_groups', $item);
  317. }
  318. }
  319. /**
  320. * Insert a group setting
  321. *
  322. * @param array $setting
  323. * @return int
  324. */
  325. public static function insertSetting($setting)
  326. {
  327. return BackendModel::getDB(true)->insert('groups_settings', $setting);
  328. }
  329. /**
  330. * Update a group
  331. *
  332. * @param array $group The group to update.
  333. * @param array $setting The setting to update.
  334. */
  335. public static function update($group, $setting)
  336. {
  337. // update group
  338. BackendModel::getDB(true)->update('groups', array('name' => $group['name']), 'id = ?', array($group['id']));
  339. // update setting
  340. self::updateSetting($setting);
  341. }
  342. /**
  343. * Update a group setting
  344. *
  345. * @param array $setting The setting to update.
  346. */
  347. public static function updateSetting($setting)
  348. {
  349. BackendModel::getDB(true)->update('groups_settings', array('value' => $setting['value']), 'group_id = ? AND name = ?', array($setting['group_id'], $setting['name']));
  350. }
  351. }