Skip to content

Commit

Permalink
Merge pull request #27 from unicef/generate-pwd
Browse files Browse the repository at this point in the history
send through email
  • Loading branch information
domdinicola authored Aug 16, 2023
2 parents 00fc361 + 0f06613 commit 01866dc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
5 changes: 5 additions & 0 deletions src/aurora/security/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from .ad import ADUSerMixin
from .forms import AuroraRoleForm
from .utils import generate_pwd

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -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",)
Expand Down
40 changes: 40 additions & 0 deletions src/aurora/security/utils.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 01866dc

Please sign in to comment.