KBEC-00228 - Logged in user

Article ID:360032829412
1 minute readKnowledge base
On this page

Summary

How do I access the name of the currently logged in user?.

Solution

From the Online Help

/myUser/... This property can be used only if the current session is associated with:


the predefined "admin" user, a user defined as "local", or a user defined by a Directory Provider (LDAP or ActiveDirectory).


This property cannot be used if the user is a "project principal", which is normally the case when running inside a Commander step.





For example, with an interactive login you can use:


ectool getProperty /myUser/userName (to get the user name of the logged in user, or...) ectool getProperty /myUser/email (to get the email address)

Examples

# Get the current userName


# This has to be handled differently if this code is running from within a Commander Step vs. externally.


# The property /myUser/userName can only be used when NOT in a Commander Step.


# In that case, use the property /myJob/launchedByUser.


use strict;


use ElectricCommander;


my $ec = ElectricCommander->new();


my $userProperty = '';


if($ENV{'COMMANDER_JOBSTEPID'}) {


    $userProperty = '/myJob/launchedByUser';


}


else {


    $userProperty = '/myUser/userName';


}


my $xpath = $ec->getProperty($userProperty);


my $currentUser = $xpath->findvalue('//value')->string_value;


print "currentUser = $currentUser\n";
This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.