在HumHub中使用第三方应用程序获取用户详细信息


Getting User details with third party app in HumHub

我正在为humhub建立一个自定义的网络研讨会。我使用的是Custom_pages模块(https://www.humhub.org/marketplace/details?id=13)我怎样才能在我的应用程序内获得当前登录用户的用户详细信息?

有人给了我这个代码来试试

//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;
}

但是由于某些原因,line 3使页面无法加载。如果我把它注释掉,页面加载,但Yii是空的。

是否有任何方法我可以在这个第三方应用程序中获得登录用户的用户详细信息?

我使用了你的代码,它对我来说工作得很好,但是我需要根据我的需要修改它。也许我的修改对你有用。

    <?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";
}
?>

我把它放在humhub的iframe自定义页面的顶部。当您需要用户名时,只需使用$hum_username。

WHERE id='$hum_uid'

更容易:

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

其他有用的变量:

$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();