Getting User details with third party app in HumHub

半世苍凉 提交于 2019-12-11 12:29:29

问题


I am building a custom webinar for humhub. I am using the Custom_pages modules (https://www.humhub.org/marketplace/details?id=13) How can i get the user details of the current logged in user inside my app?

Someone gave me this code to try

//plug in to Yii application
require_once('../protected/vendors/yii/yii.php');
Yii::createWebApplication('../protected/config/main.php');

//------------------

//print_r(Yii::app());

echo "<br />";

//check if user is logged in (not a guest)
if(!Yii::app()->user->isGuest)
{
    //if logged in, display the username and ID
    echo "<strong>Logged In.</strong>";
    echo "<br />";
    echo "Username: ".Yii::app()->user->name;
    echo "<br />";
    echo "User ID: ".Yii::app()->user->id;
}
else
{
    //if not logged in, display username (Guest)
    echo "<strong>Not Logged In.</strong>";
    echo "<br />";
    echo "Username: ".Yii::app()->user->name;
}

But for some reason line 3 makes the page not load. if i comment it out, the page loads but Yii is empty.

Is there any way i can get the user details of the logged in user in this my third party app?

Any approach would be appreciated


回答1:


I used your code and it worked fine for me however I needed to modify it for my needs. Maybe my modifications will work for you.

    <?PHP
//this script gets the id and email "as name" from hum hub 
require_once('../humhub-master/protected/vendors/yii/yii.php');
Yii::createWebApplication('../humhub-master/protected/config/main.php');

//I create these vars to use in my custom pages
    $hum_uid=Yii::app()->user->id;
    $hum_name=Yii::app()->user->name;
    // this should get me a real username
$servername = "******";
$username = "******";
$password = "*****";
$dbname = "humhub";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, username FROM user WHERE id='$hum_uid'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $hum_username =  $row["username"];
        echo $hum_username;
    }
} else {
    echo "0 results";
}
?>

I put this at the top of a iframe custom page in humhub. When you need the username just use $hum_username.

Using sql select you can get anything you want out of the user table in the database WHERE id='$hum_uid'




回答2:


Much more easy :

$userUsername = Yii::$app->user->identity->username;

Other usefull vars :

$userDisplayName = Yii::$app->user->identity->getDisplayName();
$userAttributesArray = Yii::$app->user->identity->getSearchAttributes(); // all profile fields and groups      
$userIsGuest = Yii::$app->user->isGuest; // true = loggedOut, false = loggedIn
$userIsAdmin = \humhub\modules\admin\widgets\AdminMenu::canAccess();


来源:https://stackoverflow.com/questions/31230098/getting-user-details-with-third-party-app-in-humhub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!