mirror of
https://github.com/logos-co/open-law.git
synced 2025-02-03 02:24:45 +00:00
add logic to inherit access groups
This commit is contained in:
parent
6abd2fad19
commit
be6ad04c7d
@ -3,9 +3,9 @@ from app.logger import log
|
||||
from .get_or_create_permission import get_or_create_permission
|
||||
|
||||
|
||||
def create_moderator_group():
|
||||
def create_moderator_group(book_id: int):
|
||||
log(log.INFO, "Create moderator access group")
|
||||
group: m.AccessGroup = m.AccessGroup(name="moderator").save()
|
||||
group: m.AccessGroup = m.AccessGroup(name="moderator", book_id=book_id).save()
|
||||
permissions = []
|
||||
|
||||
comment_DA = get_or_create_permission(
|
||||
@ -29,9 +29,9 @@ def create_moderator_group():
|
||||
return group
|
||||
|
||||
|
||||
def create_editor_group():
|
||||
def create_editor_group(book_id: int):
|
||||
log(log.INFO, "Create editor access group")
|
||||
group: m.AccessGroup = m.AccessGroup(name="editor").save()
|
||||
group: m.AccessGroup = m.AccessGroup(name="editor", book_id=book_id).save()
|
||||
permissions = []
|
||||
|
||||
comment_DA = get_or_create_permission(
|
||||
|
@ -18,6 +18,10 @@ from app.controllers.tags import (
|
||||
from app.controllers.delete_nested_book_entities import (
|
||||
delete_nested_book_entities,
|
||||
)
|
||||
from app.controllers.create_access_groups import (
|
||||
create_editor_group,
|
||||
create_moderator_group,
|
||||
)
|
||||
from app import models as m, db, forms as f
|
||||
from app.logger import log
|
||||
from .bp import bp
|
||||
@ -70,13 +74,25 @@ def create():
|
||||
log(log.INFO, "Form submitted. Book: [%s]", book)
|
||||
book.save()
|
||||
version = m.BookVersion(semver="1.0.0", book_id=book.id).save()
|
||||
m.Collection(
|
||||
root_collection = m.Collection(
|
||||
label="Root Collection", version_id=version.id, is_root=True
|
||||
).save()
|
||||
tags = form.tags.data
|
||||
if tags:
|
||||
set_book_tags(book, tags)
|
||||
|
||||
# access groups
|
||||
editor_access_group = create_editor_group(book_id=book.id)
|
||||
moderator_access_group = create_moderator_group(book_id=book.id)
|
||||
access_groups = [editor_access_group, moderator_access_group]
|
||||
|
||||
for access_group in access_groups:
|
||||
m.BookAccessGroups(book_id=book.id, access_group_id=access_group.id).save()
|
||||
m.CollectionAccessGroups(
|
||||
collection_id=root_collection.id, access_group_id=access_group.id
|
||||
).save()
|
||||
# -------------
|
||||
|
||||
flash("Book added!", "success")
|
||||
return redirect(url_for("book.my_library"))
|
||||
else:
|
||||
|
@ -114,6 +114,7 @@ def collection_create(book_id: int, collection_id: int | None = None):
|
||||
label=label,
|
||||
about=form.about.data,
|
||||
parent_id=book.versions[-1].root_collection.id,
|
||||
version_id=book.versions[-1].id,
|
||||
)
|
||||
if collection_id:
|
||||
collection.parent_id = collection_id
|
||||
@ -122,6 +123,11 @@ def collection_create(book_id: int, collection_id: int | None = None):
|
||||
log(log.INFO, "Create collection [%s]. Book: [%s]", collection, book.id)
|
||||
collection.save()
|
||||
|
||||
for access_group in collection.parent.access_groups:
|
||||
m.CollectionAccessGroups(
|
||||
collection_id=collection.id, access_group_id=access_group.id
|
||||
).save()
|
||||
|
||||
flash("Success!", "success")
|
||||
if collection_id:
|
||||
redirect_url = url_for("book.collection_view", book_id=book_id)
|
||||
|
@ -10,6 +10,7 @@ from tests.utils import (
|
||||
check_if_nested_collection_entities_is_deleted,
|
||||
check_if_nested_section_entities_is_deleted,
|
||||
check_if_nested_interpretation_entities_is_deleted,
|
||||
create_test_book,
|
||||
)
|
||||
|
||||
|
||||
@ -30,7 +31,7 @@ def test_create_edit_delete_book(client: FlaskClient):
|
||||
assert response.status_code == 200
|
||||
assert b"Label must be between 6 and 256 characters long." in response.data
|
||||
|
||||
book = m.Book.query.filter_by(label=BOOK_NAME).first()
|
||||
book: m.Book = m.Book.query.filter_by(label=BOOK_NAME).first()
|
||||
|
||||
assert not book
|
||||
assert not m.Book.query.count()
|
||||
@ -47,7 +48,7 @@ def test_create_edit_delete_book(client: FlaskClient):
|
||||
assert response.status_code == 200
|
||||
assert b"Label must be between 6 and 256 characters long." in response.data
|
||||
|
||||
book = m.Book.query.filter_by(label=BOOK_NAME).first()
|
||||
book: m.Book = m.Book.query.filter_by(label=BOOK_NAME).first()
|
||||
|
||||
assert not book
|
||||
assert not m.Book.query.count()
|
||||
@ -63,11 +64,18 @@ def test_create_edit_delete_book(client: FlaskClient):
|
||||
assert response.status_code == 200
|
||||
assert b"Book added!" in response.data
|
||||
|
||||
book = m.Book.query.filter_by(label=BOOK_NAME).first()
|
||||
book: m.Book = m.Book.query.filter_by(label=BOOK_NAME).first()
|
||||
|
||||
assert book
|
||||
assert book.versions
|
||||
assert len(book.versions) == 1
|
||||
assert book.access_groups
|
||||
assert len(book.access_groups) == 2
|
||||
|
||||
root_collection: m.Collection = book.last_version.collections[0]
|
||||
assert root_collection
|
||||
assert root_collection.access_groups
|
||||
assert len(root_collection.access_groups) == 2
|
||||
|
||||
response: Response = client.post(
|
||||
"/book/999/edit",
|
||||
@ -116,7 +124,7 @@ def test_add_contributor(client: FlaskClient):
|
||||
|
||||
moderator = m.User(username="Moderator", password="test").save()
|
||||
|
||||
moderators_book = m.Book(label="Test Book", user_id=moderator.id).save()
|
||||
moderators_book: m.Book = m.Book(label="Test Book", user_id=moderator.id).save()
|
||||
response: Response = client.post(
|
||||
f"/book/{moderators_book.id}/add_contributor",
|
||||
data=dict(user_id=moderator.id, role=m.BookContributor.Roles.MODERATOR),
|
||||
@ -126,7 +134,7 @@ def test_add_contributor(client: FlaskClient):
|
||||
assert response.status_code == 200
|
||||
assert b"You are not owner of this book!" in response.data
|
||||
|
||||
book = m.Book(label="Test Book", user_id=user.id).save()
|
||||
book: m.Book = m.Book(label="Test Book", user_id=user.id).save()
|
||||
m.BookVersion(semver="1.0.0", book_id=book.id).save()
|
||||
|
||||
response: Response = client.post(
|
||||
@ -245,7 +253,7 @@ def test_edit_contributor_role(client: FlaskClient, runner: FlaskCliRunner):
|
||||
|
||||
moderator = m.User(username="Moderator", password="test").save()
|
||||
|
||||
moderators_book = m.Book(label="Test Book", user_id=moderator.id).save()
|
||||
moderators_book: m.Book = m.Book(label="Test Book", user_id=moderator.id).save()
|
||||
response: Response = client.post(
|
||||
f"/book/{moderators_book.id}/add_contributor",
|
||||
data=dict(user_id=moderator.id, role=m.BookContributor.Roles.MODERATOR),
|
||||
@ -265,16 +273,10 @@ def test_edit_contributor_role(client: FlaskClient, runner: FlaskCliRunner):
|
||||
assert b"You are not owner of this book!" in response.data
|
||||
|
||||
|
||||
def test_crud_collection(client: FlaskClient, runner: FlaskCliRunner):
|
||||
def test_crud_collection(client: FlaskClient):
|
||||
_, user = login(client)
|
||||
user: m.User
|
||||
|
||||
# add dummmy data
|
||||
runner.invoke(args=["db-populate"])
|
||||
|
||||
book = db.session.get(m.Book, 1)
|
||||
book.user_id = user.id
|
||||
book.save()
|
||||
book = create_test_book(user.id)
|
||||
|
||||
response: Response = client.post(
|
||||
f"/book/{book.id}/create_collection",
|
||||
@ -305,6 +307,14 @@ def test_crud_collection(client: FlaskClient, runner: FlaskCliRunner):
|
||||
collection: m.Collection = m.Collection.query.filter_by(
|
||||
label="Test Collection #1 Label"
|
||||
).first()
|
||||
|
||||
assert collection
|
||||
assert collection.access_groups
|
||||
assert len(collection.access_groups) == 2
|
||||
for access_group in collection.access_groups:
|
||||
access_group: m.AccessGroup
|
||||
assert access_group.book_id == collection.version.book_id
|
||||
|
||||
m.Collection(
|
||||
label="Test Collection #2 Label",
|
||||
version_id=collection.version_id,
|
||||
@ -398,9 +408,7 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
|
||||
# add dummy data
|
||||
runner.invoke(args=["db-populate"])
|
||||
|
||||
book: m.Book = db.session.get(m.Book, 1)
|
||||
book.user_id = user.id
|
||||
book.save()
|
||||
book = create_test_book(user.id)
|
||||
|
||||
leaf_collection: m.Collection = m.Collection(
|
||||
label="Test Leaf Collection #1 Label",
|
||||
@ -462,6 +470,12 @@ def test_crud_subcollection(client: FlaskClient, runner: FlaskCliRunner):
|
||||
assert sub_collection.is_leaf
|
||||
assert sub_collection.parent_id == collection.id
|
||||
|
||||
assert sub_collection.access_groups
|
||||
assert len(sub_collection.access_groups) == 2
|
||||
for access_group in sub_collection.access_groups:
|
||||
access_group: m.AccessGroup
|
||||
assert access_group.book_id == sub_collection.version.book_id
|
||||
|
||||
m.Collection(
|
||||
label="Test SubCollection #2 Label",
|
||||
version_id=collection.version_id,
|
||||
|
@ -6,7 +6,7 @@ from app import models as m
|
||||
|
||||
|
||||
def test_init_moderator_group(client):
|
||||
create_moderator_group()
|
||||
create_moderator_group(book_id=0)
|
||||
|
||||
group: m.AccessGroup = m.AccessGroup.query.filter_by(name="moderator").first()
|
||||
assert group
|
||||
@ -29,13 +29,13 @@ def test_init_moderator_group(client):
|
||||
assert comment_DA
|
||||
assert comment_DA in permissions
|
||||
|
||||
create_moderator_group()
|
||||
create_moderator_group(book_id=0)
|
||||
groups: list[m.AccessGroup] = m.AccessGroup.query.filter_by(name="moderator").all()
|
||||
assert len(groups) == 2
|
||||
|
||||
|
||||
def test_init_editor_group(client):
|
||||
create_editor_group()
|
||||
create_editor_group(book_id=0)
|
||||
|
||||
group: m.AccessGroup = m.AccessGroup.query.filter_by(name="editor").first()
|
||||
assert group
|
||||
@ -79,6 +79,6 @@ def test_init_editor_group(client):
|
||||
assert book_U
|
||||
assert book_U in permissions
|
||||
|
||||
create_editor_group()
|
||||
create_editor_group(book_id=0)
|
||||
groups: list[m.AccessGroup] = m.AccessGroup.query.filter_by(name="editor").all()
|
||||
assert len(groups) == 2
|
||||
|
@ -1,4 +1,8 @@
|
||||
from app import models as m
|
||||
from app.controllers.create_access_groups import (
|
||||
create_editor_group,
|
||||
create_moderator_group,
|
||||
)
|
||||
|
||||
from random import randint
|
||||
|
||||
@ -33,8 +37,26 @@ def create_test_book(owner_id: int, entity_id: int = randint(1, 100)):
|
||||
|
||||
version: m.BookVersion = m.BookVersion(semver="1.0.0", book_id=book.id).save()
|
||||
|
||||
root_collection: m.Collection = m.Collection(
|
||||
label="Root", version_id=version.id, is_root=True
|
||||
).save()
|
||||
|
||||
# access groups
|
||||
editor_access_group = create_editor_group(book_id=book.id)
|
||||
moderator_access_group = create_moderator_group(book_id=book.id)
|
||||
access_groups = [editor_access_group, moderator_access_group]
|
||||
|
||||
for access_group in access_groups:
|
||||
m.BookAccessGroups(book_id=book.id, access_group_id=access_group.id).save()
|
||||
m.CollectionAccessGroups(
|
||||
collection_id=root_collection.id, access_group_id=access_group.id
|
||||
).save()
|
||||
# -------------
|
||||
|
||||
collection: m.Collection = m.Collection(
|
||||
label=f"Collection {entity_id}", version_id=version.id
|
||||
label=f"Collection {entity_id}",
|
||||
version_id=version.id,
|
||||
parent_id=root_collection.id,
|
||||
).save()
|
||||
|
||||
section: m.Section = m.Section(
|
||||
@ -55,6 +77,7 @@ def create_test_book(owner_id: int, entity_id: int = randint(1, 100)):
|
||||
user_id=owner_id,
|
||||
interpretation_id=interpretation.id,
|
||||
).save()
|
||||
return book
|
||||
|
||||
|
||||
def check_if_nested_book_entities_is_deleted(book: m.Book, is_deleted: bool = True):
|
||||
|
Loading…
x
Reference in New Issue
Block a user