midrange.com code scratchpad
Name:
Switching Users
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
05/31/2013 06:23:38 pm
IP:
Logged
Description:
I used an article by Shannon to get started, but for whatever reason, I was having issues with switching back to the original user when using authorities. This is what I ended up doing. $SwitchToNetworkUser & $SwitchToOriginalUser are procedures in a service program.

I just pasted this in quick, so I didn't take out our modmarks. I didn't remove the error report stuff (though didn't provide the o-specs either, that's easy enough for you to remove or make up your own print file or o-specs.
Code:
  1. 17820D ApiErrorDS      DS                  Inz Qualified                        
  2.      D  ErrBytPrv                    10i 0 Inz( %size( ApiErrorDS ) )           
  3.      D  ErrBytAvl                    10i 0                                      
  4.      D  ErrMsgID                      7a                                        
  5.      D                                1a                                        
  6.      D  ErrMsgDta                   256a 
  7.  
  8.      D gCurrentProfileHandle...                                                 
  9.      D                 S             12a 
  10.  
  11. 29563 //  Get Profile Handle API                                                
  12. 29563 //   Parameters:                                                          
  13. 29563 //      UserID = userid to retrieve a profile handle for                  
  14. 29563 //    Password = password of the user-id above                            
  15. 29563 //      Handle = the profile handle that's returned.                      
  16. 29563 //   ErrorCode = API error code, used to return any errors.               
  17. 29563 //    pwLength = Length of Password - required when supplying PW          
  18. 29563 //     pwCCSID = CCSID for the Password.                                  
  19. 29563D GetProfile      PR                  ExtPgm( 'QSYGETPH' )                 
  20. 29563D  UserID                       10a   Const                                
  21. 29563D  Password                     10a   Const                                
  22. 29563D  Handle                       12a                                        
  23. 29563D  ds_Error                           Options( *varsize )                  
  24. 29563D                                     Like( ApiErrorDS )                   
  25. 29563D  pwLength                     10i 0 Const Options( *NoPass )             
  26. 29563D  ccsidLength                  10i 0 Const Options( *NoPass )             
  27.                                                                                 
  28. 29563 //  Set User Profile API:                                                 
  29. 29563 //   Parms:                                                               
  30. 29563 //      Handle = User Profile handle (returned by QSYGETPH API)           
  31. 29563 //   ErrorCode = standard API error code structure                        
  32. 29563D SetProfile      PR                  ExtPgm( 'QWTSETP' )                  
  33. 29563D  Handle                       12a   Const                                
  34. 29563D  ds_Error                           Options( *varsize )                  
  35. 29563D                                     Like( ApiErrorDS )  
  36.  
  37. 29563 //=======================================================================*
  38. 29563 //    Procedure:  $SwitchToNetworkUser                                   *
  39. 29563 //  Description:  Switch the job to using a User with Network Authority  *
  40. 29563 //                                                                       *
  41. 29563 //         Note:  This must be called before performing needed network   *
  42. 29563 //                activity.   Afterward, $SwitchToOriginalUser must be   *
  43. 29563 //                called to return to the original user profile.         *
  44. 29563 //                                                                       *
  45. 29563 //   Parameters:                                                         *
  46. 29563 //     Input - n/a                                                       *
  47. 29563 //                                                                       *
  48. 29563 //  Return Value: '1' - Success                                          *
  49. 29563 //                '0' - Failure                                          *
  50. 29563 //                                                                       *
  51. 29563 //-----------------------------------------------------------------------*
  52. 29563P $SwitchToNetworkUser...                                                  
  53. 29563P                 B                   Export                               
  54. 29563D                 PI              n                                        
  55.                                                                                 
  56. 29563 //  Local Constants                                                       
  57. 29563 //  Local variables                                                       
  58. 29563D handle          S             12A                                        
  59. 29563D rtnSuccess      S               n   Inz( *On )                           
  60. 29563 //-----------------------------------------------------------------------*
  61. 29563 /free                                                                     
  62.                                                                                 
  63. 29563  //  Get the current user profile so we can switch back to it.            
  64. 29563  //  Only do this if we haven't switched back to the original,            
  65. 29563  //  which clears the global handle afterward.                            
  66. 29563  If gCurrentProfileHandle = *blanks;                                      
  67. 29563    retrieveProfile( '*CURRENT': '*NOPWDCHK': gCurrentProfileHandle );     
  68. 29563  EndIf;                                                                   
  69.                                                                                 
  70. 29563  //  Get the handle to the network user.                                  
  71. 29563  If retrieveProfile( 'commonuser': 'password': handle );                     
  72.                                                                                 
  73. 29563    //  Now Set the Job Profile to a NEW User Profile                      
  74. 29563    If SetUserProfile( handle );                                           
  75. 29563      rtnSuccess = *On;                                                    
  76. 29563    EndIf;                                                                 
  77.                                                                                 
  78. 29563  EndIf;                                                                   
  79.                                                                                 
  80. 29563  Return rtnSuccess;                                                       
  81.                                                                                 
  82. 29563 /end-free                                                                 
  83. 29563P $SwitchToNetworkUser...                                                  
  84. 29563P                 E                                                        
  85. 29563 //-----------------------------------------------------------------------*
  86.                                                                                 
  87. 29563 //=======================================================================*
  88. 29563 //    Procedure:  $SwitchToOriginalUser                                  *
  89. 29563 //  Description:  Switch the job back to the original User Profile       *
  90. 29563 //                                                                       *
  91. 29563 //         Note:  This must be called after performing the needed        *
  92. 29563 //                network activity after calling $SwitchToNetworkUser.   *
  93. 29563 //                                                                       *
  94. 29563 //   Parameters:                                                         *
  95. 29563 //     Input - n/a                                                       *
  96. 29563 //                                                                       *
  97. 29563 //  Return Value: '1' - Success                                          *
  98. 29563 //                '0' - Failure                                          *
  99. 29563 //                                                                       *
  100. 29563 //-----------------------------------------------------------------------*
  101. 29563P $SwitchToOriginalUser...                                                 
  102. 29563P                 B                   Export                               
  103. 29563D                 PI              n                                        
  104.                                                                                 
  105. 29563 //  Local Constants                                                       
  106. 29563 //  Local variables                                                       
  107. 29563D handle          S             12A                                        
  108. 29563D rtnSuccess      S               n   Inz( *On )                           
  109. 29563 //-----------------------------------------------------------------------*
  110. 29563 /free                                                                     
  111.                                                                                 
  112. 29563  //  Now Set the Job Profile to a NEW User Profile                        
  113. 29563  If SetUserProfile( gCurrentProfileHandle );                              
  114. 29563    rtnSuccess = *On;                                                      
  115.                                                                                 
  116. 29563    //  Clear the profile handle value so the 'switch to network user'     
  117. 29563    //  knows it needs to save the original user again.                    
  118. 29563    //  This is done to try and prevent any situation where the switch     
  119. 29563    //  to network user happens multiple times - we don't want the process 
  120. 29563    //  to incorrectly think that the network user is the original user.   
  121. 29563    Clear gCurrentProfileHandle;                                           
  122. 29563  EndIf;                                                                   
  123.                                                                                 
  124. 29563  Return rtnSuccess;                                                       
  125.                                                                                 
  126. 29563 /end-free                                                                 
  127. 29563P $SwitchToOriginalUser...                                                 
  128. 29563P                 E                                                        
  129. 29563 //-----------------------------------------------------------------------* 
  130.  
  131.       //=======================================================================*
  132.       //    Procedure:  retrieveProfile                                        *
  133.       //  Description:  Retrieve the User Profile Handle                       *
  134.       //                                                                       *
  135.       //   Parameters:                                                         *
  136.       //     Input - User ID                                                   *
  137.       //     Input - User Password                                             *
  138.       //    Output - Profile Handle                                            *
  139.       //                                                                       *
  140.       //  Return Value: '1' - Success                                          *
  141.       //                '0' - Failure                                          *
  142.       //                                                                       *
  143.       //-----------------------------------------------------------------------*
  144.      P retrieveProfile...                                                       
  145.      P                 B                                                        
  146.      D                 PI              n                                        
  147.      D  UserID                       10a   Value                                
  148.      D  Password                     10a   Value                                
  149.      D  outHandle                    12a                                        
  150.                                                                                 
  151.       //  Local Constants                                                       
  152.      D PW_CCSID        C                   37                                   
  153.       //-----------------------------------------------------------------------*
  154.       /Free                                                                     
  155.                                                                                 
  156.        UserID = %Xlate( CASE_LOWER: CASE_UPPER: UserID );                       
  157.        Password = %Xlate( CASE_LOWER: CASE_UPPER: Password );                   
  158.                                                                                 
  159.        If userID = '*CURRENT';                                                  
  160.          GetProfile( UserID: Password: outHandle: ApiErrorDS );                 
  161.        Else;                                                                    
  162.          GetProfile( UserID: Password: outHandle: ApiErrorDS: %len( Password ): 
  163.                      PW_CCSID );                                                
  164.        EndIf;                                                                   
  165.                                                                                 
  166.        //  If error bytes exist, an error occurred.                             
  167.        If ApiErrorDS.ErrBytAvl > 0;                                             
  168.          prtMsgID = ApiErrorDS.ErrMsgID;                                        
  169.          prtMessage = 'GetProfile: ' + ApiErrorDS.ErrMsgDta;                    
  170.          checkOverflow();                                                       
  171.          Except Detail;                                                         
  172.          Return *Off;                                                           
  173.                                                                                 
  174.        //  Success                                                              
  175.        Else;                                                                    
  176.          Return *On;                                                            
  177.        EndIf;                                                                   
  178.                                                                                 
  179.       /End-Free                                                                 
  180.      P retrieveProfile...                                                       
  181.      P                 E                                                        
  182.       //-----------------------------------------------------------------------*  
  183.  
  184.       //=======================================================================*
  185.       //    Procedure:  SetUserProfile                                         *
  186.       //  Description:  Set Job to use New User Profile                        *
  187.       //                                                                       *
  188.       //   Parameters:                                                         *
  189.       //     Input - Profile Handle                                            *
  190.       //                                                                       *
  191.       //  Return Value: n/a                                                    *
  192.       //                                                                       *
  193.       //-----------------------------------------------------------------------*
  194.      P SetUserProfile  B                                                        
  195.      D                 PI              n                                        
  196.      D  profileHandle                12a   Value                                
  197.                                                                                 
  198.       //-----------------------------------------------------------------------*
  199.       /Free                                                                     
  200.                                                                                 
  201.        SetProfile( profileHandle: ApiErrorDS );                                 
  202.                                                                                 
  203.        //  If error bytes exist, an error occurred.                             
  204.        If ApiErrorDS.ErrBytAvl > 0;                                             
  205.          prtMsgID = ApiErrorDS.ErrMsgID;                                        
  206.          prtMessage = 'SetProfile: ' + ApiErrorDS.ErrMsgDta;                    
  207.          checkOverflow();                                                       
  208.          Except Detail;                                                         
  209.          Return *Off;                                                           
  210.                                                                                 
  211.        //  Success                                                              
  212.        Else;                                                                    
  213.          Return *On;                                                            
  214.        EndIf;                                                                   
  215.                                                                                 
  216.       /End-Free                                                                 
  217.      P SetUserProfile  E                                                        
  218.       //-----------------------------------------------------------------------* 
© 2004-2019 by midrange.com generated in 0.01s valid xhtml & css