cancel
Showing results for 
Search instead for 
Did you mean: 

Get current user

junmei
Participant
0 Kudos

I know, old question, has been answered a milion times by now but mine is somewhat different...

I'm trying to add the current user in an inputfield for a metadata property. We need to add multiple users in on metadata property and I need the current user as the default user. No problem you would say.

Problem is I wrote this customized Java class (NO Web Dynpro or iView) based on Thilo Brandts article:

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/06cbeb90-0201-0010-b584-9b9...

This class is added as a metadata extension so it's NOT a standalone application.

Result is I do not have the 'request' value I need to get the current user id with the:

IUser me = request.getUser();

command. Also I don't find the information on how to use the context method to retrieve the user information.

Anyone?

Accepted Solutions (0)

Answers (1)

Answers (1)

harman_shahi
Contributor
0 Kudos

Hi Luc,

this may help....

If you have a hold of the IResource object (res), then you may be able to use the following command to get the current user:

com.sapportals.portal.security.usermanagement.IUser currentUser = res.getContext().getUser();

regards,

harman

junmei
Participant
0 Kudos

Indeed this looks like the code we need but where do I get the IRecourse ('res') variable from?

public IMetaValue getDefaultValue(IMetaName metaName) { ... }

This is the function where I need the user and as you can see there is no context variable that I can use. This is an obligated function that I have to implement according to the interface.

Any ideas?

Former Member
0 Kudos

Hi,

The code for getting all the users in portal is

IUserFactory userfactory = UMFactory.getUserFactory();

IUserSearchFilter userfltr = userfactory.getUserSearchFilter();

userfltr.setMaxSearchResultSize(2000);

ISearchResult result = userfactory.searchUsers(userfltr);

while (result.hasNext())

{

Vector inData = new Vector();

String uniqueid = (String) result.next();

IUser user = userfactory.getUser(uniqueid);

System.out.println("User Name " + user.getName());

}

Regards

gEorgE

junmei
Participant
0 Kudos

Thanks for the code example but this doesn't solve my problem... I need the username of the CURRENT USER, this list gives an overview of all the users.

How do I extract the current user from this list?!

Let's say I would like to upload a document... the default author value should show MY name.

Thanks!

Former Member
0 Kudos

Hi Luc,

You can try out if ur using portal components

IPortalComponentRequest request=(IPortalComponentRequest)this.getRequest();

IUser user=request.getUser();

String name=user.getUniqueID();

For webDynpro....

IWDClientUser wdUser = WDClientUser.getCurrentUser();

IUser user = wdUser.getSAPUser();

if (user != null)

{

IUserAccount[] acct = user.getUserAccounts();

if(acct[0] != null)

{

String strUserid = acct[0].getLogonUid();

}

}

Hope this Helps

gEorgE

junmei
Participant
0 Kudos

I'm not using WebDynpro, just downloaded the example and tried to adjust it. Problem is I'm not writing a full application but just an extension for my metadata in the Knowledge Management module = only one Java class with a bundle file.

As a result the getReaquest() method does not exist... because it's not a component, just an extension.

Thanks for the help but it still doesn't solve my problem.

Former Member
0 Kudos

Hi Luc,

I don't knwo if it works, but you can try this within your method:

this.model.createEmptyMetaContext().getResourceContext().getUser();

Hope this help

Cheers

Roberto

junmei
Participant
0 Kudos

Thanks for your answer but this doesn't work either...

This is the kind of solution is I'm looking for: how to get the resource context to ask for the current user. Problem is I don't have the resource context and creating an empty one like Roberto suggests doesn't solve it, it's just an empty resource context.

I was thinking of making a component (that has a valid resource context and can therefor look for the current user) but I don't know if this will work and how I should do this?!

Former Member
0 Kudos

If you want to get the current user of the resource you are uploading or creating or accessing in KM repository. (That is how I get my current user) What Harman post should be the right one:

com.sapportals.portal.security.usermanagement.IUser currentUser = res.getContext().getUser();

Kent