Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F8929630
same-as-for-resource-in-field.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
same-as-for-resource-in-field.ts
View Options
import
Policy
from
"../../chip-types/policy"
;
import
{
App
,
CollectionItem
,
Context
,
FieldTypes
,
Query
}
from
"../../main"
;
import
Collection
from
"../../chip-types/collection"
;
import
{
ActionName
}
from
"../../action"
;
import
QueryStage
from
"../../datastore/query-stage"
;
import
DenyAll
from
"../../datastore/deny-all"
;
import
{
AllowAll
}
from
"../../datastore/allow-all"
;
export
default
class
SameAsForResourceInField
extends
Policy
{
static
type_name
=
"same-as-for-resource-in-field"
;
current_collection
:
string
;
field
:
string
;
constructor
({
action_name
,
collection_name
,
field
,
}
:
{
action_name
:
ActionName
;
collection_name
:
string
;
field
:
string
;
})
{
super
({
action_name
,
collection_name
,
field
});
this
.
current_collection
=
collection_name
;
this
.
field
=
field
;
}
getCollection
(
app
:
App
)
:
Collection
{
return
app
.
collections
[
this
.
current_collection
];
}
getReferencedCollection
(
context
:
Context
)
:
Collection
{
return
(
this
.
getCollection
(
context
.
app
).
fields
[
this
.
field
]
as
FieldTypes
.
SingleReference
).
getTargetCollection
(
context
);
}
getReferencedPolicy
(
context
:
Context
)
:
Policy
{
return
this
.
getReferencedCollection
(
context
).
getPolicy
(
"show"
);
}
async
_getRestrictingQuery
(
context
:
Context
)
{
const
referenced_restricting_query
=
await
this
.
getReferencedPolicy
(
context
).
getRestrictingQuery
(
context
);
if
(
referenced_restricting_query
instanceof
DenyAll
||
referenced_restricting_query
instanceof
AllowAll
)
{
return
referenced_restricting_query
;
}
const
query
=
new
Query
();
const
parent_prefix
=
query
.
lookup
({
from
:
this
.
getReferencedCollection
(
context
).
name
,
localField
:
this
.
field
,
foreignField
:
"id"
,
});
const
referenced_restricting_pipeline
=
referenced_restricting_query
.
toPipeline
();
add_parent_prefix_to_pipeline
(
referenced_restricting_pipeline
,
parent_prefix
);
const
pipeline
=
query
.
toPipeline
()
.
concat
(
referenced_restricting_pipeline
);
return
Query
.
fromCustomPipeline
(
pipeline
);
}
async
checkerFunction
(
context
:
Context
,
item_getter
:
()
=>
Promise
<
CollectionItem
>
)
{
if
(
!
item_getter
)
{
return
null
;
}
const
item
:
CollectionItem
=
await
item_getter
();
const
field
=
await
item
.
getDecoded
(
this
.
field
,
context
);
const
response
=
this
.
getReferencedCollection
(
context
).
suGetByID
(
field
as
string
);
const
ret
=
await
this
.
getReferencedPolicy
(
context
).
check
(
context
,
()
=>
response
);
return
ret
;
}
async
isItemSensitive
(
context
:
Context
)
{
return
this
.
getReferencedPolicy
(
context
).
isItemSensitive
(
context
);
}
}
function
add_parent_prefix_to_pipeline
(
pipeline
:
QueryStage
[],
parent_property
:
string
)
{
for
(
const
stage
of
pipeline
)
{
add_parent_prefix
(
stage
,
parent_property
);
}
}
const
prop_regex
=
/^[a-z0-9_]/
;
function
add_parent_prefix
(
group
:
QueryStage
,
parent_property
:
string
)
{
const
ret
:
{
[
name
:
string
]
:
unknown
}
=
{};
for
(
const
prop
of
Object
.
keys
(
group
)
as
(
keyof
typeof
group
)[])
{
if
(
prop
==
"$or"
||
prop
==
"$and"
)
{
group
[
prop
]
=
(
group
[
prop
]
as
Array
<
QueryStage
>
).
map
((
subgroup
)
=>
add_parent_prefix
(
subgroup
,
parent_property
)
);
}
else
if
(
group
[
prop
]
instanceof
Object
)
{
group
[
prop
]
=
add_parent_prefix
(
group
[
prop
],
parent_property
);
}
const
new_prop
=
prop_regex
.
test
(
prop
)
?
parent_property
+
"."
+
prop
:
prop
;
ret
[
new_prop
]
=
group
[
prop
]
as
unknown
;
}
return
ret
;
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Sat, Sep 20, 23:30 (1 d, 4 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
946482
Default Alt Text
same-as-for-resource-in-field.ts (3 KB)
Attached To
Mode
rS Sealious
Attached
Detach File
Event Timeline
Log In to Comment