summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYingtong Li <runassudo@yingtongli.me>2020-02-12 16:02:12 +1100
committerYingtong Li <runassudo@yingtongli.me>2020-02-12 16:02:12 +1100
commitf091b20773276493652f51eae291f94ccd6c88f3 (patch)
treeb076741534d99b01126e523e628f893a75b31f54
parent1975c482738bbdfe4e4dd4fde1ec5cc678bfffbf (diff)
Add users to group by email
-rw-r--r--selfserv/settings.example.py1
-rw-r--r--ssmain/management/commands/addgroup.py38
2 files changed, 39 insertions, 0 deletions
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 <https://www.gnu.org/licenses/>.
+
+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)
Contact (issues, pull requests, etc.) at git@yingtongli.me. Generated by cgit.