Adapt UserSession

- Move from App methods to UserSession methods
- Deprecate corresponding App methods
This commit is contained in:
Philipp 2022-12-26 13:08:41 +01:00
parent c376605dd2
commit bfc1c157f1
No known key found for this signature in database
GPG Key ID: 24A7501396EB5432
6 changed files with 62 additions and 46 deletions

View File

@ -29,7 +29,6 @@ use Friendica\Core\Config\Factory\Config;
use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Database\Definition\DbaDefinition; use Friendica\Database\Definition\DbaDefinition;
use Friendica\Database\Definition\ViewDefinition; use Friendica\Database\Definition\ViewDefinition;
use Friendica\Model\User;
use Friendica\Module\Maintenance; use Friendica\Module\Maintenance;
use Friendica\Security\Authentication; use Friendica\Security\Authentication;
use Friendica\Core\Config\ValueObject\Cache; use Friendica\Core\Config\ValueObject\Cache;
@ -73,8 +72,6 @@ class App
'videoheight' => 350, 'videoheight' => 350,
]; ];
private $user_id = 0;
private $nickname = '';
private $timezone = ''; private $timezone = '';
private $profile_owner = 0; private $profile_owner = 0;
private $contact_id = 0; private $contact_id = 0;
@ -136,64 +133,39 @@ class App
private $session; private $session;
/** /**
* Set the user ID * @deprecated 2022.03
* * @see IHandleUserSessions::isAuthenticated()
* @param int $user_id
* @return void
*/ */
public function setLoggedInUserId(int $user_id)
{
$this->user_id = $user_id;
}
/**
* Set the nickname
*
* @param int $user_id
* @return void
*/
public function setLoggedInUserNickname(string $nickname)
{
$this->nickname = $nickname;
}
public function isLoggedIn(): bool public function isLoggedIn(): bool
{ {
return $this->session->getLocalUserId() && $this->user_id && ($this->user_id == $this->session->getLocalUserId()); return $this->session->isAuthenticated();
} }
/** /**
* Check if current user has admin role. * @deprecated 2022.03
* * @see IHandleUserSessions::isSiteAdmin()
* @return bool true if user is an admin
* @throws Exception
*/ */
public function isSiteAdmin(): bool public function isSiteAdmin(): bool
{ {
return return $this->session->isSiteAdmin();
$this->session->getLocalUserId()
&& $this->database->exists('user', [
'uid' => $this->getLoggedInUserId(),
'email' => User::getAdminEmailList()
]);
} }
/** /**
* Fetch the user id * @deprecated 2022.03
* @return int User id * @see IHandleUserSessions::getLocalUserId()
*/ */
public function getLoggedInUserId(): int public function getLoggedInUserId(): int
{ {
return $this->user_id; return $this->session->getLocalUserId();
} }
/** /**
* Fetch the user nick name * @deprecated 2022.03
* @return string User's nickname * @see IHandleUserSessions::getLocalUserNickname()
*/ */
public function getLoggedInUserNickname(): string public function getLoggedInUserNickname(): string
{ {
return $this->nickname; return $this->session->getLocalUserNickname();
} }
/** /**

View File

@ -33,6 +33,13 @@ interface IHandleUserSessions extends IHandleSessions
*/ */
public function getLocalUserId(); public function getLocalUserId();
/**
* Returns the user nickname of locally logged-in user.
*
* @return string|false User's nickname or false
*/
public function getLocalUserNickname();
/** /**
* Returns the public contact id of logged-in user or false. * Returns the public contact id of logged-in user or false.
* *
@ -79,6 +86,13 @@ interface IHandleUserSessions extends IHandleSessions
*/ */
public function isAuthenticated(): bool; public function isAuthenticated(): bool;
/**
* Check if current user has admin role.
*
* @return bool true if user is an admin
*/
public function isSiteAdmin(): bool;
/** /**
* Returns User ID of the managed user in case it's a different identity * Returns User ID of the managed user in case it's a different identity
* *

View File

@ -24,6 +24,7 @@ namespace Friendica\Core\Session\Model;
use Friendica\Core\Session\Capability\IHandleSessions; use Friendica\Core\Session\Capability\IHandleSessions;
use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\User;
/** /**
* This class handles user sessions, which is directly extended from regular session * This class handles user sessions, which is directly extended from regular session
@ -50,6 +51,16 @@ class UserSession implements IHandleUserSessions
return false; return false;
} }
/** {@inheritDoc} */
public function getLocalUserNickname()
{
if ($this->isAuthenticated()) {
return $this->session->get('nickname');
}
return false;
}
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getPublicContactId() public function getPublicContactId()
{ {
@ -122,6 +133,12 @@ class UserSession implements IHandleUserSessions
return $this->session->get('authenticated', false); return $this->session->get('authenticated', false);
} }
/** {@inheritDoc} */
public function isSiteAdmin(): bool
{
return User::isSiteAdmin($this->getLocalUserId());
}
/** {@inheritDoc} */ /** {@inheritDoc} */
public function setVisitorsContacts() public function setVisitorsContacts()
{ {

View File

@ -830,6 +830,22 @@ class User
return DBA::update('user', $fields, ['uid' => $uid]); return DBA::update('user', $fields, ['uid' => $uid]);
} }
/**
* Returns if the given uid is valid and in the admin list
*
* @param int $uid
*
* @return bool
* @throws Exception
*/
public static function isSiteAdmin(int $uid): bool
{
return DBA::exists('user', [
'uid' => $uid,
'email' => self::getAdminEmailList()
]);
}
/** /**
* Checks if a nickname is in the list of the forbidden nicknames * Checks if a nickname is in the list of the forbidden nicknames
* *

View File

@ -392,9 +392,6 @@ class Authentication
} }
} }
$a->setLoggedInUserId($user_record['uid']);
$a->setLoggedInUserNickname($user_record['nickname']);
if ($login_initial) { if ($login_initial) {
Hook::callAll('logged_in', $user_record); Hook::callAll('logged_in', $user_record);
} }

View File

@ -88,7 +88,7 @@ class NewDMTest extends ApiTest
*/ */
public function testApiDirectMessagesNewWithScreenName() public function testApiDirectMessagesNewWithScreenName()
{ {
DI::app()->setLoggedInUserNickname('selfcontact'); DI::session()->set('nickname', 'selfcontact');
$directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser()); $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser());
@ -112,7 +112,7 @@ class NewDMTest extends ApiTest
*/ */
public function testApiDirectMessagesNewWithTitle() public function testApiDirectMessagesNewWithTitle()
{ {
DI::app()->setLoggedInUserNickname('selfcontact'); DI::session()->set('nickname', 'selfcontact');
$directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser()); $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser());
@ -138,7 +138,7 @@ class NewDMTest extends ApiTest
*/ */
public function testApiDirectMessagesNewWithRss() public function testApiDirectMessagesNewWithRss()
{ {
DI::app()->setLoggedInUserNickname('selfcontact'); DI::session()->set('nickname', 'selfcontact');
$directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser()); $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser());