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

[Feature Request] SANCTUM SUPPORT #2991

Closed
masterbater opened this issue Jun 2, 2024 · 3 comments · Fixed by #2580
Closed

[Feature Request] SANCTUM SUPPORT #2991

masterbater opened this issue Jun 2, 2024 · 3 comments · Fixed by #2580

Comments

@masterbater
Copy link
Contributor

masterbater commented Jun 2, 2024

For now I check all the issue and found a way to add sanctum support but I think instead of manual, the library could make this integrated with test like the cache and queue by @GromNaN made to support. This has status backlog in Jira, but I think with the quick support of cache in 4.3 version it should easily added and add also in docs. Thanks

Copy original PersonalAccessToken
cp ./vendor/laravel/sanctum/src/PersonalAccessToken.php ./app/Models/PersonalAccessToken.php

<?php

namespace App\Models;

use MongoDB\Laravel\Eloquent\Model;
use Laravel\Sanctum\Contracts\HasAbilities;

class PersonalAccessToken extends Model implements HasAbilities
{
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'abilities' => 'json',
        'last_used_at' => 'datetime',
        'expires_at' => 'datetime',
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'token',
        'abilities',
        'expires_at',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'token',
    ];

    /**
     * Get the tokenable model that the access token belongs to.
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function tokenable()
    {
        return $this->morphTo('tokenable');
    }

    /**
     * Find the token instance matching the given token.
     *
     * @param  string  $token
     * @return static|null
     */
    public static function findToken($token)
    {
        if (strpos($token, '|') === false) {
            return static::where('token', hash('sha256', $token))->first();
        }

        [$id, $token] = explode('|', $token, 2);

        if ($instance = static::find($id)) {
            return hash_equals($instance->token, hash('sha256', $token)) ? $instance : null;
        }
    }

    /**
     * Determine if the token has a given ability.
     *
     * @param  string  $ability
     * @return bool
     */
    public function can($ability)
    {
        return in_array('*', $this->abilities) ||
            array_key_exists($ability, array_flip($this->abilities));
    }

    /**
     * Determine if the token is missing a given ability.
     *
     * @param  string  $ability
     * @return bool
     */
    public function cant($ability)
    {
        return !$this->can($ability);
    }
}

Then in AppServiceProvider

<?php

namespace App\Providers;

use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        $loader = AliasLoader::getInstance();
        // using custom PersonalAccessToken model instead of Sanctum's
        $loader->alias(\Laravel\Sanctum\PersonalAccessToken::class, \App\Models\PersonalAccessToken::class);
    }
}

All references
#2444
#2445
https://jira.mongodb.org/browse/PHPORM-88

@GromNaN
Copy link
Member

GromNaN commented Jun 3, 2024

We can duplicate the class in this package and add tests, but we will have to keep track of the changes in the parent class and ensure it's not used for type check. Maybe a real class_alias would be required. Have you tried?

Alternatively, I'd like to use move the MongoDB\Laravel\Eloquent\Model class into a trait, so we can extend the sanctum model class. Something I started by need to work on: #2580

@masterbater
Copy link
Contributor Author

masterbater commented Jul 2, 2024

will this update #2580, like override also the extended SpatieRole? If so this update would open up a lot package to be usable that using only Eloquent

<?php
namespace App\Models;
use Spatie\Permission\Models\Role as SpatieRole;

class Role extends SpatieRole
{ 
use DocumentModel
    // You might set a public property like guard_name or connection, or override other Eloquent Model methods/properties
}

@masterbater
Copy link
Contributor Author

I read this Fix #2120 so it does fix issue with extending

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

Successfully merging a pull request may close this issue.

2 participants