diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4623f385..8a9369b9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -39,7 +39,7 @@ jobs: python-version: [ "3.9", ] experimental: [ false ] include: - - django-version: "4.0" + - django-version: "4.2" python-version: "3.9" experimental: true continue-on-error: ${{ matrix.experimental }} diff --git a/src/aurora/security/admin.py b/src/aurora/security/admin.py index f76171fd..55cd373e 100644 --- a/src/aurora/security/admin.py +++ b/src/aurora/security/admin.py @@ -17,6 +17,7 @@ from .ad import ADUSerMixin from .forms import AuroraRoleForm +from .utils import generate_pwd logger = logging.getLogger(__name__) @@ -61,6 +62,10 @@ def hijack(self, request, pk): hijacked = self.get_object(request, pk) impersonate(request, hijacked) + @button() + def generate_password(self, request, pk): + generate_pwd(pk) + class UserProfileAdmin(SmartModelAdmin): search_fields = ("user__username",) diff --git a/src/aurora/security/utils.py b/src/aurora/security/utils.py new file mode 100644 index 00000000..d5ee40c2 --- /dev/null +++ b/src/aurora/security/utils.py @@ -0,0 +1,40 @@ +import random +import string + +from django.conf import settings +from django.contrib.auth import get_user_model +from django.core.mail import send_mail + + +def generate_password(length=20): + pwd = "" + count = 0 + length = max(8, length) + while count < length: + upper = [random.choice(string.ascii_uppercase)] + lower = [random.choice(string.ascii_lowercase)] + num = [random.choice(string.digits)] + symbol = [random.choice(string.punctuation)] + everything = upper + lower + num + symbol + pwd += random.choice(everything) + count += 1 + return pwd + + +def generate_pwd(user_pk): + subject = "Aurora Credentials" + pwd = generate_password() + user = get_user_model().objects.get(pk=user_pk) + user.set_password(pwd) + user.save() + + message = ( + f"Dear {user.first_name}, \n" + f"you can login to http://register.unicef.org using {user.email} and {pwd} \n\n" + f"Regards, \n" + f"Aurora team" + ) + recipient_list = [ + user.email, + ] + send_mail(subject, message, settings.EMAIL_HOST_USER, recipient_list)