メインコンテンツまでスキップ
バージョン: 20 R10 BETA

Session

Session オブジェクトは Session コマンドによって返されます。 このオブジェクトは、カレントユーザーセッションを管理するためのインターフェースをデベロッパーに対して提供し、コンテキストデータの保存、プロセス間の情報共有、セッションに関連したプリエンプティブプロセスの開始などのアクションの実行や、アクセス権 の管理を可能にします。

セッションの種類

このクラスは以下の種類のセッションをサポートしています:

Session オブジェクトにおいて利用可能なプロパティと関数は、セッションの種類に依存します。

概要

.clearPrivileges() : Boolean
removes all the privileges associated to the session (excluding promoted privileges) and returns True if the execution was successful
.createOTP ( { lifespan : Integer } ) : Text
セッションの新しいOTP(One Time Passcode、ワンタイムパスワード)を作成し、そのトークンUUID を返します。
.demote( promoteId : Integer )
removes the promoted privilege whose id you passed in promoteId from the web process, if it was previously added by the .promote() function
.expirationDate : Text
セッションcookie の有効期限
.getPrivileges() : Collection
対象セッションに紐づいている全アクセス権の名称のコレクションを返します
.hasPrivilege( privilege : Text ) : Boolean
対象セッションに privilege のアクセス権が紐づいていれば true、でなければ false を返します
.id : Text
ユーザーセッションの固有のID を格納しています
.idleTimeout : Integer
対象セッションが 4D によって終了されるまでの、非アクティブタイムアウト時間 (分単位)
.info : Object
サーバー上のリモートクライアントまたはストアドプロシージャーセッション、あるいはスタンドアロンセッションの情報を格納します
.isGuest() : Boolean
アクセス権のないゲストセッションの場合は true を返します
.promote( privilege : Text ) : Integer
adds the privilege defined in the privilege parameter to the current process during the execution of the calling function and returns the id of the promoted privilege
.restore ( token : Text ) : Boolean
カレントのWeb ユーザーセッションをtoken 引数のUUIDに対応したオリジナルのセッションで置き換えます
.setPrivileges( privilege : Text ) : Boolean
.setPrivileges( privileges : Collection )
.setPrivileges( settings : Object ) : Boolean

引数として渡したアクセス権やロールをセッションと紐づけ、実行が成功した場合に true を返します
.storage : Object
セッションのすべてのプロセスで利用可能な情報を保存しておける共有オブジェクトを格納します
.userName : Text
セッションと紐づいたユーザー名

To learn more

Scalable sessions for advanced web applications (blog post)
Permissions: Inspect Session Privileges for Easy Debugging (blog post)

.clearPrivileges()

履歴
リリース内容
18 R6追加

.clearPrivileges() : Boolean

引数説明
戻り値Boolean<-実行が正常に終わった場合には true

説明

この関数は、リモートクライアント、ストアドプロシージャー、およびスタンドアロンのセッションでは何もせず、常に true を返します。

The .clearPrivileges() function removes all the privileges associated to the session (excluding promoted privileges) and returns True if the execution was successful.

"強制ログイン" モード でない限り、セッションは自動的にゲストセッションとなります。 "強制ログイン" モードでは、.clearPrivileges() はセッションをゲストセッションへと変換するのではなく、セッションの権限を消去するだけです。

This function does not remove promoted privileges from the web process, whether they are added through the roles.json file or the promote() function.

例題

// Webユーザーセッションを無効にします
var $isGuest : Boolean
var $isOK : Boolean

$isOK:=Session.clearPrivileges()
$isGuest:=Session.isGuest() // $isGuest は true

.createOTP()

履歴
リリース内容
20 R9追加

.createOTP ( { lifespan : Integer } ) : Text

引数説明
lifespanInteger->セッショントークンの有効期限(秒)
戻り値Text<-セッションのUUID

説明

この関数は、Webユーザーセッションの場合にのみ使用できます。 他のコンテキストにおいては空の文字列を返します。

.createOTP() 関数は、セッションの新しいOTP(One Time Passcode、ワンタイムパスワード)を作成し、そのトークンUUID を返します。 このトークンはそれが生成されたセッションに固有のものです。

OTP トークンについてのより詳細な情報については、こちらの章を参照して下さい。

デフォルトで、lifespan 引数が省略された場合、トークンはセッションの.idleTimeOut と同じ有効期限を持って作成されます。 lifespan 引数に秒単位の値を渡すことで、カスタムのタイムアウトを設定することができます(最小値は10秒間で、それより小さい値が渡された場合にはlifespan は10にリセットされます)。 Web ユーザーセッションを復元するために失効したトークンを使用した場合、それは無視されます。

返されたトークンは、サードパーティアプリケーションや他のWebサイトとのやり取りで使用することでセッションを安全に特定することができます。 例えば、セッションOTP トークンは支払いアプリケーションなどにおいて使用することができます。

例題

var $token : Text
$token := Session.createOTP( 60 ) // トークンは1分間有効

.demote()

履歴
リリース内容
20 R10追加

.demote( promoteId : Integer )

引数説明
promoteIdInteger->Id returned by the promote() function

説明

This function does nothing in remote client, stored procedure, and standalone sessions.

The .demote() function removes the promoted privilege whose id you passed in promoteId from the web process, if it was previously added by the .promote() function.

If no privilege with promoteId was promoted using .promote() in the web process, the function does nothing.

If several privileges have been added to the web process, the demote() function must be called for each one with the appropriate promoteId. Privileges are stacked in the order they have been added to the process, it is recommended to unstack privileges in a LIFO (Last In, First Out) order.

例題

exposed Function search($search : Text) : Collection

var $employees : Collection
var $promoteId1; $promoteId2 : Integer

$promoteId1:=Session.promote("admin")
$promoteId2:=Session.promote("superAdmin")

$search:="@"+$search+"@"

$employees:=This.query("type = :1 and lastname = :2"; "Intern"; $search).toCollection()

Session.demote($promoteId2)
Session.demote($promoteId1)

return $employees

参照

.promote()

.expirationDate

履歴
リリース内容
18 R6追加

.expirationDate : Text

説明

このプロパティは、Webユーザーセッションの場合にのみ使用できます。

.expirationDate プロパティは、セッションcookie の有効期限を返します。 値は ISO 8601標準に従って文字列で表現されます: YYYY-MM-DDTHH:MM:SS.mmmZ。 値は ISO 8601標準に従って文字列で表現されます: YYYY-MM-DDTHH:MM:SS.mmmZ

このプロパティは 読み取り専用 です。 このプロパティは 読み取り専用 です。 .idleTimeout プロパティ値が変更された場合、有効期限は自動的に再計算されます。

例題

var $expiration : Text
$expiration:=Session.expirationDate // 例: "2021-11-05T17:10:42Z"

.getPrivileges()

履歴
リリース内容
20 R6追加

.getPrivileges() : Collection

引数説明
戻り値Collection<-アクセス権の名称 (文字列) のコレクション

説明

.getPrivileges() 関数は、対象セッションに紐づいている全アクセス権の名称のコレクションを返します。

This function returns privileges assigned to a Session using the setPrivileges() function only. Promoted privileges are NOT returned by the function, whether they are added through the roles.json file or the promote() function.

リモートクライアント、ストアドプロシージャーおよびスタンドアロンセッションでは、この関数は "WebAdmin" のみを含むコレクションを返します。

例題

以下の roles.json が定義されています:

{
"privileges":[
{
"privilege":"simple",
"includes":[

]
},
{
"privilege":"medium",
"includes":[
"simple"
]
}
],
"roles":[
{
"role":"Medium",
"privileges":[
"medium"
]
}
],
"permissions":{
"allowed":[

]
}
}

セッションのロールは、DaraStore クラスの authentify() 関数内で割り当てられます:

  // DataStore クラス

exposed Function authentify($role : Text) : Text
Session.clearPrivileges()
Session.setPrivileges({roles: $role})

"medium" ロールを指定して authentify() 関数が呼び出された場合:

var $privileges : Collection
$privileges := Session.getPrivileges()
// $privileges: ["simple","medium"]

参照

.setPrivileges()
ブログ記事: セッション権限を検査してデバッグを容易に

.hasPrivilege()

履歴
リリース内容
21Returns True for promoted privileges
18 R6追加

.hasPrivilege( privilege : Text ) : Boolean

引数説明
privilegeText->確認するアクセス権の名称
戻り値Boolean<-セッションが privilege のアクセス権を持っていれば true、それ以外は false

説明

.hasPrivilege() 関数は、対象セッションに privilege のアクセス権が紐づいていれば true、でなければ false を返します。

This function returns True for the privilege if called from a function that was promoted for this privilege (either through the roles.json file or the promote() function).

リモートクライアント、ストアドプロシージャーおよびスタンドアロンセッションでは、この関数は privilege に関係なく、常に True を返します。

例題

"WebAdmin" アクセス権が Webユーザーセッションに紐づいているかを確認します:

If (Session.hasPrivilege("WebAdmin"))
// アクセス権が付与されているので、何もしません
Else
// 認証ページを表示します
End if

.id

履歴
リリース内容
20 R5追加

.id : Text

説明

.id プロパティは、ユーザーセッションの固有のID を格納しています。 4D Server ではこの一意の文字列は、サーバーによって各セッションに対して自動的に割り当てられ、そのプロセスを識別することを可能にします。

tip

Session storage コマンドにこのプロパティを渡すことで、セッションの.storage オブジェクトを取得できます。

.idleTimeout

履歴
リリース内容

|18 R6|追加|

.idleTimeout : Integer

説明

このプロパティは、Webユーザーセッションの場合にのみ使用できます。

.idleTimeout プロパティは、対象セッションが 4D によって終了されるまでの、非アクティブタイムアウト時間 (分単位)を格納します。

プロパティ未設定時のデフォルト値は 60 (1時間) です。

このプロパティが設定されると、それに応じて .expirationDate プロパティも更新されます。

60 (分) 未満の値を指定することはできません (60 未満の値を設定した場合、タイムアウトは 60 (分) に設定されます)。

このプロパティは 読み書き可能 です。

例題

If (Session.isGuest())
// ゲストセッションは、60分の非アクティブ時間経過後に終了します
Session.idleTimeout:=60
Else
// その他のセッションは、120分の非アクティブ時間経過後に終了します
Session.idleTimeout:=120
End if

.info

履歴
リリース内容
20 R5追加

.info : Object

説明

このプロパティは、リモートクライアント、ストアドプロシージャーおよびスタンドアロンセッションの場合にのみ使用できます。

.info プロパティは、サーバー上のリモートクライアントまたはストアドプロシージャーセッション、あるいはスタンドアロンセッションの情報を格納します。

  • .info オブジェクトは、リモートクライアントおよびストアドプロシージャーセッションに対して Process activity コマンドの"session" プロパティによって返されるオブジェクトと同じです。
  • .info オブジェクトは、スタンドアロンセッションに対してはSession info コマンドによって返されるオブジェクトと同じです。

.info オブジェクトには、次のプロパティが格納されています:

プロパティ説明
typeTextセッションのタイプ: "remote"、"storedProcedure"、"standalone"
userNameText4Dユーザー名 (.userName と同じ値)
machineNameTextリモートセッション: リモートマシンの名前。 ストアドプロシージャセッション: サーバーマシンの名前。 スタンドアロンセッションの場合: マシン名
systemUserNameTextリモートセッション: リモートマシン上で開かれたシステムセッションの名前。
IPAddressTextリモートマシンの IPアドレス。
hostTypeTextホストタイプ: "windows" または "mac"
creationDateTime日付 (ISO 8601)セッション作成の日付と時間。 スタンドアロンセッションの場合: アプリケーション起動の日付と時間
stateTextセッションの状態: "active", "postponed", "sleeping"
IDTextセッションUUID (.id と同じ値))
persistentIDTextリモートセッション: セッションの永続的な ID

.info は計算プロパティなため、そのプロパティに対して何らかの処理をおこないたい場合は、呼び出し後にローカル変数に保存することが推奨されます。

.isGuest()

履歴
リリース内容
18 R6追加

.isGuest() : Boolean

引数説明
戻り値Boolean<-ゲストセッションの場合は true、それ以外は false

説明

この関数は、リモートクライアント、ストアドプロシージャ、およびスタンドアロンセッションでは常にFalse を返します。

.isGuest() 関数は、アクセス権のないゲストセッションの場合は true を返します。

例題

On Web Connection データベースメソッドにて:

If (Session.isGuest())
// ゲストユーザー用の処理
End if

.promote()

履歴
リリース内容
20 R10追加

.promote( privilege : Text ) : Integer

引数説明
privilegeText->アクセス権の名称
戻り値Integer<-id to use when calling the demote() function

説明

This function does nothing in remote client, stored procedure, and standalone sessions.

The .promote() function adds the privilege defined in the privilege parameter to the current process during the execution of the calling function and returns the id of the promoted privilege.

Dynamically adding privileges is useful when access rights depend on the execution context, which cannot be fully defined in the "roles.json" file. This is particularly relevant when the same function can be executed by users with different access levels. The use of .promote() ensures that only the current process is granted the necessary privileges, without affecting others.

The function does nothing and returns 0 if:

  • the privilege does not exist in the roles.json file,
  • the privilege is already assigned to the current process (using .promote() or through a static promote action declared for the calling function in the roles.json file).

You can call the promote() function several times in the same process to add different privileges.

The returned id is incremented each time a privilege is dynamically added to the process.

To remove a privilege dynamically, call the demote() function with the appropriate id.

例題

Several users connect to a single endpoint that serves different applications. A user from application #1 does not need the "super_admin" privilege because they don't create "VerySensitiveInfo". A user from application #2 needs "super_admin" privilege.

You can dynamically provide appropriate privileges in the CreateInfo function:

exposed Function createInfo($info1 : Text; $info2 : Text)

var $sensitive : cs.SensitiveInfoEntity
var $verySensitiveInfo : cs.VerySensitiveInfoEntity
var $status : Object
var $promoteId : Integer

$sensitive:=ds.SensitiveInfo.new()
$sensitive.info:=$info1
$status:=$sensitive.save()

If (Session.storage.role.name="userApp2")
$promoteId:=Session.promote("super_admin")
$verySensitiveInfo:=ds.VerySensitiveInfo.new()
$verySensitiveInfo.info:=$info2
$status:=$verySensitiveInfo.save()
Session.demote($promoteId)
End if

参照

.demote()
hasPrivilege()

.restore()

履歴
リリース内容
20 R9追加

.restore ( token : Text ) : Boolean

引数説明
tokenText->セッショントークンUUID
戻り値Boolean<-カレントのセッションがトークンのセッションで正常に置き換えられた場合にはTrue

説明

この関数は、Webユーザーセッションの場合にのみ使用できます。 それ以外のコンテキストではFalse を返します。

.restore() 関数は、カレントのWeb ユーザーセッションをtoken 引数のUUIDに対応したオリジナルのセッションで置き換えます。 セッションのストレージと権限は復元されます。

オリジナルのセッションが正常に復元された場合、この関数はtrue を返します。

今関数は以下の場合にはfalse を返します:

  • セッショントークンが既に使用されていた場合
  • セッショントークンが失効してしまっている場合
  • セッショントークンが存在しない場合
  • オリジナルのセッション自身が失効してしまっている場合

これらの場合には、カレントのWeb ユーザーセッションはそのまま残されます(セッションは復元されません)。

例題

カスタムのHTTP リクエストハンドラーから呼ばれたシングルトンの例:

shared singleton Class constructor()

Function callback($request : 4D.IncomingMessage) : 4D.OutgoingMessage
Session.restore($request.urlQuery.state)

参照

.createOTP()

.setPrivileges()

履歴
リリース内容
19 R8roles プロパティをサポート
18 R6追加

.setPrivileges( privilege : Text ) : Boolean
.setPrivileges( privileges : Collection )
.setPrivileges( settings : Object ) : Boolean

引数説明
privilegeText->アクセス権の名称
privilegesCollection->アクセス権の名称のコレクション
settingsObject->"privileges" プロパティ (文字列またはコレクション) を持つオブジェクト
戻り値Boolean<-実行が正常に終わった場合には true

説明

この関数は、リモートクライアント、ストアドプロシージャー、およびスタンドアロンのセッションでは何もせず、常に false を返します。

.setPrivileges() 関数は、引数として渡したアクセス権やロールをセッションと紐づけ、実行が成功した場合に true を返します。

  • privilege には、アクセス権の名称を文字列として渡します (複数の場合はカンマ区切り)。

  • privileges には、アクセス権の名称を文字列のコレクションとして渡します。

  • settings には、以下のプロパティを持つオブジェクトを渡します:

プロパティ説明
privilegesText または Collection
  • アクセス権名の文字列
  • アクセス権名のコレクション
  • rolesText または Collection
  • ロールの文字列
  • ロールの文字列のコレクション
  • userNameText(任意) セッションと紐づけるユーザー名

    権限とロールは、プロジェクトの roles.json ファイルで定義されます。 詳細については、権限 を参照してください。

    privileges または roles プロパティに、roles.json ファイルで宣言されていない名前が含まれている場合、その名前は無視されます。

    セッションにアクセス権またはロールが紐づいていない場合、そのセッションはデフォルトで ゲストセッション です。

    userName プロパティは Session オブジェクトレベルで利用可能です (読み取り専用)。

    例題

    カスタムな認証メソッドにおいて、ユーザーに "WebAdmin" アクセス権を付与します:

    var $userOK : Boolean

    ... // ユーザー認証

    If ($userOK) // ユーザー認証に成功した場合
    var $info : Object
    $info:=New object()
    $info.privileges:=New collection("WebAdmin")
    Session.setPrivileges($info)
    End if

    参照

    .getPrivileges()

    .storage

    履歴
    リリース内容
    20 R5リモートクライアントとストアドプロシージャーセッションをサポート
    18 R6追加

    .storage : Object

    説明

    .storage プロパティは、セッションのすべてのプロセスで利用可能な情報を保存しておける共有オブジェクトを格納します。

    Session オブジェクトの作成時には、.storage プロパティは空です。 共有オブジェクトのため、このプロパティはサーバー上の Storage オブジェクトにおいて利用可能です。

    サーバーの Storage オブジェクトと同様に、.storage プロパティは常に "single" で存在します。 共有オブジェクトや共有コレクションを .storage に追加しても、共有グループは作成されません。

    このプロパティは 読み取り専用 ですが、戻り値のオブジェクトは読み書き可能です。

    tip

    セッションの.storage プロパティはSession storage コマンドを使用することで取得できます。

    Webセッションの例題

    クライアントの IP を .storage プロパティに保存します。 On Web Authentication データベースメソッドに以下のように書けます: On Web Authentication データベースメソッドに以下のように書けます:

    If (Session.storage.clientIP=Null) // 最初のアクセス
    Use (Session.storage)
    Session.storage.clientIP:=New shared object("value"; $clientIP)
    End use
    End if

    リモートセッションの例題

    同じセッションのプロセス間でデータを共有したい場合:

    Use (Session.storage)
    Session.storage.settings:=New shared object("property"; $value; "property2"; $value2)
    End use

    .userName

    履歴
    リリース内容
    20 R5リモートクライアントとストアドプロシージャーセッションをサポート
    18 R6追加

    .userName : Text

    説明

    .userName プロパティは、セッションと紐づいたユーザー名を格納します。 このプロパティは、コード内でユーザーを確認するのに使用できます。

    • Webセッションでは、このプロパティはデフォルトで空の文字列です。 これは、setPrivileges() 関数の privileges プロパティを使って設定することができます。
    • リモートおよびストアドプロシージャーセッションでは、このプロパティは Current user コマンドと同じユーザー名を返します。
    • スタンドアロンセッションでは、このプロパティは"designer" またはSET USER ALIAS コマンドで設定された名前が格納されています。

    このプロパティは 読み取り専用 です。