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)
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 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
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.
50 page
= client
.get_user_page(count
=50, page
=1)
59 def _reset_password(self
, user
):
60 """Given a mailmanclient.restobject.user.User object, reset its password
61 to None in the database.
63 user
.password
= self
._get
_random
_password
()
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.
71 tok
= os
.urandom(PASSWORD_BYTES
)
72 return base64
.urlsafe_b64encode(tok
).rstrip(b
'=').decode('ascii')