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_AuthorizeController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39: 40:
41: protected $_sessionName = 'admin/session';
42:
43: 44: 45: 46: 47:
48: public $_publicActions = array('index', 'simple', 'confirm', 'confirmSimple','reject', 'rejectSimple');
49:
50: 51: 52: 53: 54: 55:
56: public function preDispatch()
57: {
58: $this->getRequest()->setParam('forwarded', true);
59:
60:
61: $loginError = $this->_checkLoginIsEmpty();
62:
63: parent::preDispatch();
64:
65:
66: if ($loginError) {
67: Mage::getSingleton('adminhtml/session')
68: ->addError(Mage::helper('adminhtml')->__('Invalid User Name or Password.'));
69: $params = array('_query' => array('oauth_token' => $this->getRequest()->getParam('oauth_token', null)));
70: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
71: $this->setFlag('', self::FLAG_NO_POST_DISPATCH, true);
72: $params = array('_query' => array('oauth_token' => $this->getRequest()->getParam('oauth_token', null)));
73: $this->_redirect('*/*/*', $params);
74: }
75: }
76:
77: 78: 79: 80: 81:
82: public function indexAction()
83: {
84: $this->_initForm();
85:
86: $this->_initLayoutMessages($this->_sessionName);
87: $this->renderLayout();
88: }
89:
90: 91: 92: 93: 94:
95: public function simpleAction()
96: {
97: $this->_initForm(true);
98: $this->_initLayoutMessages($this->_sessionName);
99: $this->renderLayout();
100: }
101:
102: 103: 104: 105: 106: 107:
108: protected function _initForm($simple = false)
109: {
110:
111: $server = Mage::getModel('oauth/server');
112:
113: $session = Mage::getSingleton($this->_sessionName);
114:
115: $isException = false;
116: try {
117: $server->checkAuthorizeRequest();
118: } catch (Mage_Core_Exception $e) {
119: $session->addError($e->getMessage());
120: } catch (Mage_Oauth_Exception $e) {
121: $isException = true;
122: $session->addException($e, $this->__('An error occurred. Your authorization request is invalid.'));
123: } catch (Exception $e) {
124: $isException = true;
125: $session->addException($e, $this->__('An error occurred.'));
126: }
127:
128: $this->loadLayout();
129: $layout = $this->getLayout();
130: $logged = $session->isLoggedIn();
131:
132: $contentBlock = $layout->getBlock('content');
133: if ($logged) {
134: $contentBlock->unsetChild('oauth.authorize.form');
135:
136: $block = $contentBlock->getChild('oauth.authorize.button');
137: } else {
138: $contentBlock->unsetChild('oauth.authorize.button');
139:
140: $block = $contentBlock->getChild('oauth.authorize.form');
141: }
142:
143: $block->setIsSimple($simple)
144: ->setToken($this->getRequest()->getQuery('oauth_token'))
145: ->setHasException($isException);
146: return $this;
147: }
148:
149: 150: 151: 152: 153: 154:
155: protected function _initConfirmPage($simple = false)
156: {
157:
158: $helper = Mage::helper('oauth');
159:
160:
161: $session = Mage::getSingleton($this->_sessionName);
162:
163:
164: $user = $session->getData('user');
165: if (!$user) {
166: $session->addError($this->__('Please login to proceed authorization.'));
167: $url = $helper->getAuthorizeUrl(Mage_Oauth_Model_Token::USER_TYPE_ADMIN);
168: $this->_redirectUrl($url);
169: return $this;
170: }
171:
172: $this->loadLayout();
173:
174:
175: $block = $this->getLayout()->getBlock('content')->getChild('oauth.authorize.confirm');
176: $block->setIsSimple($simple);
177:
178: try {
179:
180: $server = Mage::getModel('oauth/server');
181:
182: $token = $server->authorizeToken($user->getId(), Mage_Oauth_Model_Token::USER_TYPE_ADMIN);
183:
184: if (($callback = $helper->getFullCallbackUrl($token))) {
185: $this->getResponse()->setRedirect($callback . ($simple ? '&simple=1' : ''));
186: return $this;
187: } else {
188: $block->setVerifier($token->getVerifier());
189: $session->addSuccess($this->__('Authorization confirmed.'));
190: }
191: } catch (Mage_Core_Exception $e) {
192: $block->setHasException(true);
193: $session->addError($e->getMessage());
194: } catch (Exception $e) {
195: $block->setHasException(true);
196: $session->addException($e, $this->__('An error occurred on confirm authorize.'));
197: }
198:
199: $this->_initLayoutMessages($this->_sessionName);
200: $this->renderLayout();
201:
202: return $this;
203: }
204:
205: 206: 207: 208: 209: 210:
211: protected function _initRejectPage($simple = false)
212: {
213:
214: $server = Mage::getModel('oauth/server');
215:
216:
217: $session = Mage::getSingleton($this->_sessionName);
218:
219: $this->loadLayout();
220:
221:
222: $block = $this->getLayout()->getBlock('oauth.authorize.reject');
223: $block->setIsSimple($simple);
224:
225: try {
226: $token = $server->checkAuthorizeRequest();
227:
228: $helper = Mage::helper('oauth');
229:
230: if (($callback = $helper->getFullCallbackUrl($token, true))) {
231: $this->_redirectUrl($callback . ($simple ? '&simple=1' : ''));
232: return $this;
233: } else {
234: $session->addNotice($this->__('The application access request is rejected.'));
235: }
236: } catch (Mage_Core_Exception $e) {
237: $session->addError($e->getMessage());
238: } catch (Exception $e) {
239: $session->addException($e, $this->__('An error occurred on reject authorize.'));
240: }
241:
242:
243: $this->_initLayoutMessages($this->_sessionName);
244: $this->renderLayout();
245:
246: return $this;
247: }
248:
249: 250: 251: 252: 253: 254:
255: protected function _checkLoginIsEmpty()
256: {
257: $error = false;
258: $action = $this->getRequest()->getActionName();
259: if (($action == 'index' || $action == 'simple') && $this->getRequest()->getPost('login')) {
260: $postLogin = $this->getRequest()->getPost('login');
261: $username = isset($postLogin['username']) ? $postLogin['username'] : '';
262: $password = isset($postLogin['password']) ? $postLogin['password'] : '';
263: if (empty($username) || empty($password)) {
264: $error = true;
265: }
266: }
267: return $error;
268: }
269:
270: 271: 272:
273: public function confirmAction()
274: {
275: $this->_initConfirmPage();
276: }
277:
278: 279: 280:
281: public function confirmSimpleAction()
282: {
283: $this->_initConfirmPage();
284: }
285:
286: 287: 288:
289: public function rejectAction()
290: {
291: $this->_initRejectPage();
292: }
293:
294: 295: 296:
297: public function rejectSimpleAction()
298: {
299: $this->_initRejectPage();
300: }
301: }
302: