XOOPS  2.6.0
SessionUser.php
Go to the documentation of this file.
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 
16 
28 {
29 
33  protected $session;
34 
38  protected $xoops = null;
39 
44  public function __construct(Manager $session)
45  {
46  $this->session = $session;
47  $this->xoops = \Xoops::getInstance();
48  }
49 
50 
58  public function establish()
59  {
61 
62  // is user already set in session?
63  if ($session->has('xoopsUserId')) {
64  $this->addUserToSession($session->get('xoopsUserId'));
65  return;
66  }
67 
68  // is the usercookie available?
69  $remember = new RememberMe;
70  $userId = $remember->recall();
71  if (false !== $userId) {
72  $this->setNeedsConfirmed();
73  $this->addUserToSession($userId);
74  }
75  }
76 
77 
88  public function recordUserLogin($userId, $rememberMe = false)
89  {
90  $this->setConfirmed();
91  $this->addUserToSession($userId);
92  if ($rememberMe) {
93  $remember = new RememberMe;
94  $remember->createUserCookie($userId);
95  }
96  }
97 
105  public function recordUserLogout()
106  {
107  $remember = new RememberMe;
108  $remember->forget();
109  $this->session->clearSession();
110  }
111 
119  public function addUserToSession($userId)
120  {
122  $memberHandler = $this->xoops->getHandlerMember();
123  $user = $memberHandler->getUser($userId);
124  if ($user instanceof \XoopsUser) {
125  if ($user->isActive()) {
126  // make sure all primary user data is consistent
127  $session->set('xoopsUserId', $user->getVar('uid'));
128  $session->set('xoopsUserGroups', $user->getGroups());
129  if (!$session->has('SESSION_AUTHSTATUS')) {
130  $this->setNeedsConfirmed();
131  }
132  // all is good, leave the existing info
133  return;
134  }
135  }
136  // invalid user - clear everything
137  $session->clearSession();
138  return;
139  }
140 
146  public function setNeedsConfirmed()
147  {
148  $this->session->set('SESSION_AUTHSTATUS', 'confirm');
149  }
150 
156  public function setConfirmed()
157  {
158  $this->session->set('SESSION_AUTHSTATUS', 'ok');
159  }
160 
166  public function checkConfirmed()
167  {
168  return $this->session->has('xoopsUserId') &&
169  ('ok' == $this->session->get('SESSION_AUTHSTATUS', 'failed'));
170  }
171 }
static getInstance()
Definition: Xoops.php:160
$user
Definition: Xoops.php:58
session()
Definition: Xoops.php:898
recordUserLogin($userId, $rememberMe=false)
Definition: SessionUser.php:88
__construct(Manager $session)
Definition: SessionUser.php:44