22namespace Maicol07 \SSO ;
33
44use GuzzleHttp \Exception \ClientException ;
5+ use Maicol07 \Flarum \Api \Resource \Collection ;
6+ use Maicol07 \Flarum \Api \Resource \Item ;
57use Maicol07 \SSO \User \Attributes ;
68use Maicol07 \SSO \User \Relationships ;
79use Maicol07 \SSO \User \Traits \Auth ;
@@ -118,6 +120,7 @@ public function fetch(): bool
118120 {
119121 try {
120122 $ user = $ this ->flarum ->api ->users ($ this ->attributes ->username )->request ();
123+ assert ($ user instanceof Item);
121124 } catch (ClientException $ e ) {
122125 if ($ e ->getCode () === 404 && $ e ->getResponse ()->getReasonPhrase () === "Not Found " ) {
123126 // User doesn't exists in Flarum
@@ -128,20 +131,22 @@ public function fetch(): bool
128131 throw $ e ;
129132 }
130133
131- $ this ->id = $ user ->id ;
134+ $ this ->id = $ user ->id ?? null ;
132135
133136 // Set attributes
134- foreach ($ user ->attributes as $ attribute => $ value ) {
137+ foreach ($ user ->attributes ?? [] as $ attribute => $ value ) {
135138 $ this ->attributes ->$ attribute = $ value ;
136139 }
137140
141+ $ groups = $ user ->relationships ['groups ' ] ?? [];
142+
138143 // Admin?
139- if (array_key_exists (1 , $ user -> relationships [ ' groups ' ] )) {
144+ if (array_key_exists (1 , $ groups )) {
140145 $ this ->isAdmin = true ;
141146 }
142147
143148 // Set groups
144- foreach ($ user -> relationships [ ' groups ' ] as $ group ) {
149+ foreach ($ groups as $ group ) {
145150 $ this ->relationships ->groups [] = $ group ->attributes ['nameSingular ' ];
146151 }
147152
0 commit comments