cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

Javscript code to get logged in user

TamasBiro
12-Amethyst

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

ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

3 REPLIES 3

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

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

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

PTC.navigation.GLOBAL_USER

 

 

Announcements


Top Tags