Trevor
Trevor

Reputation: 149

Integrating Yii2 into Joomla and using Joomla's "user" table

Ultimately, I'm trying to integrate Yii2 into Joomla so that I can use Joomla's user management capabilities in conjunction with Yii2's RBAC capabilities. For example, I want to allow a user to signup using Joomla's registration process, but I want to define the roles and authentication rules using Yii2's RBAC.

The problem is that Yii2 does not seem to be able to use Joomla's "user" table to lookup the user_id value for the currently logged-in user (which is necessary if I want to check permissions for logged-in users against Yii2's RBAC auth_assignment table).

NOTE: I am using the "basic" template alongside Joomla 3

Success criteria: - "Yii::$app->user->getId();" should return the currently logged-in user's user_id from Joomla's user table - Yii2's RBAC functions should also work and use Joomla's "user" table when looking up user_id --

if (Yii::$app->user->can('some-auth-item'))
    {
        return $this->render('some-page');
    }

Here is what I've attempted unsuccessfully thus far:

1) Added Yii2 to Joomla into index.php (this gets loaded without an issue)

LINK: http://www.yiiframew...ntegration.html CODE:

require('/../../../basic/vendor/yiisoft/yii2/yii.php');
$yiiConfig = require('/../../../basic/config/web.php');
new yii\web\Application($yiiConfig); // Do NOT call run() here

2) Change default User table in models/User.php

LINK: http://stackoverflow...le-name-in-yii2 CODE:

class User extends ActiveRecord implements IdentityInterface
  {

      public static function tableName()
      {
         return 'fm3lk_user';
      }

3) Tested using the following code in index.php and logged-in to Joomla front-end using Admin account, but does not return a value

<?php 
        $zzz = Yii::$app->user->getId();
        echo $zzz;
    ?>

Upvotes: 0

Views: 554

Answers (2)

Roberto Braga
Roberto Braga

Reputation: 559

You are confusing basic user model with user identity.

When a user login in joomla it create a user session which is not connected to yii2 user identity (Yii::$app->user which, once initialized, get data from session anyway but different structure from joomla one). In yii2 the user model implements the identity interface

    class User extends ActiveRecord implements IdentityInterface

Therefore after a joomla successful login you should login also the yii2.

http://www.yiiframework.com/doc-2.0/yii-web-user.html#login%28%29-detail

    $userModel = new User;
    $userModel->findIdentity($joomlaUserId);
    Yii::$app->getUser()->login($userModel, $duration);

where $userModel implement yii\web\IdentityInterface and is mapped over joomla user table, have $duration match the joomla session duration. After that you will get access to Yii::$app->user->getId();

At joomla logout lougout yii2 app as well.

Upvotes: 2

user5201742
user5201742

Reputation:

You can create a new User model with gii from Joomla's user table. I think it could be an easier starting point.

Upvotes: 0

Related Questions