def create_user(self, username, email, password, user_type, first_name='', last_name='', phone_number=None, date_of_birth=None, validate=True, is_active=False, send_password_mail=True, send_otp_mail=False, created_by=None): if not email or not username: raise ValueError('Users must have an email and a username') if password is None: password = self.make_random_password() validate = False username = username.lower() user = self.model( first_name=first_name, last_name=last_name, email=self.normalize_email(email), username=username, phone_number=phone_number, date_of_birth=date_of_birth, user_type=user_type, assigned_to=created_by, ) if validate: validate_password(password) user.set_password(password) # Activate User on email confirmation, bypass for superuser user.is_active = is_active user.save(using=self.db) TOTPDevice.objects.create(name='Phone', user=user, confirmed=False) if not user.is_internal_user(): account = Account.objects.create(user=user) Card.objects.create(account=account) if send_password_mail: CommonHelpers.send_password_mail(user) if send_otp_mail: CommonHelpers.send_confirmation_mail(user) return user
def post(self, request): user = request.user if user.is_admin(): form = SignUpForms.PasswordResetRequestForm(request.POST) if form.is_valid(): target_user = form.cleaned_data['user'] with db_transaction.atomic(): target_user = MyUser.objects.filter(id=target_user.id).select_for_update().first() if target_user: target_user.is_active = False target_user.save() TOTPDevice.objects.filter(user=target_user).delete() TOTPDevice.objects.create(name='Phone', user=target_user, confirmed=False) else: return render(request, 'error.html', { 'err': 'Action could not be completed', }) CommonHelpers.send_password_mail(target_user) messages.success(request, 'Request Initiated') logger.info("Password Reset Initiated for %s by admin %s", str(target_user.username), str(request.user.username)) return HttpResponseRedirect(reverse('app:HomeView')) return render(request, 'form_template.html', { 'title': 'Reset User', 'form': form, }) else: return render(request, 'error.html', { 'err': 'You do not have permission for this', })