Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
django-sshkey
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
008ac513
authored
Jul 02, 2014
by
Scott Duckworth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optionally send email when a key is added
parent
4e2a4c7e
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
39 additions
and
0 deletions
+39
-0
django_sshkey/models.py
+21
-0
django_sshkey/settings.py
+12
-0
django_sshkey/views.py
+1
-0
testproject/settings.py
+5
-0
No files found.
django_sshkey/models.py
View file @
008ac513
...
@@ -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
)
django_sshkey/settings.py
View file @
008ac513
...
@@ -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
django_sshkey/views.py
View file @
008ac513
...
@@ -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
()
...
...
testproject/settings.py
View file @
008ac513
# 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'),
)
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment