1: <?php
2: /**
3: * Magento
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@magentocommerce.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 Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Api2
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * API2 observer
29: *
30: * @category Mage
31: * @package Mage_Api2
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Api2_Model_Observer
35: {
36: /**
37: * Save relation of admin user to API2 role
38: *
39: * @param Varien_Event_Observer $observer
40: * @return void
41: */
42: public function saveAdminToRoleRelation(Varien_Event_Observer $observer)
43: {
44: /** @var $user Mage_Admin_Model_User Object */
45: $user = $observer->getObject();
46:
47: if ($user->hasData('api2_roles')) {
48: $roles = $user->getData('api2_roles');
49:
50: if (!is_array($roles) || !isset($roles[0])) {
51: throw new Exception('API2 roles property has wrong data format.');
52: }
53:
54: /** @var $resourceModel Mage_Api2_Model_Resource_Acl_Global_Role */
55: $resourceModel = Mage::getResourceModel('api2/acl_global_role');
56: $resourceModel->saveAdminToRoleRelation($user->getId(), $roles[0]);
57: }
58: }
59:
60: /**
61: * After save attribute if it is not visible on front remove it from Attribute ACL
62: *
63: * @param Varien_Event_Observer $observer
64: * @return Mage_Api2_Model_Observer
65: */
66: public function catalogAttributeSaveAfter(Varien_Event_Observer $observer)
67: {
68: /** @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
69: $attribute = $observer->getEvent()->getAttribute();
70: if ($attribute->getIsUserDefined() && $attribute->dataHasChangedFor('is_visible_on_front')
71: && !$attribute->getIsVisibleOnFront()) {
72: /** @var $collection Mage_Api2_Model_Resource_Acl_Filter_Attribute_Collection */
73: $collection = Mage::getResourceModel('api2/acl_filter_attribute_collection');
74: /** @var $aclFilter Mage_Api2_Model_Acl_Filter_Attribute */
75: foreach ($collection as $aclFilter) {
76: if ($aclFilter->getResourceId() != Mage_Api2_Model_Acl_Global_Rule::RESOURCE_ALL) {
77: $allowedAttributes = explode(',', $aclFilter->getAllowedAttributes());
78: $allowedAttributes = array_diff($allowedAttributes, array($attribute->getAttributeCode()));
79: $aclFilter->setAllowedAttributes(implode(',', $allowedAttributes))->save();
80: }
81: }
82: }
83:
84: return $this;
85: }
86: }
87: