QloginController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. $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. $this->User = ClassRegistry::init(CLASS_USER);
  40. # needs to be logged in
  41. $user = $this->User->get($uid);
  42. if (!$user) {
  43. $this->Common->flashMessage(__('Invalid Account'), 'error');
  44. $this->Common->autoRedirect($default);
  45. }
  46. if ($this->Auth->login($user['User'])) {
  47. $this->Session->write('Auth.User.Login.qlogin', true);
  48. if (!Configure::read('Qlogin.suppressMessage')) {
  49. $this->Common->flashMessage(__('You successfully logged in via qlogin'), 'success');
  50. }
  51. }
  52. }
  53. $this->redirect($url);
  54. }
  55. /**
  56. * these params can be passed to preset the form
  57. * - user_id
  58. * - url (base64encoded)
  59. *
  60. * 2012-03-04 ms
  61. */
  62. public function admin_index() {
  63. if ($this->Common->isPosted()) {
  64. $this->Qlogin->set($this->request->data);
  65. if ($this->Qlogin->validates()) {
  66. $id = $this->Qlogin->generate($this->Qlogin->data['Qlogin']['url'], $this->Qlogin->data['Qlogin']['user_id']);
  67. $this->Common->flashMessage('New Key: '.h($id), 'success');
  68. $url = $this->Qlogin->urlByKey($id);
  69. $this->set(compact('url'));
  70. $this->request->data = array();
  71. }
  72. } else {
  73. if (!empty($this->request->params['named']['user_id'])) {
  74. $this->request->data['Qlogin']['user_id'] = $this->request->params['named']['user_id'];
  75. }
  76. if (!empty($this->request->params['named']['url'])) {
  77. $this->request->data['Qlogin']['url'] = base64_decode($this->request->params['named']['url']);
  78. }
  79. }
  80. $this->User = ClassRegistry::init(CLASS_USER);
  81. $users = $this->User->find('list');
  82. $this->CodeKey = ClassRegistry::init('Tools.CodeKey');
  83. $qlogins = $this->CodeKey->find('count', array('conditions'=>array('type'=>'qlogin')));
  84. $this->set(compact('users', 'qlogins'));
  85. }
  86. public function admin_listing() {
  87. }
  88. public function admin_reset() {
  89. if (!$this->Common->isPosted()) {
  90. throw new MethodNotAllowedException();
  91. }
  92. $this->CodeKey = ClassRegistry::init('Tools.CodeKey');
  93. $this->CodeKey->deleteAll(array('type'=>'qlogin'));
  94. $this->Common->flashMessage(__('Success'), 'success');
  95. $this->Common->autoRedirect(array('action'=>'index'));
  96. }
  97. }