Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c6cf298
change hr_group and primary_group to admin_group and member_group in …
simensandhaug Nov 7, 2022
d800ba4
update group constants
simensandhaug Nov 7, 2022
5faef25
change instances of hr_group and primary_group
simensandhaug Nov 7, 2022
1d6e42b
fix query error
simensandhaug Nov 7, 2022
aa5c147
write custom migration
simensandhaug Nov 14, 2022
ad93482
revert changes to previous migrations
simensandhaug Nov 17, 2022
22a5b20
revert changes to previous migrations
simensandhaug Nov 17, 2022
1eef979
revert changes to previous migrations
simensandhaug Nov 17, 2022
de5d3d7
move custom migrations to more appropriate folder
simensandhaug Nov 17, 2022
3733a79
merge migrations due to conflicts
simensandhaug Nov 17, 2022
ccd26d8
remove manage_organization permission from organizations/signals.py
simensandhaug Nov 21, 2022
bb000da
uncomment manage_organization permission in organizations/models.py
simensandhaug Nov 21, 2022
008dfdb
remove autogenerated yarn.lock file
simensandhaug Nov 21, 2022
188b95f
quality of life improvement for admin-panel, more easily readable wit…
simensandhaug Nov 21, 2022
2cde7d2
Admin-panel improvements and update signal and migration to be more d…
simensandhaug Nov 21, 2022
30d6ad4
update migration
simensandhaug Nov 22, 2022
fe30038
fix naming error
simensandhaug Nov 22, 2022
6556ecf
fix typing error
simensandhaug Nov 22, 2022
1f8b106
Update backend/apps/organizations/signals.py
simensandhaug Feb 20, 2023
a230e7b
Update backend/apps/organizations/signals.py
simensandhaug Feb 20, 2023
a6bc2b3
fix bug when referencing group.uuid
simensandhaug Feb 20, 2023
0c5e871
Merge branch 'main' into feat/migrate-org-group
simensandhaug Feb 20, 2023
3f005d2
Merge branch 'main' into feat/migrate-org-group
simensandhaug Feb 27, 2023
6a8a60e
fix black linting
simensandhaug Feb 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/apps/ecommerce/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def setUp(self) -> None:
MembershipFactory(
user=self.staff_user,
organization=self.organization,
group=self.organization.primary_group,
group=self.organization.member_group,
)
self.total_quantity = 5
self.max_buyable_quantity = 2
Expand Down
2 changes: 1 addition & 1 deletion backend/apps/forms/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
def handle_new_form(sender, instance: Form, created: bool, **kwargs) -> None:
if created:
perms = ["forms.manage_form", "forms.change_form", "forms.delete_form"]
group = instance.organization.hr_group.group
group = instance.organization.admin_group.group
for perm in perms:
assign_perm(perm, group, instance)
2 changes: 1 addition & 1 deletion backend/apps/forms/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def setUp(self) -> None:
MembershipFactory(
user=self.authorized_user,
organization=self.organization,
group=self.organization.hr_group,
group=self.organization.admin_group,
)

# Create the form
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def set_primary_groups(apps, schema_editor):
created = True
if organization.hr_group is None:
hr_group = ResponsibleGroup.objects.create(
name="HR", description=f"HR-gruppen til {organization.name}. Tillatelser for å se og behandle søknader."
name="HR",
description=f"HR-gruppen til {organization.name}. Tillatelser for å se og behandle søknader.",
)
organization.hr_group = hr_group
created = True
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.16 on 2023-01-23 18:47
# Generated by Django 3.2.16 on 2022-11-21 20:19

from django.db import migrations

Expand Down
13 changes: 7 additions & 6 deletions backend/apps/organizations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.db import models
from django.db.models import UniqueConstraint

from apps.permissions.constants import HR_TYPE, PRIMARY_TYPE
from apps.permissions.constants import ADMIN_GROUP_TYPE, MEMBER_GROUP_TYPE
from apps.permissions.models import ResponsibleGroup


Expand All @@ -28,7 +28,8 @@ class Organization(models.Model):
# Permission groups
# All members are added to the primary group
# Members can be added to groups programatically
# The HR-group has the "forms.manage_form" permission, allowing them to view and manage responses to e.g. listings.
# The ADMIN-group has the "forms.manage_form" permission, allowing them to view and manage responses to e.g.
# listings.
# The primary group is intended to act as a group for organizations who need any kind of
# special permission, e.g. hyttestyret
# Or if we wish to limit the creation of events or listings to certain organizations.
Expand All @@ -42,16 +43,16 @@ class Organization(models.Model):
)

@property
def hr_group(self) -> Optional["ResponsibleGroup"]:
def admin_group(self) -> Optional["ResponsibleGroup"]:
try:
return self.permission_groups.get(group_type=HR_TYPE)
return self.permission_groups.get(group_type=ADMIN_GROUP_TYPE)
except ResponsibleGroup.DoesNotExist:
return None

@property
def primary_group(self) -> Optional["ResponsibleGroup"]:
def member_group(self) -> Optional["ResponsibleGroup"]:
try:
return self.permission_groups.get(group_type=PRIMARY_TYPE)
return self.permission_groups.get(group_type=MEMBER_GROUP_TYPE)
except ResponsibleGroup.DoesNotExist:
return None

Expand Down
28 changes: 14 additions & 14 deletions backend/apps/organizations/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

from apps.organizations.models import Membership, Organization
from apps.permissions.constants import (
HR_GROUP_NAME,
HR_TYPE,
ADMIN_GROUP_NAME,
ADMIN_GROUP_TYPE,
ORGANIZATION,
PRIMARY_GROUP_NAME,
PRIMARY_TYPE,
MEMBER_GROUP_NAME,
MEMBER_GROUP_TYPE,
)
from apps.permissions.models import ResponsibleGroup


@receiver(post_save, sender=Membership)
def handle_new_member(instance: Membership, **kwargs):
optional_group: Optional[ResponsibleGroup] = instance.group
group: Group = instance.organization.primary_group.group
group: Group = instance.organization.member_group.group
org_group: Group = Group.objects.get(name=ORGANIZATION)
user = instance.user
user.groups.add(org_group)
Expand All @@ -31,7 +31,7 @@ def handle_new_member(instance: Membership, **kwargs):

@receiver(pre_delete, sender=Membership)
def handle_removed_member(instance: Membership, **kwargs):
group: Group = instance.organization.primary_group.group
group: Group = instance.organization.member_group.group
org_group: Group = Group.objects.get(name=ORGANIZATION)
user = instance.user
if group:
Expand All @@ -44,19 +44,19 @@ def handle_removed_member(instance: Membership, **kwargs):
@receiver(post_save, sender=Organization)
def create_default_groups(instance: Organization, created, **kwargs):
"""
Creates and assigns a primary group and HR group to members of the organization.
Creates and assigns a primary group and ADMIN group to members of the organization.
"""
if created:
ResponsibleGroup.objects.create(
name=PRIMARY_GROUP_NAME,
name=f"{instance.name}:{MEMBER_GROUP_NAME}",
description=f"Medlemmer av {instance.name}.",
organization=instance,
group_type=PRIMARY_TYPE,
group_type=MEMBER_GROUP_TYPE,
)
hr_group = ResponsibleGroup.objects.create(
name=HR_GROUP_NAME,
description=f"HR-gruppen til {instance.name}. Tillatelser for å se og behandle søknader.",
admin_group = ResponsibleGroup.objects.create(
name=f"{instance.name}:{ADMIN_GROUP_NAME}",
description=f"ADMIN-gruppen til {instance.name}. Tillatelser for å se og behandle søknader og medlemmer.",
organization=instance,
group_type=HR_TYPE,
group_type=ADMIN_GROUP_TYPE,
)
assign_perm("forms.add_form", hr_group.group)
assign_perm("forms.add_form", admin_group.group)
8 changes: 4 additions & 4 deletions backend/apps/organizations/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
class OrganizationType(DjangoObjectType):
absolute_slug = graphene.String()
listings = graphene.List(NonNull(ListingType))
primary_group = graphene.Field(source="primary_group", type=ResponsibleGroupType)
hr_group = graphene.Field(source="hr_group", type=ResponsibleGroupType)
member_group = graphene.Field(source="member_group", type=ResponsibleGroupType)
admin_group = graphene.Field(source="admin_group", type=ResponsibleGroupType)

class Meta:
model = Organization
Expand All @@ -30,8 +30,8 @@ class Meta:
"users",
"events",
"logo_url",
"primary_group",
"hr_group",
"member_group",
"admin_group",
]

@staticmethod
Expand Down
8 changes: 4 additions & 4 deletions backend/apps/permissions/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
DefaultPermissionsType = Final[list[tuple[str, str]]]

# Default ResponsibleGroup types
PRIMARY_TYPE: Literal["PRIMARY"] = "PRIMARY"
HR_TYPE: Literal["HR"] = "HR"
MEMBER_GROUP_TYPE: Literal["MEMBER"] = "MEMBER"
ADMIN_GROUP_TYPE: Literal["ADMIN"] = "ADMIN"

ORGANIZATION: Final = "Organization member"
INDOK: Final = "Indøk"
REGISTERED_USER: Final = "Registered user"
PRIMARY_GROUP_NAME: Final = "Medlem"
HR_GROUP_NAME: Final = "HR"
MEMBER_GROUP_NAME: Final = "Medlem"
ADMIN_GROUP_NAME: Final = "Administrator"

DEFAULT_ORGANIZATION_PERMISSIONS: DefaultPermissionsType = [
("events", "add_event"),
Expand Down
14 changes: 7 additions & 7 deletions backend/apps/permissions/migrations/0003_auto_20210824_1213.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from django.db import migrations
from django.db.models.query_utils import Q
from apps.permissions.constants import PRIMARY_GROUP_NAME, HR_GROUP_NAME
from apps.permissions.constants import MEMBER_GROUP_NAME, ADMIN_GROUP_NAME

if TYPE_CHECKING:
from apps.organizations import models
Expand All @@ -19,14 +19,14 @@ def improve_group_legibility(apps, _):
responsible_group: "ResponsibleGroup" = group.responsiblegroup
try:
organization: "models.Organization" = Organization.objects.get(
Q(primary_group=responsible_group) | Q(hr_group=responsible_group)
Q(member_group=responsible_group) | Q(admin_group=responsible_group)
)

responsible_group_name = responsible_group.name
if organization.primary_group == responsible_group:
responsible_group_name = PRIMARY_GROUP_NAME
elif organization.hr_group == responsible_group:
responsible_group_name = HR_GROUP_NAME
if organization.member_group == responsible_group:
responsible_group_name = MEMBER_GROUP_NAME
elif organization.admin_group == responsible_group:
responsible_group_name = ADMIN_GROUP_NAME
if responsible_group.name != responsible_group_name:
responsible_group.name = responsible_group_name
responsible_group.save()
Expand All @@ -50,7 +50,7 @@ def reverse_legible_group_names(apps, _):
responsible_group = group.responsiblegroup
try:
organization = responsible_group.organization
if organization.primary_group == responsible_group:
if organization.member_group == responsible_group:
responsible_group.name = organization.name
responsible_group.save()
except Organization.DoesNotExist:
Expand Down
32 changes: 17 additions & 15 deletions backend/apps/permissions/migrations/0005_auto_20210824_1446.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import TYPE_CHECKING, Type
from django.db import migrations

from apps.permissions.constants import HR_TYPE, PRIMARY_TYPE
from apps.permissions.constants import ADMIN_GROUP_TYPE, MEMBER_GROUP_TYPE

if TYPE_CHECKING:
from apps.organizations import models as org_models
Expand All @@ -15,22 +15,22 @@ def move_permission_groups_to_fk(apps, _):
Organization: Type["org_models.Organization"] = apps.get_model("organizations", "Organization")

for organization in Organization.objects.all():
primary_group = organization.primary_group
hr_group = organization.hr_group
member_group = organization.member_group
admin_group = organization.admin_group

if primary_group and hr_group:
primary_group.group_type = PRIMARY_TYPE
hr_group.group_type = HR_TYPE
if member_group and admin_group:
member_group.group_type = MEMBER_GROUP_TYPE
admin_group.group_type = ADMIN_GROUP_TYPE

# Name as of this migration
primary_group.temp_organization = organization
hr_group.temp_organization = organization
member_group.temp_organization = organization
admin_group.temp_organization = organization

primary_group.save()
hr_group.save()
member_group.save()
admin_group.save()

organization.primary_group = None
organization.hr_group = None
organization.member_group = None
organization.admin_group = None
organization.save()

# Delete orphan responsible groups
Expand All @@ -43,10 +43,12 @@ def move_permission_groups_to_one_to_one_field(apps, _):

organization: "org_models.Organization"
for organization in Organization.objects.all():
organization.primary_group = ResponsibleGroup.objects.get(
temp_organization=organization, group_type=PRIMARY_TYPE
organization.member_group = ResponsibleGroup.objects.get(
temp_organization=organization, group_type=MEMBER_GROUP_TYPE
)
organization.admin_group = ResponsibleGroup.objects.get(
temp_organization=organization, group_type=ADMIN_GROUP_TYPE
)
organization.hr_group = ResponsibleGroup.objects.get(temp_organization=organization, group_type=HR_TYPE)
organization.save()


Expand Down
37 changes: 37 additions & 0 deletions backend/apps/permissions/migrations/0034_auto_20221114_1854.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by Django 3.2.16 on 2022-11-14 17:54

from django.db import migrations
from apps.permissions.constants import ADMIN_GROUP_TYPE, MEMBER_GROUP_TYPE


def migrate_org_groups(apps, schema_editor):
ResponsibleGroup = apps.get_model("permissions", "ResponsibleGroup")
for responsible_group in ResponsibleGroup.objects.all():
if responsible_group.name == "HR":
# ResponsibleGroup names format is "Organization:Type" (ex "Rubberdøk:ADMIN")
# Type is either "ADMIN" or "MEMBER"
# ResponsibleGroup has a group field which is a django.contrib.auth.models.Group with outdated names
# Change the Type in group name to "ADMIN" or "MEMBER" depending on the ResponsibleGroup name (e.g. "Organization:HR:uuid" -> "Organization:ADMIN:uuid")
# Change ResponsibleGroup.group_type from "HR" or "PRIMARY" to "ADMIN" or "MEMBER"
responsible_group.name = f"{responsible_group.organization.name}:{ADMIN_GROUP_TYPE}"
responsible_group.group.name = (
f"{responsible_group.organization.name}:{ADMIN_GROUP_TYPE}:git{responsible_group.uuid}"
)
responsible_group.group_type = ADMIN_GROUP_TYPE
responsible_group.save()
if responsible_group.name == "Medlem":
responsible_group.name = f"{responsible_group.organization.name}:{MEMBER_GROUP_TYPE}"
responsible_group.group.name = (
f"{responsible_group.organization.name}:{MEMBER_GROUP_TYPE}:{responsible_group.uuid}"
)
responsible_group.group_type = MEMBER_GROUP_TYPE
responsible_group.save()


class Migration(migrations.Migration):

dependencies = [
("organizations", "0033_merge_0031_auto_20210909_1813_0032_auto_20210824_1457"),
]

operations = [migrations.RunPython(migrate_org_groups)]
13 changes: 13 additions & 0 deletions backend/apps/permissions/migrations/0035_merge_20221117_1825.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Generated by Django 3.2.16 on 2022-11-17 17:25

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("permissions", "0011_merge_0004_auto_20210824_2126_0010_auto_20210824_1546"),
("permissions", "0034_auto_20221114_1854"),
]

operations = []
3 changes: 1 addition & 2 deletions backend/apps/permissions/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def create_named_group(sender, instance: ResponsibleGroup, **kwargs):
try:
instance.group
except ObjectDoesNotExist:
prefix: str = instance.organization.name
group = Group.objects.create(name=f"{prefix}:{instance.name}:{uuid4().hex}")
group = Group.objects.create(name=f"{instance.name}:{uuid4().hex}")
instance.group = group


Expand Down
18 changes: 5 additions & 13 deletions backend/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@
}
],
"description": "Directs the executor to include this field or fragment only when the `if` argument is true.",
"locations": [
"FIELD",
"FRAGMENT_SPREAD",
"INLINE_FRAGMENT"
],
"locations": ["FIELD", "FRAGMENT_SPREAD", "INLINE_FRAGMENT"],
"name": "include"
},
{
Expand All @@ -45,11 +41,7 @@
}
],
"description": "Directs the executor to skip this field or fragment when the `if` argument is true.",
"locations": [
"FIELD",
"FRAGMENT_SPREAD",
"INLINE_FRAGMENT"
],
"locations": ["FIELD", "FRAGMENT_SPREAD", "INLINE_FRAGMENT"],
"name": "skip"
}
],
Expand Down Expand Up @@ -1581,7 +1573,7 @@
"deprecationReason": null,
"description": null,
"isDeprecated": false,
"name": "primaryGroup",
"name": "memberGroup",
"type": {
"kind": "OBJECT",
"name": "ResponsibleGroupType",
Expand All @@ -1593,7 +1585,7 @@
"deprecationReason": null,
"description": null,
"isDeprecated": false,
"name": "hrGroup",
"name": "adminGroup",
"type": {
"kind": "OBJECT",
"name": "ResponsibleGroupType",
Expand Down Expand Up @@ -10900,4 +10892,4 @@
]
}
}
}
}
Loading