QloginController.php 3.2 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 = 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. * 2011-07-11 ms
  21. */
  22. public function go($key = null) {
  23. if (!$key) {
  24. throw new NotFoundException();
  25. }
  26. $entry = $this->Qlogin->translate($key);
  27. $default = '/';
  28. if ($this->Session->read('Auth.User.id') && isset($this->Auth->loginRedirect)) {
  29. $default = $this->Auth->loginRedirect;
  30. }
  31. if (empty($entry)) {
  32. $this->Common->flashMessage(__('Invalid Key'), 'error');
  33. return $this->Common->autoRedirect($default);
  34. }
  35. //die(returns($entry));
  36. $uid = $entry['CodeKey']['user_id'];
  37. $url = $entry['CodeKey']['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. * 2012-03-04 ms
  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->CodeKey = ClassRegistry::init('Tools.CodeKey');
  80. $qlogins = $this->CodeKey->find('count', array('conditions'=>array('type'=>'qlogin')));
  81. $this->set(compact('users', 'qlogins'));
  82. }
  83. public function admin_listing() {
  84. }
  85. public function admin_reset() {
  86. if (!$this->Common->isPosted()) {
  87. throw new MethodNotAllowedException();
  88. }
  89. $this->CodeKey = ClassRegistry::init('Tools.CodeKey');
  90. $this->CodeKey->deleteAll(array('type'=>'qlogin'));
  91. $this->Common->flashMessage(__('Success'), 'success');
  92. return $this->Common->autoRedirect(array('action'=>'index'));
  93. }
  94. }