QloginController.php 2.8 KB

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