acl_copy.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCoreScripts Scripts
  7. * @{
  8. */
  9. $aPathInfo = pathinfo(__FILE__);
  10. require_once($aPathInfo['dirname'] . '/../inc/header.inc.php');
  11. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  12. class BxDolAclCopyCmd
  13. {
  14. protected $_oDb;
  15. protected $_bQuiet;
  16. protected $_LevelIdFrom;
  17. protected $_LevelIdTo;
  18. protected $_LevelMaskFrom;
  19. protected $_LevelMaskTo;
  20. public function __construct()
  21. {
  22. $this->_oDb = BxDolDb::getInstance();
  23. $this->_bQuiet = false;
  24. }
  25. public function main()
  26. {
  27. $a = getopt('hf:t:');
  28. if (isset($a['h']) || !$a)
  29. $this->finish(0, $this->getHelp());
  30. if (isset($a['f']))
  31. $this->_LevelIdFrom = (int)$a['f'];
  32. if (isset($a['t']))
  33. $this->_LevelIdTo = (int)$a['t'];
  34. $this->_LevelMaskFrom = pow(2, $this->_LevelIdFrom - 1);
  35. $this->_LevelMaskTo = pow(2, $this->_LevelIdTo - 1);
  36. $this->output($this->_setAclMatrix());
  37. $this->output($this->_setRoles('sys_objects_page'));
  38. $this->output($this->_setRoles('sys_pages_blocks'));
  39. $this->output($this->_setRoles('sys_menu_items'));
  40. $this->output($this->_setRoles('sys_form_display_inputs'));
  41. $this->output($this->_setRoles('sys_objects_grid'));
  42. $this->finish(1);
  43. }
  44. protected function getHelp()
  45. {
  46. $n = 20;
  47. $s = "Usage: php acl_copy.php [options]\n";
  48. $s .= str_pad("\t -h", $n) . "Print this help\n";
  49. $s .= str_pad("\t -f <input_id>", $n) . "Input level ID to copy from\n";
  50. $s .= str_pad("\t -t <input_id>", $n) . "Input level ID to copy to\n";
  51. $s .= "\n";
  52. return $s;
  53. }
  54. protected function _setAclMatrix()
  55. {
  56. $sResult = '';
  57. $sQuery = $this->_oDb->prepareAsString("DELETE FROM `sys_acl_matrix` WHERE `IDLevel` = ?", $this->_LevelIdTo);
  58. $this->_oDb->query($sQuery);
  59. $sResult .= $sQuery . "\n";
  60. $sQuery = $this->_oDb->prepareAsString("INSERT INTO `sys_acl_matrix` (`IDLevel`, `IDAction`, `AllowedCount`, `AllowedPeriodLen`, `AllowedPeriodStart`, `AllowedPeriodEnd`, `AdditionalParamValue`) SELECT " . $this->_LevelIdTo . ", `IDAction`, `AllowedCount`, `AllowedPeriodLen`, `AllowedPeriodStart`, `AllowedPeriodEnd`, `AdditionalParamValue` FROM `sys_acl_matrix` WHERE `IDLevel` = ?", $this->_LevelIdFrom);
  61. $this->_oDb->query($sQuery);
  62. $sResult .= $sQuery . "\n";
  63. return $sResult;
  64. }
  65. protected function _setRoles($sTable)
  66. {
  67. $sResult = '';
  68. $aItems = $this->_oDb->getAll("SELECT `id`, `visible_for_levels` FROM `" . $sTable . "` ORDER BY `id` ASC");
  69. foreach($aItems as $aItem) {
  70. if (($iValue = (int)$aItem['visible_for_levels']) && $iValue < 2147483647) {
  71. if (($iValue & $this->_LevelMaskTo) == $this->_LevelMaskTo){
  72. $iValue -= $this->_LevelMaskTo;
  73. }
  74. if (($iValue & $this->_LevelMaskFrom) == $this->_LevelMaskFrom){
  75. $iValue += $this->_LevelMaskTo;
  76. }
  77. if ($iValue != (int)$aItem['visible_for_levels']){
  78. $sQuery = "UPDATE `" . $sTable . "` SET `visible_for_levels` = " . $iValue . " WHERE `id` = ". $aItem['id'];
  79. $this->_oDb->query($sQuery);
  80. $sResult .= $sQuery . "\n";
  81. }
  82. }
  83. }
  84. return $sResult;
  85. }
  86. protected function finish ($iCode, $sMsg = null, $bAddLn = true)
  87. {
  88. if (!$this->_bQuiet && null !== $sMsg)
  89. echo $sMsg . ($bAddLn ? "\n" : '');
  90. exit($iCode);
  91. }
  92. protected function output ($sMsg, $bAddLn = true)
  93. {
  94. if (!$this->_bQuiet)
  95. echo $sMsg . ($bAddLn ? "\n" : '');
  96. }
  97. }
  98. $o = new BxDolAclCopyCmd();
  99. $o->main();
  100. /** @} */