/phpmyfaq/inc/PMF_Perm/PermBasic.php

https://github.com/noon/phpMyFAQ · PHP · 538 lines · 316 code · 52 blank · 170 comment · 28 complexity · c70f9450f3c8940044650605b565ac1c MD5 · raw file

  1. <?php
  2. /**
  3. * The basic permission class provides user rights.
  4. *
  5. * @package phpMyFAQ
  6. * @subpackage PMF_Perm
  7. * @author Lars Tiedemann <php@larstiedemann.de>
  8. * @since 2005-09-17
  9. * @copyright 2005-2009 phpMyFAQ Team
  10. * @version SVN: $Id$
  11. *
  12. * The contents of this file are subject to the Mozilla Public License
  13. * Version 1.1 (the "License"); you may not use this file except in
  14. * compliance with the License. You may obtain a copy of the License at
  15. * http://www.mozilla.org/MPL/
  16. *
  17. * Software distributed under the License is distributed on an "AS IS"
  18. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing rights and limitations
  20. * under the License.
  21. */
  22. /**
  23. * PMF_Perm_PermBasic
  24. *
  25. * @package phpMyFAQ
  26. * @subpackage PMF_Perm
  27. * @author Lars Tiedemann <php@larstiedemann.de>
  28. * @since 2005-09-17
  29. * @copyright 2005-2009 phpMyFAQ Team
  30. * @version SVN: $Id$
  31. */
  32. class PMF_Perm_PermBasic extends PMF_Perm
  33. {
  34. // --- ATTRIBUTES ---
  35. /**
  36. * default_right_data
  37. *
  38. * default right data stored when a new right is created.
  39. *
  40. * @access public
  41. * @var array
  42. */
  43. public $default_right_data = array(
  44. 'name' => 'DEFAULT_RIGHT',
  45. 'description' => 'Short description.',
  46. 'for_users' => true,
  47. 'for_groups' => true
  48. );
  49. // --- OPERATIONS ---
  50. /**
  51. * Returns true if the user given by user_id has the right
  52. * specified by right_id, otherwise false.
  53. *
  54. * @param integer $user_id User ID
  55. * @param integer $right_id Right ID
  56. * @return bool
  57. */
  58. public function checkUserRight($user_id, $right_id)
  59. {
  60. // check right id
  61. if ($right_id <= 0) {
  62. return false;
  63. }
  64. // check right
  65. $select = sprintf("
  66. SELECT
  67. fr.right_id AS right_id
  68. FROM
  69. %sfaqright fr,
  70. %sfaquser_right fur,
  71. %sfaquser fu
  72. WHERE
  73. fr.right_id = %d AND
  74. fr.right_id = fur.right_id AND
  75. fu.user_id = %d AND
  76. fu.user_id = fur.user_id",
  77. SQLPREFIX,
  78. SQLPREFIX,
  79. SQLPREFIX,
  80. $right_id,
  81. $user_id);
  82. $res = $this->db->query($select);
  83. // return result
  84. if ($this->db->num_rows($res) == 1) {
  85. return true;
  86. }
  87. return false;
  88. }
  89. /**
  90. * Returns an array with the IDs of all user-rights the user
  91. * specified by user_id owns. Group rights are not taken into
  92. * account.
  93. *
  94. * @param integer $user_id User ID
  95. * @return array
  96. */
  97. public function getUserRights($user_id)
  98. {
  99. // get user rights
  100. $select = sprintf("
  101. SELECT
  102. fr.right_id AS right_id
  103. FROM
  104. %sfaqright fr,
  105. %sfaquser_right fur,
  106. %sfaquser fu
  107. WHERE
  108. fr.right_id = fur.right_id AND
  109. fu.user_id = %d AND
  110. fu.user_id = fur.user_id",
  111. SQLPREFIX,
  112. SQLPREFIX,
  113. SQLPREFIX,
  114. $user_id);
  115. $res = $this->db->query($select);
  116. $result = array();
  117. while ($row = $this->db->fetch_assoc($res)) {
  118. $result[] = $row['right_id'];
  119. }
  120. return $result;
  121. }
  122. /**
  123. * Gives the user a new user-right.
  124. * Returns true on success, otherwise false.
  125. *
  126. * @param integer $user_id User ID
  127. * @param integer $right_id Right ID
  128. * @return boolean
  129. */
  130. public function grantUserRight($user_id, $right_id)
  131. {
  132. // is right for users?
  133. $right_data = $this->getRightData($right_id);
  134. if (!$right_data['for_users']) {
  135. return false;
  136. }
  137. $insert = sprintf("
  138. INSERT INTO
  139. %sfaquser_right
  140. (user_id, right_id)
  141. VALUES
  142. (%d, %d)",
  143. SQLPREFIX,
  144. $user_id,
  145. $right_id);
  146. $res = $this->db->query($insert);
  147. if (!$res) {
  148. return false;
  149. }
  150. return true;
  151. }
  152. /**
  153. * Refuses the user a user-right.
  154. * Returns true on succes, otherwise false.
  155. *
  156. * @param integer $user_id User ID
  157. * @param integer $right_id Right ID
  158. * @return boolean
  159. */
  160. public function refuseUserRight($user_id, $right_id)
  161. {
  162. $delete = sprintf("
  163. DELETE FROM
  164. %sfaquser_right
  165. WHERE
  166. user_id = %d AND
  167. right_id = %d",
  168. SQLPREFIX,
  169. $user_id,
  170. $right_id);
  171. $res = $this->db->query($delete);
  172. if (!$res) {
  173. return false;
  174. }
  175. return true;
  176. }
  177. /**
  178. * Returns true if the user given by user_id has the right,
  179. * otherwise false. Unlike checkUserRight(), right may be a
  180. * right-ID or a right-name. Another difference is, that also
  181. * group-rights are taken into account.
  182. *
  183. * @param integer $user_id User ID
  184. * @param mixed $right Right ID or right name
  185. * @return bool
  186. */
  187. public function checkRight($user_id, $right)
  188. {
  189. if (!is_numeric($right) and is_string($right)) {
  190. $right = $this->getRightId($right);
  191. }
  192. return $this->checkUserRight($user_id, $right);
  193. }
  194. /**
  195. * Returns an associative array with all data stored for in the
  196. * database for the specified right. The keys of the returned
  197. * array are the fieldnames.
  198. *
  199. * @access public
  200. * @author Lars Tiedemann, <php@larstiedemann.de>
  201. * @param int
  202. * @return array
  203. */
  204. public function getRightData($right_id)
  205. {
  206. // get right data
  207. $select = sprintf("
  208. SELECT
  209. right_id,
  210. name,
  211. description,
  212. for_users,
  213. for_groups
  214. FROM
  215. %sfaqright
  216. WHERE
  217. right_id = %d",
  218. SQLPREFIX,
  219. $right_id);
  220. $res = $this->db->query($select);
  221. if ($this->db->num_rows($res) != 1) {
  222. return false;
  223. }
  224. // process right data
  225. $right_data = $this->db->fetch_assoc($res);
  226. $right_data['for_users'] = (bool)$right_data['for_users'];
  227. $right_data['for_groups'] = (bool)$right_data['for_groups'];
  228. return $right_data;
  229. }
  230. /**
  231. * Returns an array that contains the IDs of all user-rights
  232. * the user owns.
  233. *
  234. * @param integer $user_id User ID
  235. * @return array
  236. */
  237. public function getAllUserRights($user_id)
  238. {
  239. return $this->getUserRights($user_id);
  240. }
  241. /**
  242. * Adds a new right into the database. Returns the ID of the
  243. * new right. The associative array right_data contains the right
  244. * data stored in the rights table.
  245. *
  246. * @param array $right_data Array if rights
  247. * @return int
  248. */
  249. public function addRight(Array $right_data)
  250. {
  251. if ($this->getRightId($right_data['name']) > 0) {
  252. return 0;
  253. }
  254. $next_id = $this->db->nextID(SQLPREFIX."faqright", "right_id");
  255. $right_data = $this->checkRightData($right_data);
  256. $insert = sprintf("
  257. INSERT INTO
  258. %sfaqright
  259. (right_id, name, description, for_users, for_groups)
  260. VALUES
  261. (%d, '%s', '%s', %d, %d)",
  262. SQLPREFIX,
  263. $next_id,
  264. $right_data['name'],
  265. $right_data['description'],
  266. (int)$right_data['for_users'],
  267. (int)$right_data['for_groups']);
  268. $res = $this->db->query($insert);
  269. if (!$res) {
  270. return 0;
  271. }
  272. return $next_id;
  273. }
  274. /**
  275. * Changes the right data. Returns true on success, otherwise false.
  276. *
  277. * @param integer $right_id Right ID
  278. * @param array $right_data Array of rights
  279. * @return boolean
  280. */
  281. public function changeRight($right_id, Array $right_data)
  282. {
  283. $checked_data = $this->checkRightData($right_data);
  284. $set = '';
  285. $comma = '';
  286. foreach ($right_data as $key => $val) {
  287. $set .= $comma.$key." = '".$checked_data[$key]."'";
  288. $comma = ",\n ";
  289. }
  290. $update = sprintf("
  291. UPDATE
  292. %sfaqright
  293. SET
  294. %s
  295. WHERE
  296. right_id = %d",
  297. SQLPREFIX,
  298. $set,
  299. $right_id);
  300. $res = $this->db->query($update);
  301. if (!$res) {
  302. return false;
  303. }
  304. return true;
  305. }
  306. /**
  307. * Deletes the right from the database.
  308. * Returns true on success, otherwise false.
  309. *
  310. * @param integer $right_id Right ID
  311. * @return boolean
  312. */
  313. public function deleteRight($right_id)
  314. {
  315. // delete right
  316. $delete = sprintf("
  317. DELETE FROM
  318. %sfaqright
  319. WHERE
  320. right_id = %d",
  321. SQLPREFIX,
  322. $right_id);
  323. $res = $this->db->query($delete);
  324. if (!$res) {
  325. return false;
  326. }
  327. // delete user-right links
  328. $delete = sprintf("
  329. DELETE FROM
  330. %sfaquser_right
  331. WHERE
  332. right_id = %d",
  333. SQLPREFIX,
  334. $right_id);
  335. $res = $this->db->query($delete);
  336. if (!$res) {
  337. return false;
  338. }
  339. // delete group-right links
  340. $delete = sprintf("
  341. DELETE FROM
  342. %sfaqgroup_right
  343. WHERE
  344. right_id = %d",
  345. SQLPREFIX,
  346. $right_id);
  347. $res = $this->db->query($delete);
  348. if (!$res) {
  349. return false;
  350. }
  351. $res = $this->db->query($delete);
  352. if (!$res) {
  353. return false;
  354. }
  355. return true;
  356. }
  357. /**
  358. * Returns the right-ID of the right with the name $name.
  359. *
  360. * @param string $name Name
  361. * @return int
  362. */
  363. public function getRightId($name)
  364. {
  365. // get right id
  366. $select = sprintf("
  367. SELECT
  368. right_id
  369. FROM
  370. %sfaqright
  371. WHERE
  372. name = '%s'",
  373. SQLPREFIX,
  374. $this->db->escape_string($name));
  375. $res = $this->db->query($select);
  376. if ($this->db->num_rows($res) != 1) {
  377. return 0;
  378. }
  379. $row = $this->db->fetch_assoc($res);
  380. return $row['right_id'];
  381. }
  382. /**
  383. * Returns an array that contains the IDs of all rights stored
  384. * in the database.
  385. *
  386. * @return array
  387. */
  388. public function getAllRights()
  389. {
  390. $select = sprintf("
  391. SELECT
  392. right_id
  393. FROM
  394. %sfaqright",
  395. SQLPREFIX);
  396. $res = $this->db->query($select);
  397. $result = array();
  398. while ($row = $this->db->fetch_assoc($res)) {
  399. $result[] = $row['right_id'];
  400. }
  401. return $result;
  402. }
  403. /**
  404. * Returns an array that contains all rights stored in the
  405. * database. Each array element is an associative array with
  406. * the complete right-data. By passing the optional parameter
  407. * $order, the order of the array may be specified. Default is
  408. * $order = 'right_id ASC'.
  409. *
  410. * @param string $order Ordering
  411. * @return array
  412. */
  413. public function getAllRightsData($order = 'ASC')
  414. {
  415. $select = sprintf("
  416. SELECT
  417. right_id,
  418. name,
  419. description,
  420. for_users,
  421. for_groups
  422. FROM
  423. %sfaqright
  424. ORDER BY
  425. right_id %s",
  426. SQLPREFIX,
  427. $order);
  428. $res = $this->db->query($select);
  429. $result = array();
  430. $i = 0;
  431. while ($row = $this->db->fetch_assoc($res)) {
  432. $result[$i] = $row;
  433. $i++;
  434. }
  435. return $result;
  436. }
  437. /**
  438. * Checks the given associative array $right_data. If a
  439. * parameter is incorrect or is missing, it will be replaced
  440. * by the default values in $this->default_right_data.
  441. * Returns the corrected $right_data associative array.
  442. *
  443. * @param array $right_data Array of rights
  444. * @return array
  445. */
  446. public function checkRightData(Array $right_data)
  447. {
  448. if (!isset($right_data['name']) || !is_string($right_data['name'])) {
  449. $right_data['name'] = $this->default_right_data['name'];
  450. }
  451. if (!isset($right_data['description']) || !is_string($right_data['description'])) {
  452. $right_data['description'] = $this->default_right_data['description'];
  453. }
  454. if (!isset($right_data['for_users'])) {
  455. $right_data['for_users'] = $this->default_right_data['for_users'];
  456. }
  457. if (!isset($right_data['for_groups'])) {
  458. $right_data['for_groups'] = $this->default_right_data['for_groups'];
  459. }
  460. $right_data['for_users'] = (int)$right_data['for_users'];
  461. $right_data['for_groups'] = (int)$right_data['for_groups'];
  462. return $right_data;
  463. }
  464. /**
  465. * Refuses all user rights.
  466. * Returns true on success, otherwise false.
  467. *
  468. * @param integer $user_id User ID
  469. * @return boolean
  470. */
  471. public function refuseAllUserRights($user_id)
  472. {
  473. $delete = sprintf("
  474. DELETE FROM
  475. %sfaquser_right
  476. WHERE
  477. user_id = %d",
  478. SQLPREFIX,
  479. $user_id);
  480. $res = $this->db->query($delete);
  481. if (!$res) {
  482. return false;
  483. }
  484. return true;
  485. }
  486. }