SessionStorage.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.1.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Auth\Storage;
  16. use Cake\Core\InstanceConfigTrait;
  17. use Cake\Network\Request;
  18. /**
  19. * Session based persistent storage for authenticated user record.
  20. */
  21. class SessionStorage implements StorageInterface
  22. {
  23. use InstanceConfigTrait;
  24. /**
  25. * User record.
  26. *
  27. * Stores user record array if fetched from session or false if session
  28. * does not have user record.
  29. *
  30. * @var array|bool
  31. */
  32. protected $_user;
  33. /**
  34. * Session object.
  35. *
  36. * @var \Cake\Network\Session
  37. */
  38. protected $_session;
  39. /**
  40. * Default configuration for this class.
  41. *
  42. * @var array
  43. */
  44. protected $_defaultConfig = [
  45. 'key' => 'Auth.User'
  46. ];
  47. /**
  48. * Constructor.
  49. *
  50. * @param \Cake\Network\Request $request Request instance.
  51. * @param array $config Configuration list.
  52. */
  53. public function __construct(Request $request, array $config = [])
  54. {
  55. $this->_session = $request->session();
  56. $this->config($config);
  57. }
  58. /**
  59. * Read user record from session.
  60. *
  61. * @return array|null User record if available else null.
  62. */
  63. public function read()
  64. {
  65. if ($this->_user !== null) {
  66. return $this->_user ?: null;
  67. }
  68. $this->_user = $this->_session->read($this->_config['key']) ?: false;
  69. return $this->_user;
  70. }
  71. /**
  72. * Write user record to session.
  73. *
  74. * The session id is also renewed to help mitigate issues with session replays.
  75. *
  76. * @param array $user User record.
  77. * @return void
  78. */
  79. public function write(array $user)
  80. {
  81. $this->_user = $user;
  82. $this->_session->renew();
  83. $this->_session->write($this->_config['key'], $user);
  84. }
  85. /**
  86. * Delete user record from session.
  87. *
  88. * The session id is also renewed to help mitigate issues with session replays.
  89. *
  90. * @return void
  91. */
  92. public function delete()
  93. {
  94. $this->_user = false;
  95. $this->_session->delete($this->_config['key']);
  96. $this->_session->renew();
  97. }
  98. }