Get Users with specified rights


how can I retrieve all users with a special Authorisation Assignments?

I try to send an E-Mail to users with specific rights?

How can I accomplish that?

Kind regards

I’m not sure if you can do that, given that the roles have a tree structure. I think your best bet is to iterate through all users and check if each has the required access.

    // Fetch all users first

    foreach ($users as $user)


        if (Yii::app()->authManager->checkAccess('role', $user->id))


            // Either add to a collection for later processing or send each email here



If you have lots of users, this won’t exactly be efficient, but it should be fine if the operation is only performed occasionally.

Do you use CDbAuthManager? If so there should be a table in your db that stores user permissions so you’ll be able to query this table (usually named “AuthAssignment”) to only get users with certain access rules (joining the records with your user table).

A rough idea:

Add a custom scope named “emailGuys” to your User AR-model and define a condition like ‘id IN (SELECT user_id FROM AuthAssignment WHERE your where clause here)’

You should then be able to do User::model()->emailGuys->findAll();

@Keith: I already thought about that solution but discarded that because there will be a lot of users in the database.

@Haensel: Thanks. I will give it a try.