00001 <?php 00002 /** 00003 * Magento 00004 * 00005 * NOTICE OF LICENSE 00006 * 00007 * This source file is subject to the Open Software License (OSL 3.0) 00008 * that is bundled with this package in the file LICENSE.txt. 00009 * It is also available through the world-wide-web at this URL: 00010 * http://opensource.org/licenses/osl-3.0.php 00011 * If you did not receive a copy of the license and are unable to 00012 * obtain it through the world-wide-web, please send an email 00013 * to license@magentocommerce.com so we can send you a copy immediately. 00014 * 00015 * DISCLAIMER 00016 * 00017 * Do not edit or add to this file if you wish to upgrade Magento to newer 00018 * versions in the future. If you wish to customize Magento for your 00019 * needs please refer to http://www.magentocommerce.com for more information. 00020 * 00021 * @category Mage 00022 * @package Mage_Admin 00023 * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com) 00024 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 00025 */ 00026 00027 class Mage_Admin_Model_Mysql4_Roles extends Mage_Core_Model_Mysql4_Abstract 00028 { 00029 protected $_usersTable; 00030 protected $_ruleTable; 00031 00032 protected function _construct() { 00033 $this->_init('admin/role', 'role_id'); 00034 00035 $this->_usersTable = $this->getTable('admin/user'); 00036 $this->_ruleTable = $this->getTable('admin/rule'); 00037 } 00038 00039 protected function _beforeSave(Mage_Core_Model_Abstract $role) 00040 { 00041 if ($role->getId() == '') { 00042 if ($role->getIdFieldName()) { 00043 $role->unsetData($role->getIdFieldName()); 00044 } else { 00045 $role->unsetData('id'); 00046 } 00047 } 00048 00049 if ($role->getPid() > 0) { 00050 $row = $this->load($role->getPid()); 00051 } else { 00052 $row = array('tree_level' => 0); 00053 } 00054 $role->setTreeLevel($row['tree_level'] + 1); 00055 $role->setRoleName($role->getName()); 00056 return $this; 00057 } 00058 00059 protected function _afterSave(Mage_Core_Model_Abstract $role) 00060 { 00061 $this->_updateRoleUsersAcl($role); 00062 Mage::app()->getCache()->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, 00063 array(Mage_Adminhtml_Block_Page_Menu::CACHE_TAGS)); 00064 return $this; 00065 } 00066 00067 protected function _afterDelete(Mage_Core_Model_Abstract $role) 00068 { 00069 $this->_getWriteAdapter()->delete($this->getMainTable(), "parent_id={$role->getId()}"); 00070 $this->_getWriteAdapter()->delete($this->_ruleTable, "role_id={$role->getId()}"); 00071 return $this; 00072 } 00073 00074 public function getRoleUsers(Mage_Admin_Model_Roles $role) 00075 { 00076 $read = $this->_getReadAdapter(); 00077 $select = $read->select()->from($this->getMainTable(), array('user_id'))->where("(parent_id = '{$role->getId()}' AND role_type = 'U') AND user_id > 0"); 00078 return $read->fetchCol($select); 00079 } 00080 00081 private function _updateRoleUsersAcl(Mage_Admin_Model_Roles $role) 00082 { 00083 $write = $this->_getWriteAdapter(); 00084 $users = $this->getRoleUsers($role); 00085 $rowsCount = 0; 00086 if ( sizeof($users) > 0 ) { 00087 $inStatement = implode(", ", $users); 00088 $rowsCount = $write->update($this->_usersTable, array('reload_acl_flag' => 1), "user_id IN({$inStatement})"); 00089 } 00090 if ($rowsCount > 0) { 00091 return true; 00092 } else { 00093 return false; 00094 } 00095 } 00096 }