src/snac/server/database/DBUser.php
/**
     * Check that a session is active, with side effects.
     *
     * You probably want to use sessionExists(), sessionActive() and readUser(). This function may be
     * deprecated.
     *
     * Check that we have a non-expired session for $user and with token getToken(). Time is assumed to be
     * "now" UTC. Return the User on success, false otherwise. If the user does not exist, a DB record is
     * created in appuser. If the session does not exist, a session is created in table session. If the
     * session is active, 'expires' is updated. If the session has expired, it is deleted, and the User object
     * token is cleared.
     *
     * This checks that a session is active and associated with the $user. In theory is it possible to
     * ask if a session is active, without knowing the user. In fact, a session could be check as active, and
     * could return the user id.
     *
     * It is important to read or create a user at the top of the function. Everything after depends on a
     * valid User from the SNAC appuser database table.
     *
     * Features: auto-create unknown user, auto-create unknown session, delete expired session.
     *
     * @param \snac\data\User $user User object
     *
     * @return \snac\data\User when successful or return false on failure.
     */
Check that a session is active, with side effects.
You probably want to use sessionExists(), sessionActive() and readUser(). This function may be
deprecated.

Check that we have a non-expired session for $user and with token getToken(). Time is assumed to be
"now" UTC. Return the User on success, false otherwise. If the user does not exist, a DB record is
created in appuser. If the session does not exist, a session is created in table session. If the
session is active, 'expires' is updated. If the session has expired, it is deleted, and the User object
token is cleared.

This checks that a session is active and associated with the $user. In theory is it possible to
ask if a session is active, without knowing the user. In fact, a session could be check as active, and
could return the user id.

It is important to read or create a user at the top of the function. Everything after depends on a
valid User from the SNAC appuser database table.

auto-create unknown user, auto-create unknown session, delete expired session.

@param \snac\data\User $user User object

@return \snac\data\User when successful or return false on failure.
Check that a session is active with side effects . You probably want to use sessionExists () sessionActive () and readUser () . This function may be deprecated . Check that we have a non - expired session for $user and with token getToken () . Time is assumed to be " now " UTC . Return the User on success false otherwise . If the user does not exist a DB record is created in appuser . If the session does not exist a session is created in table session . If the session is active ' expires ' is updated . If the session has expired it is deleted and the User object token is cleared . This checks that a session is active and associated with the $user . In theory is it possible to ask if a session is active without knowing the user . In fact a session could be check as active and could return the user id . It is important to read or create a user at the top of the function . Everything after depends on a valid User from the SNAC appuser database table . auto - create unknown user auto - create unknown session delete expired session . @param \ snac \ data \ User $user User object @return \ snac \ data \ User when successful or return false on failure .
public function checkSessionActive($user)
    {
        $currentToken = $user->getToken();
        $accessToken = $currentToken['access_token'];
        $newUser = $this->readUser($user);
        if ($newUser === false) {
            $newUser = $this->createUser($user);
        }
        $newUser->setToken($user->getToken());
        if (! $this->sessionExists($newUser))
        {
            $this->addSession($newUser); 
            return $newUser;
        }
        if (! $this->sql->selectActive($newUser->getUserID(), $accessToken))
        {
            $this->sql->deleteSession($newUser->getToken()['access_token']);
            $newUser->setToken(array('access_token' => '', 'expires' => 0));
            return false;
        }
        return $newUser;
    }
public function checkSessionActive ( $ user ) { $ currentToken = $ user -> getToken ( ) ; $ accessToken = $ currentToken [ 'access_token' ] ; $ newUser = $ this -> readUser ( $ user ) ; if ( $ newUser === false ) { $ newUser = $ this -> createUser ( $ user ) ; } $ newUser -> setToken ( $ user -> getToken ( ) ) ; if ( ! $ this -> sessionExists ( $ newUser ) ) { $ this -> addSession ( $ newUser ) ; return $ newUser ; } if ( ! $ this -> sql -> selectActive ( $ newUser -> getUserID ( ) , $ accessToken ) ) { $ this -> sql -> deleteSession ( $ newUser -> getToken ( ) [ 'access_token' ] ) ; $ newUser -> setToken ( array ( 'access_token' => '' , 'expires' => 0 ) ) ; return false ; } return $ newUser ; }
Check that a session is active, with side effects.
Check that a session is active with side effects .
// Try to get this user from the database // If the user doesn't exist, then create them // Now we have a good $newUser with the original $user token. Either the user was created or read from // the db. // Create the session if it doesn't exist, and then return (no need to check that the session is active // adds a new session /*
         * Do this last, since if this fails we need to essentially logoff the user.
         *
         * If the session exists, but doesn't belong to this user, selectActive() will fail.
         * If the sesion has expired, selectActive() will fail.
         */ /*
             * Shouldn't this call removeSession() instead of a low-level SQL function.
             */ // The user isn't logged in, so we should not let validate their session // The user is set, their session is active