1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2017-2023 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)
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
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/>.
23 from django
.core
.management
.base
import BaseCommand
25 from django_mailman3
.lib
.mailman
import get_mailman_client
31 class Command(BaseCommand
):
33 help = """Reset passwords of all users in Mailman Core. This does not
34 affect the login passwords of users in Django or any other social
35 authentication. Users will be able to login with their current passwords.
36 Mailman Core maintains a second set of passwords for every user, which
37 would be set to a random value of base64 encode 32 bytes."""
39 def handle(self
, *args
, **kwargs
):
40 client
= get_mailman_client()
41 for user
in self
._get
_all
_users
(client
):
42 self
._reset
_password
(user
)
44 def _get_all_users(self
, client
):
45 """Given a mailmanclient.Client instance, returns an iterator of
46 paginated user records.
48 page
= client
.get_user_page(count
=50, page
=1)
57 def _reset_password(self
, user
):
58 """Given a mailmanclient.restobject.user.User object, reset its
59 password to None in the database.
61 user
.password
= self
._get
_random
_password
()
63 msg
= 'Password reset for {}'.format(user
)
64 self
.stdout
.write(self
.style
.SUCCESS(msg
))
66 def _get_random_password(self
):
67 """Generate a random password for a user."""
68 tok
= os
.urandom(PASSWORD_BYTES
)
69 return base64
.urlsafe_b64encode(tok
).rstrip(b
'=').decode('ascii')