1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xoops\Core\Session;
13:
14: use Xoops\Core\HttpRequest;
15: use Xoops\Core\Request;
16: use Xoops\Core\Kernel\Handlers\XoopsUser;
17:
18: /**
19: * Manage the session representation of a the current User
20: *
21: * @category Xoops\Core\Session
22: * @package SessionUser
23: * @author Richard Griffith <richard@geekwright.com>
24: * @copyright 2015 XOOPS Project (http://xoops.org)
25: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
26: * @link http://xoops.org
27: */
28: class SessionUser
29: {
30:
31: /**
32: * @var Manager
33: */
34: protected $session;
35:
36: /**
37: * @var \Xoops
38: */
39: protected $xoops = null;
40:
41: /**
42: * constructor
43: * @param Manager $session the session manager object
44: */
45: public function __construct(Manager $session)
46: {
47: $this->session = $session;
48: $this->xoops = \Xoops::getInstance();
49: }
50:
51:
52: /**
53: * Check any user data in the current session and clear if invalid.
54: *
55: * If no user data, check if "remember me" data should be applied
56: *
57: * @return void
58: */
59: public function establish()
60: {
61: $session = $this->session;
62:
63: // is user already set in session?
64: if ($session->has('xoopsUserId')) {
65: $this->addUserToSession($session->get('xoopsUserId'));
66: return;
67: }
68:
69: // is the usercookie available?
70: $remember = new RememberMe;
71: $userId = $remember->recall();
72: if (false !== $userId) {
73: $this->setNeedsConfirmed();
74: $this->addUserToSession($userId);
75: }
76: }
77:
78:
79: /**
80: * Record a login event in the session. This is to be called by the login
81: * process, i.e. the user has entered the name and password, and that
82: * combination was found valid.
83: *
84: * @param integer $userId id of user to establish in the session
85: * @param boolean $rememberMe add a persistent login cookie
86: *
87: * @return void
88: */
89: public function recordUserLogin($userId, $rememberMe = false)
90: {
91: $this->setConfirmed();
92: $this->addUserToSession($userId);
93: if ($rememberMe) {
94: $remember = new RememberMe;
95: $remember->createUserCookie($userId);
96: }
97: }
98:
99: /**
100: * Record a login event in the session. This is to be called by the login
101: * process, i.e. the user has entered the name and password, and that
102: * combination was found valid.
103: *
104: * @return void
105: */
106: public function recordUserLogout()
107: {
108: $remember = new RememberMe;
109: $remember->forget();
110: $this->session->clearSession();
111: }
112:
113: /**
114: * Check the we have a remember me cookie, and apply if valid
115: *
116: * @param integer $userId id of user to establish in the session
117: *
118: * @return void
119: */
120: public function addUserToSession($userId)
121: {
122: $session = $this->session;
123: $memberHandler = $this->xoops->getHandlerMember();
124: $user = $memberHandler->getUser($userId);
125: if ($user instanceof XoopsUser) {
126: if ($user->isActive()) {
127: // make sure all primary user data is consistent
128: $session->set('xoopsUserId', $user->getVar('uid'));
129: $session->set('xoopsUserGroups', $user->getGroups());
130: if (!$session->has('SESSION_AUTHSTATUS')) {
131: $this->setNeedsConfirmed();
132: }
133: // all is good, leave the existing info
134: return;
135: }
136: }
137: // invalid user - clear everything
138: $session->clearSession();
139: return;
140: }
141:
142: /**
143: * set authorization status to needs confirmed
144: *
145: * @return void
146: */
147: public function setNeedsConfirmed()
148: {
149: $this->session->set('SESSION_AUTHSTATUS', 'confirm');
150: }
151:
152: /**
153: * set authorization status to is confirmed
154: *
155: * @return void
156: */
157: public function setConfirmed()
158: {
159: $this->session->set('SESSION_AUTHSTATUS', 'ok');
160: }
161:
162: /**
163: * verify the authorization status is confirmed
164: *
165: * @return boolean true if confirmed, otherwise false
166: */
167: public function checkConfirmed()
168: {
169: return $this->session->has('xoopsUserId') &&
170: ('ok' === $this->session->get('SESSION_AUTHSTATUS', 'failed'));
171: }
172: }
173: