From f091b20773276493652f51eae291f94ccd6c88f3 Mon Sep 17 00:00:00 2001 From: Yingtong Li Date: Wed, 12 Feb 2020 16:02:12 +1100 Subject: [PATCH] Add users to group by email --- selfserv/settings.example.py | 1 + ssmain/management/commands/addgroup.py | 38 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 ssmain/management/commands/addgroup.py diff --git a/selfserv/settings.example.py b/selfserv/settings.example.py index 1aa4c52..4c53afb 100644 --- a/selfserv/settings.example.py +++ b/selfserv/settings.example.py @@ -150,6 +150,7 @@ SOCIAL_AUTH_PIPELINE = ( 'social_core.pipeline.social_auth.social_uid', 'social_core.pipeline.social_auth.social_user', 'social_core.pipeline.user.get_username', + 'social_core.pipeline.social_auth.associate_by_email', 'social_core.pipeline.user.create_user', 'social_core.pipeline.social_auth.associate_user', 'social_core.pipeline.social_auth.load_extra_data', diff --git a/ssmain/management/commands/addgroup.py b/ssmain/management/commands/addgroup.py new file mode 100644 index 0000000..f082da1 --- /dev/null +++ b/ssmain/management/commands/addgroup.py @@ -0,0 +1,38 @@ +# Society Self-Service +# Copyright © 2018-2020 Yingtong Li (RunasSudo) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.core.management.base import BaseCommand, CommandError + +from django.contrib.auth.models import User, Group + +class Command(BaseCommand): + help = 'Adds the users with the specified emails to the specified group' + + def add_arguments(self, parser): + parser.add_argument('group') + parser.add_argument('email', nargs='*') + + def handle(self, *args, **options): + group = Group.objects.get(name=options['group']) + + for email in options['email']: + try: + user = User.objects.get(email=email) + except User.DoesNotExist: + user = User.objects.create_user(email.split('@')[0], email) + user.save() + + user.groups.add(group)