| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- if (!defined('CLASS_USER')) {
- define('CLASS_USER', 'User');
- }
- App::uses('ToolsAppController', 'Tools.Controller');
- class QloginController extends ToolsAppController {
- public $uses = ['Tools.Qlogin'];
- public $components = ['Tools.Flash', 'Tools.Common'];
- public function beforeFilter() {
- parent::beforeFilter();
- if (isset($this->Auth)) {
- $this->Auth->allow('go');
- }
- }
- /**
- * Main login function
- *
- * @return void
- */
- public function go($key = null) {
- if (!$key) {
- throw new NotFoundException();
- }
- $entry = $this->Qlogin->translate($key);
- $default = '/';
- if ($this->Session->read('Auth.User.id') && isset($this->Auth->loginRedirect)) {
- $default = $this->Auth->loginRedirect;
- }
- if (empty($entry)) {
- $this->Flash->error(__d('tools', 'Invalid Key'));
- return $this->Common->autoRedirect($default);
- }
- $alias = Configure::read('Qlogin.generator') ?: 'Token';
- $uid = $entry[$alias]['user_id'];
- $url = $entry[$alias]['url'];
- if (!$this->Session->read('Auth.User.id')) {
- if ($this->Common->manualLogin($uid)) {
- $this->Session->write('Auth.User.Login.qlogin', true);
- if (!Configure::read('Qlogin.suppressMessage')) {
- $this->Flash->success(__d('tools', 'You successfully logged in via qlogin'));
- }
- } else {
- $this->Flash->error($this->Auth->loginError);
- $url = $default;
- trigger_error($this->Auth->loginError . ' - uid ' . $uid);
- }
- }
- return $this->redirect($url);
- }
- /**
- * These params can be passed to preset the form
- * - user_id
- * - url (base64encoded)
- *
- * @return void
- */
- public function admin_index() {
- if ($this->Common->isPosted()) {
- $this->Qlogin->set($this->request->data);
- if ($this->Qlogin->validates()) {
- $id = $this->Qlogin->generate($this->Qlogin->data['Qlogin']['url'], $this->Qlogin->data['Qlogin']['user_id']);
- $this->Flash->success('New Key: ' . h($id));
- $url = $this->Qlogin->urlByKey($id);
- $this->set(compact('url'));
- $this->request->data = [];
- }
- } else {
- $this->request->data['Qlogin'] = $this->request->query;
- }
- $this->User = ClassRegistry::init(CLASS_USER);
- $users = $this->User->find('list');
- $this->Token = ClassRegistry::init('Tools.Token');
- $qlogins = $this->Token->find('count', ['conditions' => ['type' => 'qlogin']]);
- $this->set(compact('users', 'qlogins'));
- }
- /**
- * QloginController::admin_listing()
- *
- * @return void
- */
- public function admin_listing() {
- }
- /**
- * QloginController::admin_reset()
- *
- * @return void
- */
- public function admin_reset() {
- $this->request->allowMethod(['post', 'delete']);
- $this->Token = ClassRegistry::init('Tools.Token');
- $this->Token->deleteAll(['type' => 'qlogin']);
- $this->Flash->success(__d('tools', 'Success'));
- return $this->Common->autoRedirect(['action' => 'index']);
- }
- }
|