88 use Magento \Framework \App \Config \Storage \WriterInterface ;
99 use Magento \Framework \App \Helper \AbstractHelper ;
1010 use Magento \Framework \App \ObjectManager ;
11+ use Magento \Framework \App \Request \Http ;
1112 use Magento \PageCache \Model \Config as CacheConfig ;
1213 use Magento \Store \Model \ScopeInterface ;
1314 use Magento \Store \Model \StoreManagerInterface ;
@@ -31,10 +32,12 @@ class Data extends AbstractHelper {
3132 * @var ScopeConfigInterface _configReader Instance of the ScopeConfigInterface
3233 * @var StoreManagerInterface _configWriter Instance of the StoreManagerInterface
3334 * @var WriterInterface _storeManager Instance of the WriterInterface
35+ * @var Http _http Instance of the Http
3436 */
3537 protected $ _configReader ;
3638 protected $ _configWriter ;
3739 protected $ _storeManager ;
40+ protected $ _http ;
3841
3942 /**
4043 * This constructor is overloaded from the parent class in order to use dependency injection
@@ -46,12 +49,14 @@ class Data extends AbstractHelper {
4649 public function __construct (
4750 ScopeConfigInterface $ configReader ,
4851 StoreManagerInterface $ storeManager ,
49- WriterInterface $ configWriter
52+ WriterInterface $ configWriter ,
53+ Http $ http
5054 ) {
5155 // Save the injected class instances
5256 $ this ->_configReader = $ configReader ;
5357 $ this ->_storeManager = $ storeManager ;
5458 $ this ->_configWriter = $ configWriter ;
59+ $ this ->_http = $ http ;
5560 }
5661
5762 /**
@@ -75,7 +80,7 @@ private function _getStoreValue ( $path, $scope = ScopeInterface::SCOPE_STORE )
7580 */
7681 private function _getClientBrowser () {
7782 // Initialize the user agent and set the default values
78- $ agent = $ _SERVER ["HTTP_USER_AGENT " ];
83+ $ agent = isset ( $ _SERVER ["HTTP_USER_AGENT " ] ) ? $ _SERVER [ " HTTP_USER_AGENT " ] : "" ;
7984 $ type = "Unknown " ;
8085 $ os = "Unknown " ;
8186 // Extract the operating system
@@ -103,15 +108,26 @@ private function _getClientBrowser () {
103108 * @return Object User information in object
104109 */
105110 public function getLoggedInUserInfo () {
111+ // We use object manager here because with DI in CLI there is no session
112+ $ objectManager = ObjectManager::getInstance ();
106113 // Check to see if caller is using CLI
107114 if ( php_sapi_name () === "cli " ) {
108115 // If it is then gather some information and return it
109116 return ( object ) [ "interface " => "console " , "username " => get_current_user () ];
110117 }
118+ // Check to see if request is being made via token authorization (with rest api)
119+ else if ( $ this ->_http ->getHeader ("Authorization " ) ) {
120+ // Instantiate objects
121+ $ remoteAddress = "Magento\Framework\HTTP\PhpEnvironment\RemoteAddress " ;
122+ $ remoteAddress = $ objectManager ->get ( $ remoteAddress );
123+ return ( object ) [
124+ "interface " => "api " ,
125+ "ip " => $ remoteAddress ->getRemoteAddress ()
126+ ];
127+ }
111128 // Otherwise this is a request that has a session attached to it
112129 else {
113- // We use object manager here because with DI in CLI there is no session
114- $ objectManager = ObjectManager::getInstance ();
130+ // Instantiate objects
115131 $ session = "Magento\Backend\Model\Auth\Session " ;
116132 $ session = $ objectManager ->create ( $ session );
117133 $ remoteAddress = "Magento\Framework\HTTP\PhpEnvironment\RemoteAddress " ;
@@ -121,9 +137,9 @@ public function getLoggedInUserInfo () {
121137 $ clientBrowser = $ this ->_getClientBrowser ();
122138 return ( object ) [
123139 "interface " => "backend " ,
124- "id " => $ user ->getId (),
125- "username " => $ user ->getUserName (),
126- "email " => $ user ->getEmail (),
140+ "id " => $ user ? $ user ->getId () : " n/a " ,
141+ "username " => $ user ? $ user ->getUserName () : " n/a " ,
142+ "email " => $ user ? $ user ->getEmail () : " n/a " ,
127143 "ip " => $ remoteAddress ->getRemoteAddress (),
128144 "browser " => $ clientBrowser ->browser ,
129145 "system " => $ clientBrowser ->os
0 commit comments