Update po files
[mailman-postorious.git] / src / postorius / management / commands / reset_passwords.py
blob7f4f32593c74d82a8920ab3ee35298f38deb1346
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2017-2021 by the Free Software Foundation, Inc.
4 # This file is part of Postorius.
6 # Postorius is free software: you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option)
9 # any later version.
11 # Postorius is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 # more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Postorius. If not, see <http://www.gnu.org/licenses/>.
20 import base64
21 import os
23 from django.core.management.base import BaseCommand
25 from django_mailman3.lib.mailman import get_mailman_client
28 PASSWORD_BYTES = 32
31 class Command(BaseCommand):
33 help = '''Reset passwords of all users in Mailman Core. This does not affect
34 the login passwords of users in Django or any other social
35 authentication. Users will be able to login with their current
36 passwords. Mailman Core maintains a second set of passwords for
37 every user, which would be set to a random value of base64 encode
38 32 bytes.
39 '''
41 def handle(self, *args, **kwargs):
42 client = get_mailman_client()
43 for user in self._get_all_users(client):
44 self._reset_password(user)
46 def _get_all_users(self, client):
47 """Given a mailmanclient.Client instance, returns an iterator of
48 paginated user records.
49 """
50 page = client.get_user_page(count=50, page=1)
51 while True:
52 for user in page:
53 yield user
54 if page.has_next:
55 page = page.next
56 else:
57 break
59 def _reset_password(self, user):
60 """Given a mailmanclient.restobject.user.User object, reset its password
61 to None in the database.
62 """
63 user.password = self._get_random_password()
64 user.save()
65 msg = 'Password reset for {}'.format(user)
66 self.stdout.write(self.style.SUCCESS(msg))
68 def _get_random_password(self):
69 """Generate a random password for a user.
70 """
71 tok = os.urandom(PASSWORD_BYTES)
72 return base64.urlsafe_b64encode(tok).rstrip(b'=').decode('ascii')