From 8636b456a8660eef30fe688a02a903a498f27f10 Mon Sep 17 00:00:00 2001 From: Eliot Berriot Date: Tue, 19 Jun 2018 20:11:40 +0200 Subject: [PATCH] See #212: user detail profile --- api/funkwhale_api/manage/serializers.py | 22 ++- api/funkwhale_api/users/models.py | 4 + api/tests/manage/test_serializers.py | 23 +++ .../components/manage/users/UsersTable.vue | 2 +- front/src/router/index.js | 7 + front/src/views/admin/users/UsersDetail.vue | 177 ++++++++++++++++++ 6 files changed, 231 insertions(+), 4 deletions(-) create mode 100644 front/src/views/admin/users/UsersDetail.vue diff --git a/api/funkwhale_api/manage/serializers.py b/api/funkwhale_api/manage/serializers.py index 13f886a7e..6e57db81f 100644 --- a/api/funkwhale_api/manage/serializers.py +++ b/api/funkwhale_api/manage/serializers.py @@ -70,8 +70,16 @@ class ManageTrackFileActionSerializer(common_serializers.ActionSerializer): return objects.delete() +class PermissionsSerializer(serializers.Serializer): + def to_representation(self, o): + return o.get_permissions(defaults=self.context.get("default_permissions")) + + def to_internal_value(self, o): + return {"permissions": o} + + class ManageUserSerializer(serializers.ModelSerializer): - permissions = serializers.SerializerMethodField() + permissions = PermissionsSerializer(source="*") class Meta: model = users_models.User @@ -97,5 +105,13 @@ class ManageUserSerializer(serializers.ModelSerializer): "last_activity", ] - def get_permissions(self, o): - return o.get_permissions(defaults=self.context.get("default_permissions")) + def update(self, instance, validated_data): + instance = super().update(instance, validated_data) + permissions = validated_data.pop("permissions", {}) + if permissions: + for p, value in permissions.items(): + setattr(instance, "permission_{}".format(p), value) + instance.save( + update_fields=["permission_{}".format(p) for p in permissions.keys()] + ) + return instance diff --git a/api/funkwhale_api/users/models.py b/api/funkwhale_api/users/models.py index 055a971b3..15d16db23 100644 --- a/api/funkwhale_api/users/models.py +++ b/api/funkwhale_api/users/models.py @@ -94,6 +94,10 @@ class User(AbstractUser): perms[p] = v return perms + @property + def all_permissions(self): + return self.get_permissions() + def has_permissions(self, *perms, **kwargs): operator = kwargs.pop("operator", "and") if operator not in ["and", "or"]: diff --git a/api/tests/manage/test_serializers.py b/api/tests/manage/test_serializers.py index 893cfd86e..2f0c6bc25 100644 --- a/api/tests/manage/test_serializers.py +++ b/api/tests/manage/test_serializers.py @@ -8,3 +8,26 @@ def test_manage_track_file_action_delete(factories): s.handle_delete(tfs.__class__.objects.all()) assert tfs.__class__.objects.count() == 0 + + +def test_user_update_permission(factories): + user = factories["users.User"]( + permission_library=False, + permission_upload=False, + permission_federation=True, + permission_settings=True, + is_active=True, + ) + s = serializers.ManageUserSerializer( + user, + data={"is_active": False, "permissions": {"federation": False, "upload": True}}, + ) + s.is_valid(raise_exception=True) + s.save() + user.refresh_from_db() + + assert user.is_active is False + assert user.permission_federation is False + assert user.permission_upload is True + assert user.permission_library is False + assert user.permission_settings is True diff --git a/front/src/components/manage/users/UsersTable.vue b/front/src/components/manage/users/UsersTable.vue index 746b158a7..5658583c3 100644 --- a/front/src/components/manage/users/UsersTable.vue +++ b/front/src/components/manage/users/UsersTable.vue @@ -45,7 +45,7 @@