Commit 008ac513 by Scott Duckworth

optionally send email when a key is added

parent 4e2a4c7e
...@@ -29,7 +29,10 @@ ...@@ -29,7 +29,10 @@
from django.db import models from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db.models.signals import pre_save
from django.dispatch import receiver
from django_sshkey.util import PublicKeyParseError, pubkey_parse from django_sshkey.util import PublicKeyParseError, pubkey_parse
from django_sshkey import settings
class UserKey(models.Model): class UserKey(models.Model):
user = models.ForeignKey(User, db_index=True) user = models.ForeignKey(User, db_index=True)
...@@ -97,3 +100,21 @@ class UserKey(models.Model): ...@@ -97,3 +100,21 @@ class UserKey(models.Model):
def touch(self): def touch(self):
import datetime import datetime
self.last_used = datetime.datetime.now() self.last_used = datetime.datetime.now()
@receiver(pre_save, sender=UserKey)
def send_email_add_key(sender, instance, **kwargs):
if not settings.SSHKEY_EMAIL_ADD_KEY or instance.pk:
return
meta = getattr(instance, 'META', None)
remote_addr = None
if meta:
remote_addr = meta.get('REMOTE_ADDR')
body = settings.SSHKEY_EMAIL_ADD_KEY_BODY.format(
user_first_name = instance.user.first_name,
user_last_name = instance.user.last_name,
user_full_name = instance.user.get_full_name(),
key_name = instance.name,
key_fingerprint = instance.fingerprint,
remote_addr = remote_addr or "<unknown>",
)
instance.user.email_user(settings.SSHKEY_EMAIL_ADD_KEY_SUBJECT, body)
...@@ -29,3 +29,15 @@ ...@@ -29,3 +29,15 @@
from django.conf import settings from django.conf import settings
SSHKEY_AUTHORIZED_KEYS_OPTIONS = getattr(settings, 'SSHKEY_AUTHORIZED_KEYS_OPTIONS', None) SSHKEY_AUTHORIZED_KEYS_OPTIONS = getattr(settings, 'SSHKEY_AUTHORIZED_KEYS_OPTIONS', None)
SSHKEY_EMAIL_ADD_KEY = getattr(settings, 'SSHKEY_EMAIL_ADD_KEY', True)
SSHKEY_EMAIL_ADD_KEY_SUBJECT = getattr(settings, 'SSHKEY_EMAIL_ADD_KEY_SUBJECT',
"A new public key was added to your account"
)
SSHKEY_EMAIL_ADD_KEY_BODY = getattr(settings, 'SSHKEY_EMAIL_ADD_KEY_BODY',
"{user_first_name},\n\n"
"The following SSH public key was added to your account from {remote_addr}:\n\n"
"{key_name}\n"
"{key_fingerprint}\n\n"
"If you believe this key was added in error, you should delete the key."
)
DEFAULT_FROM_EMAIL = settings.DEFAULT_FROM_EMAIL
...@@ -77,6 +77,7 @@ def userkey_list(request): ...@@ -77,6 +77,7 @@ def userkey_list(request):
def userkey_add(request): def userkey_add(request):
if request.method == 'POST': if request.method == 'POST':
userkey = UserKey(user=request.user) userkey = UserKey(user=request.user)
userkey.META = request.META
form = UserKeyForm(request.POST, instance=userkey) form = UserKeyForm(request.POST, instance=userkey)
if form.is_valid(): if form.is_valid():
form.save() form.save()
......
# Django settings for testproject project. # Django settings for testproject project.
import getpass
import socket
DEBUG = True DEBUG = True
TEMPLATE_DEBUG = DEBUG TEMPLATE_DEBUG = DEBUG
DEFAULT_FROM_EMAIL = '%s@%s' % (getpass.getuser(), socket.gethostname())
ADMINS = ( ADMINS = (
# ('Your Name', 'your_email@example.com'), # ('Your Name', 'your_email@example.com'),
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment