How to get the current user? External App

Hi there,

some time ago we wrote a little helping app, which worked fine.

After upgrading from NC 18 > 19, we got an issue with the current logged in user.

Working solution:

  • including the base.php (require_once(’…/lib/base.php’);).

  • getting the current user with admin privileges ($getusername = OCP\User::getUser();).

  • checked the user/group db and allowed the access.

Problem now:

  • including the base.php now crashes the app (unlimited loading)

Question:

What’s the easiest way to simple get the current user name (display name) in PHP?

Sry for the (simple) question, but we stuck at the moment.

Thx, with best regards

Maybe
<?php echo get_current_user(); ?>

Thx for the reply.
This is the server/php user.
We need the current nextcloud user, which we formaly got with: OCP\User::getUser();.

You can use the userSession from dependency Injection.
This gives you an Instance of IUser, of which you can use getDisplayName()
$userSession->getUser()->getDisplayName()

See e.g.

1 Like