mirror of
https://github.com/grocy/grocy.git
synced 2026-06-04 02:11:43 +00:00
Applied PHP formatting rules
This commit is contained in:
@@ -4,6 +4,47 @@ namespace Grocy\Services;
|
||||
|
||||
class SessionService extends BaseService
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function CreateSession($userId, $stayLoggedInPermanently = false)
|
||||
{
|
||||
$newSessionKey = $this->GenerateSessionKey();
|
||||
|
||||
$expires = date('Y-m-d H:i:s', intval(time() + 2592000));
|
||||
|
||||
// Default is that sessions expire in 30 days
|
||||
if ($stayLoggedInPermanently === true)
|
||||
{
|
||||
$expires = date('Y-m-d H:i:s', PHP_INT_SIZE == 4 ? PHP_INT_MAX : PHP_INT_MAX >> 32); // Never
|
||||
}
|
||||
|
||||
$sessionRow = $this->getDatabase()->sessions()->createRow([
|
||||
'user_id' => $userId,
|
||||
'session_key' => $newSessionKey,
|
||||
'expires' => $expires
|
||||
]);
|
||||
$sessionRow->save();
|
||||
|
||||
return $newSessionKey;
|
||||
}
|
||||
|
||||
public function GetDefaultUser()
|
||||
{
|
||||
return $this->getDatabase()->users(1);
|
||||
}
|
||||
|
||||
public function GetUserBySessionKey($sessionKey)
|
||||
{
|
||||
$sessionRow = $this->getDatabase()->sessions()->where('session_key', $sessionKey)->fetch();
|
||||
|
||||
if ($sessionRow !== null)
|
||||
{
|
||||
return $this->getDatabase()->users($sessionRow->user_id);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
@@ -17,14 +58,15 @@ class SessionService extends BaseService
|
||||
else
|
||||
{
|
||||
$sessionRow = $this->getDatabase()->sessions()->where('session_key = :1 AND expires > :2', $sessionKey, date('Y-m-d H:i:s', time()))->fetch();
|
||||
|
||||
if ($sessionRow !== null)
|
||||
{
|
||||
// This should not change the database file modification time as this is used
|
||||
// This should not change the database file modification time as this is used
|
||||
// to determine if REALLY something has changed
|
||||
$dbModTime = $this->getDatabaseService()->GetDbChangedTime();
|
||||
$sessionRow->update(array(
|
||||
$sessionRow->update([
|
||||
'last_used' => date('Y-m-d H:i:s', time())
|
||||
));
|
||||
]);
|
||||
$this->getDatabaseService()->SetDbChangedTime($dbModTime);
|
||||
|
||||
return true;
|
||||
@@ -33,30 +75,9 @@ class SessionService extends BaseService
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function CreateSession($userId, $stayLoggedInPermanently = false)
|
||||
{
|
||||
$newSessionKey = $this->GenerateSessionKey();
|
||||
|
||||
$expires = date('Y-m-d H:i:s', intval(time() + 2592000)); // Default is that sessions expire in 30 days
|
||||
if ($stayLoggedInPermanently === true)
|
||||
{
|
||||
$expires = date('Y-m-d H:i:s', PHP_INT_SIZE == 4 ? PHP_INT_MAX : PHP_INT_MAX>>32); // Never
|
||||
}
|
||||
|
||||
$sessionRow = $this->getDatabase()->sessions()->createRow(array(
|
||||
'user_id' => $userId,
|
||||
'session_key' => $newSessionKey,
|
||||
'expires' => $expires
|
||||
));
|
||||
$sessionRow->save();
|
||||
|
||||
return $newSessionKey;
|
||||
}
|
||||
|
||||
public function RemoveSession($sessionKey)
|
||||
@@ -64,23 +85,9 @@ class SessionService extends BaseService
|
||||
$this->getDatabase()->sessions()->where('session_key', $sessionKey)->delete();
|
||||
}
|
||||
|
||||
public function GetUserBySessionKey($sessionKey)
|
||||
{
|
||||
$sessionRow = $this->getDatabase()->sessions()->where('session_key', $sessionKey)->fetch();
|
||||
if ($sessionRow !== null)
|
||||
{
|
||||
return $this->getDatabase()->users($sessionRow->user_id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function GetDefaultUser()
|
||||
{
|
||||
return $this->getDatabase()->users(1);
|
||||
}
|
||||
|
||||
private function GenerateSessionKey()
|
||||
{
|
||||
return RandomString(50);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user