QloginController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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) {
  23. $entry = $this->Qlogin->translate($key);
  24. $default = '/';
  25. if ($this->Session->read('Auth.User.id') && isset($this->Auth->loginRedirect)) {
  26. $default = $this->Auth->loginRedirect;
  27. }
  28. if (empty($entry)) {
  29. $this->Common->flashMessage(__('Invalid Key'), 'error');
  30. $this->Common->autoRedirect($default);
  31. }
  32. //die(returns($entry));
  33. $uid = $entry['CodeKey']['user_id'];
  34. $url = $entry['CodeKey']['url'];
  35. if (!$this->Session->read('Auth.User.id')) {
  36. $this->User = ClassRegistry::init(CLASS_USER);
  37. # needs to be logged in
  38. $user = $this->User->get($uid);
  39. if (!$user) {
  40. $this->Common->flashMessage(__('Invalid Account'), 'error');
  41. $this->Common->autoRedirect($default);
  42. }
  43. if ($this->Auth->login($user['User'])) {
  44. $this->Session->write('Auth.User.Login.qlogin', true);
  45. if (!Configure::read('Qlogin.suppressMessage')) {
  46. $this->Common->flashMessage(__('You successfully logged in via qlogin'), 'success');
  47. }
  48. }
  49. }
  50. $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. $this->Common->autoRedirect(array('action'=>'index'));
  93. }
  94. }