1111 Any ,
1212 Callable ,
1313 ClassVar ,
14- Generic ,
1514 Iterable ,
1615 Optional ,
1716 TypeVar ,
1817 cast ,
1918)
2019from uuid import UUID , uuid4
2120
22- from jaclang .compiler .constant import EdgeDir , T
21+ from jaclang .compiler .constant import EdgeDir
2322from jaclang .core .utils import collect_node_connections
2423from jaclang .vendor .orjson import dumps
2524
@@ -53,60 +52,50 @@ class AnchorType(Enum):
5352
5453
5554@dataclass
56- class Access ( Generic [ T ]) :
55+ class Access :
5756 """Access Structure."""
5857
5958 whitelist : bool = True # whitelist or blacklist
60- level : tuple [set [T ], set [T ], set [T ]] = field (
61- default_factory = lambda : (set (), set (), set ())
62- ) # index 0 == read access, 1 == write access
59+ anchors : dict [Anchor , int ] = field (default_factory = dict )
6360
6461 def check (
65- self , id : T
62+ self , anchor : Anchor
6663 ) -> tuple [bool , int ]: # whitelist or blacklist, has_read_access, level
6764 """Validate access."""
6865 if self .whitelist :
69- for i in range (2 , - 1 , - 1 ):
70- if id in self .level [i ]:
71- return self .whitelist , i
72- return self .whitelist , - 1
66+ return self .whitelist , self .anchors .get (anchor , - 1 )
7367 else :
74- access = - 1
75- for i in range (0 , 3 , 1 ):
76- if id in self .level [i ]:
77- break
78- access = i
79- return self .whitelist , access
68+ return self .whitelist , self .anchors .get (anchor , 2 )
8069
8170 def serialize (self ) -> dict [str , object ]:
8271 """Serialize Access."""
8372 return {
8473 "whitelist" : self .whitelist ,
85- "level " : [ list ( level ) for level in self .level ] ,
74+ "anchors " : { key . ref_id : val for key , val in self .anchors . items ()} ,
8675 }
8776
8877 @classmethod
8978 def deserialize (cls , data : dict [str , Any ]) -> Access :
9079 """Deserialize Access."""
91- level = cast (list [ list [ T ]] , data .get ("level " ))
80+ anchors = cast (dict [ str , int ] , data .get ("anchors " ))
9281 return Access (
9382 whitelist = bool (data .get ("whitelist" )),
94- level = (
95- ( set ( level [ 0 ]), set ( level [ 1 ]), set ( level [ 2 ]))
96- if level
97- else ( set (), set (), set ( ))
98- ) ,
83+ anchors = {
84+ anchor : val
85+ for key , val in anchors . items ()
86+ if ( anchor := Anchor . ref ( key ))
87+ } ,
9988 )
10089
10190
10291@dataclass
103- class Permission ( Generic [ T ]) :
92+ class Permission :
10493 """Anchor Access Handler."""
10594
10695 all : int = - 1
107- roots : Access [ T ] = field (default_factory = Access [ T ] )
108- # types: dict[type[Architype], Access[T] ] = field(default_factory=dict)
109- # nodes: Access[T] = field(default_factory=Access[T] )
96+ roots : Access = field (default_factory = Access )
97+ # types: dict[type[Architype], Access] = field(default_factory=dict)
98+ # nodes: Access = field(default_factory=Access)
11099
111100 def serialize (self ) -> dict [str , object ]:
112101 """Serialize Permission."""
@@ -147,7 +136,7 @@ class Anchor:
147136 name : str = ""
148137 id : UUID = field (default_factory = uuid4 )
149138 root : Optional [UUID ] = None
150- access : Permission [ UUID ] = field (default_factory = Permission [ UUID ] )
139+ access : Permission = field (default_factory = Permission )
151140 architype : Optional [Architype ] = None
152141 connected : bool = False
153142 current_access_level : int = - 1
@@ -248,7 +237,7 @@ def access_level(self, to: Anchor) -> int:
248237 if (to_access := to .access ).all > - 1 :
249238 to .current_access_level = to_access .all
250239
251- # whitelist, level = to_access.nodes.check(self.id )
240+ # whitelist, level = to_access.nodes.check(self)
252241 # if not whitelist and level < 0:
253242 # to.current_access_level = -1
254243 # return to.current_access_level
@@ -258,22 +247,22 @@ def access_level(self, to: Anchor) -> int:
258247 # if (architype := self.architype) and (
259248 # access_type := to_access.types.get(architype.__class__)
260249 # ):
261- # whitelist, level = access_type.check(self.id )
250+ # whitelist, level = access_type.check(self)
262251 # if not whitelist and level < 0:
263252 # to.current_access_level = -1
264253 # return to.current_access_level
265254 # elif whitelist and level > -1 and to.current_access_level == -1:
266255 # to.current_access_level = level
267256
268- whitelist , level = to_access .roots .check (jroot . id )
257+ whitelist , level = to_access .roots .check (jroot )
269258 if not whitelist and level < 0 :
270259 to .current_access_level = - 1
271260 return to .current_access_level
272261 elif whitelist and level > - 1 and to .current_access_level == - 1 :
273262 to .current_access_level = level
274263
275264 if to .root and (to_root := jctx .datasource .find_one (to .root )):
276- whitelist , level = to_root .access .roots .check (jroot . id )
265+ whitelist , level = to_root .access .roots .check (jroot )
277266 if not whitelist and level < 0 :
278267 to .current_access_level = - 1
279268 return to .current_access_level
0 commit comments