ロールと権限
ORDA セキュリティアーキテクチャーはWeb およびREST セッションに対してプロジェクトリソースへのアクセスを制御します。これを使用するとデータストア、データクラス、属性、関数といったリソースに対する特定の権限を付与することができます。
概要
セキュリティモデルはprivileges、actions (read や create などのアクション)、およびresources などを組み合わせて構成されています。
Session.setPrivileges() を使用してsession に権限を割り当てます。
各リクエストはプロジェクトのroles.json ファイル内で定義されているパーミッションに対してチェックされます。未認証のアクションは権限エラーを起こします。読み出し(read)権限がない属性はレスポンスから省略されます。

パーミッションは、データストアオブジェクトや関数へのアクセスを制御します。特定の条件に基づいて読み取りデータをフィルタリングしたい場合は、制限付エンティティセレクション の利用がより適切かもしれません。
スコープ
Permissions apply to web and REST sessions: REST requests, including Qodly pages, Open datastore sessions, MCP sessions and 4D code executed in a web process. これには公開された関数、HTTP リクエストハンドラ、On Web Connection データベースメソッド、 およびそれらが呼び出すあらゆるコードが含まれます。例えば、関数がデータクラスを読み出すがセッションに読み出しのパーミッション(許可)がない場合には、許可された関数の呼び出しも失敗します。
共有シングルトン あるいは シングルトンクラス のexecute パーミッションは、関数がREST 経由で呼び出された ときにのみチェックされます。 Web セッション内で実行された4D コードがそれ(cs.MySingleton.me.myFunction()) を直接呼び出した場合、パーミッションは何もチェックされません。これはどちらの場合にもチェックされるデータクラス、エンティティ、エンティティセレクションおよびデータストア関数などとは異なります。
パーミッションは、デスクトップセッションコード(スタンドアロン、クライアント/サーバー、ストアドプロシージャー) あるいは tool4d に対して適用されることは決してありません。
パーミッションエントリが宣言されていない権限 を参照する場合、4D はエラーをログに記録し、そのエントリを無視します。この場合、パーミッションは無視されます。デフォルトの制限モード が有効化されていない場合、リソースはguest セッションからアクセス可能となります。
リソース
4D は、リソースがアクセスされるときにセッションのパーミッションをチェックします。以下のリソースは特定のパーミッションを持つことができます:
| リソース | applyTo | type |
|---|---|---|
| データストア | ds | datastore |
| データストアクラス関数 | ds.<function> | method |
| データクラス | <Dataclass> | dataclass |
| 属性 (計算属性 およびエイリアス属性 を含む) | <Dataclass>.<attribute> | attribute |
| データクラス、エンティティまたはエンティティセレクション関数 | <Dataclass>.<function> | method |
| 共有シングルトン (ただし公開された関数を除く) | <Singleton> | singleton |
| シングルトン関数 | <Singleton>.<function> | singletonMethod |
パーミッションをroles.json 内で定義する際には、上記の表のapplyTo および type の値を使用してください。
roles.json 内では、以下のシンタックスを使用してapplyTo 内でORDA クラス関数を識別します:
<DataclassName>.<functionName>
例えば、以下の関数にパーミッションを適用したい場合を考えます:
// cs.CityEntity class
Class extends Entity
Function getPopulation() : Integer
...
この場合、以下の値を使用します:
"applyTo":"City.getPopulation"
パーミッションはデータクラスと関数名で関数を識別するため、異なるORDA クラス(エンティティ、エンティティセレクション、データクラス)で同じ名前をもつ関数を異なる権限に割り当てることはできません。その場合にはそれぞれ個別の名前をつけてください。例えば、cs.CityEntity および cs.CitySelection 内のdropEntity() および dropSelection() 関数は、それぞれ以下のように名前をつけます:
"permissions": {
"allowed": [
{
"applyTo": "City.dropEntity",
"type": "method",
"execute": [
"cityAdmin"
],
"promote": [
"name"
]
},
{
"applyTo": "City.dropSelection",
"type": "method",
"promote": [
"name"
]
}
]
}
}
パーミッション
パーミッション は、リソースに対するアクションを許可します。例えばds.myTable.myFunction() を実行するなどです。パーミッションはroles.json 内で定義され、一つまたは複数の権限 に対して付与されます。
リソースに対してパーミッションが何も定義されていなければ、そこへのアクセスはプロジェクトの制限モード に依存します。
許諾アクション
利用可能なアクションは、リソースに依存します:
| 動作 | データストア | データクラス | 属性 | Function |
|---|---|---|---|---|
| create | 任意のデータクラス内にてエンティティを作成する | 当該データクラス内にてエンティティを作成する | 当該属性にデフォルト以外の値でエンティティを作成する(エイリアス属性の場合は無視されます) | n/a |
| read | 任意のデータクラスにおいて属性を読み取り | 当該データクラスにおいて属性を読み取り | 当該属性を読み取り | n/a |
| update | 任意のデータクラスにおいて属性を更新 | 当該データクラスにおいて属性を更新 | 当該属性を更新(エイリアス属性の場合は無視されます) | n/a |
| drop (Qodly Studio では"Delete") | 任意のデータクラスにおいてデータを削除 | 当該データクラスにおいてデータを削除 | 当該属性の null でない値を削除 (エイリアス属性と計算属性を除く) | n/a |
| execute | プロジェクトの任意の関数を実行 (データストア、データクラス、エンティティセレクション、エンティティ、またはシングルトン) | デークラス上で任意の関数を実行。データクラス、エンティティ、およびエンティティセレクション関数はデータクラス関数として扱われます | n/a | 当該関数を実行 |
| promote | n/a | n/a | n/a | 関数の呼び出しの間、権限を一時的に追加します。セッション全体ではなく、関数を実行しているプロセスのみがその権限を受け取ります。 Web プロセス内においてこのアクションを動的に設定あるいは削除したい場合にはpromote() および demote() を使用します。 |
パーミッションルール
- update および drop はリソースに対してread を持っている必要があります。create を持っているは必要ありません。
- 関数は複数の権限でpromoted (昇格)させることができます。実行の間、関数はこれらすべての権限を組み合わせたパーミッションを受け取ります。優先順位はありません。
- エイリアス属性 は、セッションがそのエイリアスを読み出すパーミッションを持っていれば(たとえそのエイリアスが解決する先の属性を読むことができなくても)読み出すことができます。
- 計算属性 は、セッションがその計算属性の下地となる属性に対するアクセス件を持っていなくても、その計算属性へのアクセス権があれば、それを読み出すことができます。
- REST 強制ログインモード においては、パーミッション設定に関係なく、ゲストユーザーは常に
authentify()を実行することができます。
継承されたパーミッション
パーミッションは、以下のルールに従って継承されます:
- パーミッションは データストア → データクラス → 属性 / 関数、および データストア → シングルトン → シングルトン関数 の順番で継承されます。
- 解決はアクションごと に行われます。データクラスに対して
readを定義しても、create、update、dropあるいはexecuteがデータクラスに対してどのように解決されるかは変わりません。 - データクラスはデータストアを置き換えます。アクションに対してデータクラスに定義されたパーミッションは、そのアクションに対するデータストアパーミッションを置き換えます。データストア上でのみ権限が付与されたアクションは、データクラス上ではその権限を失います。
- 関数はデータクラスを置き換えます。関数に対して定義された
executeパーミッションは、その関数に対するデータクラス(およびデータストア) のexecuteパーミッションを置き換えます。エンティティとエンティティセレクションの関数はデータクラス関数と同じルールに従います。 - シングルトンはデータストアを置き換え、シングルトン関数はシングルトンを置き換えます。
- データストア関数はデータストアを置き換えます。
- A function inherits what it does not set. At every level, an action that a function permission does not list is inherited from its dataclass or singleton, then from the datastore. For example, a singleton function permission that only defines
promotekeeps theexecutepermission of its singleton. Apromotepermission never grantsexecute: to let a privilege call a promoted function, list it in the function'sexecutepermission. - 属性はその親に対して追加されます。独自のパーミッションを持つ属性に対してアクションを行うには、セッションは属性パーミッションから付与された権限 に加えて データクラス上でのアクションを許可する権限も(直接、あるいはデータクラスを通して)必要になります。
- 権限が与えられていないアクション (例えば、
"read": []など) は"未設定"とみなされ、一つ上のレベルのパーミッションが適用されます。これは"誰も実行できない"ということを意味しません。アクションを拒否するためには、パーミッションを定義しないで制限モード を使用するか、どのセッションにも付与されない権限に付与します。
パーミッションはアクセスを許可するだけです。明示的な拒否を示すものではありません。そのためデータクラスまたは関数のパーミッションを使うと、より広いデータストアパーミッションを上書きすることでそれを狭めることができます。属性パーミッションは加算的です。セッションがその属性のデータクラスへのアクションを持っている場合にのみ属性へのアクセスが認可されます。
Session.setPrivileges({roles: [...]})を使用してroles を通してセッションにアクセス件を割り当てるようにします。ビジネスプロフィールに基づいてロールを定義してください。権限は言わば再利用可能な構成要素として活用します。 Qodly Studio では、ロールごとに有効なパーミッションを表示します。- 管理者のロールに完全なアクセスが必要な場合、ロック権限(使用していれば)を除く全ての権限を含めてください。そのロールに新しい権限を加えていってください。データクラスレベルのパーミッションはより広いデータストアのパーミッションを置き換えるからです。
- 全てのロールに対して権限の変更がおよび影響を確認してください。 Qodly Studio では、詳細なパーミッションレビュー を見ればロールの有効なアクセス権が表示されます。権限のないロールを使用することでゲスト用のアクセス権を確認することができます。
権限とロール
権限 はリソースへのアクション許可します。ロール は権限をビジネスプロフィールとしてまとめたもので、管理者はこれをセッションへと割り当てることができます。
例えば、manageInvoices (請求管理)は権限の一例で、secretary はそれとその他の権限を持ったロールの一例です。ロールはSession.setPrivileges({roles: [...]})を使用してセッションへと割り当てます。ロールは持っている全ての権限を付与します。
権限は、複数のアクションとリソースの組み合わせに対して付与することができ、単一のアクションに複数の権限を付与することもできます。
権限とロールをroles.json で定義したあとに、Session.setPrivileges() を使用してセッションにロールを付与します。
権限を含める
権限は、roles.json 内でincludes プロパティを使用することで他の権限を内包することができます。権限を含めた場合それは一方向に伝播します。つまりA がB を内包し、B がC を内包する場合、A はB とC 両方の権限を受け取ります。
Session.getPrivileges() は権限の展開されたコレクション(例えばA、B、およびC) を順不同で返します。権限を内包する場合、内包される権限を、内包する権限よりも先に宣言して下さい。そうでない場合、4D はその内包を無視し、エラーをログに記録します。
例題
認証用の関数はユーザーのロールをセッションに割り当てます:
exposed Function authenticate($identifier : Text; $password : Text)->$result : Text
var $user : cs.UsersEntity
Session.clearPrivileges()
$result:="You are authenticated as Guest"
$user:=ds.Users.query("identifier = :1"; $identifier).first()
If ($user#Null)
If (Verify password hash($password; $user.password))
Session.setPrivileges(New object("roles"; $user.role))
$result:="Your are authenticated as "+$user.role
End if
End if
guest 権限
guest はビルトインされた権限です。Guest という名前の権限を宣言した場合、エラーとなります("権限がすでに定義されています")。権限がないセッション(Session.isGuest() = True の場合)は、guest に付与された権限を受け取ります。
guest に付与された権限は、全てのセッションで利用可能です(そのセッションに割り当てられた他の権限に関わらず)。他のセッションからは拒否したいアクセスにはguest 権限を付与しないでください。またデータクラスレベルでアクセスを制限したい場合、このアカウントの権限を考慮するようにして下さい。
roles.json ファイル
プロジェクトのWeb セキュリティ設定はroles.json 内で定義されています:
| プロパティ | 型 | 必須 | 説明 |
|---|---|---|---|
privileges | 権限オブジェクトのコレクション | ◯ | 定義された権限。それぞれのオブジェクトはprivilege 名を持ち、また権限名のコレクションであるincludes を持ちます。 |
roles | ロールオブジェクトのコレクション | × | 定義されたロール。それぞれのオブジェクトはrole 名を持ち、権限名のprivileges コレクションであるを持ちます。 |
permissions.allowed | Collection of permission objects | ◯ | Permissions assigned to resources. |
permissions.allowed[].applyTo | Text | ◯ | Target resource name. |
permissions.allowed[].type | Text | ◯ | Resource type: datastore, dataclass, attribute, method, singletonMethod, or singleton. |
permissions.allowed[].read | String の Collection | × | Privileges granted read access. |
permissions.allowed[].create | String の Collection | × | Privileges granted create access. |
permissions.allowed[].update | String の Collection | × | Privileges granted update access. |
permissions.allowed[].drop | String の Collection | × | Privileges granted delete access. |
permissions.allowed[].execute | String の Collection | × | Privileges granted execute access. |
permissions.allowed[].promote | String の Collection | × | Privileges temporarily granted during function execution. |
restrictedByDefault | Boolean | × | If true, access to resources without explicit permissions is denied. |
forceLogin | Boolean | × | If true, enables force login mode. |
- The
WebAdminprivilege name is reserved for the application; do not use it for custom privileges. - Privileges および roles の名称においては文字の大小が区別されません。 Unknown names passed to
.setPrivileges()are ignored. - Qodly Studio can add an optional
idproperty to privileges and roles to preserve references when they are renamed. - Multiple entries with the same
applyToare not merged: the last entry wins without an error. Define only one entry per resource. - A resource entry that grants no actions has no effect.
パーミッションエントリが宣言されていない権限 を参照する場合、4D はエラーをログに記録し、そのエントリを無視します。この場合、パーミッションは無視されます。デフォルトの制限モード が有効化されていない場合、リソースはguest セッションからアクセス可能となります。
Default location and content
New projects generate roles.json at:
<project folder>/Project/Sources/
See the Architecture section for details.
デフォルトの内容:
{
"privileges": [
],
"roles": [
],
"permissions": {
"allowed": [
{
"applyTo": "ds",
"type": "datastore",
"read": [],
"create": [],
"update": [],
"drop": [],
"execute": [],
"promote": []
}
]
},
"restrictedByDefault": false,
"forceLogin": false
}
In releases before 4D 20 R6, roles.json was not created by default. In 4D 20 R6 and later, opening an existing project without a roles.json file or "forceLogin": true setting makes the Activate REST authentication through ds.authentify() function button available on the Web Features page of the Settings dialog box. This button upgrades the security settings; you may need to modify your code. こちらの blog 記事 をご覧ください。
Qodly Studio for 4D においては、ログインモードはRoles and Privileges パネルの Force login オプション を使用することで設定することが可能です。
制限モード
restrictedByDefault determines access to a resource when no specific permission is defined:
- Unrestricted mode (
restrictedByDefault: false): Resources without defined permissions are accessible to all requests. - Restricted mode (
restrictedByDefault: true): Resources without defined permissions are blocked. Access is granted explicitly, and the permission inheritance rules still apply.
- New projects set
restrictedByDefaulttofalseby default; see recommended configurations. - For projects converted from earlier releases, enabling access to Qodly Studio through the One-click configuration dialog adds
restrictedByDefault: truetoroles.json.
Restriction strategies
Lock everything and open gradually
Choose either restricted mode or a lock privilege to close access by default:
| Restricted mode | Lock privilege | |
|---|---|---|
| Setting | "restrictedByDefault": true (Qodly Studio: Restrict Access By Default) | "restrictedByDefault": false, with every action on ds granted to a privilege that no session receives (for example, none) |
| What is closed | Every resource without a permission | Every resource that inherits datastore permissions |
| How to open | Grant permissions on the datastore, dataclasses, attributes, and functions | Grant permissions on dataclasses and functions to replace the lock |
| If 4D drops an invalid entry | The resource stays closed | The resource falls back to the lock and stays closed |
| 重要 | Explicitly grant access to public resources, usually to guest, including the login function unless force login is used, and HTTP request handlers called before login. In this mode, Qodly Studio's role review is currently incorrect. | Never assign the lock privilege to a role, including the administration role. |
Both approaches close access by default and follow the same inheritance and replacement rules. A lock privilege makes the lock visible in the editor; restricted mode requires no extra privilege.
Open everything, restrict gradually
Set unrestricted mode ("restrictedByDefault": false), grant guest read access to ds, then restrict sensitive dataclasses with dataclass-level permissions.
Remember that guest permissions apply to every session. Without a lock privilege, an invalid entry dropped by 4D leaves the resource open in unrestricted mode.
推奨される設定
Use these settings as a starting point:
- Development: Set both properties to
falsefor easier access while developing and debugging. Apply restrictions gradually as needed. - Production: Set
restrictedByDefaultandforceLogintotrueso that users must authenticate and resource access must be explicitly granted.
Permission errors
Roles_Errors.json ファイル
4D parses roles.json at startup. Restart the application for changes to take effect.
If parsing fails, 4D writes the errors to Roles_Errors.json in the project's Logs folder. It deletes this file after roles.json is corrected.
Check at startup, especially in production, whether Roles_Errors.json exists. Its presence indicates a parsing error, and permissions may not be enforced as intended. たとえば、次のように書くことができます:
If (Not(File("/LOGS/"+"Roles_Errors.json").exists))
...
Else // you can prevent the project to open
ALERT("The roles.json file is malformed or contains inconsistencies, the application will quit.")
QUIT 4D
End if
Permission errors are returned as follows:
| パス | read / create / update / drop refused | execute refused |
|---|---|---|
| REST | HTTP 401 with message | HTTP 500 with message (singleton: "Bad permission...") |
| Code (web session) | Read: error 1557 (dataclass) or 1607 (attribute). Create: error 1608 when assigning an attribute to a new entity if create is restricted by a permission; in restricted mode without permission, save() returns success: false and statusText: "Permission Error". Update/drop: error 1557 when loading the entity (both require read). | Error 1561 |
例題
This example grants a broad set of permissions through a manager privilege, while the payroll privilege grants read access to the sensitive Salaries dataclass.
{
"forceLogin": true,
"restrictedByDefault": true,
"privileges": [
{"privilege": "payroll", "includes": []},
{"privilege": "manager", "includes": []}
],
"roles": [
{"role": "administrator", "privileges": ["manager", "payroll"]}
],
"permissions": {
"allowed": [
{
"applyTo": "ds",
"type": "datastore",
"read": ["manager"],
"create": ["manager"],
"update": ["manager"],
"drop": ["manager"],
"execute": ["manager"]
},
{
"applyTo": "Salaries",
"type": "dataclass",
"read": ["payroll"]
}
]
}
}
Here are the resulting rules:
| Session | Read Salaries | Create Salaries | Update/drop Salaries | Read other dataclasses |
|---|---|---|---|---|
manager | Refused (the Salaries read permission replaces the datastore permission) | Allowed (inherited from the datastore) | Refused (read is required) | Allowed |
payroll | Allowed | Refused (the datastore permission is granted only to manager) | Refused | Refused |
administrator role | Allowed | Allowed | Allowed | Allowed |
The Salaries read permission replaces the datastore read permission, so manager alone cannot read that dataclass. The administrator role receives the combined permissions of manager and payroll.