Skip to content

Instantly share code, notes, and snippets.

@aguileraq
Created September 7, 2021 23:13
Show Gist options
  • Save aguileraq/fd44daeb03c1a0b529a00e0a49c3b392 to your computer and use it in GitHub Desktop.
Save aguileraq/fd44daeb03c1a0b529a00e0a49c3b392 to your computer and use it in GitHub Desktop.
Call to undefined method stdClass::getRole()
<?php
namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class Auth implements FilterInterface{
public function before(RequestInterface $request, $arguments = null) {
// if user not logged in
if(! session()->get('is_logged')){
// then redirct to login page
return redirect()->to('/');
}
$model = model('UsersModel');
if( !$user = $model->getUserBy('id', session()->id_user) ) {
session()->destroy();
return redirect()->route('/')->with('msg',[
'body' => 'Usuario no disponible'
]);
}
dd($user->getRole());
$modelGroup = model('GroupsModel');
$role = $modelGroup->getRole('id', session()->group_id);
//dd(session()->group_id);
if(in_array($role->value, $arguments)){
throw PageNotFoundException::forPageNotFound();
}
}
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null){
// Do something here
}
}
<?php
namespace App\Entities;
use CodeIgniter\Entity;
class User extends Entity {
protected $datamap = [];
protected $dates = [
'created_at',
'updated_at',
'deleted_at',
];
protected $casts = [];
public function getRole(){
$model = model('GroupsModel');
return $model->asObject()->where('id', $this->group)->first();
}
}
<?php
namespace App\Models;
use CodeIgniter\Model;
use App\Entities\User;
class UsersModel extends Model{
protected $DBGroup = 'default';
protected $table = 'users';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $insertID = 0;
protected $returnType = User::class;
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [];
// Dates
protected $useTimestamps = false;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
// Validation
protected $validationRules = [];
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
public function getUserBy(string $column, string $value){
return $this->asObject()->where($column,$value)->first();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment