1 .. -*- coding: utf-8 -*- |
|
2 |
|
3 Entity type definition |
|
4 ---------------------- |
|
5 |
|
6 An entity type is defined by a Python class which inherits from `EntityType`. |
|
7 The class definition contains the description of attributes and relations |
|
8 for the defined entity type. |
|
9 The class name corresponds to the entity type name. It is exepected to be |
|
10 defined in the module ``mycube.schema``. |
|
11 |
|
12 |
|
13 For example :: |
|
14 |
|
15 class Person(EntityType): |
|
16 """A person with the properties and the relations necessary for my |
|
17 application""" |
|
18 |
|
19 last_name = String(required=True, fulltextindexed=True) |
|
20 first_name = String(required=True, fulltextindexed=True) |
|
21 title = String(vocabulary=('Mr', 'Mrs', 'Miss')) |
|
22 date_of_birth = Date() |
|
23 works_for = SubjectRelation('Company', cardinality='?*') |
|
24 |
|
25 |
|
26 The entity described above defines three attributes of type String, |
|
27 last_name, first_name and title, an attribute of type Date for the date of |
|
28 birth and a relation that connects a `Person` to another entity of type |
|
29 `Company` through the semantic `works_for`. |
|
30 |
|
31 The name of the Python attribute corresponds to the name of the attribute |
|
32 or the relation in `CubicWeb` application. |
|
33 |
|
34 Built-in types for attributes |
|
35 ````````````````````````````` |
|
36 |
|
37 All `CubicWeb` built-in types are available : `String`, `Int`, `Float`, |
|
38 `Decimal`, `Boolean`, `Date`, `Datetime`, `Time`, `Interval`, `Byte` |
|
39 and `Password`. |
|
40 They are implicitely imported (as well as the special the function "_" |
|
41 for translation :ref:`internationalization`). |
|
42 |
|
43 An attribute is defined in the schema as follows:: |
|
44 |
|
45 attr_name = attr_type(properties*) |
|
46 |
|
47 where `attr_type` is one of the type listed above and `properties` is |
|
48 a list of the attribute needs to statisfy (see :ref:`properties` |
|
49 for more details). |
|
50 |
|
51 |
|
52 Meta-data |
|
53 ````````` |
|
54 |
|
55 Each entity type has at least the following meta-relations : |
|
56 |
|
57 - `eid` (`Int`) |
|
58 |
|
59 - `creation_date` (`Datetime`) |
|
60 |
|
61 - `modification_date` (`Datetime`) |
|
62 |
|
63 - `created_by` (`CWUser`) (which user created the entity) |
|
64 |
|
65 - `owned_by` (`CWUser`) (to whom the entity belongs; by default the |
|
66 creator but not necessary, and it could have multiple owners) |
|
67 |
|
68 - `is` (`CWEType`) (of which type the entity is) |
|
69 |
|
70 |
|
71 * relations can be defined by using `ObjectRelation` or `SubjectRelation`. |
|
72 The first argument of `SubjectRelation` or `ObjectRelation` gives respectively |
|
73 the object/subject entity type of the relation. This could be : |
|
74 |
|
75 * a string corresponding to an entity type |
|
76 |
|
77 * a tuple of string corresponding to multiple entity types |
|
78 |
|
79 * special string such as follows : |
|
80 |
|
81 - "**" : all types of entities |
|
82 - "*" : all types of non-meta entities |
|
83 - "@" : all types of meta entities but not system entities (e.g. used for |
|
84 the basic schema description) |
|
85 |
|
86 * it is possible to use the attribute `meta` to flag an entity type as a `meta` |
|
87 (e.g. used to describe/categorize other entities) |
|
88 |
|
89 Optionnal properties |
|
90 ```````````````````` |
|
91 .. _properties: |
|
92 |
|
93 |
|
94 * optional properties for attributes and relations : |
|
95 |
|
96 - `description` : a string describing an attribute or a relation. By default |
|
97 this string will be used in the editing form of the entity, which means |
|
98 that it is supposed to help the end-user and should be flagged by the |
|
99 function `_` to be properly internationalized. |
|
100 |
|
101 - `constraints` : a list of conditions/constraints that the relation has to |
|
102 satisfy (c.f. `Contraints`_) |
|
103 |
|
104 - `cardinality` : a two character string which specify the cardinality of the |
|
105 relation. The first character defines the cardinality of the relation on |
|
106 the subject, and the second on the object. When a relation can have |
|
107 multiple subjects or objects, the cardinality applies to all, |
|
108 not on a one-to-one basis (so it must be consistent...). The possible |
|
109 values are inspired from regular expression syntax : |
|
110 |
|
111 * `1`: 1..1 |
|
112 * `?`: 0..1 |
|
113 * `+`: 1..n |
|
114 * `*`: 0..n |
|
115 |
|
116 - `meta` : boolean indicating that the relation is a meta-relation (false by |
|
117 default) |
|
118 |
|
119 * optional properties for attributes : |
|
120 |
|
121 - `required` : boolean indicating if the attribute is required (false by default) |
|
122 |
|
123 - `unique` : boolean indicating if the value of the attribute has to be unique |
|
124 or not within all entities of the same type (false by default) |
|
125 |
|
126 - `indexed` : boolean indicating if an index needs to be created for this |
|
127 attribute in the database (false by default). This is useful only if |
|
128 you know that you will have to run numerous searches on the value of this |
|
129 attribute. |
|
130 |
|
131 - `default` : default value of the attribute. In case of date types, the values |
|
132 which could be used correspond to the RQL keywords `TODAY` and `NOW`. |
|
133 |
|
134 - `vocabulary` : specify static possible values of an attribute |
|
135 |
|
136 * optional properties of type `String` : |
|
137 |
|
138 - `fulltextindexed` : boolean indicating if the attribute is part of |
|
139 the full text index (false by default) (*applicable on the type `Byte` |
|
140 as well*) |
|
141 |
|
142 - `internationalizable` : boolean indicating if the value of the attribute |
|
143 is internationalizable (false by default) |
|
144 |
|
145 - `maxsize` : integer providing the maximum size of the string (no limit by default) |
|
146 |
|
147 * optional properties for relations : |
|
148 |
|
149 - `composite` : string indicating that the subject (composite == 'subject') |
|
150 is composed of the objects of the relations. For the opposite case (when |
|
151 the object is composed of the subjects of the relation), we just set |
|
152 'object' as value. The composition implies that when the relation |
|
153 is deleted (so when the composite is deleted), the composed are also deleted. |
|
154 |
|
155 Contraints |
|
156 `````````` |
|
157 By default, the available constraint types are : |
|
158 |
|
159 * `SizeConstraint` : allows to specify a minimum and/or maximum size on |
|
160 string (generic case of `maxsize`) |
|
161 |
|
162 * `BoundConstraint` : allows to specify a minimum and/or maximum value on |
|
163 numeric types |
|
164 |
|
165 * `UniqueConstraint` : identical to "unique=True" |
|
166 |
|
167 * `StaticVocabularyConstraint` : identical to "vocabulary=(...)" |
|
168 |
|
169 * `RQLConstraint` : allows to specify a RQL query that has to be satisfied |
|
170 by the subject and/or the object of the relation. In this query the variables |
|
171 `S` and `O` are reserved for the entities subject and object of the |
|
172 relation. |
|
173 |
|
174 * `RQLVocabularyConstraint` : similar to the previous type of constraint except |
|
175 that it does not express a "strong" constraint, which means it is only used to |
|
176 restrict the values listed in the drop-down menu of editing form, but it does |
|
177 not prevent another entity to be selected. |
|
178 |
|
179 |
|
180 Definition of relations |
|
181 ----------------------- |
|
182 |
|
183 XXX add note about defining relation type / definition |
|
184 |
|
185 A relation is defined by a Python class heriting `RelationType`. The name |
|
186 of the class corresponds to the name of the type. The class then contains |
|
187 a description of the properties of this type of relation, and could as well |
|
188 contain a string for the subject and a string for the object. This allows to create |
|
189 new definition of associated relations, (so that the class can have the |
|
190 definition properties from the relation) for example :: |
|
191 |
|
192 class locked_by(RelationType): |
|
193 """relation on all entities indicating that they are locked""" |
|
194 inlined = True |
|
195 cardinality = '?*' |
|
196 subject = '*' |
|
197 object = 'CWUser' |
|
198 |
|
199 In addition to the permissions, the properties of the relation types |
|
200 (shared also by all definition of relation of this type) are : |
|
201 |
|
202 |
|
203 * `inlined` : boolean handling the physical optimization for archiving |
|
204 the relation in the subject entity table, instead of creating a specific |
|
205 table for the relation. This applies to the relation when the cardinality |
|
206 of subject->relation->object is 0..1 (`?`) or 1..1 (`1`) |
|
207 |
|
208 * `symmetric` : boolean indicating that the relation is symmetrical, which |
|
209 means `X relation Y` implies `Y relation X` |
|
210 |
|
211 In the case of simultaneous relations definitions, `subject` and `object` |
|
212 can both be equal to the value of the first argument of `SubjectRelation` |
|
213 and `ObjectRelation`. |
|
214 |
|
215 When a relation is not inlined and not symmetrical, and it does not require |
|
216 specific permissions, its definition (by using `SubjectRelation` and |
|
217 `ObjectRelation`) is all we need. |
|
218 |
|
219 |
|
220 The security model |
|
221 ------------------ |
|
222 |
|
223 The security model of `cubicWeb` is based on `Access Control List`. |
|
224 The main principles are: |
|
225 |
|
226 * users and groups of users |
|
227 * a user belongs to at least one group of user |
|
228 * permissions (read, update, create, delete) |
|
229 * permissions are assigned to groups (and not to users) |
|
230 |
|
231 For `CubicWeb` in particular: |
|
232 |
|
233 * we associate rights at the enttities/relations schema level |
|
234 * for each entity, we distinguish four kind of permissions: read, |
|
235 add, update and delete |
|
236 * for each relation, we distinguish three king of permissions: read, |
|
237 add and delete (we can not modify a relation) |
|
238 * the basic groups are: Administrators, Users and Guests |
|
239 * by default, users belongs to the group Users |
|
240 * there is a virtual group called `Owners users` to which we |
|
241 can associate only deletion and update permissions |
|
242 * we can not add users to the `Owners users` group, they are |
|
243 implicetely added to it according to the context of the objects |
|
244 they own |
|
245 * the permissions of this group are only be checked on update/deletion |
|
246 actions if all the other groups the user belongs does not provide |
|
247 those permissions |
|
248 |
|
249 |
|
250 Permissions definition |
|
251 `````````````````````` |
|
252 |
|
253 Setting permissions is done with the attribute `permissions` of entities and |
|
254 relation types. It defines a dictionary where the keys are the access types |
|
255 (action), and the values are the authorized groups or expressions. |
|
256 |
|
257 For an entity type, the possible actions are `read`, `add`, `update` and |
|
258 `delete`. |
|
259 |
|
260 For a relation type, the possible actions are `read`, `add`, and `delete`. |
|
261 |
|
262 For each access type, a tuple indicates the name of the authorized groups and/or |
|
263 one or multiple RQL expressions to satisfy to grant access. The access is |
|
264 provided once the user is in the listed groups or one of the RQL condition is |
|
265 satisfied. |
|
266 |
|
267 The standard groups are : |
|
268 |
|
269 * `guests` |
|
270 |
|
271 * `users` |
|
272 |
|
273 * `managers` |
|
274 |
|
275 * `owners` : virtual group corresponding to the entity's owner. |
|
276 This can only be used for the actions `update` and `delete` of an entity |
|
277 type. |
|
278 |
|
279 It is also possible to use specific groups if they are defined in the precreate |
|
280 of the cube (``migration/precreate.py``). |
|
281 |
|
282 |
|
283 Use of RQL expression for writing rights |
|
284 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
285 It is possible to define RQL expression to provide update permission |
|
286 (`add`, `delete` and `update`) on relation and entity types. |
|
287 |
|
288 RQL expression for entity type permission : |
|
289 |
|
290 * you have to use the class `ERQLExpression` |
|
291 |
|
292 * the used expression corresponds to the WHERE statement of an RQL query |
|
293 |
|
294 * in this expression, the variables X and U are pre-defined references |
|
295 respectively on the current entity (on which the action is verified) and |
|
296 on the user who send the request |
|
297 |
|
298 * it is possible to use, in this expression, a special relation |
|
299 "has_<ACTION>_permission" where the subject is the user and the |
|
300 object is a any variable, meaning that the user needs to have |
|
301 permission to execute the action <ACTION> on the entities related |
|
302 to this variable |
|
303 |
|
304 For RQL expressions on a relation type, the principles are the same except |
|
305 for the following : |
|
306 |
|
307 * you have to use the class `RQLExpression` in the case of a non-final relation |
|
308 |
|
309 * in the expression, the variables S, O and U are pre-defined references |
|
310 to respectively the subject and the object of the current relation (on |
|
311 which the action is being verified) and the user who executed the query |
|
312 |
|
313 * we can also defined rights on attributes of an entity (non-final relation), |
|
314 knowing that : |
|
315 |
|
316 - to defines RQL expression, we have to use the class `ERQLExpression` |
|
317 in which X represents the entity the attribute belongs to |
|
318 |
|
319 - the permissions `add` and `delete` are equivalent. Only `add`/`read` |
|
320 are actually taken in consideration. |
|
321 |
|
322 In addition to that the entity type `CWPermission` from the standard library |
|
323 allow to build very complex and dynamic security architecture. The schema of |
|
324 this entity type is as follow : :: |
|
325 |
|
326 class CWPermission(MetaEntityType): |
|
327 """entity type that may be used to construct some advanced security configuration |
|
328 """ |
|
329 name = String(required=True, indexed=True, internationalizable=True, maxsize=100) |
|
330 require_group = SubjectRelation('CWGroup', cardinality='+*', |
|
331 description=_('groups to which the permission is granted')) |
|
332 require_state = SubjectRelation('State', |
|
333 description=_("entity'state in which the permission is applyable")) |
|
334 # can be used on any entity |
|
335 require_permission = ObjectRelation('**', cardinality='*1', composite='subject', |
|
336 description=_("link a permission to the entity. This " |
|
337 "permission should be used in the security " |
|
338 "definition of the entity's type to be useful.")) |
|
339 |
|
340 |
|
341 Example of configuration :: |
|
342 |
|
343 |
|
344 ... |
|
345 |
|
346 class Version(EntityType): |
|
347 """a version is defining the content of a particular project's release""" |
|
348 |
|
349 permissions = {'read': ('managers', 'users', 'guests',), |
|
350 'update': ('managers', 'logilab', 'owners',), |
|
351 'delete': ('managers', ), |
|
352 'add': ('managers', 'logilab', |
|
353 ERQLExpression('X version_of PROJ, U in_group G,' |
|
354 'PROJ require_permission P, P name "add_version",' |
|
355 'P require_group G'),)} |
|
356 |
|
357 ... |
|
358 |
|
359 class version_of(RelationType): |
|
360 """link a version to its project. A version is necessarily linked to one and only one project. |
|
361 """ |
|
362 permissions = {'read': ('managers', 'users', 'guests',), |
|
363 'delete': ('managers', ), |
|
364 'add': ('managers', 'logilab', |
|
365 RRQLExpression('O require_permission P, P name "add_version",' |
|
366 'U in_group G, P require_group G'),) |
|
367 } |
|
368 inlined = True |
|
369 |
|
370 This configuration indicates that an entity `CWPermission` named |
|
371 "add_version" can be associated to a project and provides rights to create |
|
372 new versions on this project to specific groups. It is important to notice that : |
|
373 |
|
374 * in such case, we have to protect both the entity type "Version" and the relation |
|
375 associating a version to a project ("version_of") |
|
376 |
|
377 * because of the genricity of the entity type `CWPermission`, we have to execute |
|
378 a unification with the groups and/or the states if necessary in the expression |
|
379 ("U in_group G, P require_group G" in the above example) |
|
380 |
|
381 Use of RQL expression for reading rights |
|
382 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
383 |
|
384 The principles are the same but with the following restrictions : |
|
385 |
|
386 * we can not use `RRQLExpression` on relation types for reading |
|
387 |
|
388 * special relations "has_<ACTION>_permission" can not be used |
|
389 |
|
390 |
|
391 Note on the use of RQL expression for `add` permission |
|
392 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
393 Potentially, the use of an RQL expression to add an entity or a relation |
|
394 can cause problems for the user interface, because if the expression uses |
|
395 the entity or the relation to create, then we are not able to verify the |
|
396 permissions before we actually add the entity (please note that this is |
|
397 not a problem for the RQL server at all, because the permissions checks are |
|
398 done after the creation). In such case, the permission check methods |
|
399 (check_perm, has_perm) can indicate that the user is not allowed to create |
|
400 this entity but can obtain the permission. |
|
401 To compensate this problem, it is usually necessary, for such case, |
|
402 to use an action that reflects the schema permissions but which enables |
|
403 to check properly the permissions so that it would show up if necessary. |
|
404 |
|
405 |
|
406 Updating your application with your new schema |
|
407 `````````````````````````````````````````````` |
|
408 |
|
409 If you modified your schema, the update is not automatic; indeed, this is |
|
410 in general not a good idea. |
|
411 Instead, you call a shell on your application, which is a |
|
412 an interactive python shell, with an appropriate |
|
413 cubicweb environment :: |
|
414 |
|
415 cubicweb-ctl shell myinstance |
|
416 |
|
417 and type :: |
|
418 |
|
419 add_entity_type('Person') |
|
420 |
|
421 And restart your application! |
|