QloginController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. if (!defined('CLASS_USER')) {
  3. define('CLASS_USER', 'User');
  4. }
  5. App::uses('ToolsAppController', 'Tools.Controller');
  6. class QloginController extends ToolsAppController {
  7. public $uses = array('Tools.Qlogin');
  8. public $components = array('Tools.Common');
  9. public function beforeFilter() {
  10. parent::beforeFilter();
  11. if (isset($this->Auth)) {
  12. $this->Auth->allow('go');
  13. }
  14. }
  15. /****************************************************************************************
  16. * ADMIN functions
  17. ****************************************************************************************/
  18. /**
  19. * Main login function
  20. */
  21. public function go($key = null) {
  22. if (!$key) {
  23. throw new NotFoundException();
  24. }
  25. $entry = $this->Qlogin->translate($key);
  26. $default = '/';
  27. if ($this->Session->read('Auth.User.id') && isset($this->Auth->loginRedirect)) {
  28. $default = $this->Auth->loginRedirect;
  29. }
  30. if (empty($entry)) {
  31. $this->Common->flashMessage(__('Invalid Key'), 'error');
  32. return $this->Common->autoRedirect($default);
  33. }
  34. //die(returns($entry));
  35. $alias = Configure::read('Qlogin.generator') ?: 'Token';
  36. $uid = $entry[$alias]['user_id'];
  37. $url = $entry[$alias]['url'];
  38. if (!$this->Session->read('Auth.User.id')) {
  39. if ($this->Common->manualLogin($uid)) {
  40. $this->Session->write('Auth.User.Login.qlogin', true);
  41. if (!Configure::read('Qlogin.suppressMessage')) {
  42. $this->Common->flashMessage(__('You successfully logged in via qlogin'), 'success');
  43. }
  44. } else {
  45. $this->Common->flashMessage($this->Auth->loginError, 'error');
  46. $url = $default;
  47. trigger_error($this->Auth->loginError . ' - uid ' . $uid);
  48. }
  49. }
  50. return $this->redirect($url);
  51. }
  52. /**
  53. * These params can be passed to preset the form
  54. * - user_id
  55. * - url (base64encoded)
  56. *
  57. * @return void
  58. */
  59. public function admin_index() {
  60. if ($this->Common->isPosted()) {
  61. $this->Qlogin->set($this->request->data);
  62. if ($this->Qlogin->validates()) {
  63. $id = $this->Qlogin->generate($this->Qlogin->data['Qlogin']['url'], $this->Qlogin->data['Qlogin']['user_id']);
  64. $this->Common->flashMessage('New Key: ' . h($id), 'success');
  65. $url = $this->Qlogin->urlByKey($id);
  66. $this->set(compact('url'));
  67. $this->request->data = array();
  68. }
  69. } else {
  70. if (!empty($this->request->params['named']['user_id'])) {
  71. $this->request->data['Qlogin']['user_id'] = $this->request->params['named']['user_id'];
  72. }
  73. if (!empty($this->request->params['named']['url'])) {
  74. $this->request->data['Qlogin']['url'] = base64_decode($this->request->params['named']['url']);
  75. }
  76. }
  77. $this->User = ClassRegistry::init(CLASS_USER);
  78. $users = $this->User->find('list');
  79. $this->Token = ClassRegistry::init('Tools.Token');
  80. $qlogins = $this->Token->find('count', array('conditions' => array('type' => 'qlogin')));
  81. $this->set(compact('users', 'qlogins'));
  82. }
  83. /**
  84. * QloginController::admin_listing()
  85. *
  86. * @return void
  87. */
  88. public function admin_listing() {
  89. }
  90. /**
  91. * QloginController::admin_reset()
  92. *
  93. * @return void
  94. */
  95. public function admin_reset() {
  96. $this->request->onlyAllow('post', 'delete');
  97. $this->Token = ClassRegistry::init('Tools.Token');
  98. $this->Token->deleteAll(array('type' => 'qlogin'));
  99. $this->Common->flashMessage(__('Success'), 'success');
  100. return $this->Common->autoRedirect(array('action' => 'index'));
  101. }
  102. }