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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在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。

使用sql select,您可以从数据库中的用户表中获取任何您想要的内容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 = humhubmodulesadminwidgetsAdminMenu::canAccess();

以上是关于在HumHub中使用第三方应用程序获取用户详细信息的主要内容,如果未能解决你的问题,请参考以下文章

Humhub:“索引正在处理中”

如何从推荐URL获取推荐的用户详细信息?

如何在 keycloak 中验证令牌并获取用户详细信息?

在 Android 中获取在线广播歌曲详细信息

如何在 C# 中获取当前用户的 Active Directory 详细信息

获取由用户管理的公司的详细信息 - Ruby on Rails