1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Oauth_Adminhtml_Oauth_ConsumerController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39: 40: 41: 42:
43: protected function _filter(array $data)
44: {
45: foreach (array('id', 'back', 'form_key', 'key', 'secret') as $field) {
46: if (isset($data[$field])) {
47: unset($data[$field]);
48: }
49: }
50: return $data;
51: }
52:
53: 54: 55: 56: 57:
58: public function preDispatch()
59: {
60: $this->_title($this->__('System'))
61: ->_title($this->__('OAuth'))
62: ->_title($this->__('Consumers'));
63: parent::preDispatch();
64: return $this;
65: }
66:
67: 68: 69:
70: public function indexAction()
71: {
72: $this->loadLayout();
73: $this->renderLayout();
74: }
75:
76: 77: 78:
79: public function gridAction()
80: {
81: $this->loadLayout();
82: $this->renderLayout();
83: }
84:
85: 86: 87:
88: public function newAction()
89: {
90:
91: $model = Mage::getModel('oauth/consumer');
92:
93: $formData = $this->_getFormData();
94: if ($formData) {
95: $this->_setFormData($formData);
96: $model->addData($formData);
97: } else {
98:
99: $helper = Mage::helper('oauth');
100: $model->setKey($helper->generateConsumerKey());
101: $model->setSecret($helper->generateConsumerSecret());
102: $this->_setFormData($model->getData());
103: }
104:
105: Mage::register('current_consumer', $model);
106:
107: $this->loadLayout();
108: $this->renderLayout();
109: }
110:
111: 112: 113:
114: public function editAction()
115: {
116: $id = (int) $this->getRequest()->getParam('id');
117:
118: if (!$id) {
119: $this->_getSession()->addError(Mage::helper('oauth')->__('Invalid ID parameter.'));
120: $this->_redirect('*/*/index');
121: return;
122: }
123:
124:
125: $model = Mage::getModel('oauth/consumer');
126: $model->load($id);
127:
128: if (!$model->getId()) {
129: $this->_getSession()->addError(Mage::helper('oauth')->__('Entry with ID #%s not found.', $id));
130: $this->_redirect('*/*/index');
131: return;
132: }
133:
134: $model->addData($this->_filter($this->getRequest()->getParams()));
135: Mage::register('current_consumer', $model);
136:
137: $this->loadLayout();
138: $this->renderLayout();
139: }
140:
141: 142: 143:
144: public function saveAction()
145: {
146: $id = $this->getRequest()->getParam('id');
147: if (!$this->_validateFormKey()) {
148: if ($id) {
149: $this->_redirect('*/*/edit', array('id' => $id));
150: } else {
151: $this->_redirect('*/*/new', array('id' => $id));
152: }
153: return;
154: }
155:
156: $data = $this->_filter($this->getRequest()->getParams());
157:
158:
159: $model = Mage::getModel('oauth/consumer');
160:
161: if ($id) {
162: if (!(int) $id) {
163: $this->_getSession()->addError(
164: $this->__('Invalid ID parameter.'));
165: $this->_redirect('*/*/index');
166: return;
167: }
168: $model->load($id);
169:
170: if (!$model->getId()) {
171: $this->_getSession()->addError(
172: $this->__('Entry with ID #%s not found.', $id));
173: $this->_redirect('*/*/index');
174: return;
175: }
176: } else {
177: $dataForm = $this->_getFormData();
178: if ($dataForm) {
179: $data['key'] = $dataForm['key'];
180: $data['secret'] = $dataForm['secret'];
181: } else {
182:
183:
184:
185: $helper = Mage::helper('oauth');
186:
187: $data['key'] = $helper->generateConsumerKey();
188: $data['secret'] = $helper->generateConsumerSecret();
189: }
190: }
191:
192: try {
193: $model->addData($data);
194: $model->save();
195: $this->_getSession()->addSuccess($this->__('The consumer has been saved.'));
196: $this->_setFormData(null);
197: } catch (Mage_Core_Exception $e) {
198: $this->_setFormData($data);
199: $this->_getSession()->addError(Mage::helper('core')->escapeHtml($e->getMessage()));
200: $this->getRequest()->setParam('back', 'edit');
201: } catch (Exception $e) {
202: $this->_setFormData(null);
203: Mage::logException($e);
204: $this->_getSession()->addError($this->__('An error occurred on saving consumer data.'));
205: }
206:
207: if ($this->getRequest()->getParam('back')) {
208: if ($id || $model->getId()) {
209: $this->_redirect('*/*/edit', array('id' => $model->getId()));
210: } else {
211: $this->_redirect('*/*/new');
212: }
213: } else {
214: $this->_redirect('*/*/index');
215: }
216: }
217:
218: 219: 220: 221: 222:
223: protected function _isAllowed()
224: {
225: $action = $this->getRequest()->getActionName();
226: if ('index' == $action) {
227: $action = null;
228: } else {
229: if ('new' == $action || 'save' == $action) {
230: $action = 'edit';
231: }
232: $action = '/' . $action;
233: }
234:
235: $session = Mage::getSingleton('admin/session');
236: return $session->isAllowed('system/oauth/consumer' . $action);
237: }
238:
239: 240: 241: 242: 243:
244: protected function _getFormData()
245: {
246: return $this->_getSession()->getData('consumer_data', true);
247: }
248:
249: 250: 251: 252: 253: 254:
255: protected function _setFormData($data)
256: {
257: $this->_getSession()->setData('consumer_data', $data);
258: return $this;
259: }
260:
261: 262: 263:
264: public function deleteAction()
265: {
266: $consumerId = (int) $this->getRequest()->getParam('id');
267: if ($consumerId) {
268: try {
269:
270: $consumer = Mage::getModel('oauth/consumer')->load($consumerId);
271: if (!$consumer->getId()) {
272: Mage::throwException(Mage::helper('oauth')->__('Unable to find a consumer.'));
273: }
274:
275: $consumer->delete();
276:
277: $this->_getSession()->addSuccess(Mage::helper('oauth')->__('The consumer has been deleted.'));
278: } catch (Mage_Core_Exception $e) {
279: $this->_getSession()->addError($e->getMessage());
280: } catch (Exception $e) {
281: $this->_getSession()->addException(
282: $e, Mage::helper('oauth')->__('An error occurred while deleting the consumer.')
283: );
284: }
285: }
286: $this->_redirect('*/*/index');
287: }
288: }
289: