PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/application/modules/core/models/dao/mysql/Privilege.php

https://github.com/bblc/tomatocms208v1
PHP | 130 lines | 101 code | 11 blank | 18 comment | 4 complexity | 5daba2d823c29194c194326437a7909d MD5 | raw file
  1. <?php
  2. /**
  3. * TomatoCMS
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.gnu.org/licenses/gpl-2.0.txt
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@tomatocms.com so we can send you a copy immediately.
  14. *
  15. * @copyright Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
  16. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
  17. * @version $Id: Privilege.php 5029 2010-08-28 17:02:10Z huuphuoc $
  18. * @since 2.0.5
  19. */
  20. class Core_Models_Dao_Mysql_Privilege extends Tomato_Model_Dao
  21. implements Core_Models_Interface_Privilege
  22. {
  23. public function convert($entity)
  24. {
  25. return new Core_Models_Privilege($entity);
  26. }
  27. public function getPrivileges()
  28. {
  29. $sql = "SELECT * FROM " . $this->_prefix . "core_privilege";
  30. $rs = mysql_query($sql);
  31. $rows = array();
  32. while ($row = mysql_fetch_object($rs)) {
  33. $rows[] = $row;
  34. }
  35. mysql_free_result($rs);
  36. return new Tomato_Model_RecordSet($rows, $this);
  37. }
  38. public function getById($id)
  39. {
  40. $sql = sprintf("SELECT * FROM " . $this->_prefix . "core_privilege WHERE privilege_id = '%s' LIMIT 1",
  41. mysql_real_escape_string($id));
  42. $rs = mysql_query($sql);
  43. $return = (0 == mysql_num_rows($rs)) ? null : new Core_Models_Privilege(mysql_fetch_object($rs));
  44. mysql_free_result($rs);
  45. return $return;
  46. }
  47. public function add($privilege)
  48. {
  49. $sql = sprintf("INSERT INTO " . $this->_prefix . "core_privilege (name, description, module_name, controller_name)
  50. VALUES ('%s', '%s', '%s', '%s')",
  51. mysql_real_escape_string($privilege->name),
  52. mysql_real_escape_string($privilege->description),
  53. mysql_real_escape_string($privilege->module_name),
  54. mysql_real_escape_string($privilege->controller_name));
  55. mysql_query($sql);
  56. return mysql_insert_id();
  57. }
  58. public function delete($id)
  59. {
  60. $sql = sprintf("DELETE FROM " . $this->_prefix . "core_privilege WHERE privilege_id = '%s'",
  61. mysql_real_escape_string($id));
  62. mysql_query($sql);
  63. return mysql_affected_rows();
  64. }
  65. public function getByRole($resource, $roleId)
  66. {
  67. $module = $resource->module_name;
  68. $controller = $resource->controller_name;
  69. $sql = sprintf("SELECT p.privilege_id, name, description, r.allow
  70. FROM " . $this->_prefix . "core_privilege AS p
  71. LEFT JOIN " . $this->_prefix . "core_rule AS r
  72. ON r.obj_type = 'role'
  73. AND r.obj_id = '%s'
  74. AND ((r.privilege_id IS NULL AND r.resource_name IS NULL)
  75. OR (r.privilege_id IS NULL AND (r.resource_name = '%s'))
  76. OR ((r.resource_name = '%s')
  77. AND (r.privilege_id = p.privilege_id)))
  78. WHERE p.module_name = '%s' AND p.controller_name = '%s'",
  79. mysql_real_escape_string($roleId),
  80. mysql_real_escape_string($module . ':' . $controller),
  81. mysql_real_escape_string($module . ':' . $controller),
  82. mysql_real_escape_string($module),
  83. mysql_real_escape_string($controller));
  84. $rs = mysql_query($sql);
  85. $rows = array();
  86. while ($row = mysql_fetch_object($rs)) {
  87. $rows[] = $row;
  88. }
  89. mysql_free_result($rs);
  90. return new Tomato_Model_RecordSet($rows, $this);
  91. }
  92. public function getByUser($resource, $userId)
  93. {
  94. $module = $resource->module_name;
  95. $controller = $resource->controller_name;
  96. $sql = sprintf("SELECT p.privilege_id, name, description, r.allow
  97. FROM " . $this->_prefix . "core_privilege AS p
  98. LEFT JOIN " . $this->_prefix . "core_rule AS r
  99. ON r.obj_type = 'user'
  100. AND r.obj_id = '%s'
  101. AND ((r.privilege_id IS NULL AND r.resource_name IS NULL)
  102. OR (r.privilege_id IS NULL AND (r.resource_name = '%s'))
  103. OR ((r.resource_name = '%s')
  104. AND (r.privilege_id = p.privilege_id)))
  105. WHERE p.module_name = '%s' AND p.controller_name = '%s'",
  106. mysql_real_escape_string($userId),
  107. mysql_real_escape_string($module . ':' . $controller),
  108. mysql_real_escape_string($module . ':' . $controller),
  109. mysql_real_escape_string($module),
  110. mysql_real_escape_string($controller));
  111. $rs = mysql_query($sql);
  112. $rows = array();
  113. while ($row = mysql_fetch_object($rs)) {
  114. $rows[] = $row;
  115. }
  116. mysql_free_result($rs);
  117. return new Tomato_Model_RecordSet($rows, $this);
  118. }
  119. }