Move login status calculation to its own method and add API in member module

This commit is contained in:
Kijin Sung 2022-08-05 10:21:14 +09:00
parent a0f895a02f
commit 0cc1246898
3 changed files with 33 additions and 12 deletions

View file

@ -305,18 +305,8 @@ class Session
*/ */
public static function checkLoginStatusCookie() public static function checkLoginStatusCookie()
{ {
// Members are identified by a hash of member_srl. Guests are identified as 'none'. // If the cookie value is different from the current login status, overwrite it.
if (isset($_SESSION['RHYMIX']) && $_SESSION['RHYMIX']['login']) $value = self::getLoginStatus();
{
$data = sprintf('%s:%s:%d:%s', $_SERVER['HTTP_HOST'] ?? '', RX_BASEDIR, $_SESSION['RHYMIX']['login'], config('crypto.session_key'));
$value = base64_encode_urlsafe(substr(hash('sha256', $data, true), 0, 18));
}
else
{
$value = 'none';
}
// If the cookie value is different from the current value, overwrite it.
if (!isset($_COOKIE['rx_login_status']) || $_COOKIE['rx_login_status'] !== $value) if (!isset($_COOKIE['rx_login_status']) || $_COOKIE['rx_login_status'] !== $value)
{ {
list($lifetime, $refresh_interval, $domain, $path, $secure, $samesite) = self::_getParams(); list($lifetime, $refresh_interval, $domain, $path, $secure, $samesite) = self::_getParams();
@ -1003,6 +993,27 @@ class Session
} }
} }
/**
* Get a string that identifies login status.
*
* Members are identified by a hash that is unique to each member.
* Guests are identified as 'none'.
*
* @return string
*/
public static function getLoginStatus()
{
if (isset($_SESSION['RHYMIX']) && $_SESSION['RHYMIX']['login'])
{
$data = sprintf('%s:%s:%d:%s', $_SERVER['HTTP_HOST'] ?? '', RX_BASEDIR, $_SESSION['RHYMIX']['login'], config('crypto.session_key'));
return base64_encode_urlsafe(substr(hash('sha256', $data, true), 0, 18));
}
else
{
return 'none';
}
}
/** /**
* Get validity information. * Get validity information.
* *

View file

@ -32,6 +32,7 @@
<action name="dispMemberLogout" type="view" meta-noindex="true" /> <action name="dispMemberLogout" type="view" meta-noindex="true" />
<action name="dispMemberSpammer" type="view" permission="manager" check_var="module_srl" meta-noindex="true" /> <action name="dispMemberSpammer" type="view" permission="manager" check_var="module_srl" meta-noindex="true" />
<action name="getLoginStatus" type="model" meta-noindex="true" />
<action name="getMemberMenu" type="model" /> <action name="getMemberMenu" type="model" />
<action name="getApiGroups" type="model" permission="root" /> <action name="getApiGroups" type="model" permission="root" />

View file

@ -174,6 +174,15 @@ class memberModel extends member
return null; return null;
} }
/**
* Display login status as JSON API
*/
public function getLoginStatus()
{
Context::setResponseMethod('JSON');
$this->add('status', Rhymix\Framework\Session::getLoginStatus());
}
/** /**
* @brief Display menus of the member * @brief Display menus of the member
*/ */