diff --git a/routes/api.php b/routes/api.php index e675d81..67cac72 100644 --- a/routes/api.php +++ b/routes/api.php @@ -53,6 +53,7 @@ Route::middleware(config('fintech.auth.middleware'))->group(function () { Route::apiResource('users', \Fintech\Auth\Http\Controllers\UserController::class); + Route::post('users/{user}/restore', [\Fintech\Auth\Http\Controllers\UserController::class, 'restore'])->name('users.restore'); Route::apiResource('roles', \Fintech\Auth\Http\Controllers\RoleController::class); Route::post('roles/{role}/restore', [\Fintech\Auth\Http\Controllers\RoleController::class, 'restore'])->name('roles.restore'); diff --git a/src/Http/Controllers/UserController.php b/src/Http/Controllers/UserController.php index 82dba24..4ba49cf 100644 --- a/src/Http/Controllers/UserController.php +++ b/src/Http/Controllers/UserController.php @@ -11,7 +11,6 @@ use Fintech\Auth\Http\Resources\UserCollection; use Fintech\Auth\Http\Resources\UserResource; use Fintech\Core\Exceptions\DeleteOperationException; -use Fintech\Core\Exceptions\ResourceNotFoundException; use Fintech\Core\Exceptions\RestoreOperationException; use Fintech\Core\Exceptions\StoreOperationException; use Fintech\Core\Exceptions\UpdateOperationException; diff --git a/src/Http/Resources/UserCollection.php b/src/Http/Resources/UserCollection.php index babe48b..5993034 100644 --- a/src/Http/Resources/UserCollection.php +++ b/src/Http/Resources/UserCollection.php @@ -2,6 +2,7 @@ namespace Fintech\Auth\Http\Resources; +use Fintech\Core\Facades\Core; use Fintech\Core\Supports\Constant; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\ResourceCollection; @@ -16,7 +17,73 @@ class UserCollection extends ResourceCollection */ public function toArray($request) { - return parent::toArray($request); + return $this->collection->map(function ($user) { + $data = [ + 'parent_id' => $user->parent_id ?? null, + 'parent_name' => ($user->parent) ? $user->parent->name : null, + 'name' => $user->name ?? null, + 'mobile' => $user->mobile ?? null, + 'email' => $user->email ?? null, + 'login_id' => $user->login_id ?? null, + 'wrong_password' => $user->wrong_password ?? null, + 'wrong_pin' => $user->wrong_pin ?? null, + 'status' => $user->status ?? null, + 'language' => $user->language ?? null, + 'currency' => $user->currency ?? null, + 'app_version' => $user->app_version ?? null, + 'remember_token' => $user->remember_token ?? null, + 'fcm_token' => $user->fcm_token ?? null, + 'roles' => ($user->roles) ? $user->roles : [], + 'links' => $user->links, + 'created_at' => $user->created_at, + 'updated_at' => $user->updated_at, + ]; + + $profile = $user->profile; + + $profile_data = [ + 'profile_photo' => $user->getFirstMediaUrl('profile_data'), + 'user_profile_data' => $profile->user_profile_data ?? null, + 'id_type' => $profile->id_type ?? null, + 'id_no' => $profile->id_no ?? null, + 'id_issue_country' => $profile->id_issue_country ?? null, + 'id_expired_at' => $profile->id_expired_at ?? null, + 'id_issue_at' => $profile->id_issue_at ?? null, + 'id_no_duplicate' => $profile->id_no_duplicate ?? null, + 'date_of_birth' => $profile->date_of_birth ?? null, + 'address' => $profile->permanent_address ?? null, + 'city_id' => $profile->city_id ?? null, + 'city_name' => null, + 'state_id' => $profile->state_id ?? null, + 'state_name' => null, + 'country_id' => $profile->country_id ?? null, + 'country_name' => null, + 'post_code' => $profile->post_code ?? null, + 'present_address' => $profile->present_address ?? null, + 'present_city_id' => $profile->present_city_id ?? null, + 'present_city_name' => null, + 'present_state_id' => $profile->present_state_id ?? null, + 'present_state_name' => null, + 'present_country_id' => $profile->present_country_id ?? null, + 'present_country_name' => null, + 'present_post_code' => $profile->present_post_code ?? null, + 'blacklisted' => $profile->blacklisted ?? null, + 'metadata_available' => Core::packageExists('MetaData') + ]; + + if (class_exists(\Fintech\MetaData\Facades\MetaData::class)) { + + $profile_data['city_name'] = $profile->city?->name ?? null; + $profile_data['state_name'] = $profile->state?->name ?? null; + $profile_data['country_name'] = $profile->country?->name ?? null; + $profile_data['present_city_name'] = $profile->presentCity?->name ?? null; + $profile_data['present_state_name'] = $profile->presentState?->name ?? null; + $profile_data['present_country_name'] = $profile->presentCountry?->name ?? null; + } + + return array_merge($data, $profile_data); + + })->toArray(); } /** diff --git a/src/Models/Profile.php b/src/Models/Profile.php index ae0a5df..2332171 100644 --- a/src/Models/Profile.php +++ b/src/Models/Profile.php @@ -2,13 +2,14 @@ namespace Fintech\Auth\Models; +use Fintech\Auth\Traits\MetaDataRelations; +use Fintech\Core\Traits\AuditableTrait; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; -use Fintech\Auth\Traits\MetaDataRelations; class Profile extends Model { - use \Fintech\Core\Traits\AuditableTrait; + use AuditableTrait; use SoftDeletes; use MetaDataRelations; diff --git a/src/Models/User.php b/src/Models/User.php index a5a5f4c..d1a6429 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -3,11 +3,16 @@ namespace Fintech\Auth\Models; use Fintech\Core\Traits\AuditableTrait; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; +use Spatie\Image\Manipulations; +use Spatie\MediaLibrary\HasMedia; +use Spatie\MediaLibrary\InteractsWithMedia; +use Spatie\MediaLibrary\MediaCollections\Models\Media; use Spatie\Permission\Traits\HasRoles; /** @@ -15,13 +20,14 @@ * @package Fintech\Auth\Models * @method getTeamIdFromToken() */ -class User extends Authenticatable +class User extends Authenticatable implements HasMedia { use HasApiTokens; use HasRoles; use AuditableTrait; use SoftDeletes; use Notifiable; + use InteractsWithMedia; /* |-------------------------------------------------------------------------- @@ -42,6 +48,10 @@ class User extends Authenticatable protected $hidden = ['creator_id', 'editor_id', 'destroyer_id', 'restorer_id']; + protected $appends = ['links']; + + protected $files = ['profile_photo']; + /* |-------------------------------------------------------------------------- | FUNCTIONS @@ -60,12 +70,44 @@ public function authField() } + public function registerMediaConversions(Media $media = null): void + { + $this + ->addMediaConversion('preview') + ->fit(Manipulations::FIT_CROP, 300, 300) + ->nonQueued(); + } + + public function registerMediaCollections(): void + { + $this + ->addMediaCollection('profile_photo') + ->acceptsMimeTypes(['image/jpeg', 'image/png', 'image/gif', 'image/jpg', 'image/svg+xml']) + ->useFallbackUrl('/images/anonymous-user.jpg') + ->useFallbackPath(storage_path('/app/public/images/anonymous-user.jpg')) + ->useFallbackUrl('/images/anonymous-user.jpg', 'thumb') + ->useFallbackPath(storage_path('/app/public/images/anonymous-user.jpg'), 'thumb') + ->useDisk('public') + ->singleFile() + ->registerMediaConversions(function (Media $media) { + $this + ->addMediaConversion('thumb') + ->width(128) + ->height(128); + }); + } + /* |-------------------------------------------------------------------------- | RELATIONS |-------------------------------------------------------------------------- */ + public function parent(): BelongsTo + { + return $this->belongsTo(self::class, 'parent_id', 'id'); + } + public function profile(): HasOne { return $this->hasOne(config('fintech.auth.user_profile_model')); @@ -83,6 +125,31 @@ public function profile(): HasOne |-------------------------------------------------------------------------- */ + /** + * return all resource link for this object + * + * @return array[] + */ + public function getLinksAttribute() + { + $primaryKey = $this->getKey(); + + $links = [ + 'show' => action_link(route('auth.users.show', $primaryKey), __('core::messages.action.show'), 'get'), + 'update' => action_link(route('auth.users.update', $primaryKey), __('core::messages.action.update'), 'put'), + 'destroy' => action_link(route('auth.users.destroy', $primaryKey), __('core::messages.action.destroy'), 'delete'), + 'restore' => action_link(route('auth.users.restore', $primaryKey), __('core::messages.action.restore'), 'post'), + ]; + + if ($this->getAttribute('deleted_at') == null) { + unset($links['restore']); + } else { + unset($links['destroy']); + } + + return $links; + } + /* |-------------------------------------------------------------------------- | MUTATORS diff --git a/tests/Feature/PermissionTest.php b/tests/Feature/PermissionTest.php index 925cae1..99ed497 100644 --- a/tests/Feature/PermissionTest.php +++ b/tests/Feature/PermissionTest.php @@ -1,7 +1,6 @@