Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to properly use the childTypes variable for authenticated routes #116

Open
mikeslinkman opened this issue Aug 24, 2023 · 3 comments
Open

Comments

@mikeslinkman
Copy link

Love the idea of the package but I'm running into some issues with properly defining the $childTypes. I've added a char value type for my user where each char refers to a different child so I setup my $childtypes like below:

protected array $childTypes = [
        'A' => Admin::class,
        'U' => SearchUser::class,
        'C' => CompanyUser::class,
];

But now when I try to visit a page where the authenticated user is used e.g. $user = auth()->user() I'm getting the error Class "A" not found or whatever other value is set in the type column.

Any help is greatly appreciated

@RomainMazB
Copy link

I don't see any problem into your code, are you sure that the fully qualified class name is used or imported into your file?

Something like:

<?php namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Models\Admin;
use App\Models\SearchUser;
use App\Models\CompanyUser;
use Parental\HasChildren;

class User extends Authenticatable {
    use HasChildren;

    protected array $childTypes = [
        'A' => Admin::class,
        'U' => SearchUser::class,
        'C' => CompanyUser::class,
    ];
}

@mikeslinkman
Copy link
Author

Yes I verified the classes are imported, although they weren't at first (since they were in the same folder before) but after adding them the problem still persisted. I've added most of my user class below as a reference. Might be overlooking something but I don't see it

<?php

namespace App\Models;

use App\Models\Enums\DriverLicenses;
use App\Models\Enums\UserType;
use App\Models\Parental\Admin;
use App\Models\Parental\CompanyUser;
use App\Models\Parental\SearchUser;
use App\Models\Traits\HasLocation;
use App\Models\Traits\UpdatesAddress;
use App\Notifications\ResetPassword;
use App\Services\MessageBird\MessageBirdService;
use Carbon\Carbon;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
use Orchid\Filters\Types\Like;
use Orchid\Filters\Types\Where;
use Orchid\Filters\Types\WhereDateStartEnd;
use Orchid\Platform\Models\User as Authenticatable;
use Parental\HasChildren;
use Random\Randomizer;

class User extends Authenticatable
{
    use Notifiable, CanResetPassword, HasLocation, UpdatesAddress, HasChildren;

    /**
     * @var array|string[]
     */
    protected array $childTypes = [
        'A' => Admin::class,
        'U' => SearchUser::class,
        'C' => CompanyUser::class,
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [ // @phpstan-ignore-line
        'name',
        'email',
        'password',
        'permissions',
        'phone_number',
        'anonymous_gender',
    ];

    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPassword($token));
    }

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [ // @phpstan-ignore-line
        'password',
        'remember_token',
        'permissions',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array<string,string>
     */
    protected $casts = [
        'password' => 'hashed',
        'experience' => 'array',
        'permissions' => 'array',
        'last_reminder' => 'date',
        'type' => UserType::class,
        'email_verified_at' => 'datetime',
        'phone_number_verified_at' => 'datetime',
        'phone_number_verification_expiry' => 'datetime',
        'drivers_licenses' => AsEnumCollection::class.':'.DriverLicenses::class,
    ];

    protected static function boot()
    {
        parent::boot();

        self::creating(function (User $user) {});

        self::updating(function (User $user) {});

        self::created(function (User $user) {});
    }
}```

@CerebralFart
Copy link

I'm working on the same project as Mike, and I've found the cause of this problem. The problem doesn't necessarily have to do with authenticated routes, but that's the only place we use the user object in this project. Instead, the problem was in the migration adding the type column. In the designs for this project, the column was defined as a char (the C kind). But databases use the same term to refer to a fixed-length string, so too does Laravel's Migration Blueprint class. So our migration contained the line:

$table->char('type')->default('U');

This meant that all values in this field would automatically be padded to the default string length (255 chars). Upon retrieving, the value would then be the letter U followed by 254 spaces. This key is not present in the $childTypes array, for which Parental threw an error. Instead, we should've explicitly defined the length of the field:

$table->char('type', 1)->default('U');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants