Qlogin.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * @author Mark Scherer
  9. * @cakephp 2.x
  10. * @license MIT
  11. * 2011-11-17 ms
  12. */
  13. class Qlogin extends ToolsAppModel {
  14. public $useTable = false;
  15. public $validate = array(
  16. 'url' => array(
  17. 'notEmpty' => array(
  18. 'rule' => array('notEmpty'),
  19. 'message' => 'valErrMandatoryField',
  20. 'last' => true
  21. ),
  22. 'validateUrl' => array(
  23. 'rule' => array('validateUrl', array('deep'=>false, 'sameDomain'=>true, 'autoComplete'=>true)),
  24. 'message' => 'valErrInvalidQloginUrl',
  25. 'last' => true
  26. )
  27. ),
  28. 'user_id' => array(
  29. 'notEmpty' => array(
  30. 'rule' => array('notEmpty'),
  31. 'message' => 'valErrMandatoryField',
  32. 'last' => true
  33. ),
  34. /*
  35. 'validateUnique' => array(
  36. 'rule' => array('validateUnique', array('url')),
  37. 'message' => 'key already exists',
  38. ),
  39. */
  40. ),
  41. );
  42. protected function _useKey($key) {
  43. if (!isset($this->CodeKey)) {
  44. $this->CodeKey = ClassRegistry::init('Tools.CodeKey');
  45. }
  46. return $this->CodeKey->useKey('qlogin', $key);
  47. }
  48. protected function _newKey($uid, $content) {
  49. if (!isset($this->CodeKey)) {
  50. $this->CodeKey = ClassRegistry::init('Tools.CodeKey');
  51. }
  52. return $this->CodeKey->newKey('qlogin', null, $uid, $content);
  53. }
  54. public function translate($key) {
  55. $res = $this->_useKey($key);
  56. if (!$res) {
  57. return false;
  58. }
  59. $res['CodeKey']['content'] = unserialize($res['CodeKey']['content']);
  60. $res['CodeKey']['url'] = Router::url($res['CodeKey']['content'], true);
  61. return $res;
  62. }
  63. /**
  64. * generates a qlogin key
  65. * @param mixed $url
  66. * @param string $uid
  67. * @return string $key
  68. * 2011-07-12 ms
  69. */
  70. public function generate($url, $uid) {
  71. $content = serialize($url);
  72. return $this->_newKey($uid, $content);
  73. }
  74. public static function urlByKey($key) {
  75. return Router::url(array('admin' => false, 'plugin'=>'tools', 'controller'=>'qlogin', 'action'=>'go', $key), true);
  76. }
  77. /**
  78. * makes an absolute url string ready to input anywhere
  79. * uses generate() internally to get the key
  80. * @param mixed $url
  81. * @return string $url (absolute)
  82. */
  83. public function url($url, $uid = null) {
  84. if ($uid === null) {
  85. $uid = CakeSession::read('Auth.User.id');
  86. }
  87. $key = $this->generate($url, $uid);
  88. return $this->urlByKey($key);
  89. }
  90. }