diff --git a/releases/unreleased/users-permissions-migrated.yml b/releases/unreleased/users-permissions-migrated.yml new file mode 100644 index 00000000..f01a314f --- /dev/null +++ b/releases/unreleased/users-permissions-migrated.yml @@ -0,0 +1,9 @@ +--- +title: Users permissions migrated +category: fixed +author: Jose Javier Merchante +issue: 849 +notes: > + User permissions are stored in a separate table within + the database. These permissions will be automatically + migrated when running `sortinghat-admin upgrade`. diff --git a/sortinghat/core/migrations/0009_tenant_perm_group.py b/sortinghat/core/migrations/0009_tenant_perm_group.py index 7f6a15a7..0f4a016d 100644 --- a/sortinghat/core/migrations/0009_tenant_perm_group.py +++ b/sortinghat/core/migrations/0009_tenant_perm_group.py @@ -13,6 +13,6 @@ class Migration(migrations.Migration): migrations.AddField( model_name='tenant', name='perm_group', - field=models.CharField(default='readonly', max_length=128), + field=models.CharField(default='user', max_length=128), ), ] diff --git a/sortinghat/server/sortinghat_admin.py b/sortinghat/server/sortinghat_admin.py index ac8d41ba..a187629a 100644 --- a/sortinghat/server/sortinghat_admin.py +++ b/sortinghat/server/sortinghat_admin.py @@ -168,6 +168,7 @@ def upgrade(no_database): _install_static_files() _setup_group_permissions(database='default') + _migrate_users_permissions() click.secho("SortingHat upgrade completed", fg='bright_cyan') @@ -384,6 +385,35 @@ def _setup_group_permissions(database='default'): click.echo("SortingHat groups created.\n") +def _migrate_users_permissions(): + """Migrate permissions for users from the previous version""" + + from sortinghat.core.models import Tenant + from django.contrib.auth.models import Group + + users = get_user_model().objects.all() + + if settings.MULTI_TENANT: + for user in users: + group = user.groups.first() + if not group: + continue + Tenant.objects.filter(user=user).update(perm_group=group.name) + click.echo(f"Permissions for '{user}' updated to '{group.name}'.") + user.groups.clear() + else: + for user in users: + group = user.groups.first() + if group: + continue + if user.is_superuser: + group = Group.objects.get(name='admin') + else: + group = Group.objects.get(name='user') + click.echo(f"Permissions for '{user}' updated to '{group.name}'.") + user.groups.set([group.id]) + + def _install_static_files(): """Collect static files and installed them."""