Packaging... This mess... #12
nathan-curtis
started this conversation in
Ideas
Replies: 2 comments 14 replies
-
|
I have a similar system to yours, it's all packages and included blocks with!include. Almost all the jinja is in 5 custom templates making it harder to redistribute. I have experimented with rewrite of all jinja to scripts but it degrades performance too much. Such a shame we cannot specify alternative/additional folders from which t load custom templates. |
Beta Was this translation helpful? Give feedback.
2 replies
-
|
I am using something like this for ACL {#- users.jinja -#}
{%- set __USERS = [
dict(
person = 'person.teskanoo',
id = state_attr( 'person.teskanoo', 'id' ) | default(none),
friendly_name = state_attr( 'person.teskanoo', 'friendly_name' ) | default(none),
user_id = state_attr( 'person.teskanoo', 'user_id' ) | default(none),
device_trackers = state_attr( 'person.teskanoo', 'device_trackers' ) | default([]),
entity_picture = state_attr( 'person.teskanoo', 'entity_picture' ) | default(none),
entity_count = label_entities( 'person_teskanoo' ) | count(),
),
dict(
person = 'person.xxxx',
id = state_attr( 'person.xxxx', 'id' ) | default(none),
friendly_name = state_attr( 'person.xxxx', 'friendly_name' ) | default(none),
user_id = state_attr( 'person.xxxx', 'user_id' ) | default(none),
device_trackers = state_attr( 'person.xxxx', 'device_trackers' ) | default([]),
entity_picture = state_attr( 'person.xxxx', 'entity_picture' ) | default(none),
entity_count = label_entities( 'person_xxxx' ) | count(),
),
dict(
person = 'person.zen_ai',
id = state_attr( 'person.zen_ai', 'id' ) | default(none),
friendly_name = state_attr( 'person.zen_ai', 'friendly_name' ) | default(none),
user_id = state_attr( 'person.zen_ai', 'user_id' ) | default(none),
device_trackers = state_attr( 'person.zen_ai', 'device_trackers' ) | default([]),
entity_picture = state_attr( 'person.zen_ai', 'entity_picture' ) | default(none),
entity_count = label_entities( 'person_zen_ai' ) | count(),
)
] -%}
{%- macro users() -%}
{{- __USERS | to_json( pretty_print=true ) -}}
{%- endmacro -%}
{%- macro find( user_id ) -%}
{%- set match = __USERS
| selectattr('user_id', 'eq', user_id)
| list
| first
| default({})
-%}
{{- match | to_json(pretty_print=true) -}}
{%- endmacro -%}
And in zen/acl.jinja ... {#- acl.jinja -#}
{%- import "zen/users.jinja" as USERS -%}
{%- macro user_can( user_id, action, object ) -%}
{%- set rv = false -%}
{%- set user_id = user_id | default('') -%}
{%- set action = action | default('') -%}
{%- set object = object | default('') -%}
{%- set user = USERS.find(user_id) | from_json() | default({}) -%}
{%- set acl = {} -%}
{%- if object -%}
{%- set acl = state_attr(object, 'acl') | default({}) -%}
{%- endif -%}
{%- set permissions = acl.get(action) | default({}) -%}
{%- set person = user.person | default(none) -%}
{%- if acl -%}
{%- if user -%}
{%- if permissions -%}
{%- if person -%}
{%- if person in permissions -%}
{%- set rv = true -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- set data =
dict(
user = user,
action = action,
object = object,
permissions = permissions,
person = person,
granted = rv,
acl = acl,
)
| to_json(pretty_print=true) -%}
{{- data -}}
{%- endmacro -%}
finally in my todo_conroller.yaml # -----------------------------------------------------------------------
# Process Variables - ACL
# -----------------------------------------------------------------------
- alias: Variables - ACL - User
variables:
zen_ai_user_id: >
{%- import "zeni/users.jinja" as USERS -%}
{{- USERS.users() | from_json() | selectattr('person', 'eq', 'person.zen_ai') | map(attribute='user_id') | first -}}
acl_user_id: >
{{- this.get('context',{}).get('user_id', none) or zen_ai_user_id -}}
acl_user: >
{%- import "zeni/users.jinja" as USERS -%}
{{- USERS.find(acl_user_id) | from_json() -}}
- alias: Variables - ACL Data
variables:
# -------------------------------------------------------------------
# When the user is attempting to set an item 'completed', they need
# specific permission to do that so we 'modify' the acl_action to
# ensure that we check against that special form of 'update'
# -------------------------------------------------------------------
acl_action: >
{%- set rv = v_request_data.get('action_type','') -%}
{%- if rv in ['update'] -%}
{%- for todo_entry in target_items -%}
{%- set item_status = todo_entry.get('status','') -%}
{%- if item_status in ['completed'] -%}
{%- set rv = 'completed' -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{{- rv -}}
acl_objects: >
{%- set rv = [] -%}
{%- if acl_action in ['read'] -%}
{%- if read_sub_mode in ['Mode_Read_Todos'] -%}
{%- set rv = [] -%}
{%- else -%}
{%- set rv = lists_to_read -%}
{%- endif -%}
{%- else -%}
{%- set rv = lists_to_write -%}
{%- endif -%}
{{- rv -}}
# -----------------------------------------------------------------------
# Process ACL validation
# -----------------------------------------------------------------------
- alias: 'Variables: acl_errors'
variables:
acl_errors: >
{%- import "zen/acl.jinja" as ACL -%}
{%- set ns = namespace(errors=[]) -%}
{%- set req = v_request_data -%}
{#- ========================================================================= -#}
{#- ========================================================================= -#}
{#- ========================================================================= -#}
{%- if (acl_objects | length) > 0 -%}
{%- for target_object in acl_objects -%}
{%- if not acl_user_id -%}
{%- set e = "Error: Permission denied user_id not defined in context" -%}
{%- set ns.errors = ns.errors + [e] -%}
{%- elif not acl_user -%}
{%- set e = "Error: Permission denied user {} not found".format(acl_user_id) -%}
{%- set ns.errors = ns.errors + [e] -%}
{%- else -%}
{%- set test = ACL.user_can(acl_user_id, acl_action, target_object) -%}
{%- if ( not test ) -%}
{%- set e = "Error: Permission denied user {} cannot do action {} on {}. Please request access if you think you should perhaps be able to perform this action".format(acl_user.get('person'), acl_action, target_object) -%}
{%- set ns.errors = ns.errors + [e] -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- if ( (ns.errors|length) > 0 ) -%}
{%- set e = "Please request access if you think you should perhaps be able to perform this action" -%}
{%- set ns.errors = ns.errors + [e] -%}
{%- endif -%}
{{- ns.errors -}}
- if:
condition: template
value_template: "{{- acl_errors | length > 0 -}}"
then:
- variables:
v_script_response: >
{%- import 'zen/zen_response.jinja' as ZR -%}
{%- set v_script_response = ZR.set_response_status(v_script_response, 'error', acl_errors) | from_json() -%}
{{- v_script_response -}}
- stop: Return ACL Errors to LLM
response_variable: v_script_response
|
Beta Was this translation helpful? Give feedback.
12 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Uh oh!
There was an error while loading. Please reload this page.
-
Whats the 'best way to package this up... Is it packages? Blueprints?
Beta Was this translation helpful? Give feedback.
All reactions