PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/webservice/WebserviceKey.php

https://bitbucket.org/enurkov/prestashop
PHP | 157 lines | 113 code | 14 blank | 30 comment | 15 complexity | a54357493e268707c719161c8c61ca87 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2012 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  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://opensource.org/licenses/osl-3.0.php
  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@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2012 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class WebserviceKeyCore extends ObjectModel
  27. {
  28. /** @var string Key */
  29. public $key;
  30. /** @var boolean Webservice Account statuts */
  31. public $active = true;
  32. /** @var string Webservice Account description */
  33. public $description;
  34. /**
  35. * @see ObjectModel::$definition
  36. */
  37. public static $definition = array(
  38. 'table' => 'webservice_account',
  39. 'primary' => 'id_webservice_account',
  40. 'fields' => array(
  41. 'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  42. 'key' => array('type' => self::TYPE_STRING, 'required' => true, 'size' => 32),
  43. 'description' => array('type' => self::TYPE_STRING),
  44. ),
  45. );
  46. public function add($autodate = true, $nullValues = false)
  47. {
  48. if (WebserviceKey::keyExists($this->key))
  49. return false;
  50. return parent::add($autodate = true, $nullValues = false);
  51. }
  52. public static function keyExists($key)
  53. {
  54. return (!Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT `key`
  55. FROM '._DB_PREFIX_.'webservice_account
  56. WHERE `key` = \''.pSQL($key).'\'') ? false : true);
  57. }
  58. public function delete()
  59. {
  60. if (!parent::delete() || $this->deleteAssociations() === false)
  61. return false;
  62. return true;
  63. }
  64. public function deleteAssociations()
  65. {
  66. if (Db::getInstance()->execute('
  67. DELETE FROM `'._DB_PREFIX_.'webservice_permission`
  68. WHERE `id_webservice_account` = '.(int)$this->id) === false
  69. ||
  70. Db::getInstance()->execute('
  71. DELETE FROM `'._DB_PREFIX_.'webservice_permission`
  72. WHERE `id_webservice_account` = '.(int)$this->id) === false)
  73. return false;
  74. return true;
  75. }
  76. public static function getPermissionForAccount($auth_key)
  77. {
  78. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  79. SELECT p.*
  80. FROM `'._DB_PREFIX_.'webservice_permission` p
  81. LEFT JOIN `'._DB_PREFIX_.'webservice_account` a ON (a.id_webservice_account = p.id_webservice_account)
  82. WHERE a.key = \''.pSQL($auth_key).'\'
  83. ');
  84. $permissions = array();
  85. if ($result)
  86. foreach ($result as $row)
  87. $permissions[$row['resource']][] = $row['method'];
  88. return $permissions;
  89. }
  90. public static function isKeyActive($auth_key)
  91. {
  92. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  93. SELECT a.active
  94. FROM `'._DB_PREFIX_.'webservice_account` a
  95. WHERE a.key = \''.pSQL($auth_key).'\'
  96. ');
  97. if (!isset($result[0]))
  98. return null;
  99. else
  100. return isset($result[0]['active']) && $result[0]['active'];
  101. }
  102. public static function getClassFromKey($auth_key)
  103. {
  104. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  105. SELECT a.class_name as class
  106. FROM `'._DB_PREFIX_.'webservice_account` a
  107. WHERE a.key = \''.pSQL($auth_key).'\'
  108. ');
  109. if (!isset($result[0]))
  110. return null;
  111. else
  112. return $result[0]['class'];
  113. }
  114. public static function setPermissionForAccount($id_account, $permissions_to_set)
  115. {
  116. $ok = true;
  117. $sql = 'DELETE FROM `'._DB_PREFIX_.'webservice_permission` WHERE `id_webservice_account` = '.(int)$id_account;
  118. if (!Db::getInstance()->execute($sql))
  119. $ok = false;
  120. if (isset($permissions_to_set))
  121. {
  122. $permissions = array();
  123. $resources = WebserviceRequest::getResources();
  124. $methods = array('GET', 'PUT', 'POST', 'DELETE', 'HEAD');
  125. foreach ($permissions_to_set as $resource_name => $resource_methods)
  126. if (in_array($resource_name, array_keys($resources)))
  127. foreach (array_keys($resource_methods) as $method_name)
  128. if (in_array($method_name, $methods))
  129. $permissions[] = array($method_name, $resource_name);
  130. $account = new WebserviceKey($id_account);
  131. if ($account->deleteAssociations() && $permissions)
  132. {
  133. $sql = 'INSERT INTO `'._DB_PREFIX_.'webservice_permission` (`id_webservice_permission` ,`resource` ,`method` ,`id_webservice_account`) VALUES ';
  134. foreach ($permissions as $permission)
  135. $sql .= '(NULL , \''.pSQL($permission[1]).'\', \''.pSQL($permission[0]).'\', '.(int)$id_account.'), ';
  136. $sql = rtrim($sql, ', ');
  137. if (!Db::getInstance()->execute($sql))
  138. $ok = false;
  139. }
  140. }
  141. return $ok;
  142. }
  143. }