]> git.mxchange.org Git - friendica.git/commitdiff
Adapt UserSession
authorPhilipp <admin@philipp.info>
Mon, 26 Dec 2022 12:08:41 +0000 (13:08 +0100)
committerPhilipp <admin@philipp.info>
Mon, 26 Dec 2022 20:18:04 +0000 (21:18 +0100)
- Move from App methods to UserSession methods
- Deprecate corresponding App methods

src/App.php
src/Core/Session/Capability/IHandleUserSessions.php
src/Core/Session/Model/UserSession.php
src/Model/User.php
src/Security/Authentication.php
tests/src/Module/Api/Twitter/DirectMessages/NewDMTest.php

index eb0d1cc2176e1a258c6baf1dc2b311230ebf8b2d..b41215380f92b70d82ff00e371fdd3478c48b316 100644 (file)
@@ -29,7 +29,6 @@ use Friendica\Core\Config\Factory\Config;
 use Friendica\Core\Session\Capability\IHandleUserSessions;
 use Friendica\Database\Definition\DbaDefinition;
 use Friendica\Database\Definition\ViewDefinition;
-use Friendica\Model\User;
 use Friendica\Module\Maintenance;
 use Friendica\Security\Authentication;
 use Friendica\Core\Config\ValueObject\Cache;
@@ -73,8 +72,6 @@ class App
                'videoheight'       => 350,
        ];
 
-       private $user_id       = 0;
-       private $nickname      = '';
        private $timezone      = '';
        private $profile_owner = 0;
        private $contact_id    = 0;
@@ -136,64 +133,39 @@ class App
        private $session;
 
        /**
-        * Set the user ID
-        *
-        * @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
+        * @deprecated 2022.03
+        * @see IHandleUserSessions::isAuthenticated()
         */
-       public function setLoggedInUserNickname(string $nickname)
-       {
-               $this->nickname = $nickname;
-       }
-
        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.
-        *
-        * @return bool true if user is an admin
-        * @throws Exception
+        * @deprecated 2022.03
+        * @see IHandleUserSessions::isSiteAdmin()
         */
        public function isSiteAdmin(): bool
        {
-               return
-                       $this->session->getLocalUserId()
-                       && $this->database->exists('user', [
-                               'uid'   => $this->getLoggedInUserId(),
-                               'email' => User::getAdminEmailList()
-                       ]);
+               return $this->session->isSiteAdmin();
        }
 
        /**
-        * Fetch the user id
-        * @return int User id
+        * @deprecated 2022.03
+        * @see IHandleUserSessions::getLocalUserId()
         */
        public function getLoggedInUserId(): int
        {
-               return $this->user_id;
+               return $this->session->getLocalUserId();
        }
 
        /**
-        * Fetch the user nick name
-        * @return string User's nickname
+        * @deprecated 2022.03
+        * @see IHandleUserSessions::getLocalUserNickname()
         */
        public function getLoggedInUserNickname(): string
        {
-               return $this->nickname;
+               return $this->session->getLocalUserNickname();
        }
 
        /**
index e65749c8dfcc746a8c58333e9cbb58f63e117d55..7a6ca64ba185c0e17faeb54f89b5b1e01cd98f7d 100644 (file)
@@ -33,6 +33,13 @@ interface IHandleUserSessions extends IHandleSessions
         */
        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.
         *
@@ -79,6 +86,13 @@ interface IHandleUserSessions extends IHandleSessions
         */
        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
         *
index 959ca1af2d405c4d8bb5654eb2e764d38011e029..6cd689e9c7f1b87ada228e6002bbe768b51cb74a 100644 (file)
@@ -24,6 +24,7 @@ namespace Friendica\Core\Session\Model;
 use Friendica\Core\Session\Capability\IHandleSessions;
 use Friendica\Core\Session\Capability\IHandleUserSessions;
 use Friendica\Model\Contact;
+use Friendica\Model\User;
 
 /**
  * This class handles user sessions, which is directly extended from regular session
@@ -50,6 +51,16 @@ class UserSession implements IHandleUserSessions
                return false;
        }
 
+       /** {@inheritDoc} */
+       public function getLocalUserNickname()
+       {
+               if ($this->isAuthenticated()) {
+                       return $this->session->get('nickname');
+               }
+
+               return false;
+       }
+
        /** {@inheritDoc} */
        public function getPublicContactId()
        {
@@ -122,6 +133,12 @@ class UserSession implements IHandleUserSessions
                return $this->session->get('authenticated', false);
        }
 
+       /** {@inheritDoc} */
+       public function isSiteAdmin(): bool
+       {
+               return User::isSiteAdmin($this->getLocalUserId());
+       }
+
        /** {@inheritDoc} */
        public function setVisitorsContacts()
        {
index 132e4f11a947d112ccc405e650faccc8804b8560..916844251e5c69710faa69890c52f826d0ef56f4 100644 (file)
@@ -830,6 +830,22 @@ class User
                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
         *
index 5dcc399403004af1fafc9892b8f48aec42eb309d..c6a84036728c652384872c69c1d4c61c4592486a 100644 (file)
@@ -392,9 +392,6 @@ class Authentication
                        }
                }
 
-               $a->setLoggedInUserId($user_record['uid']);
-               $a->setLoggedInUserNickname($user_record['nickname']);
-
                if ($login_initial) {
                        Hook::callAll('logged_in', $user_record);
                }
index b2bcfb37fa835b8b56c05e45a362eb51ce3e8da2..cab3c2202690f162f8fa36c9fde8159f9c473d26 100644 (file)
@@ -88,7 +88,7 @@ class NewDMTest extends ApiTest
         */
        public function testApiDirectMessagesNewWithScreenName()
        {
-               DI::app()->setLoggedInUserNickname('selfcontact');
+               DI::session()->set('nickname', 'selfcontact');
 
                $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser());
 
@@ -112,7 +112,7 @@ class NewDMTest extends ApiTest
         */
        public function testApiDirectMessagesNewWithTitle()
        {
-               DI::app()->setLoggedInUserNickname('selfcontact');
+               DI::session()->set('nickname', 'selfcontact');
 
                $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser());
 
@@ -138,7 +138,7 @@ class NewDMTest extends ApiTest
         */
        public function testApiDirectMessagesNewWithRss()
        {
-               DI::app()->setLoggedInUserNickname('selfcontact');
+               DI::session()->set('nickname', 'selfcontact');
 
                $directMessage = new DirectMessage(DI::logger(), DI::dba(), DI::twitterUser());