-
Notifications
You must be signed in to change notification settings - Fork 7
Implemente a gestão dos grupos e perfis de usuários #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
5
commits into
main
Choose a base branch
from
copilot/implementar-gestao-grupos-perfis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ac204d4
Initial plan
Copilot f33ff75
feat: implement user group and profile management for team module
Copilot 740b884
feat: COLLECTION_TEAM_ADMIN can CRUD all company team members
Copilot b2b618c
feat: sync user auth.Group when team member is created/updated/deleted
Copilot 6d9d38d
refactor: move get_queryset filtering to model classmethods; fix cont…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| from django.contrib.auth.models import Group, Permission | ||
| from django.contrib.contenttypes.models import ContentType | ||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from team.models import ( | ||
| GROUP_NAMES, | ||
| COLLECTION_TEAM_ADMIN, | ||
| JOURNAL_TEAM_ADMIN, | ||
| CollectionTeamMember, | ||
| Company, | ||
| CompanyTeamMember, | ||
| JournalCompanyContract, | ||
| JournalTeamMember, | ||
| ) | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Create default user groups and assign permissions for team management" | ||
|
|
||
| def handle(self, *args, **options): | ||
| for name in GROUP_NAMES: | ||
| Group.objects.get_or_create(name=name) | ||
| self.stdout.write(f"Group '{name}' ensured.") | ||
|
|
||
| self._assign_permissions() | ||
| self.stdout.write(self.style.SUCCESS("User groups created/updated successfully.")) | ||
|
|
||
| def _assign_permissions(self): | ||
| # COLLECTION_TEAM_ADMIN: can manage all team members and Company CRUD | ||
| collection_admin_group, _ = Group.objects.get_or_create(name=COLLECTION_TEAM_ADMIN) | ||
| collection_admin_permissions = self._get_model_permissions( | ||
| [CollectionTeamMember, Company, JournalTeamMember, CompanyTeamMember, JournalCompanyContract] | ||
| ) | ||
| collection_admin_group.permissions.set(collection_admin_permissions) | ||
|
|
||
| # JOURNAL_TEAM_ADMIN: can manage journal team members and Company Contracts CRUD | ||
| journal_admin_group, _ = Group.objects.get_or_create(name=JOURNAL_TEAM_ADMIN) | ||
| journal_admin_permissions = self._get_model_permissions( | ||
| [JournalTeamMember, JournalCompanyContract] | ||
| ) | ||
| journal_admin_group.permissions.set(journal_admin_permissions) | ||
|
|
||
| # COMPANY_TEAM_ADMIN: can manage company team members | ||
| company_admin_group, _ = Group.objects.get_or_create(name=COMPANY_TEAM_ADMIN) | ||
| company_admin_permissions = self._get_model_permissions([CompanyTeamMember]) | ||
| company_admin_group.permissions.set(company_admin_permissions) | ||
|
|
||
| def _get_model_permissions(self, models): | ||
| permissions = [] | ||
| for model in models: | ||
| ct = ContentType.objects.get_for_model(model) | ||
| permissions.extend(Permission.objects.filter(content_type=ct)) | ||
| return permissions | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| from django.contrib.auth.models import Group | ||
| from django.db.models.signals import post_delete, post_save | ||
|
|
||
| from .models import ( | ||
| COLLECTION_TEAM_ADMIN, | ||
| COLLECTION_TEAM_MEMBER, | ||
| COMPANY_MEMBER, | ||
| COMPANY_TEAM_ADMIN, | ||
| JOURNAL_TEAM_ADMIN, | ||
| JOURNAL_TEAM_MEMBER, | ||
| CollectionTeamMember, | ||
| CompanyTeamMember, | ||
| JournalTeamMember, | ||
| TeamRole, | ||
| ) | ||
|
|
||
|
|
||
| def _roles_for_user(model_class, user): | ||
| """Return the set of active roles the user holds in a team model.""" | ||
| return set( | ||
| model_class.objects.filter(user=user, is_active_member=True) | ||
| .values_list("role", flat=True) | ||
| ) | ||
|
|
||
|
|
||
| def update_user_groups(user): | ||
| """ | ||
| Synchronise a user's auth.Group memberships to reflect their current | ||
| active team-member roles. Called after any team member is saved or deleted. | ||
| """ | ||
| if user is None: | ||
| return | ||
|
|
||
| collection_roles = _roles_for_user(CollectionTeamMember, user) | ||
| journal_roles = _roles_for_user(JournalTeamMember, user) | ||
| company_roles = _roles_for_user(CompanyTeamMember, user) | ||
|
|
||
| _sync_group(user, COLLECTION_TEAM_ADMIN, TeamRole.MANAGER in collection_roles) | ||
| _sync_group(user, COLLECTION_TEAM_MEMBER, TeamRole.MEMBER in collection_roles) | ||
| _sync_group(user, JOURNAL_TEAM_ADMIN, TeamRole.MANAGER in journal_roles) | ||
| _sync_group(user, JOURNAL_TEAM_MEMBER, TeamRole.MEMBER in journal_roles) | ||
| _sync_group(user, COMPANY_TEAM_ADMIN, TeamRole.MANAGER in company_roles) | ||
| _sync_group(user, COMPANY_MEMBER, TeamRole.MEMBER in company_roles) | ||
|
|
||
|
|
||
| def _sync_group(user, group_name, should_belong): | ||
| """Add or remove a user from a group, creating the group if needed.""" | ||
| group, _ = Group.objects.get_or_create(name=group_name) | ||
| if should_belong: | ||
| user.groups.add(group) | ||
| else: | ||
| user.groups.remove(group) | ||
|
|
||
|
|
||
| def _make_signal_handler(description): | ||
| def handler(sender, instance, **kwargs): | ||
| update_user_groups(instance.user) | ||
| handler.__name__ = description | ||
| return handler | ||
|
|
||
|
|
||
| _TEAM_MODELS = [CollectionTeamMember, JournalTeamMember, CompanyTeamMember] | ||
|
|
||
| for _model in _TEAM_MODELS: | ||
| post_save.connect( | ||
| _make_signal_handler(f"sync_{_model.__name__.lower()}_groups_on_save"), | ||
| sender=_model, | ||
| weak=False, | ||
| ) | ||
| post_delete.connect( | ||
| _make_signal_handler(f"sync_{_model.__name__.lower()}_groups_on_delete"), | ||
| sender=_model, | ||
| weak=False, | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The management command assigns Django model permissions to the groups (view, add, change, delete), but the ViewSets don't use a permission_helper_class to enforce these permissions for CRUD operations. The get_queryset methods only filter which records users can see, but don't prevent users from creating/editing/deleting records they shouldn't have access to. Consider implementing a custom permission_helper_class for each ViewSet to enforce the permission rules described in the PR (e.g., COLLECTION_TEAM_ADMIN can manage Company CRUD, JOURNAL_TEAM_ADMIN can manage JournalCompanyContract CRUD). See upload/permission_helper.py and upload/wagtail_hooks.py for examples of this pattern used in the codebase.