Skip to main content
1-Visitor
Jul 1, 2016 11:13 AM UTC
Solved

Javscript code to get logged in user

Hi all,

is there a way to get the currently logged in user from javascript? I want to validate the promotion approvers table, so that the user didn't select himself. I added a new afterJS method to the workflowParticipantsStep where I can get the table with the principals but I have to get the the logged in user somehow.

Thx,

Tamás

16-Pearl
Jul 14, 2016 6:08 PM UTC

Hi Tamas,

One of the other ways is to do this from Java code using custom delegate, the OOTB delegate is marked supported for customization.

Change the highlighted  in Windchill\codebase\config\actions\PromotionRequest-actions.xml  (I will prefer to do this in custom*Action file, refer guide for more details.)

<action name="workflowParticipantsStep" required="false" preloadWizardPage="false">

        <description>Promote wizard promotion objects step</description>

        <command class="ext.CustomPromotionParticipantsFormDelegate" windowType="wizard_step"/>

      </action>

e.g.

public class CustomPromotionParticipantsFormDelegate  extends PromotionParticipantsFormDelegate  {

  @Override

  public FormResult preProcess(NmCommandBean commandBean, List<ObjectBean> objectBeans)

  throws WTException {

  // TODO Auto-generated method stub

  //getParticipantsToProcess(arg0)

  for (ObjectBean objectBean : objectBeans) {

  Object object = objectBean.getObject();

  if (object instanceof LifeCycleManaged) {

  HashMap mapp= (HashMap) getParticipantsToProcess(objectBean);

  Iterator it = mapp.entrySet().iterator();

  while (it.hasNext()) {

  Map.Entry pair = (Map.Entry)it.next();

  Iterator iter = ((Set)pair.getValue()).iterator();

  while (iter.hasNext()) {

  WTUser principal=(WTUser) iter.next();

  WTUser Sessionuser= (WTUser) SessionHelper.manager.getPrincipal();

  //System.out.println(principal+"--"+Sessionuser);

  /*

  * Note pair.getKey() will give you the role, you can execute the following only for a particular role

  *

  */

  if (principal.getName().equalsIgnoreCase(Sessionuser.getName())) {

  FormResult result;

  FeedbackMessage errors= new FeedbackMessage();

  errors.addMessage("You cannot select yourself as  "+ pair.getKey()+ " Please select any other user");

  result = new FormResult(FormProcessingStatus.FAILURE);

  result.addFeedbackMessage(errors);

  return result;

  }

  }

  }

  }

  }

  return super.preProcess(commandBean, objectBeans);

  }

}

Regards,

Bhushan

3 replies

2-Explorer
Jul 1, 2016 11:50 AM UTC

I think +you can use getRemoteUser() and getUserPrincipal() methods of HttpServletRequest object or else

wt.org.WTUser loggedInUser = (wt.org.WTUser)wt.session.SessionHelper.manager.getPrincipal();

Regards

Binesh Kumar

Medtronic MITG

Translate
16-Pearl
Jul 14, 2016 6:08 PM UTC

Hi Tamas,

One of the other ways is to do this from Java code using custom delegate, the OOTB delegate is marked supported for customization.

Change the highlighted  in Windchill\codebase\config\actions\PromotionRequest-actions.xml  (I will prefer to do this in custom*Action file, refer guide for more details.)

<action name="workflowParticipantsStep" required="false" preloadWizardPage="false">

        <description>Promote wizard promotion objects step</description>

        <command class="ext.CustomPromotionParticipantsFormDelegate" windowType="wizard_step"/>

      </action>

e.g.

public class CustomPromotionParticipantsFormDelegate  extends PromotionParticipantsFormDelegate  {

  @Override

  public FormResult preProcess(NmCommandBean commandBean, List<ObjectBean> objectBeans)

  throws WTException {

  // TODO Auto-generated method stub

  //getParticipantsToProcess(arg0)

  for (ObjectBean objectBean : objectBeans) {

  Object object = objectBean.getObject();

  if (object instanceof LifeCycleManaged) {

  HashMap mapp= (HashMap) getParticipantsToProcess(objectBean);

  Iterator it = mapp.entrySet().iterator();

  while (it.hasNext()) {

  Map.Entry pair = (Map.Entry)it.next();

  Iterator iter = ((Set)pair.getValue()).iterator();

  while (iter.hasNext()) {

  WTUser principal=(WTUser) iter.next();

  WTUser Sessionuser= (WTUser) SessionHelper.manager.getPrincipal();

  //System.out.println(principal+"--"+Sessionuser);

  /*

  * Note pair.getKey() will give you the role, you can execute the following only for a particular role

  *

  */

  if (principal.getName().equalsIgnoreCase(Sessionuser.getName())) {

  FormResult result;

  FeedbackMessage errors= new FeedbackMessage();

  errors.addMessage("You cannot select yourself as  "+ pair.getKey()+ " Please select any other user");

  result = new FormResult(FormProcessingStatus.FAILURE);

  result.addFeedbackMessage(errors);

  return result;

  }

  }

  }

  }

  }

  return super.preProcess(commandBean, objectBeans);

  }

}

Regards,

Bhushan

Translate
15-Moonstone
Jul 5, 2017 2:27 PM UTC

Well....
A one line solution...

PTC.navigation.GLOBAL_USER

 

 

Translate