laravel-auditing
laravel-auditing copied to clipboard
Call to undefined method App\\User::getMorphClass()
Q | A |
---|---|
Bug? | not sure |
New Feature? | maybe |
Framework | Laravel |
Framework version | 5.6.39 |
Package version | 8.0.4 |
PHP version | 7.2.13 |
Actual Behaviour
I'm getting this error while I try to retrieve a model:
Call to undefined method App\\User::getMorphClass()
I'm working on a microservice architecture and I don't have a users table on it, so I created a simple user model but It generated the error.
This is my Resolver:
<?php
namespace App\Resolvers;
use App\User;
class UserResolver implements \OwenIt\Auditing\Contracts\UserResolver
{
/**
* {@inheritdoc}
*/
public static function resolve()
{
return new User; // If I change this to null, It doens't have any problem.
}
}
And this is my User class:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
public function getAuthIdentifier()
{
return 1;
}
}
Expected Behaviour
I would like to set custom values on user_type and user_id:
Steps to Reproduce
You just need to create a model with Auditable interface and trait.
Then, add the retrieved event:
Possible Solutions
We're using Auth0, so It would be great to make the User class customizable. https://github.com/auth0/laravel-auth0
@andreshg112 Did you manage to resolve this? Having the same issue with the undefined getMorphClass
and we're also using Auth0.
@andreshg112 Did you manage to resolve this? Having the same issue with the undefined
getMorphClass
and we're also using Auth0.
I haven't. I'm using Audit transformation to customize user details. However, I would like it to be possible from the UserResolver.
@andreshg112 Did you manage to resolve this? Having the same issue with the undefined
getMorphClass
and we're also using Auth0.
I am having the same issue with auth0.
Call to undefined method Auth0\Login\Auth0JWTUser::getMorphClass()
Just ran into this, so if I find a reasonable solution, I'll post it here.
Looks like I didn't need to look too hard for my auth0 solution. In my user resolver, I just grab my eloquent user (which has implemented the Auditible
contract) and return that if found.
<?php
namespace My\Api;
use Illuminate\Support\Facades\Auth;
use My\Api\Models\User;
use OwenIt\Auditing\Contracts as AuditingContracts;
class UserResolver implements AuditingContracts\UserResolver
{
public static function resolve()
{
$user = Auth::user() ?: null;
if ($user !== null) {
$user = User::where('sub', $user->sub)->first();
}
return $user;
}
}
using @thril idea, I make it work in this way the My own UserResolver Stays the same But Will Create an Empty Model for a new Auth0JWTUser
<?php
namespace App\VendorContract;
use App\Models\Auth0User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use OwenIt\Auditing\Contracts as AuditingContracts;
class UserResolver implements AuditingContracts\UserResolver
{
public static function resolve()
{
$guards = Config::get('audit.user.guards', [
'web',
'api',
]);
foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
$user = Auth::guard($guard)->user();
return new Auth0User($user->getUserInfo());
}
}
}
}
My Own Auth0JWTUser
<?php
namespace App\Models;
class Auth0User extends \Auth0\Login\Auth0JWTUser
{
public function getMorphClass()
{
return "";
}
}
Also make a little change on the larave-auditing migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAuditsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('audits', function (Blueprint $table) {
....
$table->string('user_type')->nullable();
....
$table->index(['user_id', 'user_type']);
});
}