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

Is there a guide around models #3

Open
dimitriadamou opened this issue Jul 15, 2022 · 1 comment
Open

Is there a guide around models #3

dimitriadamou opened this issue Jul 15, 2022 · 1 comment

Comments

@dimitriadamou
Copy link

dimitriadamou commented Jul 15, 2022

Hey there, I swapped my User and PersonalAccessToken (using Sanctum) over to Datastore - I am able to insert models when I use User::create([ 'xxx' ]); or PersonalAccessToken::create([ 'xxx' ]); however when I do something like the following

                $user = User::create([
                    'name' => $name,
                    'password' => $password
                ]);

                $token = $user->createToken("auth_token"); //error occurs here.

I am getting errors like User::__key__ not defined. However when I looked deeper into the Appsero Model, key looks like it needs to be a class object with a function path that returns a 0 based index with either key or name.

I am happy to create a PR if I'm right in thinking that something is missing - otherwise if I am using this wrong with my models, I would love to know how they are supposed to look. It has been a bit tricky getting Datastore working in Laravel but it looks like the fundamentals are working - thank you for this library.

I've added my models below - they are very simple.

DatastoreAuth

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Appsero\LaravelDatastore\Eloquent\Model;
use Illuminate\Foundation\Auth\Access\Authorizable;


class DatastoreAuth extends Model implements
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword, MustVerifyEmail;
}

PersonalAccessToken (The replacement for the Sanctum default)

<?php

namespace App\Models;

use Appsero\LaravelDatastore\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',
    ];

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

    /**
     * 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);
    }
}

And my User Model

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends DatastoreAuth
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}
@arafatkn
Copy link
Collaborator

Thanks for opening the issue.

createToken() function is not working correctly as we haven't implemented the relationship functionality yet. I am still working on this. Hopefully, "relationship" will be available in the next couple of days. After finishing it, I will let you know here.

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

2 participants