Qlogin.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. //TODO: later Auth Plugin
  3. App::uses('ToolsAppModel', 'Tools.Model');
  4. App::uses('CakeSession', 'Model/Datasource');
  5. /**
  6. * Manage Quick Logins
  7. *
  8. * TODO: Remove CodeKey BC
  9. *
  10. * @author Mark Scherer
  11. * @cakephp 2.x
  12. * @license MIT
  13. */
  14. class Qlogin extends ToolsAppModel {
  15. public $useTable = false;
  16. public $generator = 'Token'; // TODO: switch to Token ASAP, then remove this
  17. public $validate = array(
  18. 'url' => array(
  19. 'notEmpty' => array(
  20. 'rule' => array('notEmpty'),
  21. 'message' => 'valErrMandatoryField',
  22. 'last' => true
  23. ),
  24. 'validateUrl' => array(
  25. 'rule' => array('validateUrl', array('deep' => false, 'sameDomain' => true, 'autoComplete' => true)),
  26. 'message' => 'valErrInvalidQloginUrl',
  27. 'last' => true
  28. )
  29. ),
  30. 'user_id' => array(
  31. 'notEmpty' => array(
  32. 'rule' => array('notEmpty'),
  33. 'message' => 'valErrMandatoryField',
  34. 'last' => true
  35. ),
  36. ),
  37. );
  38. public function __construct($id = false, $table = null, $ds = null) {
  39. if ($generator = Configure::read('Qlogin.generator')) {
  40. $this->generator = $generator;
  41. }
  42. parent::__construct($id, $table, $ds);
  43. }
  44. /**
  45. * Qlogin::_useKey()
  46. *
  47. * @param mixed $key
  48. * @return bool Success
  49. */
  50. protected function _useKey($key) {
  51. if (!isset($this->{$this->generator})) {
  52. $this->{$this->generator} = ClassRegistry::init('Tools.' . $this->generator);
  53. }
  54. return $this->{$this->generator}->useKey('qlogin', $key);
  55. }
  56. /**
  57. * Qlogin::_newKey()
  58. *
  59. * @param mixed $uid
  60. * @param mixed $content
  61. * @return string key
  62. */
  63. protected function _newKey($uid, $content) {
  64. if (!isset($this->{$this->generator})) {
  65. $this->{$this->generator} = ClassRegistry::init('Tools.' . $this->generator);
  66. }
  67. return $this->{$this->generator}->newKey('qlogin', null, $uid, $content);
  68. }
  69. /**
  70. * Qlogin::translate()
  71. *
  72. * @param mixed $key
  73. * @return array
  74. */
  75. public function translate($key) {
  76. $res = $this->_useKey($key);
  77. if (!$res) {
  78. return false;
  79. }
  80. $res[$this->generator]['content'] = unserialize($res[$this->generator]['content']);
  81. $res[$this->generator]['url'] = Router::url($res[$this->generator]['content'], true);
  82. return $res;
  83. }
  84. /**
  85. * Generates a qlogin key
  86. *
  87. * @param mixed $url
  88. * @param string $uid
  89. * @return string key
  90. */
  91. public function generate($url, $uid) {
  92. $content = serialize($url);
  93. return $this->_newKey($uid, $content);
  94. }
  95. /**
  96. * Qlogin::urlByKey()
  97. *
  98. * @param string $key
  99. * @return string URL (absolute)
  100. */
  101. public static function urlByKey($key) {
  102. return Router::url(array('admin' => false, 'plugin' => 'tools', 'controller' => 'qlogin', 'action' => 'go', $key), true);
  103. }
  104. /**
  105. * Makes an absolute url string ready to input anywhere
  106. * uses generate() internally to get the key
  107. *
  108. * @param mixed $url
  109. * @param midex $uid (optional)
  110. * @return string URL (absolute)
  111. */
  112. public function url($url, $uid = null) {
  113. if ($uid === null) {
  114. $uid = CakeSession::read('Auth.User.id');
  115. }
  116. $key = $this->generate($url, $uid);
  117. return $this->urlByKey($key);
  118. }
  119. }