jumpserver/apps/users/utils.py

130 lines
3.5 KiB
Python
Raw Normal View History

2016-08-09 09:27:37 +00:00
# ~*~ coding: utf-8 ~*~
#
2016-08-28 15:58:22 +00:00
2016-08-31 17:12:02 +00:00
from __future__ import unicode_literals
2016-08-30 17:00:20 +00:00
import os
import logging
from paramiko.rsakey import RSAKey
2016-08-28 15:58:22 +00:00
from django.contrib.auth.mixins import UserPassesTestMixin
from django.urls import reverse_lazy
2016-09-03 16:51:36 +00:00
from django.utils.translation import ugettext as _
2016-08-28 15:58:22 +00:00
2016-08-31 17:12:02 +00:00
from common.tasks import send_mail_async
from common.utils import reverse
from users.models import User
2016-08-31 11:28:06 +00:00
2016-08-30 17:00:20 +00:00
try:
import cStringIO as StringIO
except ImportError:
import StringIO
logger = logging.getLogger('jumpserver')
2016-08-28 15:58:22 +00:00
class AdminUserRequiredMixin(UserPassesTestMixin):
login_url = reverse_lazy('users:login')
def test_func(self):
return self.request.user.is_staff
2016-08-30 17:00:20 +00:00
def ssh_key_gen(length=2048, password=None, username='root', hostname=None):
"""Generate user ssh private and public key
Use paramiko RSAKey generate it.
"""
if hostname is None:
hostname = os.uname()[1]
f = StringIO.StringIO()
try:
2016-09-03 16:51:36 +00:00
logger.debug(_('Begin to generate ssh private key ...'))
2016-08-30 17:00:20 +00:00
private_key_obj = RSAKey.generate(length)
private_key_obj.write_private_key(f, password=password)
private_key = f.getvalue()
public_key = "%(key_type)s %(key_content)s %(username)s@%(hostname)s" % {
'key_type': private_key_obj.get_name(),
'key_content': private_key_obj.get_base64(),
'username': username,
'hostname': hostname,
}
2016-09-03 16:51:36 +00:00
logger.debug(_('Finish to generate ssh private key ...'))
2016-08-30 17:00:20 +00:00
return private_key, public_key
except IOError:
2016-09-03 16:51:36 +00:00
raise IOError(_('These is error when generate ssh key.'))
2016-08-30 17:00:20 +00:00
2016-08-31 17:12:02 +00:00
def user_add_success_next(user):
2016-09-03 16:51:36 +00:00
subject = _('Create account successfully')
2016-08-31 17:12:02 +00:00
recipient_list = [user.email]
2016-09-03 16:51:36 +00:00
message = _("""
Hello %(name)s:
2016-08-31 17:12:02 +00:00
</br>
2016-09-03 16:51:36 +00:00
Your account has been created successfully
2016-08-31 17:12:02 +00:00
</br>
2016-09-03 16:51:36 +00:00
<a href="%(rest_password_url)s?token=%(rest_password_token)s">click here to set your password</a>
2016-08-31 17:12:02 +00:00
</br>
2016-09-03 16:51:36 +00:00
This link is valid for 1 hour. After it expires, <a href="%(forget_password_url)s?email=%(email)s">request new one</a>
2016-08-31 17:12:02 +00:00
</br>
---
</br>
2016-09-03 16:51:36 +00:00
<a href="%(login_url)s">Login direct</a>
2016-08-31 17:12:02 +00:00
</br>
2016-09-03 16:51:36 +00:00
""") % {
2016-08-31 17:12:02 +00:00
'name': user.name,
'rest_password_url': reverse('users:reset-password', external=True),
2016-09-01 15:09:58 +00:00
'rest_password_token': user.generate_reset_token(),
2016-09-06 07:09:00 +00:00
'forget_password_url': reverse('users:forgot-password', external=True),
2016-09-01 15:09:58 +00:00
'email': user.email,
'login_url': reverse('users:login', external=True),
}
send_mail_async.delay(subject, message, recipient_list, html_message=message)
def send_reset_password_mail(user):
2016-09-03 16:51:36 +00:00
subject = _('Reset password')
2016-09-01 15:09:58 +00:00
recipient_list = [user.email]
2016-09-03 16:51:36 +00:00
message = _("""
Hello %(name)s:
2016-09-01 15:09:58 +00:00
</br>
2016-09-03 16:51:36 +00:00
Please click the link below to reset your password, if not your request, concern your account security
2016-09-01 15:09:58 +00:00
</br>
2016-09-03 16:51:36 +00:00
<a href="%(rest_password_url)s?token=%(rest_password_token)s">Click here reset password</a>
2016-09-01 15:09:58 +00:00
</br>
2016-09-03 16:51:36 +00:00
This link is valid for 1 hour. After it expires, <a href="%(forget_password_url)s?email=%(email)s">request new one<</a>
2016-09-01 15:09:58 +00:00
</br>
---
</br>
2016-09-03 16:51:36 +00:00
<a href="%(login_url)s">Login direct</a>
2016-09-01 15:09:58 +00:00
</br>
2016-09-03 16:51:36 +00:00
""") % {
2016-09-01 15:09:58 +00:00
'name': user.name,
'rest_password_url': reverse('users:reset-password', external=True),
'rest_password_token': user.generate_reset_token(),
2016-09-06 07:09:00 +00:00
'forget_password_url': reverse('users:forgot-password', external=True),
2016-08-31 17:12:02 +00:00
'email': user.email,
'login_url': reverse('users:login', external=True),
}
send_mail_async.delay(subject, message, recipient_list, html_message=message)
2016-08-30 17:00:20 +00:00