Qlogin.php 3.0 KB

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