diff --git a/oshipka/persistance/__init__.py b/oshipka/persistance/__init__.py index c217419..956570f 100644 --- a/oshipka/persistance/__init__.py +++ b/oshipka/persistance/__init__.py @@ -184,7 +184,9 @@ user_datastore = SQLAlchemyUserDatastore(db, User, Role) def register_filters(app): + # register jinja filters _paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}') + from oshipka.webapp.views import MODEL_VIEWS @app.template_filter('nl2br') def nl2br(text): @@ -234,6 +236,26 @@ def register_filters(app): def bool_filter(v): return bool(v) + def has_permission(model, verb, instance=None): + acl = MODEL_VIEWS.get(model, {}).model.acls.get(verb) + # Anonymous user -> do we require AuthN? + if current_user.is_anonymous: + return not acl.get('authn') + # Not Anonymous user -> Check roles + roles = acl.get('authz') + # No roles required -> has permission + if not roles: + return True + # One role is enough to grant permission + for role in roles: + if role in ['owner']: + return instance.user == current_user + if current_user.has_role(role): + return True + return False + + app.jinja_env.globals.update(has_permission=has_permission) + class Proxy(object): def __init__(self, proxied): diff --git a/vm_gen/templates/_model_py b/vm_gen/templates/_model_py index aa8ab79..963e006 100644 --- a/vm_gen/templates/_model_py +++ b/vm_gen/templates/_model_py @@ -6,7 +6,8 @@ class [[ name ]](db.Model, ModelController[% for inherit in interits %], [[ inhe [%- include "_model_choice_header_py" %] [%- include "_model_searchable_header_py" %] - _file_columns = [ [%- for column in columns %][%- if column.is_file %]"[[ column.name ]]"[%- endif %] [%- endfor %] ] + _file_columns = [ [%- for column in columns %][%- if column.is_file %]"[[ column.name ]]", [%- endif %] [%- endfor %] ] + acls = [[ acls ]] [%- for column in columns %] [%- if column._type == 'relationship' %] diff --git a/vm_gen/templates/html/_action_create.html b/vm_gen/templates/html/_action_create.html new file mode 100644 index 0000000..ae27d9c --- /dev/null +++ b/vm_gen/templates/html/_action_create.html @@ -0,0 +1,3 @@ +{% if has_permission('[[ name|camel_to_snake ]]', 'create', instance) %} +{{ _("Create") }} +{% endif %} \ No newline at end of file diff --git a/vm_gen/templates/html/_action_delete.html b/vm_gen/templates/html/_action_delete.html index 7592e8c..55fd47e 100644 --- a/vm_gen/templates/html/_action_delete.html +++ b/vm_gen/templates/html/_action_delete.html @@ -1 +1,3 @@ -x \ No newline at end of file +{% if has_permission('[[ name|camel_to_snake ]]', 'delete', instance) %} +x +{% endif %} \ No newline at end of file diff --git a/vm_gen/templates/html/_action_edit.html b/vm_gen/templates/html/_action_edit.html index 65f945f..2e6c91c 100644 --- a/vm_gen/templates/html/_action_edit.html +++ b/vm_gen/templates/html/_action_edit.html @@ -1 +1,3 @@ -e \ No newline at end of file +{% if has_permission('[[ name|camel_to_snake ]]', 'update', instance) %} +e +{% endif %} \ No newline at end of file diff --git a/vm_gen/templates/html/_action_list.html b/vm_gen/templates/html/_action_list.html new file mode 100644 index 0000000..3bba876 --- /dev/null +++ b/vm_gen/templates/html/_action_list.html @@ -0,0 +1,3 @@ +{% if has_permission('[[ name|camel_to_snake ]]', 'list') %} +{{ _("list") }} +{% endif %} \ No newline at end of file diff --git a/vm_gen/templates/html/_action_search.html b/vm_gen/templates/html/_action_search.html new file mode 100644 index 0000000..998e46f --- /dev/null +++ b/vm_gen/templates/html/_action_search.html @@ -0,0 +1,6 @@ +{% if has_permission('[[ name|camel_to_snake ]]', 'search') %} +
+{% endif %} \ No newline at end of file diff --git a/vm_gen/templates/html/_action_table.html b/vm_gen/templates/html/_action_table.html new file mode 100644 index 0000000..698067e --- /dev/null +++ b/vm_gen/templates/html/_action_table.html @@ -0,0 +1,3 @@ +{% if has_permission('[[ name|camel_to_snake ]]', 'table') %} +{{ _("table") }} +{% endif %} \ No newline at end of file diff --git a/vm_gen/templates/html/_actions.html b/vm_gen/templates/html/_actions.html index bcf77ea..c234b29 100644 --- a/vm_gen/templates/html/_actions.html +++ b/vm_gen/templates/html/_actions.html @@ -1,4 +1,7 @@ -[ -{% include "[[ name|camel_to_snake ]]/_action_edit.html" %} | -{% include "[[ name|camel_to_snake ]]/_action_delete.html" %} -] \ No newline at end of file +{% if not has_permission('[[ name|camel_to_snake ]]', 'update', instance) and not has_permission('[[ name|camel_to_snake ]]', 'delete', instance) %} +{% else %} + [ + {% include "[[ name|camel_to_snake ]]/_action_edit.html" %} | + {% include "[[ name|camel_to_snake ]]/_action_delete.html" %} + ] +{% endif %} \ No newline at end of file diff --git a/vm_gen/templates/html/_actions_multiple.html b/vm_gen/templates/html/_actions_multiple.html new file mode 100644 index 0000000..ab0a357 --- /dev/null +++ b/vm_gen/templates/html/_actions_multiple.html @@ -0,0 +1,2 @@ +{% include "[[ name|camel_to_snake ]]/_action_list.html" %} | +{% include "[[ name|camel_to_snake ]]/_action_table.html" %} \ No newline at end of file diff --git a/vm_gen/templates/html/_list_item.html b/vm_gen/templates/html/_list_item.html index 738a70c..d99cf1d 100644 --- a/vm_gen/templates/html/_list_item.html +++ b/vm_gen/templates/html/_list_item.html @@ -1,5 +1,10 @@