author | Stephanie Marcu <stephanie.marcu@logilab.fr> |
Wed, 15 Sep 2010 10:54:25 +0200 | |
branch | stable |
changeset 6242 | d9ebb391782c |
parent 6233 | 612b7d947a4c |
child 6262 | 84901d735156 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
6017
5f6a60ea8544
[relations in ui] provide context information when computing label for a relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5896
diff
changeset
|
18 |
"""classes to define schemas for CubicWeb""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
2142
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
21 |
_ = unicode |
0 | 22 |
|
23 |
import re |
|
2147
476a75ede2cc
merge and add missing import in schema.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2142
diff
changeset
|
24 |
from os.path import join |
0 | 25 |
from logging import getLogger |
1133 | 26 |
from warnings import warn |
0 | 27 |
|
624
258e5692ae06
provide a new RichString attribute type
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
479
diff
changeset
|
28 |
from logilab.common.decorators import cached, clear_cache, monkeypatch |
2730
bb6fcb8c5d71
to make cw schemas importable, they have to be installed w/ cw code, not in /usr/share/cubicweb/schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2718
diff
changeset
|
29 |
from logilab.common.logging_ext import set_log_methods |
4717
535705688f4f
proper deprecation warning when import class that should be imported from yams
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4702
diff
changeset
|
30 |
from logilab.common.deprecation import deprecated, class_moved |
2926
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
31 |
from logilab.common.graph import get_cycles |
0 | 32 |
from logilab.common.compat import any |
33 |
||
2531
531ea4e7013e
[cleanup] nicer imports
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2526
diff
changeset
|
34 |
from yams import BadSchemaDefinition, buildobjs as ybo |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
35 |
from yams.schema import Schema, ERSchema, EntitySchema, RelationSchema, \ |
5030
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4913
diff
changeset
|
36 |
RelationDefinitionSchema, PermissionMixIn, role_name |
4717
535705688f4f
proper deprecation warning when import class that should be imported from yams
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4702
diff
changeset
|
37 |
from yams.constraints import BaseConstraint, FormatConstraint |
2531
531ea4e7013e
[cleanup] nicer imports
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2526
diff
changeset
|
38 |
from yams.reader import (CONSTRAINTS, PyFileReader, SchemaLoader, |
531ea4e7013e
[cleanup] nicer imports
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2526
diff
changeset
|
39 |
obsolete as yobsolete, cleanup_sys_modules) |
0 | 40 |
|
41 |
from rql import parse, nodes, RQLSyntaxError, TypeResolverException |
|
42 |
||
2730
bb6fcb8c5d71
to make cw schemas importable, they have to be installed w/ cw code, not in /usr/share/cubicweb/schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2718
diff
changeset
|
43 |
import cubicweb |
0 | 44 |
from cubicweb import ETYPE_NAME_MAP, ValidationError, Unauthorized |
45 |
||
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2531
diff
changeset
|
46 |
PURE_VIRTUAL_RTYPES = set(('identity', 'has_text',)) |
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2531
diff
changeset
|
47 |
VIRTUAL_RTYPES = set(('eid', 'identity', 'has_text',)) |
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2531
diff
changeset
|
48 |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4843
diff
changeset
|
49 |
# set of meta-relations available for every entity types |
2622
3c7edaa6c6d2
oops, should have been in a earlier commit (META_RELATION_TYPES renaming)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2616
diff
changeset
|
50 |
META_RTYPES = set(( |
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
51 |
'owned_by', 'created_by', 'is', 'is_instance_of', 'identity', |
2456
aa25d6b244c8
new cwuri metadata + a few tests fixes on the way
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2437
diff
changeset
|
52 |
'eid', 'creation_date', 'modification_date', 'has_text', 'cwuri', |
2184 | 53 |
)) |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4843
diff
changeset
|
54 |
SYSTEM_RTYPES = set(('require_permission', 'custom_workflow', 'in_state', |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4843
diff
changeset
|
55 |
'wf_info_for')) |
0 | 56 |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4843
diff
changeset
|
57 |
# set of entity and relation types used to build the schema |
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
58 |
SCHEMA_TYPES = set(( |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
59 |
'CWEType', 'CWRType', 'CWAttribute', 'CWRelation', |
6230
91c053c110c3
[schema] mark CWUniqueTogetherConstraint and its relations as schema entity/relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6191
diff
changeset
|
60 |
'CWConstraint', 'CWConstraintType', 'CWUniqueTogetherConstraint', |
91c053c110c3
[schema] mark CWUniqueTogetherConstraint and its relations as schema entity/relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6191
diff
changeset
|
61 |
'RQLExpression', |
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
62 |
'relation_type', 'from_entity', 'to_entity', |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
63 |
'constrained_by', 'cstrtype', |
6230
91c053c110c3
[schema] mark CWUniqueTogetherConstraint and its relations as schema entity/relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6191
diff
changeset
|
64 |
'constraint_of', 'relations', |
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
65 |
)) |
0 | 66 |
|
4434
101344a6ff9b
Improve the schema command with filtering option.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
4252
diff
changeset
|
67 |
WORKFLOW_TYPES = set(('Transition', 'State', 'TrInfo', 'Workflow', |
4759 | 68 |
'WorkflowTransition', 'BaseTransition', |
69 |
'SubWorkflowExitPoint')) |
|
70 |
||
4434
101344a6ff9b
Improve the schema command with filtering option.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
4252
diff
changeset
|
71 |
INTERNAL_TYPES = set(('CWProperty', 'CWPermission', 'CWCache', 'ExternalUri')) |
101344a6ff9b
Improve the schema command with filtering option.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
4252
diff
changeset
|
72 |
|
101344a6ff9b
Improve the schema command with filtering option.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
4252
diff
changeset
|
73 |
|
2142
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
74 |
_LOGGER = getLogger('cubicweb.schemaloader') |
0 | 75 |
|
76 |
# schema entities created from serialized schema have an eid rproperty |
|
77 |
ybo.ETYPE_PROPERTIES += ('eid',) |
|
78 |
ybo.RTYPE_PROPERTIES += ('eid',) |
|
79 |
ybo.RDEF_PROPERTIES += ('eid',) |
|
80 |
||
2926
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
81 |
|
4754
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
82 |
PUB_SYSTEM_ENTITY_PERMS = { |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
83 |
'read': ('managers', 'users', 'guests',), |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
84 |
'add': ('managers',), |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
85 |
'delete': ('managers',), |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
86 |
'update': ('managers',), |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
87 |
} |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
88 |
PUB_SYSTEM_REL_PERMS = { |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
89 |
'read': ('managers', 'users', 'guests',), |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
90 |
'add': ('managers',), |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
91 |
'delete': ('managers',), |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
92 |
} |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
93 |
PUB_SYSTEM_ATTR_PERMS = { |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
94 |
'read': ('managers', 'users', 'guests',), |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
95 |
'update': ('managers',), |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
96 |
} |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
97 |
RO_REL_PERMS = { |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
98 |
'read': ('managers', 'users', 'guests',), |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
99 |
'add': (), |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
100 |
'delete': (), |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
101 |
} |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
102 |
RO_ATTR_PERMS = { |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
103 |
'read': ('managers', 'users', 'guests',), |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
104 |
'update': (), |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
105 |
} |
6bf17f810975
[schema] new constants for permissions definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4717
diff
changeset
|
106 |
|
2926
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
107 |
# XXX same algorithm as in reorder_cubes and probably other place, |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
108 |
# may probably extract a generic function |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
109 |
def order_eschemas(eschemas): |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
110 |
"""return entity schemas ordered such that entity types which specializes an |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
111 |
other one appears after that one |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
112 |
""" |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
113 |
graph = {} |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
114 |
for eschema in eschemas: |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
115 |
if eschema.specializes(): |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
116 |
graph[eschema] = set((eschema.specializes(),)) |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
117 |
else: |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
118 |
graph[eschema] = set() |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
119 |
cycles = get_cycles(graph) |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
120 |
if cycles: |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
121 |
cycles = '\n'.join(' -> '.join(cycle) for cycle in cycles) |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
122 |
raise Exception('cycles in entity schema specialization: %s' |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
123 |
% cycles) |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
124 |
eschemas = [] |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
125 |
while graph: |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
126 |
# sorted to get predictable results |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
127 |
for eschema, deps in sorted(graph.items()): |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
128 |
if not deps: |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
129 |
eschemas.append(eschema) |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
130 |
del graph[eschema] |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
131 |
for deps in graph.itervalues(): |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
132 |
try: |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
133 |
deps.remove(eschema) |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
134 |
except KeyError: |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
135 |
continue |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
136 |
return eschemas |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
137 |
|
0 | 138 |
def bw_normalize_etype(etype): |
139 |
if etype in ETYPE_NAME_MAP: |
|
140 |
msg = '%s has been renamed to %s, please update your code' % ( |
|
1451 | 141 |
etype, ETYPE_NAME_MAP[etype]) |
0 | 142 |
warn(msg, DeprecationWarning, stacklevel=4) |
143 |
etype = ETYPE_NAME_MAP[etype] |
|
144 |
return etype |
|
145 |
||
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
146 |
def display_name(req, key, form='', context=None): |
0 | 147 |
"""return a internationalized string for the key (schema entity or relation |
148 |
name) in a given form |
|
149 |
""" |
|
150 |
assert form in ('', 'plural', 'subject', 'object') |
|
151 |
if form == 'subject': |
|
152 |
form = '' |
|
153 |
if form: |
|
154 |
key = key + '_' + form |
|
155 |
# ensure unicode |
|
3284
036cf5a25714
ensure pgettext return unicode as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
156 |
# .lower() in case no translation are available XXX done whatever a translation is there or not! |
036cf5a25714
ensure pgettext return unicode as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
157 |
if context is not None: |
036cf5a25714
ensure pgettext return unicode as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
158 |
return unicode(req.pgettext(context, key)).lower() |
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
159 |
else: |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
160 |
return unicode(req._(key)).lower() |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
161 |
|
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2782
diff
changeset
|
162 |
__builtins__['display_name'] = deprecated('[3.4] display_name should be imported from cubicweb.schema')(display_name) |
0 | 163 |
|
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
164 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
165 |
# rql expression utilities function ############################################ |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
166 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
167 |
def guess_rrqlexpr_mainvars(expression): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
168 |
defined = set(split_expression(expression)) |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
169 |
mainvars = [] |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
170 |
if 'S' in defined: |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
171 |
mainvars.append('S') |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
172 |
if 'O' in defined: |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
173 |
mainvars.append('O') |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
174 |
if 'U' in defined: |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
175 |
mainvars.append('U') |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
176 |
if not mainvars: |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
177 |
raise Exception('unable to guess selection variables') |
5372
b74eed7e8b37
Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5174
diff
changeset
|
178 |
return ','.join(sorted(mainvars)) |
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
179 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
180 |
def split_expression(rqlstring): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
181 |
for expr in rqlstring.split(','): |
4132
440d383367eb
fix mainvars detection bug with EXISTS (paren, actually)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3985
diff
changeset
|
182 |
for noparen in expr.split('('): |
440d383367eb
fix mainvars detection bug with EXISTS (paren, actually)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3985
diff
changeset
|
183 |
for word in noparen.split(): |
440d383367eb
fix mainvars detection bug with EXISTS (paren, actually)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3985
diff
changeset
|
184 |
yield word |
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
185 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
186 |
def normalize_expression(rqlstring): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
187 |
"""normalize an rql expression to ease schema synchronization (avoid |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
188 |
suppressing and reinserting an expression if only a space has been added/removed |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
189 |
for instance) |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
190 |
""" |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
191 |
return u', '.join(' '.join(expr.split()) for expr in rqlstring.split(',')) |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
192 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
193 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
194 |
# Schema objects definition ################################################### |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
195 |
|
3471
8c57c71b859c
can now give context to rschema.display_name(...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3401
diff
changeset
|
196 |
def ERSchema_display_name(self, req, form='', context=None): |
0 | 197 |
"""return a internationalized string for the entity/relation type name in |
198 |
a given form |
|
199 |
""" |
|
3471
8c57c71b859c
can now give context to rschema.display_name(...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3401
diff
changeset
|
200 |
return display_name(req, self.type, form, context) |
0 | 201 |
ERSchema.display_name = ERSchema_display_name |
202 |
||
203 |
@cached |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
204 |
def get_groups(self, action): |
0 | 205 |
"""return the groups authorized to perform <action> on entities of |
206 |
this type |
|
207 |
||
208 |
:type action: str |
|
209 |
:param action: the name of a permission |
|
210 |
||
211 |
:rtype: tuple |
|
212 |
:return: names of the groups with the given permission |
|
213 |
""" |
|
214 |
assert action in self.ACTIONS, action |
|
215 |
#assert action in self._groups, '%s %s' % (self, action) |
|
216 |
try: |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
217 |
return frozenset(g for g in self.permissions[action] if isinstance(g, basestring)) |
0 | 218 |
except KeyError: |
219 |
return () |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
220 |
PermissionMixIn.get_groups = get_groups |
0 | 221 |
|
222 |
@cached |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
223 |
def get_rqlexprs(self, action): |
0 | 224 |
"""return the rql expressions representing queries to check the user is allowed |
225 |
to perform <action> on entities of this type |
|
226 |
||
227 |
:type action: str |
|
228 |
:param action: the name of a permission |
|
229 |
||
230 |
:rtype: tuple |
|
231 |
:return: the rql expressions with the given permission |
|
232 |
""" |
|
233 |
assert action in self.ACTIONS, action |
|
234 |
#assert action in self._rqlexprs, '%s %s' % (self, action) |
|
235 |
try: |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
236 |
return tuple(g for g in self.permissions[action] if not isinstance(g, basestring)) |
0 | 237 |
except KeyError: |
238 |
return () |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
239 |
PermissionMixIn.get_rqlexprs = get_rqlexprs |
0 | 240 |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
241 |
orig_set_action_permissions = PermissionMixIn.set_action_permissions |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
242 |
def set_action_permissions(self, action, permissions): |
0 | 243 |
"""set the groups and rql expressions allowing to perform <action> on |
244 |
entities of this type |
|
245 |
||
246 |
:type action: str |
|
247 |
:param action: the name of a permission |
|
248 |
||
249 |
:type permissions: tuple |
|
250 |
:param permissions: the groups and rql expressions allowing the given action |
|
251 |
""" |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
252 |
orig_set_action_permissions(self, action, tuple(permissions)) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
253 |
clear_cache(self, 'get_rqlexprs') |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
254 |
clear_cache(self, 'get_groups') |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
255 |
PermissionMixIn.set_action_permissions = set_action_permissions |
0 | 256 |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
257 |
def has_local_role(self, action): |
0 | 258 |
"""return true if the action *may* be granted localy (eg either rql |
259 |
expressions or the owners group are used in security definition) |
|
260 |
||
261 |
XXX this method is only there since we don't know well how to deal with |
|
262 |
'add' action checking. Also find a better name would be nice. |
|
263 |
""" |
|
264 |
assert action in self.ACTIONS, action |
|
265 |
if self.get_rqlexprs(action): |
|
266 |
return True |
|
267 |
if action in ('update', 'delete'): |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
268 |
return 'owners' in self.get_groups(action) |
0 | 269 |
return False |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
270 |
PermissionMixIn.has_local_role = has_local_role |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
271 |
|
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
272 |
def may_have_permission(self, action, req): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
273 |
if action != 'read' and not (self.has_local_role('read') or |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
274 |
self.has_perm(req, 'read')): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
275 |
return False |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
276 |
return self.has_local_role(action) or self.has_perm(req, action) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
277 |
PermissionMixIn.may_have_permission = may_have_permission |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
278 |
|
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
279 |
def has_perm(self, session, action, **kwargs): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
280 |
"""return true if the action is granted globaly or localy""" |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
281 |
try: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
282 |
self.check_perm(session, action, **kwargs) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
283 |
return True |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
284 |
except Unauthorized: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
285 |
return False |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
286 |
PermissionMixIn.has_perm = has_perm |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
287 |
|
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
288 |
def check_perm(self, session, action, **kwargs): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
289 |
# NB: session may be a server session or a request object check user is |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
290 |
# in an allowed group, if so that's enough internal sessions should |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
291 |
# always stop there |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
292 |
groups = self.get_groups(action) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
293 |
if session.user.matching_groups(groups): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
294 |
return |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
295 |
# if 'owners' in allowed groups, check if the user actually owns this |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
296 |
# object, if so that's enough |
4607
55eab66c6592
[schema security] fix so that when cheking attributes perms for an entity being created, 'owners' and has_*_permission in erqlexpr are considered satisfied
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4586
diff
changeset
|
297 |
if 'owners' in groups and ( |
55eab66c6592
[schema security] fix so that when cheking attributes perms for an entity being created, 'owners' and has_*_permission in erqlexpr are considered satisfied
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4586
diff
changeset
|
298 |
kwargs.get('creating') |
4608 | 299 |
or ('eid' in kwargs and session.user.owns(kwargs['eid']))): |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
300 |
return |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
301 |
# else if there is some rql expressions, check them |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
302 |
if any(rqlexpr.check(session, **kwargs) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
303 |
for rqlexpr in self.get_rqlexprs(action)): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
304 |
return |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
305 |
raise Unauthorized(action, str(self)) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
306 |
PermissionMixIn.check_perm = check_perm |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
307 |
|
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
308 |
|
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
309 |
RelationDefinitionSchema._RPROPERTIES['eid'] = None |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
310 |
|
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
311 |
def rql_expression(self, expression, mainvars=None, eid=None): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
312 |
"""rql expression factory""" |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
313 |
if self.rtype.final: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
314 |
return ERQLExpression(expression, mainvars, eid) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
315 |
return RRQLExpression(expression, mainvars, eid) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
316 |
RelationDefinitionSchema.rql_expression = rql_expression |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
317 |
|
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
318 |
orig_check_permission_definitions = RelationDefinitionSchema.check_permission_definitions |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
319 |
def check_permission_definitions(self): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
320 |
orig_check_permission_definitions(self) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
321 |
schema = self.subject.schema |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
322 |
for action, groups in self.permissions.iteritems(): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
323 |
for group_or_rqlexpr in groups: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
324 |
if action == 'read' and \ |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
325 |
isinstance(group_or_rqlexpr, RQLExpression): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
326 |
msg = "can't use rql expression for read permission of %s" |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
327 |
raise BadSchemaDefinition(msg % self) |
4574
2380444d982c
[schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
328 |
if self.final and isinstance(group_or_rqlexpr, RRQLExpression): |
2380444d982c
[schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
329 |
msg = "can't use RRQLExpression on %s, use an ERQLExpression" |
2380444d982c
[schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
330 |
raise BadSchemaDefinition(msg % self) |
2380444d982c
[schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
331 |
if not self.final and isinstance(group_or_rqlexpr, ERQLExpression): |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
332 |
msg = "can't use ERQLExpression on %s, use a RRQLExpression" |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
333 |
raise BadSchemaDefinition(msg % self) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
334 |
RelationDefinitionSchema.check_permission_definitions = check_permission_definitions |
0 | 335 |
|
336 |
||
337 |
class CubicWebEntitySchema(EntitySchema): |
|
338 |
"""a entity has a type, a set of subject and or object relations |
|
339 |
the entity schema defines the possible relations for a given type and some |
|
340 |
constraints on those relations |
|
341 |
""" |
|
342 |
def __init__(self, schema=None, edef=None, eid=None, **kwargs): |
|
343 |
super(CubicWebEntitySchema, self).__init__(schema, edef, **kwargs) |
|
344 |
if eid is None and edef is not None: |
|
345 |
eid = getattr(edef, 'eid', None) |
|
346 |
self.eid = eid |
|
4574
2380444d982c
[schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
347 |
|
2380444d982c
[schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
348 |
def check_permission_definitions(self): |
2380444d982c
[schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
349 |
super(CubicWebEntitySchema, self).check_permission_definitions() |
2380444d982c
[schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
350 |
for groups in self.permissions.itervalues(): |
2380444d982c
[schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
351 |
for group_or_rqlexpr in groups: |
2380444d982c
[schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
352 |
if isinstance(group_or_rqlexpr, RRQLExpression): |
2380444d982c
[schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
353 |
msg = "can't use RRQLExpression on %s, use an ERQLExpression" |
2380444d982c
[schema] refactor/cleanup check_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
354 |
raise BadSchemaDefinition(msg % self.type) |
1451 | 355 |
|
0 | 356 |
def attribute_definitions(self): |
357 |
"""return an iterator on attribute definitions |
|
1451 | 358 |
|
0 | 359 |
attribute relations are a subset of subject relations where the |
360 |
object's type is a final entity |
|
1451 | 361 |
|
0 | 362 |
an attribute definition is a 2-uple : |
363 |
* name of the relation |
|
364 |
* schema of the destination entity type |
|
365 |
""" |
|
366 |
iter = super(CubicWebEntitySchema, self).attribute_definitions() |
|
367 |
for rschema, attrschema in iter: |
|
368 |
if rschema.type == 'has_text': |
|
369 |
continue |
|
370 |
yield rschema, attrschema |
|
1451 | 371 |
|
2128
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
372 |
def main_attribute(self): |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
373 |
"""convenience method that returns the *main* (i.e. the first non meta) |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
374 |
attribute defined in the entity schema |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
375 |
""" |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
376 |
for rschema, _ in self.attribute_definitions(): |
2622
3c7edaa6c6d2
oops, should have been in a earlier commit (META_RELATION_TYPES renaming)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2616
diff
changeset
|
377 |
if not (rschema in META_RTYPES |
2128
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
378 |
or self.is_metadata(rschema)): |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
379 |
return rschema |
464edb198faa
drop @ wildcard in relation subject/object, override main_attribute for proper behaviour
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2126
diff
changeset
|
380 |
|
0 | 381 |
def add_subject_relation(self, rschema): |
382 |
"""register the relation schema as possible subject relation""" |
|
383 |
super(CubicWebEntitySchema, self).add_subject_relation(rschema) |
|
384 |
self._update_has_text() |
|
385 |
||
386 |
def del_subject_relation(self, rtype): |
|
387 |
super(CubicWebEntitySchema, self).del_subject_relation(rtype) |
|
2632
920bfaff60a4
B [schema update] may remove has_text unexpectedly, give another argument to fix it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2622
diff
changeset
|
388 |
self._update_has_text(True) |
1451 | 389 |
|
2632
920bfaff60a4
B [schema update] may remove has_text unexpectedly, give another argument to fix it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2622
diff
changeset
|
390 |
def _update_has_text(self, deletion=False): |
0 | 391 |
may_need_has_text, has_has_text = False, False |
2632
920bfaff60a4
B [schema update] may remove has_text unexpectedly, give another argument to fix it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2622
diff
changeset
|
392 |
need_has_text = None |
0 | 393 |
for rschema in self.subject_relations(): |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
394 |
if rschema.final: |
0 | 395 |
if rschema == 'has_text': |
396 |
has_has_text = True |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
397 |
elif self.rdef(rschema).get('fulltextindexed'): |
0 | 398 |
may_need_has_text = True |
399 |
elif rschema.fulltext_container: |
|
400 |
if rschema.fulltext_container == 'subject': |
|
401 |
may_need_has_text = True |
|
402 |
else: |
|
403 |
need_has_text = False |
|
404 |
for rschema in self.object_relations(): |
|
405 |
if rschema.fulltext_container: |
|
406 |
if rschema.fulltext_container == 'object': |
|
407 |
may_need_has_text = True |
|
408 |
else: |
|
409 |
need_has_text = False |
|
410 |
if need_has_text is None: |
|
411 |
need_has_text = may_need_has_text |
|
2632
920bfaff60a4
B [schema update] may remove has_text unexpectedly, give another argument to fix it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2622
diff
changeset
|
412 |
if need_has_text and not has_has_text and not deletion: |
4755
13a5d3a7410e
[schema] tweaks meta-relations and schema/workflow entities attributes permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4754
diff
changeset
|
413 |
rdef = ybo.RelationDefinition(self.type, 'has_text', 'String', |
13a5d3a7410e
[schema] tweaks meta-relations and schema/workflow entities attributes permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4754
diff
changeset
|
414 |
__permissions__=RO_ATTR_PERMS) |
0 | 415 |
self.schema.add_relation_def(rdef) |
416 |
elif not need_has_text and has_has_text: |
|
5122
c06078d59e87
[schema] ensure we don't remove has_text relation type unfortunatly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5030
diff
changeset
|
417 |
# use rschema.del_relation_def and not schema.del_relation_def to |
c06078d59e87
[schema] ensure we don't remove has_text relation type unfortunatly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5030
diff
changeset
|
418 |
# avoid deleting the relation type accidentally... |
c06078d59e87
[schema] ensure we don't remove has_text relation type unfortunatly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5030
diff
changeset
|
419 |
self.schema['has_text'].del_relation_def(self, self.schema['String']) |
1451 | 420 |
|
5896 | 421 |
def schema_entity(self): # XXX @property for consistency with meta |
0 | 422 |
"""return True if this entity type is used to build the schema""" |
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
423 |
return self.type in SCHEMA_TYPES |
0 | 424 |
|
425 |
def rql_expression(self, expression, mainvars=None, eid=None): |
|
426 |
"""rql expression factory""" |
|
427 |
return ERQLExpression(expression, mainvars, eid) |
|
1451 | 428 |
|
2252 | 429 |
|
0 | 430 |
class CubicWebRelationSchema(RelationSchema): |
1451 | 431 |
|
0 | 432 |
def __init__(self, schema=None, rdef=None, eid=None, **kwargs): |
433 |
if rdef is not None: |
|
434 |
# if this relation is inlined |
|
435 |
self.inlined = rdef.inlined |
|
436 |
super(CubicWebRelationSchema, self).__init__(schema, rdef, **kwargs) |
|
437 |
if eid is None and rdef is not None: |
|
438 |
eid = getattr(rdef, 'eid', None) |
|
439 |
self.eid = eid |
|
1451 | 440 |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
441 |
@property |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
442 |
def meta(self): |
2622
3c7edaa6c6d2
oops, should have been in a earlier commit (META_RELATION_TYPES renaming)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2616
diff
changeset
|
443 |
return self.type in META_RTYPES |
1451 | 444 |
|
5896 | 445 |
def schema_relation(self): # XXX @property for consistency with meta |
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
446 |
"""return True if this relation type is used to build the schema""" |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
447 |
return self.type in SCHEMA_TYPES |
1451 | 448 |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
449 |
def may_have_permission(self, action, req, eschema=None, role=None): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
450 |
if eschema is not None: |
4019 | 451 |
for tschema in self.targets(eschema, role): |
452 |
rdef = self.role_rdef(eschema, tschema, role) |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
453 |
if rdef.may_have_permission(action, req): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
454 |
return True |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
455 |
else: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
456 |
for rdef in self.rdefs.itervalues(): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
457 |
if rdef.may_have_permission(action, req): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
458 |
return True |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
459 |
return False |
0 | 460 |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
461 |
def has_perm(self, session, action, **kwargs): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
462 |
"""return true if the action is granted globaly or localy""" |
4575
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
463 |
if self.final: |
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
464 |
assert not ('fromeid' in kwargs or 'toeid' in kwargs), kwargs |
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
465 |
assert action in ('read', 'update') |
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
466 |
if 'eid' in kwargs: |
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
467 |
subjtype = session.describe(kwargs['eid'])[0] |
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
468 |
else: |
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
469 |
subjtype = objtype = None |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
470 |
else: |
4575
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
471 |
assert not 'eid' in kwargs, kwargs |
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
472 |
assert action in ('read', 'add', 'delete') |
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
473 |
if 'fromeid' in kwargs: |
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
474 |
subjtype = session.describe(kwargs['fromeid'])[0] |
5746
f4fc424747db
[security] SchemaRelation.has_perm grows to new arguments allowing to specify subject/object entity type when actual eid are not known, but their types are
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5703
diff
changeset
|
475 |
elif 'frometype' in kwargs: |
f4fc424747db
[security] SchemaRelation.has_perm grows to new arguments allowing to specify subject/object entity type when actual eid are not known, but their types are
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5703
diff
changeset
|
476 |
subjtype = kwargs.pop('frometype') |
4575
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
477 |
else: |
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
478 |
subjtype = None |
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
479 |
if 'toeid' in kwargs: |
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
480 |
objtype = session.describe(kwargs['toeid'])[0] |
5746
f4fc424747db
[security] SchemaRelation.has_perm grows to new arguments allowing to specify subject/object entity type when actual eid are not known, but their types are
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5703
diff
changeset
|
481 |
elif 'toetype' in kwargs: |
f4fc424747db
[security] SchemaRelation.has_perm grows to new arguments allowing to specify subject/object entity type when actual eid are not known, but their types are
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5703
diff
changeset
|
482 |
objtype = kwargs.pop('toetype') |
4575
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
483 |
else: |
4a7fe84f7803
[schema] fix RelationSchema.has_perm to properly works with attribute relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4574
diff
changeset
|
484 |
objtype = None |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
485 |
if objtype and subjtype: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
486 |
return self.rdef(subjtype, objtype).has_perm(session, action, **kwargs) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
487 |
elif subjtype: |
4004
c52619c738a5
api renaming update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4003
diff
changeset
|
488 |
for tschema in self.targets(subjtype, 'subject'): |
c52619c738a5
api renaming update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4003
diff
changeset
|
489 |
rdef = self.rdef(subjtype, tschema) |
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4037
diff
changeset
|
490 |
if not rdef.has_perm(session, action, **kwargs): |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
491 |
return False |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
492 |
elif objtype: |
4004
c52619c738a5
api renaming update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4003
diff
changeset
|
493 |
for tschema in self.targets(objtype, 'object'): |
c52619c738a5
api renaming update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4003
diff
changeset
|
494 |
rdef = self.rdef(tschema, objtype) |
c52619c738a5
api renaming update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4003
diff
changeset
|
495 |
if not rdef.has_perm(session, action, **kwargs): |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
496 |
return False |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
497 |
else: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
498 |
for rdef in self.rdefs.itervalues(): |
4004
c52619c738a5
api renaming update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4003
diff
changeset
|
499 |
if not rdef.has_perm(session, action, **kwargs): |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
500 |
return False |
4233
94ffaecd8e8c
ouch, has_perm was always returning False...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4181
diff
changeset
|
501 |
return True |
0 | 502 |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
503 |
@deprecated('use .rdef(subjtype, objtype).role_cardinality(role)') |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
504 |
def cardinality(self, subjtype, objtype, target): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
505 |
return self.rdef(subjtype, objtype).role_cardinality(target) |
0 | 506 |
|
1451 | 507 |
|
0 | 508 |
class CubicWebSchema(Schema): |
509 |
"""set of entities and relations schema defining the possible data sets |
|
510 |
used in an application |
|
511 |
||
512 |
:type name: str |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2460
diff
changeset
|
513 |
:ivar name: name of the schema, usually the instance identifier |
1451 | 514 |
|
0 | 515 |
:type base: str |
516 |
:ivar base: path of the directory where the schema is defined |
|
517 |
""" |
|
1451 | 518 |
reading_from_database = False |
0 | 519 |
entity_class = CubicWebEntitySchema |
520 |
relation_class = CubicWebRelationSchema |
|
2958
44e5446b649b
no inference for the identity relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2926
diff
changeset
|
521 |
no_specialization_inference = ('identity',) |
0 | 522 |
|
523 |
def __init__(self, *args, **kwargs): |
|
524 |
self._eid_index = {} |
|
525 |
super(CubicWebSchema, self).__init__(*args, **kwargs) |
|
526 |
ybo.register_base_types(self) |
|
2300
c8151d004e06
meta has been drop
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
527 |
rschema = self.add_relation_type(ybo.RelationType('eid')) |
0 | 528 |
rschema.final = True |
2300
c8151d004e06
meta has been drop
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
529 |
rschema = self.add_relation_type(ybo.RelationType('has_text')) |
0 | 530 |
rschema.final = True |
2300
c8151d004e06
meta has been drop
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
531 |
rschema = self.add_relation_type(ybo.RelationType('identity')) |
0 | 532 |
rschema.final = False |
1451 | 533 |
|
0 | 534 |
def add_entity_type(self, edef): |
535 |
edef.name = edef.name.encode() |
|
536 |
edef.name = bw_normalize_etype(edef.name) |
|
537 |
assert re.match(r'[A-Z][A-Za-z0-9]*[a-z]+[0-9]*$', edef.name), repr(edef.name) |
|
538 |
eschema = super(CubicWebSchema, self).add_entity_type(edef) |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3554
diff
changeset
|
539 |
if not eschema.final: |
0 | 540 |
# automatically add the eid relation to non final entity types |
541 |
rdef = ybo.RelationDefinition(eschema.type, 'eid', 'Int', |
|
4755
13a5d3a7410e
[schema] tweaks meta-relations and schema/workflow entities attributes permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4754
diff
changeset
|
542 |
cardinality='11', uid=True, |
13a5d3a7410e
[schema] tweaks meta-relations and schema/workflow entities attributes permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4754
diff
changeset
|
543 |
__permissions__=RO_ATTR_PERMS) |
0 | 544 |
self.add_relation_def(rdef) |
4755
13a5d3a7410e
[schema] tweaks meta-relations and schema/workflow entities attributes permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4754
diff
changeset
|
545 |
rdef = ybo.RelationDefinition(eschema.type, 'identity', eschema.type, |
13a5d3a7410e
[schema] tweaks meta-relations and schema/workflow entities attributes permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4754
diff
changeset
|
546 |
__permissions__=RO_REL_PERMS) |
0 | 547 |
self.add_relation_def(rdef) |
548 |
self._eid_index[eschema.eid] = eschema |
|
549 |
return eschema |
|
1451 | 550 |
|
0 | 551 |
def add_relation_type(self, rdef): |
552 |
rdef.name = rdef.name.lower().encode() |
|
553 |
rschema = super(CubicWebSchema, self).add_relation_type(rdef) |
|
554 |
self._eid_index[rschema.eid] = rschema |
|
555 |
return rschema |
|
1451 | 556 |
|
0 | 557 |
def add_relation_def(self, rdef): |
558 |
"""build a part of a relation schema |
|
559 |
(i.e. add a relation between two specific entity's types) |
|
560 |
||
561 |
:type subject: str |
|
562 |
:param subject: entity's type that is subject of the relation |
|
563 |
||
564 |
:type rtype: str |
|
565 |
:param rtype: the relation's type (i.e. the name of the relation) |
|
566 |
||
567 |
:type obj: str |
|
568 |
:param obj: entity's type that is object of the relation |
|
569 |
||
570 |
:rtype: RelationSchema |
|
571 |
:param: the newly created or just completed relation schema |
|
572 |
""" |
|
573 |
rdef.name = rdef.name.lower() |
|
574 |
rdef.subject = bw_normalize_etype(rdef.subject) |
|
575 |
rdef.object = bw_normalize_etype(rdef.object) |
|
6191
ece6996e6ac8
[schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6109
diff
changeset
|
576 |
rdefs = super(CubicWebSchema, self).add_relation_def(rdef) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
577 |
if rdefs: |
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
578 |
try: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
579 |
self._eid_index[rdef.eid] = rdefs |
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
580 |
except AttributeError: |
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
581 |
pass # not a serialized schema |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
582 |
return rdefs |
1451 | 583 |
|
0 | 584 |
def del_relation_type(self, rtype): |
585 |
rschema = self.rschema(rtype) |
|
586 |
self._eid_index.pop(rschema.eid, None) |
|
587 |
super(CubicWebSchema, self).del_relation_type(rtype) |
|
1451 | 588 |
|
0 | 589 |
def del_relation_def(self, subjtype, rtype, objtype): |
590 |
for k, v in self._eid_index.items(): |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
591 |
if not isinstance(v, RelationDefinitionSchema): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
592 |
continue |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
593 |
if v.subject == subjtype and v.rtype == rtype and v.object == objtype: |
0 | 594 |
del self._eid_index[k] |
2718
3a56b87bb5d6
[schema] break when found
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2716
diff
changeset
|
595 |
break |
0 | 596 |
super(CubicWebSchema, self).del_relation_def(subjtype, rtype, objtype) |
1451 | 597 |
|
0 | 598 |
def del_entity_type(self, etype): |
599 |
eschema = self.eschema(etype) |
|
600 |
self._eid_index.pop(eschema.eid, None) |
|
601 |
# deal with has_text first, else its automatic deletion (see above) |
|
602 |
# may trigger an error in ancestor's del_entity_type method |
|
603 |
if 'has_text' in eschema.subject_relations(): |
|
604 |
self.del_relation_def(etype, 'has_text', 'String') |
|
605 |
super(CubicWebSchema, self).del_entity_type(etype) |
|
1451 | 606 |
|
0 | 607 |
def schema_by_eid(self, eid): |
608 |
return self._eid_index[eid] |
|
609 |
||
610 |
||
611 |
# Possible constraints ######################################################## |
|
612 |
||
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
613 |
class BaseRQLConstraint(BaseConstraint): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
614 |
"""base class for rql constraints |
0 | 615 |
""" |
6232
d992176d17b7
[schema] ensure DISTINCT is displayed for constraint using it, this ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6230
diff
changeset
|
616 |
distinct_query = None |
1451 | 617 |
|
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
618 |
def __init__(self, restriction, mainvars=None): |
3963
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
619 |
self.restriction = normalize_expression(restriction) |
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
620 |
if mainvars is None: |
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
621 |
mainvars = guess_rrqlexpr_mainvars(restriction) |
3963
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
622 |
else: |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
623 |
normmainvars = [] |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
624 |
for mainvar in mainvars.split(','): |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
625 |
mainvar = mainvar.strip() |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
626 |
if not mainvar.isalpha(): |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
627 |
raise Exception('bad mainvars %s' % mainvars) |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
628 |
normmainvars.append(mainvar) |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
629 |
assert mainvars, 'bad mainvars %s' % mainvars |
0d592677e55f
nicer mainvars/expression handling when initializing rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3961
diff
changeset
|
630 |
mainvars = ','.join(sorted(normmainvars)) |
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
631 |
self.mainvars = mainvars |
0 | 632 |
|
633 |
def serialize(self): |
|
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
634 |
# start with a comma for bw compat, see below |
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
635 |
return ';' + self.mainvars + ';' + self.restriction |
1451 | 636 |
|
5378
0f54a0e128ac
[schema] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5372
diff
changeset
|
637 |
@classmethod |
0 | 638 |
def deserialize(cls, value): |
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
639 |
# XXX < 3.5.10 bw compat |
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
640 |
if not value.startswith(';'): |
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
641 |
return cls(value) |
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
642 |
_, mainvars, restriction = value.split(';', 2) |
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
643 |
return cls(restriction, mainvars) |
1451 | 644 |
|
0 | 645 |
def check(self, entity, rtype, value): |
646 |
"""return true if the value satisfy the constraint, else false""" |
|
647 |
# implemented as a hook in the repository |
|
648 |
return 1 |
|
649 |
||
650 |
def repo_check(self, session, eidfrom, rtype, eidto): |
|
651 |
"""raise ValidationError if the relation doesn't satisfy the constraint |
|
652 |
""" |
|
3554
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3347
diff
changeset
|
653 |
pass # this is a vocabulary constraint, not enforce XXX why? |
1451 | 654 |
|
0 | 655 |
def __str__(self): |
6232
d992176d17b7
[schema] ensure DISTINCT is displayed for constraint using it, this ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6230
diff
changeset
|
656 |
if self.distinct_query: |
d992176d17b7
[schema] ensure DISTINCT is displayed for constraint using it, this ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6230
diff
changeset
|
657 |
selop = 'Any' |
d992176d17b7
[schema] ensure DISTINCT is displayed for constraint using it, this ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6230
diff
changeset
|
658 |
else: |
d992176d17b7
[schema] ensure DISTINCT is displayed for constraint using it, this ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6230
diff
changeset
|
659 |
selop = 'DISTINCT Any' |
d992176d17b7
[schema] ensure DISTINCT is displayed for constraint using it, this ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6230
diff
changeset
|
660 |
return '%s(%s %s WHERE %s)' % (self.__class__.__name__, selop, |
d992176d17b7
[schema] ensure DISTINCT is displayed for constraint using it, this ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6230
diff
changeset
|
661 |
self.mainvars, self.restriction) |
0 | 662 |
|
663 |
def __repr__(self): |
|
3965
94f95928f5ae
nicer __str__ and __repr__ methods on rql constraints
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3964
diff
changeset
|
664 |
return '<%s @%#x>' % (self.__str__(), id(self)) |
0 | 665 |
|
666 |
||
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
667 |
class RQLVocabularyConstraint(BaseRQLConstraint): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
668 |
"""the rql vocabulary constraint : |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
669 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
670 |
limit the proposed values to a set of entities returned by a rql query, |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
671 |
but this is not enforced at the repository level |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
672 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
673 |
restriction is additional rql restriction that will be added to |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
674 |
a predefined query, where the S and O variables respectivly represent |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
675 |
the subject and the object of the relation |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
676 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
677 |
mainvars is a string that should be used as selection variable (eg |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
678 |
`'Any %s WHERE ...' % mainvars`). If not specified, an attempt will be |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
679 |
done to guess it according to variable used in the expression. |
0 | 680 |
""" |
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
681 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
682 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
683 |
class RepoEnforcedRQLConstraintMixIn(object): |
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
684 |
|
3964
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
685 |
def __init__(self, restriction, mainvars=None, msg=None): |
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
686 |
super(RepoEnforcedRQLConstraintMixIn, self).__init__(restriction, mainvars) |
3964
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
687 |
self.msg = msg |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
688 |
|
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
689 |
def serialize(self): |
3968
e8dbad65a7a2
fix format string
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3967
diff
changeset
|
690 |
# start with a semicolon for bw compat, see below |
e8dbad65a7a2
fix format string
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3967
diff
changeset
|
691 |
return ';%s;%s\n%s' % (self.mainvars, self.restriction, |
3964
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
692 |
self.msg or '') |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
693 |
|
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
694 |
def deserialize(cls, value): |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
695 |
# XXX < 3.5.10 bw compat |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
696 |
if not value.startswith(';'): |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
697 |
return cls(value) |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
698 |
value, msg = value.split('\n', 1) |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
699 |
_, mainvars, restriction = value.split(';', 2) |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
700 |
return cls(restriction, mainvars, msg) |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
701 |
deserialize = classmethod(deserialize) |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
702 |
|
3554
26e586f3c15c
[schema] make RQL* constraints usable w/ attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3347
diff
changeset
|
703 |
def repo_check(self, session, eidfrom, rtype, eidto=None): |
0 | 704 |
"""raise ValidationError if the relation doesn't satisfy the constraint |
705 |
""" |
|
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
706 |
if not self.match_condition(session, eidfrom, eidto): |
3964
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
707 |
# XXX at this point if both or neither of S and O are in mainvar we |
6109
47d9c0e0f7b7
integrate Celso's work on translation file: proper/complete spanish translation, fixed some typos in french translation, occured -> occurred fix in various places
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6017
diff
changeset
|
708 |
# dunno if the validation error `occurred` on eidfrom or eidto (from |
3964
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
709 |
# user interface point of view) |
5030
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4913
diff
changeset
|
710 |
# |
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4913
diff
changeset
|
711 |
# possible enhancement: check entity being created, it's probably |
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4913
diff
changeset
|
712 |
# the main eid unless this is a composite relation |
3964
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
713 |
if eidto is None or 'S' in self.mainvars or not 'O' in self.mainvars: |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
714 |
maineid = eidfrom |
5030
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4913
diff
changeset
|
715 |
qname = role_name(rtype, 'subject') |
3964
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
716 |
else: |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
717 |
maineid = eidto |
5030
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4913
diff
changeset
|
718 |
qname = role_name(rtype, 'object') |
3964
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
719 |
if self.msg: |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
720 |
msg = session._(self.msg) |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
721 |
else: |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
722 |
msg = '%(constraint)s %(restriction)s failed' % { |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
723 |
'constraint': session._(self.type()), |
21387ffb0731
refactor so that we can specify an extra argument to constraints which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3963
diff
changeset
|
724 |
'restriction': self.restriction} |
5030
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4913
diff
changeset
|
725 |
raise ValidationError(maineid, {qname: msg}) |
0 | 726 |
|
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
727 |
def exec_query(self, session, eidfrom, eidto): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
728 |
if eidto is None: |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
729 |
# checking constraint for an attribute relation |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
730 |
restriction = 'S eid %(s)s, ' + self.restriction |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
731 |
args = {'s': eidfrom} |
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
732 |
else: |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
733 |
restriction = 'S eid %(s)s, O eid %(o)s, ' + self.restriction |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
734 |
args = {'s': eidfrom, 'o': eidto} |
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
735 |
rql = 'Any %s WHERE %s' % (self.mainvars, restriction) |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
736 |
if self.distinct_query: |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
737 |
rql = 'DISTINCT ' + rql |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
738 |
return session.execute(rql, args, build_descr=False) |
0 | 739 |
|
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
740 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
741 |
class RQLConstraint(RepoEnforcedRQLConstraintMixIn, RQLVocabularyConstraint): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
742 |
"""the rql constraint is similar to the RQLVocabularyConstraint but |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
743 |
are also enforced at the repository level |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
744 |
""" |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
745 |
distinct_query = False |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
746 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
747 |
def match_condition(self, session, eidfrom, eidto): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
748 |
return self.exec_query(session, eidfrom, eidto) |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
749 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
750 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
751 |
class RQLUniqueConstraint(RepoEnforcedRQLConstraintMixIn, BaseRQLConstraint): |
0 | 752 |
"""the unique rql constraint check that the result of the query isn't |
6233
612b7d947a4c
[schema] turn comment into docstring for RQLUniqueConstraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6232
diff
changeset
|
753 |
greater than one. |
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
754 |
|
6233
612b7d947a4c
[schema] turn comment into docstring for RQLUniqueConstraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6232
diff
changeset
|
755 |
You *must* specify mainvars when instantiating the constraint since there is |
612b7d947a4c
[schema] turn comment into docstring for RQLUniqueConstraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6232
diff
changeset
|
756 |
no way to guess it correctly (e.g. if using S,O or U the constraint will |
612b7d947a4c
[schema] turn comment into docstring for RQLUniqueConstraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6232
diff
changeset
|
757 |
always be satisfied because we've to use a DISTINCT query). |
612b7d947a4c
[schema] turn comment into docstring for RQLUniqueConstraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6232
diff
changeset
|
758 |
""" |
612b7d947a4c
[schema] turn comment into docstring for RQLUniqueConstraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6232
diff
changeset
|
759 |
# XXX turns mainvars into a required argument in __init__ |
612b7d947a4c
[schema] turn comment into docstring for RQLUniqueConstraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6232
diff
changeset
|
760 |
distinct_query = True |
3985
d5bf894fcf02
add some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3980
diff
changeset
|
761 |
|
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
762 |
def match_condition(self, session, eidfrom, eidto): |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
763 |
return len(self.exec_query(session, eidfrom, eidto)) <= 1 |
0 | 764 |
|
765 |
||
766 |
class RQLExpression(object): |
|
767 |
def __init__(self, expression, mainvars, eid): |
|
768 |
self.eid = eid # eid of the entity representing this rql expression |
|
769 |
if not isinstance(mainvars, unicode): |
|
770 |
mainvars = unicode(mainvars) |
|
771 |
self.mainvars = mainvars |
|
772 |
self.expression = normalize_expression(expression) |
|
773 |
try: |
|
774 |
self.rqlst = parse(self.full_rql, print_errors=False).children[0] |
|
775 |
except RQLSyntaxError: |
|
776 |
raise RQLSyntaxError(expression) |
|
777 |
for mainvar in mainvars.split(','): |
|
778 |
if len(self.rqlst.defined_vars[mainvar].references()) <= 2: |
|
2142
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
779 |
_LOGGER.warn('You did not use the %s variable in your RQL ' |
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
780 |
'expression %s', mainvar, self) |
3825 | 781 |
# syntax tree used by read security (inserted in queries when necessary) |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
782 |
self.snippet_rqlst = parse(self.minimal_rql, print_errors=False).children[0] |
1451 | 783 |
|
0 | 784 |
def __str__(self): |
785 |
return self.full_rql |
|
786 |
def __repr__(self): |
|
787 |
return '%s(%s)' % (self.__class__.__name__, self.full_rql) |
|
1451 | 788 |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
789 |
def __cmp__(self, other): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
790 |
if hasattr(other, 'expression'): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
791 |
return cmp(other.expression, self.expression) |
4066
0555f170c4d1
__cmp__ should *NOT* return False when different
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4045
diff
changeset
|
792 |
return -1 |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
793 |
|
0 | 794 |
def __deepcopy__(self, memo): |
795 |
return self.__class__(self.expression, self.mainvars) |
|
796 |
def __getstate__(self): |
|
797 |
return (self.expression, self.mainvars) |
|
798 |
def __setstate__(self, state): |
|
799 |
self.__init__(*state) |
|
1451 | 800 |
|
0 | 801 |
@cached |
802 |
def transform_has_permission(self): |
|
803 |
found = None |
|
804 |
rqlst = self.rqlst |
|
805 |
for var in rqlst.defined_vars.itervalues(): |
|
806 |
for varref in var.references(): |
|
807 |
rel = varref.relation() |
|
808 |
if rel is None: |
|
809 |
continue |
|
810 |
try: |
|
811 |
prefix, action, suffix = rel.r_type.split('_') |
|
812 |
except ValueError: |
|
813 |
continue |
|
814 |
if prefix != 'has' or suffix != 'permission' or \ |
|
815 |
not action in ('add', 'delete', 'update', 'read'): |
|
816 |
continue |
|
817 |
if found is None: |
|
818 |
found = [] |
|
819 |
rqlst.save_state() |
|
820 |
assert rel.children[0].name == 'U' |
|
821 |
objvar = rel.children[1].children[0].variable |
|
822 |
rqlst.remove_node(rel) |
|
823 |
selected = [v.name for v in rqlst.get_selected_variables()] |
|
824 |
if objvar.name not in selected: |
|
825 |
colindex = len(selected) |
|
826 |
rqlst.add_selected(objvar) |
|
827 |
else: |
|
828 |
colindex = selected.index(objvar.name) |
|
4717
535705688f4f
proper deprecation warning when import class that should be imported from yams
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4702
diff
changeset
|
829 |
found.append((action, colindex)) |
0 | 830 |
# remove U eid %(u)s if U is not used in any other relation |
831 |
uvrefs = rqlst.defined_vars['U'].references() |
|
832 |
if len(uvrefs) == 1: |
|
833 |
rqlst.remove_node(uvrefs[0].relation()) |
|
834 |
if found is not None: |
|
835 |
rql = rqlst.as_string() |
|
836 |
if len(rqlst.selection) == 1 and isinstance(rqlst.where, nodes.Relation): |
|
837 |
# only "Any X WHERE X eid %(x)s" remaining, no need to execute the rql |
|
838 |
keyarg = rqlst.selection[0].name.lower() |
|
839 |
else: |
|
840 |
keyarg = None |
|
841 |
rqlst.recover() |
|
842 |
return rql, found, keyarg |
|
843 |
return rqlst.as_string(), None, None |
|
1451 | 844 |
|
0 | 845 |
def _check(self, session, **kwargs): |
846 |
"""return True if the rql expression is matching the given relation |
|
847 |
between fromeid and toeid |
|
848 |
||
849 |
session may actually be a request as well |
|
850 |
""" |
|
4607
55eab66c6592
[schema security] fix so that when cheking attributes perms for an entity being created, 'owners' and has_*_permission in erqlexpr are considered satisfied
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4586
diff
changeset
|
851 |
creating = kwargs.get('creating') |
55eab66c6592
[schema security] fix so that when cheking attributes perms for an entity being created, 'owners' and has_*_permission in erqlexpr are considered satisfied
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4586
diff
changeset
|
852 |
if not creating and self.eid is not None: |
0 | 853 |
key = (self.eid, tuple(sorted(kwargs.iteritems()))) |
854 |
try: |
|
855 |
return session.local_perm_cache[key] |
|
856 |
except KeyError: |
|
857 |
pass |
|
858 |
rql, has_perm_defs, keyarg = self.transform_has_permission() |
|
5703
24ca7615379b
[security] consider any rql expression refering to X while X is being created as satisfied
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5531
diff
changeset
|
859 |
# when creating an entity, expression related to X satisfied |
24ca7615379b
[security] consider any rql expression refering to X while X is being created as satisfied
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5531
diff
changeset
|
860 |
if creating and 'X' in self.rqlst.defined_vars: |
24ca7615379b
[security] consider any rql expression refering to X while X is being created as satisfied
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5531
diff
changeset
|
861 |
return True |
0 | 862 |
if keyarg is None: |
4619
f4254586e867
[security] allow to call .check on rql expression with a user eid specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4608
diff
changeset
|
863 |
kwargs.setdefault('u', session.user.eid) |
0 | 864 |
try: |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5122
diff
changeset
|
865 |
rset = session.execute(rql, kwargs, build_descr=True) |
0 | 866 |
except NotImplementedError: |
867 |
self.critical('cant check rql expression, unsupported rql %s', rql) |
|
868 |
if self.eid is not None: |
|
869 |
session.local_perm_cache[key] = False |
|
870 |
return False |
|
871 |
except TypeResolverException, ex: |
|
872 |
# some expression may not be resolvable with current kwargs |
|
873 |
# (type conflict) |
|
874 |
self.warning('%s: %s', rql, str(ex)) |
|
875 |
if self.eid is not None: |
|
876 |
session.local_perm_cache[key] = False |
|
877 |
return False |
|
5531
0ce62ac0d1d7
catch unauthorized when executing rql to check an rql expression. Fix #968824
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
878 |
except Unauthorized, ex: |
0ce62ac0d1d7
catch unauthorized when executing rql to check an rql expression. Fix #968824
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
879 |
self.debug('unauthorized %s: %s', rql, str(ex)) |
0ce62ac0d1d7
catch unauthorized when executing rql to check an rql expression. Fix #968824
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
880 |
if self.eid is not None: |
0ce62ac0d1d7
catch unauthorized when executing rql to check an rql expression. Fix #968824
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
881 |
session.local_perm_cache[key] = False |
0ce62ac0d1d7
catch unauthorized when executing rql to check an rql expression. Fix #968824
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
882 |
return False |
0 | 883 |
else: |
884 |
rset = session.eid_rset(kwargs[keyarg]) |
|
885 |
# if no special has_*_permission relation in the rql expression, just |
|
886 |
# check the result set contains something |
|
887 |
if has_perm_defs is None: |
|
888 |
if rset: |
|
889 |
if self.eid is not None: |
|
890 |
session.local_perm_cache[key] = True |
|
891 |
return True |
|
892 |
elif rset: |
|
893 |
# check every special has_*_permission relation is satisfied |
|
894 |
get_eschema = session.vreg.schema.eschema |
|
895 |
try: |
|
4717
535705688f4f
proper deprecation warning when import class that should be imported from yams
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4702
diff
changeset
|
896 |
for eaction, col in has_perm_defs: |
0 | 897 |
for i in xrange(len(rset)): |
898 |
eschema = get_eschema(rset.description[i][col]) |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
899 |
eschema.check_perm(session, eaction, eid=rset[i][col]) |
0 | 900 |
if self.eid is not None: |
901 |
session.local_perm_cache[key] = True |
|
902 |
return True |
|
903 |
except Unauthorized: |
|
904 |
pass |
|
905 |
if self.eid is not None: |
|
906 |
session.local_perm_cache[key] = False |
|
907 |
return False |
|
1451 | 908 |
|
0 | 909 |
@property |
910 |
def minimal_rql(self): |
|
911 |
return 'Any %s WHERE %s' % (self.mainvars, self.expression) |
|
912 |
||
913 |
||
914 |
class ERQLExpression(RQLExpression): |
|
915 |
def __init__(self, expression, mainvars=None, eid=None): |
|
916 |
RQLExpression.__init__(self, expression, mainvars or 'X', eid) |
|
917 |
||
918 |
@property |
|
919 |
def full_rql(self): |
|
920 |
rql = self.minimal_rql |
|
921 |
rqlst = getattr(self, 'rqlst', None) # may be not set yet |
|
922 |
if rqlst is not None: |
|
923 |
defined = rqlst.defined_vars |
|
924 |
else: |
|
925 |
defined = set(split_expression(self.expression)) |
|
926 |
if 'X' in defined: |
|
927 |
rql += ', X eid %(x)s' |
|
928 |
if 'U' in defined: |
|
929 |
rql += ', U eid %(u)s' |
|
930 |
return rql |
|
1451 | 931 |
|
4619
f4254586e867
[security] allow to call .check on rql expression with a user eid specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4608
diff
changeset
|
932 |
def check(self, session, eid=None, creating=False, **kwargs): |
0 | 933 |
if 'X' in self.rqlst.defined_vars: |
934 |
if eid is None: |
|
4607
55eab66c6592
[schema security] fix so that when cheking attributes perms for an entity being created, 'owners' and has_*_permission in erqlexpr are considered satisfied
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4586
diff
changeset
|
935 |
if creating: |
4619
f4254586e867
[security] allow to call .check on rql expression with a user eid specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4608
diff
changeset
|
936 |
return self._check(session, creating=True, **kwargs) |
0 | 937 |
return False |
4607
55eab66c6592
[schema security] fix so that when cheking attributes perms for an entity being created, 'owners' and has_*_permission in erqlexpr are considered satisfied
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4586
diff
changeset
|
938 |
assert creating == False |
4619
f4254586e867
[security] allow to call .check on rql expression with a user eid specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4608
diff
changeset
|
939 |
return self._check(session, x=eid, **kwargs) |
f4254586e867
[security] allow to call .check on rql expression with a user eid specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4608
diff
changeset
|
940 |
return self._check(session, **kwargs) |
1451 | 941 |
|
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
942 |
|
0 | 943 |
class RRQLExpression(RQLExpression): |
944 |
def __init__(self, expression, mainvars=None, eid=None): |
|
945 |
if mainvars is None: |
|
3961
d1cbf77db999
fix RQLUniqueConstraint behaviour by using a DISTINCT query and allowing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3827
diff
changeset
|
946 |
mainvars = guess_rrqlexpr_mainvars(expression) |
0 | 947 |
RQLExpression.__init__(self, expression, mainvars, eid) |
3827 | 948 |
# graph of links between variable, used by rql rewriter |
3826
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
949 |
self.vargraph = {} |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
950 |
for relation in self.rqlst.get_nodes(nodes.Relation): |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
951 |
try: |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
952 |
rhsvarname = relation.children[1].children[0].variable.name |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
953 |
lhsvarname = relation.children[0].name |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
954 |
except AttributeError: |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
955 |
pass |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
956 |
else: |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
957 |
self.vargraph.setdefault(lhsvarname, []).append(rhsvarname) |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
958 |
self.vargraph.setdefault(rhsvarname, []).append(lhsvarname) |
0c0c051863cb
close #511810: bad rql generated when looking for vocabulary for a relation on an entity which doesn't exist (yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3825
diff
changeset
|
959 |
#self.vargraph[(lhsvarname, rhsvarname)] = relation.r_type |
0 | 960 |
|
961 |
@property |
|
962 |
def full_rql(self): |
|
963 |
rql = self.minimal_rql |
|
964 |
rqlst = getattr(self, 'rqlst', None) # may be not set yet |
|
965 |
if rqlst is not None: |
|
966 |
defined = rqlst.defined_vars |
|
967 |
else: |
|
968 |
defined = set(split_expression(self.expression)) |
|
969 |
if 'S' in defined: |
|
970 |
rql += ', S eid %(s)s' |
|
971 |
if 'O' in defined: |
|
972 |
rql += ', O eid %(o)s' |
|
973 |
if 'U' in defined: |
|
974 |
rql += ', U eid %(u)s' |
|
975 |
return rql |
|
1451 | 976 |
|
0 | 977 |
def check(self, session, fromeid=None, toeid=None): |
978 |
kwargs = {} |
|
979 |
if 'S' in self.rqlst.defined_vars: |
|
980 |
if fromeid is None: |
|
981 |
return False |
|
982 |
kwargs['s'] = fromeid |
|
983 |
if 'O' in self.rqlst.defined_vars: |
|
984 |
if toeid is None: |
|
985 |
return False |
|
986 |
kwargs['o'] = toeid |
|
987 |
return self._check(session, **kwargs) |
|
1451 | 988 |
|
4570
ede247bbbf62
follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
989 |
# in yams, default 'update' perm for attributes granted to managers and owners. |
ede247bbbf62
follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
990 |
# Within cw, we want to default to users who may edit the entity holding the |
ede247bbbf62
follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
991 |
# attribute. |
4586
440e340c61fe
DEFAULT_ATTRPERMS is now public
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4575
diff
changeset
|
992 |
ybo.DEFAULT_ATTRPERMS['update'] = ( |
4570
ede247bbbf62
follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
993 |
'managers', ERQLExpression('U has_update_permission X')) |
0 | 994 |
|
629
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
995 |
# workflow extensions ######################################################### |
2926
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
996 |
|
2460
ce1a7ffc6c90
fix a couple NameErrors
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2459
diff
changeset
|
997 |
from yams.buildobjs import _add_relation as yams_add_relation |
629
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
998 |
|
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
999 |
class workflowable_definition(ybo.metadefinition): |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
1000 |
"""extends default EntityType's metaclass to add workflow relations |
5378
0f54a0e128ac
[schema] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5372
diff
changeset
|
1001 |
(i.e. in_state, wf_info_for and custom_workflow). This is the default |
0f54a0e128ac
[schema] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5372
diff
changeset
|
1002 |
metaclass for WorkflowableEntityType. |
629
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
1003 |
""" |
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
1004 |
def __new__(mcs, name, bases, classdict): |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
1005 |
abstract = classdict.pop('__abstract__', False) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
1006 |
cls = super(workflowable_definition, mcs).__new__(mcs, name, bases, |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
1007 |
classdict) |
629
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
1008 |
if not abstract: |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
1009 |
make_workflowable(cls) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
1010 |
return cls |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
1011 |
|
5379
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1012 |
class WorkflowableEntityType(ybo.EntityType): |
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1013 |
"""Use this base class instead of :class:`EntityType` to have workflow |
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1014 |
relations (i.e. `in_state`, `wf_info_for` and `custom_workflow`) on your |
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1015 |
entity type. |
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1016 |
""" |
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1017 |
__metaclass__ = workflowable_definition |
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1018 |
__abstract__ = True |
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1019 |
|
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1020 |
|
2957
2daabf4c646e
[schema] make_workflowable now take an option description for the in_state relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2931
diff
changeset
|
1021 |
def make_workflowable(cls, in_state_descr=None): |
5378
0f54a0e128ac
[schema] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5372
diff
changeset
|
1022 |
"""Adds workflow relations as :class:`WorkflowableEntityType`, but usable on |
0f54a0e128ac
[schema] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5372
diff
changeset
|
1023 |
existing classes which are not using that base class. |
0f54a0e128ac
[schema] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5372
diff
changeset
|
1024 |
""" |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
1025 |
existing_rels = set(rdef.name for rdef in cls.__relations__) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
1026 |
# let relation types defined in cw.schemas.workflow carrying |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
1027 |
# cardinality, constraints and other relation definition properties |
5379
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1028 |
etype = getattr(cls, 'name', cls.__name__) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
1029 |
if 'custom_workflow' not in existing_rels: |
5379
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1030 |
rdef = ybo.RelationDefinition(etype, 'custom_workflow', 'Workflow') |
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1031 |
yams_add_relation(cls.__relations__, rdef) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
1032 |
if 'in_state' not in existing_rels: |
5379
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1033 |
rdef = ybo.RelationDefinition(etype, 'in_state', 'State', |
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1034 |
description=in_state_descr) |
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1035 |
yams_add_relation(cls.__relations__, rdef) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2782
diff
changeset
|
1036 |
if 'wf_info_for' not in existing_rels: |
5379
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1037 |
rdef = ybo.RelationDefinition('TrInfo', 'wf_info_for', etype) |
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5378
diff
changeset
|
1038 |
yams_add_relation(cls.__relations__, rdef) |
1451 | 1039 |
|
629
59b6542f5729
provide a new WorkflowableEntityType base class (will be refactored later, maybe with schema interfaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
628
diff
changeset
|
1040 |
|
0 | 1041 |
# schema loading ############################################################## |
1042 |
||
1043 |
CONSTRAINTS['RQLConstraint'] = RQLConstraint |
|
1044 |
CONSTRAINTS['RQLUniqueConstraint'] = RQLUniqueConstraint |
|
1045 |
CONSTRAINTS['RQLVocabularyConstraint'] = RQLVocabularyConstraint |
|
3980
b17cf4a2e09e
this constraint is actually unusable in yams schema for cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3979
diff
changeset
|
1046 |
CONSTRAINTS.pop('MultipleStaticVocabularyConstraint', None) # don't want this in cw yams schema |
0 | 1047 |
PyFileReader.context.update(CONSTRAINTS) |
1048 |
||
1049 |
||
1050 |
class BootstrapSchemaLoader(SchemaLoader): |
|
1051 |
"""cubicweb specific schema loader, loading only schema necessary to read |
|
1052 |
the persistent schema |
|
1053 |
""" |
|
1054 |
schemacls = CubicWebSchema |
|
1055 |
||
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
1056 |
def load(self, config, path=(), **kwargs): |
0 | 1057 |
"""return a Schema instance from the schema definition read |
1058 |
from <directory> |
|
1059 |
""" |
|
1060 |
return super(BootstrapSchemaLoader, self).load( |
|
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
1061 |
path, config.appid, register_base_types=False, **kwargs) |
1451 | 1062 |
|
0 | 1063 |
def _load_definition_files(self, cubes=None): |
1064 |
# bootstraping, ignore cubes |
|
2735
39c942241b48
no need for lib_directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2730
diff
changeset
|
1065 |
filepath = join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'bootstrap.py') |
2142
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
1066 |
self.info('loading %s', filepath) |
098aa2075903
include_schema_files is useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
1067 |
self.handle_file(filepath) |
1451 | 1068 |
|
0 | 1069 |
def unhandled_file(self, filepath): |
1070 |
"""called when a file without handler associated has been found""" |
|
1071 |
self.warning('ignoring file %r', filepath) |
|
1072 |
||
1073 |
||
1074 |
class CubicWebSchemaLoader(BootstrapSchemaLoader): |
|
1075 |
"""cubicweb specific schema loader, automatically adding metadata to the |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2460
diff
changeset
|
1076 |
instance's schema |
0 | 1077 |
""" |
1078 |
||
1034
0356bbfb2f26
fix to pass arguments to base class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
1079 |
def load(self, config, **kwargs): |
0 | 1080 |
"""return a Schema instance from the schema definition read |
1081 |
from <directory> |
|
1082 |
""" |
|
1083 |
self.info('loading %s schemas', ', '.join(config.cubes())) |
|
2782
998f24dabd0d
compute extrapath, necessary for proper schema module detection when CW_CUBES_PATH is set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2742
diff
changeset
|
1084 |
self.extrapath = {} |
998f24dabd0d
compute extrapath, necessary for proper schema module detection when CW_CUBES_PATH is set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2742
diff
changeset
|
1085 |
for cubesdir in config.cubes_search_path(): |
998f24dabd0d
compute extrapath, necessary for proper schema module detection when CW_CUBES_PATH is set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2742
diff
changeset
|
1086 |
if cubesdir != config.CUBES_DIR: |
998f24dabd0d
compute extrapath, necessary for proper schema module detection when CW_CUBES_PATH is set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2742
diff
changeset
|
1087 |
self.extrapath[cubesdir] = 'cubes' |
372
a8a975a88368
check apphome is not None
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
1088 |
if config.apphome: |
2598
a66fe74bd9fc
[R schema] dont give an iterator, it's a trap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1089 |
path = tuple(reversed([config.apphome] + config.cubes_path())) |
372
a8a975a88368
check apphome is not None
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
1090 |
else: |
2598
a66fe74bd9fc
[R schema] dont give an iterator, it's a trap
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1091 |
path = tuple(reversed(config.cubes_path())) |
2526
40e41eb34a7d
we've to call cleanup_sys_modules manually for proper schema modules cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
1092 |
try: |
40e41eb34a7d
we've to call cleanup_sys_modules manually for proper schema modules cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
1093 |
return super(CubicWebSchemaLoader, self).load(config, path=path, **kwargs) |
40e41eb34a7d
we've to call cleanup_sys_modules manually for proper schema modules cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
1094 |
finally: |
40e41eb34a7d
we've to call cleanup_sys_modules manually for proper schema modules cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
1095 |
# we've to cleanup modules imported from cubicweb.schemas as well |
2735
39c942241b48
no need for lib_directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2730
diff
changeset
|
1096 |
cleanup_sys_modules([join(cubicweb.CW_SOFTWARE_ROOT, 'schemas')]) |
0 | 1097 |
|
1098 |
def _load_definition_files(self, cubes): |
|
2741
148d0cea1fb5
[schema] oops
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2736
diff
changeset
|
1099 |
for filepath in (join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'bootstrap.py'), |
148d0cea1fb5
[schema] oops
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2736
diff
changeset
|
1100 |
join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'base.py'), |
148d0cea1fb5
[schema] oops
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2736
diff
changeset
|
1101 |
join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'workflow.py'), |
148d0cea1fb5
[schema] oops
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2736
diff
changeset
|
1102 |
join(cubicweb.CW_SOFTWARE_ROOT, 'schemas', 'Bookmark.py')): |
0 | 1103 |
self.info('loading %s', filepath) |
1104 |
self.handle_file(filepath) |
|
1105 |
for cube in cubes: |
|
1106 |
for filepath in self.get_schema_files(cube): |
|
1107 |
self.info('loading %s', filepath) |
|
1108 |
self.handle_file(filepath) |
|
1109 |
||
1110 |
||
2459
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
1111 |
set_log_methods(CubicWebSchemaLoader, getLogger('cubicweb.schemaloader')) |
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
1112 |
set_log_methods(BootstrapSchemaLoader, getLogger('cubicweb.bootstrapschemaloader')) |
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
1113 |
set_log_methods(RQLExpression, getLogger('cubicweb.schema')) |
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
1114 |
|
0 | 1115 |
# _() is just there to add messages to the catalog, don't care about actual |
1116 |
# translation |
|
1117 |
PERM_USE_TEMPLATE_FORMAT = _('use_template_format') |
|
2459
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
1118 |
NEED_PERM_FORMATS = [_('text/cubicweb-page-template')] |
0 | 1119 |
|
2531
531ea4e7013e
[cleanup] nicer imports
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2526
diff
changeset
|
1120 |
@monkeypatch(FormatConstraint) |
3347
428f95118556
fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3287
diff
changeset
|
1121 |
def vocabulary(self, entity=None, form=None): |
3401
f893f4f199fc
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
1122 |
cw = None |
3347
428f95118556
fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3287
diff
changeset
|
1123 |
if form is None and entity is not None: |
3401
f893f4f199fc
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
1124 |
cw = entity._cw |
3347
428f95118556
fix vocab param to avoid deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3287
diff
changeset
|
1125 |
elif form is not None: |
3401
f893f4f199fc
use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
1126 |
cw = form._cw |
4757
5d937aeaa6fb
do not check for use template permission when session is super session or integrity hooks are deactivated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4755
diff
changeset
|
1127 |
if cw is not None: |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
1128 |
if hasattr(cw, 'write_security'): # test it's a session and not a request |
4757
5d937aeaa6fb
do not check for use template permission when session is super session or integrity hooks are deactivated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4755
diff
changeset
|
1129 |
# cw is a server session |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
1130 |
hasperm = not cw.write_security or \ |
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1131 |
not cw.is_hook_category_activated('integrity') or \ |
4757
5d937aeaa6fb
do not check for use template permission when session is super session or integrity hooks are deactivated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4755
diff
changeset
|
1132 |
cw.user.has_permission(PERM_USE_TEMPLATE_FORMAT) |
5d937aeaa6fb
do not check for use template permission when session is super session or integrity hooks are deactivated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4755
diff
changeset
|
1133 |
else: |
5d937aeaa6fb
do not check for use template permission when session is super session or integrity hooks are deactivated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4755
diff
changeset
|
1134 |
hasperm = cw.user.has_permission(PERM_USE_TEMPLATE_FORMAT) |
5d937aeaa6fb
do not check for use template permission when session is super session or integrity hooks are deactivated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4755
diff
changeset
|
1135 |
if hasperm: |
5d937aeaa6fb
do not check for use template permission when session is super session or integrity hooks are deactivated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4755
diff
changeset
|
1136 |
return self.regular_formats + tuple(NEED_PERM_FORMATS) |
2459
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
1137 |
return self.regular_formats |
0 | 1138 |
|
1139 |
# XXX monkey patch PyFileReader.import_erschema until bw_normalize_etype is |
|
1140 |
# necessary |
|
1141 |
orig_import_erschema = PyFileReader.import_erschema |
|
1142 |
def bw_import_erschema(self, ertype, schemamod=None, instantiate=True): |
|
1143 |
return orig_import_erschema(self, bw_normalize_etype(ertype), schemamod, instantiate) |
|
1144 |
PyFileReader.import_erschema = bw_import_erschema |
|
1451 | 1145 |
|
0 | 1146 |
# XXX itou for some Statement methods |
1147 |
from rql import stmts |
|
1148 |
orig_get_etype = stmts.ScopeNode.get_etype |
|
1149 |
def bw_get_etype(self, name): |
|
1150 |
return orig_get_etype(self, bw_normalize_etype(name)) |
|
1151 |
stmts.ScopeNode.get_etype = bw_get_etype |
|
1152 |
||
1153 |
orig_add_main_variable_delete = stmts.Delete.add_main_variable |
|
1154 |
def bw_add_main_variable_delete(self, etype, vref): |
|
1155 |
return orig_add_main_variable_delete(self, bw_normalize_etype(etype), vref) |
|
1156 |
stmts.Delete.add_main_variable = bw_add_main_variable_delete |
|
1157 |
||
1158 |
orig_add_main_variable_insert = stmts.Insert.add_main_variable |
|
1159 |
def bw_add_main_variable_insert(self, etype, vref): |
|
1160 |
return orig_add_main_variable_insert(self, bw_normalize_etype(etype), vref) |
|
1161 |
stmts.Insert.add_main_variable = bw_add_main_variable_insert |
|
1162 |
||
1163 |
orig_set_statement_type = stmts.Select.set_statement_type |
|
1164 |
def bw_set_statement_type(self, etype): |
|
1165 |
return orig_set_statement_type(self, bw_normalize_etype(etype)) |
|
1166 |
stmts.Select.set_statement_type = bw_set_statement_type |
|
2736
c5048502409f
restore backward compat, still imported a lot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2735
diff
changeset
|
1167 |
|
c5048502409f
restore backward compat, still imported a lot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2735
diff
changeset
|
1168 |
# XXX deprecated |
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
1169 |
|
2736
c5048502409f
restore backward compat, still imported a lot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2735
diff
changeset
|
1170 |
from yams.buildobjs import RichString |
4717
535705688f4f
proper deprecation warning when import class that should be imported from yams
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4702
diff
changeset
|
1171 |
from yams.constraints import StaticVocabularyConstraint |
535705688f4f
proper deprecation warning when import class that should be imported from yams
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4702
diff
changeset
|
1172 |
|
535705688f4f
proper deprecation warning when import class that should be imported from yams
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4702
diff
changeset
|
1173 |
RichString = class_moved(RichString) |
535705688f4f
proper deprecation warning when import class that should be imported from yams
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4702
diff
changeset
|
1174 |
|
535705688f4f
proper deprecation warning when import class that should be imported from yams
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4702
diff
changeset
|
1175 |
StaticVocabularyConstraint = class_moved(StaticVocabularyConstraint) |
535705688f4f
proper deprecation warning when import class that should be imported from yams
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4702
diff
changeset
|
1176 |
FormatConstraint = class_moved(FormatConstraint) |
3978
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
1177 |
|
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
1178 |
PyFileReader.context['ERQLExpression'] = yobsolete(ERQLExpression) |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
1179 |
PyFileReader.context['RRQLExpression'] = yobsolete(RRQLExpression) |
2c95e3033f64
finish yesterday work on rql constraints:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3968
diff
changeset
|
1180 |
PyFileReader.context['WorkflowableEntityType'] = WorkflowableEntityType |