QloginController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. $uid = $entry['CodeKey']['user_id'];
  36. $url = $entry['CodeKey']['url'];
  37. if (!$this->Session->read('Auth.User.id')) {
  38. if ($this->Common->manualLogin($uid)) {
  39. $this->Session->write('Auth.User.Login.qlogin', true);
  40. if (!Configure::read('Qlogin.suppressMessage')) {
  41. $this->Common->flashMessage(__('You successfully logged in via qlogin'), 'success');
  42. }
  43. } else {
  44. $this->Common->flashMessage($this->Auth->loginError, 'error');
  45. $url = $default;
  46. trigger_error($this->Auth->loginError . ' - uid ' . $uid);
  47. }
  48. }
  49. return $this->redirect($url);
  50. }
  51. /**
  52. * These params can be passed to preset the form
  53. * - user_id
  54. * - url (base64encoded)
  55. *
  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->Common->flashMessage('New Key: ' . h($id), 'success');
  63. $url = $this->Qlogin->urlByKey($id);
  64. $this->set(compact('url'));
  65. $this->request->data = array();
  66. }
  67. } else {
  68. if (!empty($this->request->params['named']['user_id'])) {
  69. $this->request->data['Qlogin']['user_id'] = $this->request->params['named']['user_id'];
  70. }
  71. if (!empty($this->request->params['named']['url'])) {
  72. $this->request->data['Qlogin']['url'] = base64_decode($this->request->params['named']['url']);
  73. }
  74. }
  75. $this->User = ClassRegistry::init(CLASS_USER);
  76. $users = $this->User->find('list');
  77. $this->CodeKey = ClassRegistry::init('Tools.CodeKey');
  78. $qlogins = $this->CodeKey->find('count', array('conditions' => array('type' => 'qlogin')));
  79. $this->set(compact('users', 'qlogins'));
  80. }
  81. public function admin_listing() {
  82. }
  83. public function admin_reset() {
  84. if (!$this->Common->isPosted()) {
  85. throw new MethodNotAllowedException();
  86. }
  87. $this->CodeKey = ClassRegistry::init('Tools.CodeKey');
  88. $this->CodeKey->deleteAll(array('type' => 'qlogin'));
  89. $this->Common->flashMessage(__('Success'), 'success');
  90. return $this->Common->autoRedirect(array('action' => 'index'));
  91. }
  92. }