Qlogin.php 2.2 KB

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