lonnieezell / myth-auth

One-stop Auth package for CodeIgniter 4
MIT License
632 stars 207 forks source link

published myth auth withGroup() not working #572

Closed halimkun closed 1 year ago

halimkun commented 1 year ago

hello i am currently using myth auth to build an application that i am making.

I'm having a problem calling the withGroup() function, where my model initialization has been done as follows.

protected $userModel;

// in construction 
$this->userModel = new \App\Models\UserModel();

userModel is the default from myth/auth which has been published using spark

$this->userModel->withGroup('admin')->findAll(),

but when you want to use the withGroup function it still displays all data outside the group.


roughly looks like the following small part of the model.

namespace App\Models;

use CodeIgniter\Model;
use Myth\Auth\Models\GroupModel;
use \App\Entities\User;
use Faker\Generator;

/**
 * @method User|null first()
 */
class UserModel extends Model
{
...

/**
     * Sets the group to assign any users created.
     *
     * @return $this
     */
    public function withGroup(string $groupName)
    {
        $group = $this->db->table('auth_groups')->where('name', $groupName)->get()->getFirstRow();

        $this->assignGroup = $group->id;

        return $this;
    }

...
}
manageruz commented 1 year ago

Hi @halimkun User model's withGroup() method is used when creating new users, not when searching existing users from db.

The UserModel can automatically assign a role during user creation. Pass the group name to the withGroup() method prior to calling insert() or save() to create a new user and the user will be automatically added to that group. $user = $userModel ->withGroup('guests') ->insert($data);

manageruz commented 1 year ago

No answer from author, closing as solved.