Qlogin.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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: make it work with Token by default.
  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 = 'CodeKey'; // 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. /**
  39. * Qlogin::_useKey()
  40. *
  41. * @param mixed $key
  42. * @return boolean Success
  43. */
  44. protected function _useKey($key) {
  45. if (!isset($this->{$this->generator})) {
  46. $this->{$this->generator} = ClassRegistry::init('Tools.' . $this->generator);
  47. }
  48. return $this->{$this->generator}->useKey('qlogin', $key);
  49. }
  50. /**
  51. * Qlogin::_newKey()
  52. *
  53. * @param mixed $uid
  54. * @param mixed $content
  55. * @return string $key
  56. */
  57. protected function _newKey($uid, $content) {
  58. if (!isset($this->{$this->generator})) {
  59. $this->{$this->generator} = ClassRegistry::init('Tools.' . $this->generator);
  60. }
  61. return $this->{$this->generator}->newKey('qlogin', null, $uid, $content);
  62. }
  63. /**
  64. * Qlogin::translate()
  65. *
  66. * @param mixed $key
  67. * @return array
  68. */
  69. public function translate($key) {
  70. $res = $this->_useKey($key);
  71. if (!$res) {
  72. return false;
  73. }
  74. $res[$this->generator]['content'] = unserialize($res[$this->generator]['content']);
  75. $res[$this->generator]['url'] = Router::url($res[$this->generator]['content'], true);
  76. return $res;
  77. }
  78. /**
  79. * Generates a qlogin key
  80. *
  81. * @param mixed $url
  82. * @param string $uid
  83. * @return string key
  84. */
  85. public function generate($url, $uid) {
  86. $content = serialize($url);
  87. return $this->_newKey($uid, $content);
  88. }
  89. /**
  90. * Qlogin::urlByKey()
  91. *
  92. * @param string $key
  93. * @return string URL (absolute)
  94. */
  95. public static function urlByKey($key) {
  96. return Router::url(array('admin' => false, 'plugin' => 'tools', 'controller' => 'qlogin', 'action' => 'go', $key), true);
  97. }
  98. /**
  99. * Makes an absolute url string ready to input anywhere
  100. * uses generate() internally to get the key
  101. *
  102. * @param mixed $url
  103. * @param midex $uid (optional)
  104. * @return string URL (absolute)
  105. */
  106. public function url($url, $uid = null) {
  107. if ($uid === null) {
  108. $uid = CakeSession::read('Auth.User.id');
  109. }
  110. $key = $this->generate($url, $uid);
  111. return $this->urlByKey($key);
  112. }
  113. }