How do I assign user roles to members from external database?

I hope this is simple.

I have an external database for an SMF forum which contains all my users. I can setup Yii so people are able to login with their forum name and password, but I don’t know how to assign each user a role, such as reader, moderator, admin and so on.

What I’d like to do is, create user groups in the forum and then use the user group IDs to assign a user role in Yii.

For instance I have a “content moderator” group within the forum with ID 10 and when people log in, I’d like Yii to take that ID 10 and assign a user role to it based on the ID.

Hello,

A possible implementation could be this:




$user = User::model()->findByPk(Yii::app()->user->id);

            

if($user !== null)

{

    Yii::app()->authManager->assign($user->role, $user->id);

}



This code should be placed in controller init() method. Or, if you want roles to be available anywhere in your application, you might consider extending CController with your own abstract controller class, placing the code inside it’s init() method, and extending this new class instead of CController:




abstract class BaseController extends CController 

{

    function init()

    {

        parent::init();

        

        $user = User::model()->findByPk(Yii::app()->user->id);

            

        if($user !== null)

        {

            Yii::app()->authManager->assign($user->role, $user->id);

        }

    }

}







class SiteController extends BaseController 

{

    // ...

}




With works with ROLE based authorization, not GROUP based.

So, you have to think your different authorizations group by ROLES. A good read at RBAC on the guide must help you.