Refactor UserTransformers (#423)
* remove AccountTransformer and update UserTransformer (client api) to match UserTransformer (application api) * rename "toVueObject" * fix tests * forgot to rename this * backwards compat * fix tests
This commit is contained in:
		
							parent
							
								
									5409532ca1
								
							
						
					
					
						commit
						70c31eef8f
					
				| @ -8,9 +8,9 @@ use Illuminate\Auth\AuthManager; | |||||||
| use Illuminate\Http\JsonResponse; | use Illuminate\Http\JsonResponse; | ||||||
| use App\Facades\Activity; | use App\Facades\Activity; | ||||||
| use App\Services\Users\UserUpdateService; | use App\Services\Users\UserUpdateService; | ||||||
| use App\Transformers\Api\Client\AccountTransformer; |  | ||||||
| use App\Http\Requests\Api\Client\Account\UpdateEmailRequest; | use App\Http\Requests\Api\Client\Account\UpdateEmailRequest; | ||||||
| use App\Http\Requests\Api\Client\Account\UpdatePasswordRequest; | use App\Http\Requests\Api\Client\Account\UpdatePasswordRequest; | ||||||
|  | use App\Transformers\Api\Client\UserTransformer; | ||||||
| 
 | 
 | ||||||
| class AccountController extends ClientApiController | class AccountController extends ClientApiController | ||||||
| { | { | ||||||
| @ -25,7 +25,7 @@ class AccountController extends ClientApiController | |||||||
|     public function index(Request $request): array |     public function index(Request $request): array | ||||||
|     { |     { | ||||||
|         return $this->fractal->item($request->user()) |         return $this->fractal->item($request->user()) | ||||||
|             ->transformWith($this->getTransformer(AccountTransformer::class)) |             ->transformWith($this->getTransformer(UserTransformer::class)) | ||||||
|             ->toArray(); |             ->toArray(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -83,7 +83,7 @@ abstract class AbstractLoginController extends Controller | |||||||
|             'data' => [ |             'data' => [ | ||||||
|                 'complete' => true, |                 'complete' => true, | ||||||
|                 'intended' => $this->redirectPath(), |                 'intended' => $this->redirectPath(), | ||||||
|                 'user' => $user->toVueObject(), |                 'user' => $user->toReactObject(), | ||||||
|             ], |             ], | ||||||
|         ]); |         ]); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -215,9 +215,9 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Return the user model in a format that can be passed over to Vue templates. |      * Return the user model in a format that can be passed over to React templates. | ||||||
|      */ |      */ | ||||||
|     public function toVueObject(): array |     public function toReactObject(): array | ||||||
|     { |     { | ||||||
|         return collect($this->toArray())->except(['id', 'external_id'])->toArray(); |         return collect($this->toArray())->except(['id', 'external_id'])->toArray(); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -37,7 +37,8 @@ class UserTransformer extends BaseTransformer | |||||||
|             'last_name' => $user->name_last, |             'last_name' => $user->name_last, | ||||||
|             'language' => $user->language, |             'language' => $user->language, | ||||||
|             'root_admin' => (bool) $user->root_admin, |             'root_admin' => (bool) $user->root_admin, | ||||||
|             '2fa' => (bool) $user->use_totp, |             '2fa_enabled' => (bool) $user->use_totp, | ||||||
|  |             '2fa' => (bool) $user->use_totp, // deprecated, use "2fa_enabled"
 | ||||||
|             'created_at' => $this->formatTimestamp($user->created_at), |             'created_at' => $this->formatTimestamp($user->created_at), | ||||||
|             'updated_at' => $this->formatTimestamp($user->updated_at), |             'updated_at' => $this->formatTimestamp($user->updated_at), | ||||||
|         ]; |         ]; | ||||||
|  | |||||||
| @ -1,32 +0,0 @@ | |||||||
| <?php |  | ||||||
| 
 |  | ||||||
| namespace App\Transformers\Api\Client; |  | ||||||
| 
 |  | ||||||
| use App\Models\User; |  | ||||||
| 
 |  | ||||||
| class AccountTransformer extends BaseClientTransformer |  | ||||||
| { |  | ||||||
|     /** |  | ||||||
|      * Return the resource name for the JSONAPI output. |  | ||||||
|      */ |  | ||||||
|     public function getResourceName(): string |  | ||||||
|     { |  | ||||||
|         return 'user'; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     /** |  | ||||||
|      * Return basic information about the currently logged-in user. |  | ||||||
|      */ |  | ||||||
|     public function transform(User $model): array |  | ||||||
|     { |  | ||||||
|         return [ |  | ||||||
|             'id' => $model->id, |  | ||||||
|             'admin' => $model->root_admin, |  | ||||||
|             'username' => $model->username, |  | ||||||
|             'email' => $model->email, |  | ||||||
|             'first_name' => $model->name_first, |  | ||||||
|             'last_name' => $model->name_last, |  | ||||||
|             'language' => $model->language, |  | ||||||
|         ]; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -19,15 +19,21 @@ class UserTransformer extends BaseClientTransformer | |||||||
|      * Transforms a User model into a representation that can be shown to regular |      * Transforms a User model into a representation that can be shown to regular | ||||||
|      * users of the API. |      * users of the API. | ||||||
|      */ |      */ | ||||||
|     public function transform(User $model): array |     public function transform(User $user): array | ||||||
|     { |     { | ||||||
|         return [ |         return [ | ||||||
|             'uuid' => $model->uuid, |             'uuid' => $user->uuid, | ||||||
|             'username' => $model->username, |             'username' => $user->username, | ||||||
|             'email' => $model->email, |             'email' => $user->email, | ||||||
|             'image' => 'https://gravatar.com/avatar/' . md5(Str::lower($model->email)), |             'first_name' => $user->name_first, | ||||||
|             '2fa_enabled' => $model->use_totp, |             'last_name' => $user->name_last, | ||||||
|             'created_at' => $model->created_at->toAtomString(), |             'language' => $user->language, | ||||||
|  |             'image' => 'https://gravatar.com/avatar/' . md5(Str::lower($user->email)), // deprecated
 | ||||||
|  |             'admin' => (bool) $user->root_admin, // deprecated, use "root_admin"
 | ||||||
|  |             'root_admin' => (bool) $user->root_admin, | ||||||
|  |             '2fa_enabled' => (bool) $user->use_totp, | ||||||
|  |             'created_at' => $this->formatTimestamp($user->created_at), | ||||||
|  |             'updated_at' => $this->formatTimestamp($user->updated_at), | ||||||
|         ]; |         ]; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -17,7 +17,7 @@ | |||||||
|         @section('user-data') |         @section('user-data') | ||||||
|             @if(!is_null(Auth::user())) |             @if(!is_null(Auth::user())) | ||||||
|                 <script> |                 <script> | ||||||
|                     window.PanelUser = {!! json_encode(Auth::user()->toVueObject()) !!}; |                     window.PanelUser = {!! json_encode(Auth::user()->toReactObject()) !!}; | ||||||
|                 </script> |                 </script> | ||||||
|             @endif |             @endif | ||||||
|             @if(!empty($siteConfiguration)) |             @if(!empty($siteConfiguration)) | ||||||
|  | |||||||
| @ -24,8 +24,8 @@ class UserControllerTest extends ApplicationApiIntegrationTestCase | |||||||
|         $response->assertJsonStructure([ |         $response->assertJsonStructure([ | ||||||
|             'object', |             'object', | ||||||
|             'data' => [ |             'data' => [ | ||||||
|                 ['object', 'attributes' => ['id', 'external_id', 'uuid', 'username', 'email', 'first_name', 'last_name', 'language', 'root_admin', '2fa', 'created_at', 'updated_at']], |                 ['object', 'attributes' => ['id', 'external_id', 'uuid', 'username', 'email', 'first_name', 'last_name', 'language', 'root_admin', '2fa_enabled', '2fa', 'created_at', 'updated_at']], | ||||||
|                 ['object', 'attributes' => ['id', 'external_id', 'uuid', 'username', 'email', 'first_name', 'last_name', 'language', 'root_admin', '2fa', 'created_at', 'updated_at']], |                 ['object', 'attributes' => ['id', 'external_id', 'uuid', 'username', 'email', 'first_name', 'last_name', 'language', 'root_admin', '2fa_enabled', '2fa', 'created_at', 'updated_at']], | ||||||
|             ], |             ], | ||||||
|             'meta' => ['pagination' => ['total', 'count', 'per_page', 'current_page', 'total_pages']], |             'meta' => ['pagination' => ['total', 'count', 'per_page', 'current_page', 'total_pages']], | ||||||
|         ]); |         ]); | ||||||
| @ -56,6 +56,7 @@ class UserControllerTest extends ApplicationApiIntegrationTestCase | |||||||
|                     'last_name' => $this->getApiUser()->name_last, |                     'last_name' => $this->getApiUser()->name_last, | ||||||
|                     'language' => $this->getApiUser()->language, |                     'language' => $this->getApiUser()->language, | ||||||
|                     'root_admin' => $this->getApiUser()->root_admin, |                     'root_admin' => $this->getApiUser()->root_admin, | ||||||
|  |                     '2fa_enabled' => (bool) $this->getApiUser()->totp_enabled, | ||||||
|                     '2fa' => (bool) $this->getApiUser()->totp_enabled, |                     '2fa' => (bool) $this->getApiUser()->totp_enabled, | ||||||
|                     'created_at' => $this->formatTimestamp($this->getApiUser()->created_at), |                     'created_at' => $this->formatTimestamp($this->getApiUser()->created_at), | ||||||
|                     'updated_at' => $this->formatTimestamp($this->getApiUser()->updated_at), |                     'updated_at' => $this->formatTimestamp($this->getApiUser()->updated_at), | ||||||
| @ -73,6 +74,7 @@ class UserControllerTest extends ApplicationApiIntegrationTestCase | |||||||
|                     'last_name' => $user->name_last, |                     'last_name' => $user->name_last, | ||||||
|                     'language' => $user->language, |                     'language' => $user->language, | ||||||
|                     'root_admin' => (bool) $user->root_admin, |                     'root_admin' => (bool) $user->root_admin, | ||||||
|  |                     '2fa_enabled' => (bool) $user->totp_enabled, | ||||||
|                     '2fa' => (bool) $user->totp_enabled, |                     '2fa' => (bool) $user->totp_enabled, | ||||||
|                     'created_at' => $this->formatTimestamp($user->created_at), |                     'created_at' => $this->formatTimestamp($user->created_at), | ||||||
|                     'updated_at' => $this->formatTimestamp($user->updated_at), |                     'updated_at' => $this->formatTimestamp($user->updated_at), | ||||||
|  | |||||||
| @ -22,13 +22,18 @@ class AccountControllerTest extends ClientApiIntegrationTestCase | |||||||
|         $response->assertOk()->assertJson([ |         $response->assertOk()->assertJson([ | ||||||
|             'object' => 'user', |             'object' => 'user', | ||||||
|             'attributes' => [ |             'attributes' => [ | ||||||
|                 'id' => $user->id, |                 'uuid' => $user->uuid, | ||||||
|                 'admin' => false, |  | ||||||
|                 'username' => $user->username, |                 'username' => $user->username, | ||||||
|                 'email' => $user->email, |                 'email' => $user->email, | ||||||
|                 'first_name' => $user->name_first, |                 'first_name' => $user->name_first, | ||||||
|                 'last_name' => $user->name_last, |                 'last_name' => $user->name_last, | ||||||
|                 'language' => $user->language, |                 'language' => 'en', | ||||||
|  |                 'image' => 'https://gravatar.com/avatar/' . md5(Str::lower($user->email)), | ||||||
|  |                 'admin' => false, | ||||||
|  |                 'root_admin' => false, | ||||||
|  |                 '2fa_enabled' => false, | ||||||
|  |                 'created_at' => $this->formatTimestamp($user->created_at), | ||||||
|  |                 'updated_at' => $this->formatTimestamp($user->updated_at), | ||||||
|             ], |             ], | ||||||
|         ]); |         ]); | ||||||
|     } |     } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Boy132
						Boy132