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_Api 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_Api_Model_Mysql4_Roles extends Mage_Core_Model_Mysql4_Abstract 00028 { 00029 protected $_usersTable; 00030 protected $_ruleTable; 00031 00032 protected function _construct() { 00033 $this->_init('api/role', 'role_id'); 00034 00035 $this->_usersTable = $this->getTable('api/user'); 00036 $this->_ruleTable = $this->getTable('api/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 return $this; 00064 } 00065 00066 protected function _afterDelete(Mage_Core_Model_Abstract $role) 00067 { 00068 $this->_getWriteAdapter()->delete($this->getMainTable(), "parent_id={$role->getId()}"); 00069 $this->_getWriteAdapter()->delete($this->_ruleTable, "role_id={$role->getId()}"); 00070 return $this; 00071 } 00072 00073 public function getRoleUsers(Mage_Api_Model_Roles $role) 00074 { 00075 $read = $this->_getReadAdapter(); 00076 $select = $read->select()->from($this->getMainTable(), array('user_id'))->where("(parent_id = '{$role->getId()}' AND role_type = 'U') AND user_id > 0"); 00077 return $read->fetchCol($select); 00078 } 00079 00080 private function _updateRoleUsersAcl(Mage_Api_Model_Roles $role) 00081 { 00082 $write = $this->_getWriteAdapter(); 00083 $users = $this->getRoleUsers($role); 00084 $rowsCount = 0; 00085 if ( sizeof($users) > 0 ) { 00086 $inStatement = implode(", ", $users); 00087 $rowsCount = $write->update($this->_usersTable, array('reload_acl_flag' => 1), "user_id IN({$inStatement})"); 00088 } 00089 if ($rowsCount > 0) { 00090 return true; 00091 } else { 00092 return false; 00093 } 00094 } 00095 }