Kohana 3 authorization module, getting users with the role of "staff" or "manager"

I am studying the structure and now I am creating an application using it.

I need all users to have a user or staff role, but I could not find this in the documentation.

Help someone? (I think this is more of an ORM problem in the auth module)

+5
source share
2 answers

I have not found an easy way to do this with ORM, but I have a workaround.
This is my code for anyone who might run into the same problem with me.

// One for each role
$staffs = ORM::factory('role', array('name' => 'staff'))->users->find_all()->as_array();
$managers = ORM::factory('role', array('name' => 'manager'))->users->find_all()->as_array();

// Merge the results
$results = array_merge($staffs, $managers);
+9
source

, ORM? - :

public function get_users(array $roles)
{
    $users = DB::select(array($this->_has_many['roles']['foreign_key'], 'id'))
               ->distinct(TRUE)
               ->from($this->_has_many['roles']['through'])
               ->where($this->_has_many['roles']['far_key'], 'IN', DB::expr('('.implode(',', $roles).')'))
               ->execute($this->_db);
    if (count($users) == 0)
    {
        // return empty list
        return array();
    }
    // now we need only IDs from result
    $ids = array();
    foreach($users as $columns)
    {
        $ids[] = $columns['id'];
    }
    // load users by id
    return $this->where($this->_primary_key, 'IN', DB::expr('('.implode(',', $ids).')'))->find_all();
}

$role - role_id ( !). PS. , "WHERE IN", DB.

+1

All Articles