パスワードリセット の拡張スクリプト

  • リリースバージョン: Xanadu
  • 更新日 2024年08月01日
  • 所要時間:19分
  • 拡張スクリプトを使用すると、パスワードリセットの認証情報ストア、検証、または識別タイプの機能を拡張することができます。

    パスワードリセット の拡張スクリプトインクルード

    スクリプトインクルードはそれぞれ特定のカテゴリに関連付けられています。カテゴリは、パスワードリセット フォームの該当するフィールドで使用できます。

    注:
    パスワードリセット 拡張スクリプトフォーム (パスワードリセット > 拡張 > 新しい拡張スクリプト). 拡張スクリプトは、 システム定義 > スクリプトインクルード 使用した結果ですスクリプトインクルードを拡張スクリプトとして使用する場合は、スクリプトインクルードを [アクティブ] ステータスに設定する必要があります。

    「登録チェック」スクリプトインクルード カテゴリ

    スクリプトインクルード カテゴリ 説明 メソッド シグネチャ 入力フィールド 出力フィールド
    登録チェック 指定した検証用にユーザーが登録されているかどうかをチェックします。 process(params) パラメーター:
    • params.userId:チェックするユーザーの sys_id (テーブル:sys_user)。
    • params.verificationId:チェックする検証の sys_id (テーブル:pwd_verification)。
    戻り値:(ブール値) 指定した検証でユーザーが登録されている場合は true を返します。それ以外の場合は false を返します。

    この登録チェックの例は、想定されている両方のパラメーターが指定されている場合に、ユーザーが登録されていることを通知します。コードは、SampleEnrollmentCheck という名前の拡張スクリプトの Script フィールドに含めます。

    var SampleEnrollmentCheck = Class.create ();
    SampleEnrollmentCheck. prototype = {
      category:'password_reset.extension.enrollment_check', // DO NOT REMOVE THIS LINE!
     
       /**********
       * Returns boolean telling whether the user is enrolled.
       * This sample returns true if both parameters are supplied, false otherwise 
       *
       * @param params.userId         The sys-id of the user being checked (table: sys_user)
       * @param params.verificationId The sys-id of the verification being checked (table: pwd_verification)
       * @return Boolean indicating whether the user is enrolled into the specified verification
       **********/
      process:function (params) {return (params.userId && params.verificationId)? true:false; },
     
      type:'SampleEnrollmentCheck'};

    「登録フォーム プロセッサー」スクリプトインクルード カテゴリ

    スクリプトインクルード カテゴリ 説明 メソッド シグネチャ 入力フィールド 出力フィールド
    登録フォーム プロセッサー ユーザーから必要な情報をすべて収集したかどうかをチェックします。ユーザーがパスワードをリセットするときに検証に使用できるように、これらの情報を格納します。 process(params) パラメーター:
    • params.resetRequestId:現在のパスワードリセット要求の sys_id (テーブル:pwd_reset_request)。
    • params.userId:検証するユーザーの sys_id (テーブル:sys_user)。
    • params.verificationId:処理する検証の sys_id (テーブル:pwd_verification)。
    • request:フォーム要求オブジェクト。request.getParameter('<element-id>') を使用してフォーム内のフィールドにアクセスできます。
    登録プロセスのステータスに次の情報を追加してください。
    • gs.getSession().putProperty("result.status",status):ユーザーが正常に登録されたかどうか。
    • gs.getSession().putProperty("result.message",message):UI に返す関連メッセージ (詳細なエラーメッセージなど)。
    • gs.getSession().putProperty("result.value",value):登録に関連付けられたカスタムの値。
    戻り値:(ブール値) 指定した検証でユーザーが登録されている場合は true を返します。それ以外の場合は false を返します。

    この例は、ユーザーが送信した応答が成功した場合に、登録フォームの送信を正常に処理します。コードは、SampleEnrollmentProcessor という名前の拡張スクリプトの Script フィールドに含めます。

    var SampleEnrollmentProcessor = Class.create ();
    SampleEnrollmentProcessor.prototype = {
        category: 'password_reset.extension.enrollment_form_processor', // DO NOT REMOVE THIS LINE!
     
         /**********
        * Process the enrollment form request, and return whether the user was successfully enrolled.
        * 
        * @param params.userId         The sys_id of the user trying to enroll (table: sys_user)
        * @param params.verificationId The sys_id of the verification to be enrolled into (table: pwd_verification)
        * @param params.enrollmentId   The sys_id of this enrollment process
        * @param request               The form request object. Felds in the form can be accessed with
        *                                           request.getParameter('<element-id>')
        * @return boolean telling whether the user was successfully enrolled
        * The following information should be added to the state of the enrollment process
        *     gs.getSession().putProperty("result.status",status) - whether the user was successfully enrolled
        *     gs.getSession().putProperty("result.message",message) - an associated message to be returned 
        *             to the UI. Eg. a detailed error message
        *     gs.getSession().putProperty("result.value",value) - custom value associated with the enrollment
        **********/
        processForm:function (params, request) {var verificationId = params.verificationId; var sampleInput = request.getParameter ('sample_input');
     
    	 if (gs.nil (verificationId) || (sampleInput!= 'success')) { return false; }
     
    	 var now_GR = new GlideRecord ('sys_user');
    	now_GR.get (params. userId);
    	now_GR.print ('User:' + now_GR.getValue ('user_name') + ' successfully enrolled'); return true; },
        type: 'SampleEnrollmentProcessor' };

    「識別フォーム プロセッサー」スクリプトインクルード カテゴリ

    スクリプトインクルード カテゴリ 説明 メソッド シグネチャ 入力フィールド 出力フィールド
    識別フォーム プロセッサー 識別フォーム要求を処理します。 processForm(params, request) パラメーター:
    • params.processId:呼び出し側のパスワードリセット プロセスの sys_id (テーブル:pwd_process)。
    • request:フォーム要求オブジェクト。request.getParameter('<element-id>') を使用してフォーム内のフィールドにアクセスできます。フォームに入力されたユーザー ID を取得するには、request.getParameter('sysparm_user_id') を使用します。
    戻り値:要求した入力に対応するユーザーの sys_id を返します。ユーザーが見つからなかった場合は null を返します。

    この例は、識別フォームから送信されたユーザー名を指定して、sys_user テーブル内のユーザーの識別を試みます。コードは、PwdIdentifyViaUsername という名前の拡張スクリプトの Script フィールドに含めます。

    var PwdIdentifyViaUsername = Class.create ();
    PwdIdentifyViaUsername. prototype = {
        category: 'password_reset.extension.identification_form_processor', // DO NOT REMOVE THIS LINE!
     
        initialize: function () { },
     
       /**********
       * Process the identification form request, and returns the user's sys_id. If user was not identified return null.
       *
       * @param params.processId   The sys_id of the calling パスワードリセット process (table: pwd_process)
       * @param request            The form request object. fields in the form can be accessed with
       *  request.getParameter('<element-id>')
       *  Supported request parameters: sysparm_user_id - the user identifier value entered in the form                       
       * @return The sys_id of the user that corresponds to the requested input; 
       *  if no user was found, null should be returned
       **********/
       processForm: function (params, request) {var processId = params. processId; var sysparm_user_id = request.getParameter ('sysparm_user_id');
          now_GR = new GlideRecord ('sys_user') ;
          now_GR.addQuery ('user_name', sysparm_user_id) ;
          now_GR.query (); if (!now_GR.next ()) {return null; } return now_GR.sys_id;},
     
        type:'PwdIdentifyViaUsername' }

    「パスワード生成」スクリプトインクルード カテゴリ

    スクリプトインクルード カテゴリ 説明 メソッド シグネチャ 入力フィールド 出力フィールド
    パスワード生成 自動生成されたパスワードを返します。 process(params) パラメーター:params.processId:呼び出し側のパスワードリセット プロセスの sys_id (テーブル:pwd_process)。 戻り値:(文字列) 自動生成されたパスワードを返します。

    この例は、ベースの単語と数字からランダムにパスワードを生成します。ベースの単語は認証情報ストアに応じて選択されます。コードは、SamplePasswordGenerator という名前の拡張スクリプトの Script フィールドに含めます。

    var SamplePasswordGenerator = Class.create ();
    SamplePasswordGenerator. prototype = {
      category: 'password_reset.extension.password_generator', // DO NOT REMOVE THIS LINE!
     
       /**********
       * Returns an auto-generated string password.
       * This sample randomly generates 4 digits to add to the password.
       * 
       * @param params.credentialStoreId The sys_id of the target パスワードリセット credential store to generate 
       *  a password for (table: pwd_cred_store)
       * @return An auto-generated string password
       **********/
      process:function (params) { var basePassword ;
     
    	 var now_GR = new GlideRecord ('pwd_cred_store');
    	now_GR.addQuery ('name', 'Local ServiceNow Instance');
    	now_GR.query (); if (now_GR.next ()) { if (params.credentialStoreId = now_GR.getValue ('sys_id'))
    			basePassword = "Password"; else
    			basePassword = "Dorwssap"; } return this. generateSimple (basePassword); },
     
      generateSimple:function (base) {var pwd = base; var numbers = '0123456789'; var length = 4;
     
         for (var i  = 0 , n  = numbers. length ; i  < length ; i ++) {
            pwd += numbers.charAt (Math.floor (Math.random () * n) + 1); } return pwd;}, 
     
      type:'SamplePasswordGenerator'} ;

    「ポスト リセット」スクリプトインクルード カテゴリ

    スクリプトインクルード カテゴリ 説明 メソッド シグネチャ 入力フィールド 出力フィールド
    ポスト リセット パスワードリセット プロセスの完了後に追加の操作を実行します。 process(params) パラメーター:
    • params.resetRequestId:呼び出し側のパスワードリセット プロセスの sys_id (テーブル:pwd_process)。
    • params.wfSuccess:ワークフローが正常に完了したかどうかを示すフラグを返します。正常に完了した場合のみ true を返します。
    リターン:void

    この例は、失敗したリセット要求をシステム ログに追加します。コードは、PwdPostProcessor という名前の拡張スクリプトの Script フィールドに含めます。

    var PwdPostProcessor = Class.create ();
     
    PwdPostProcessor. prototype = {
        category: 'password_reset.extension.post_reset_script', // DO NOT REMOVE THIS LINE!
     
        initialize:function () { },
     
         /**********
         * Execute custom actions after the パスワードリセット process has completed.
        * 
        * @param params.resetRequestId The sys_id of the current パスワードリセット request (table: pwd_reset_request)
        * @param params.wfSuccess      A flag indicating if the workflow completed sucessfully. 
        *  True if (and only if) successful.
        * @return no return value
        **********/
        process: function (params) {if (!params. wfSuccess) {
               now_GS.log ('[PwdPostProcessor.process] failure post processing for request [' + params. resetRequestId + ']'); }
     
             // We could place actions here that we always want executed return; },
     
        type:'PwdPostProcessor' }

    「ユーザーアカウントのルックアップ」スクリプトインクルード カテゴリ

    スクリプトインクルード カテゴリ 説明 メソッド シグネチャ 入力フィールド 出力フィールド
    ユーザーアカウントのルックアップ 指定したユーザーの認証情報ストアのアカウント ID を取得します。 process(params) パラメーター:params.userId:チェックするユーザーの sys_id (テーブル:sys_user)。 戻り値:(文字列) 指定したユーザーの認証情報ストアのアカウント ID を返します。

    この例は、ユーザーの認証情報ストアのアカウントを取得します。コードは、SampleUserAccountLookupExtension という名前の拡張スクリプトの Script フィールドに含めます。

    var SampleUserAccountLookupExtension = Class.create ();
    SampleUserAccountLookupExtension. prototype = {
      category:'password_reset.extension.user_account_lookup', // DO NOT REMOVE THIS LINE!
     
       /**********
      * Returns the credential store account id for a given user.
      * This sample echoes the user_id supplied as the credential store account id for that user.
      * 
      * @param params.userId  The sys_id of the user being checked (table: sys_user)
      * @return               The credential store account id (string) for a given user
      **********/
      process:function (params) {return params.userId;},
     
      type:'SampleUserAccountLookupExtension' }

    「検証フォーム プロセッサー」スクリプトインクルード カテゴリ

    スクリプトインクルード カテゴリ 説明 メソッド シグネチャ 入力フィールド 出力フィールド
    検証フォーム プロセッサー 検証フォーム要求を処理し、ユーザーが検証されたかどうかを示します。 processForm(params, request) パラメーター:
    • params.resetRequestId:現在のパスワードリセット要求の sys_id (テーブル:pwd_reset_request)。
    • params.userId:検証するユーザーの sys_id (テーブル:sys_user)。
    • params.verificationId:処理する検証の sys_id (テーブル:pwd_verification)。
    • request:フォーム要求オブジェクト。request.getParameter('<element-id>') を使用してフォーム内のフィールドにアクセスできます。
    戻り値:(ブール値) ユーザーが検証された場合は true を返します。それ以外の場合は false を返します。

    この例は、ユーザーが入力フィールドで [OK] を送信した場合にのみ true を返し、それ以外の場合は false を返す検証プロセッサーを示しています。コードは、SampleVerificationFormProcessor という名前の拡張スクリプトの Script フィールドに含めます。

    var SampleVerificationFormProcessor = Class.create ();
    SampleVerificationFormProcessor.prototype = {
      category:'password_reset.extension.verification_form_processor', // DO NOT REMOVE THIS LINE!
     
       /**********
       * Process the verification form request, and return whether the user was successfully verified.
       * This is a sample verification processor returns true only if the user sent "ok" in the input field; 
       * otherwise, it returns false.
       * 
       * @param params.resetRequestId The sys_id of the current パスワードリセット request (table: pwd_reset_request)
       * @param params.userId         The sys_id of the user trying to be verified (table: sys_user)
       * @param params.verificationId The sys_id of the verification to be processed (table: pwd_verification)
       * @param request               The form request object. Fields in the form can be accessed with
       * request.getParameter('<element-id>')
       * @return Boolean indicating whether the user is successfully verified
       **********/
      processForm:function (params, request) {if (request.getParameter ("sysparm_simple_input") == "ok") return true; else return false; },
     
      type:'SampleVerificationFormProcessor'};