Пример #1
0
    def send_notification(self, user, unhashed_password, is_update=False):

        title = mem.customizer._configurations \
            .get('project', {}) \
            .get('title', "Unkown title")

        subject = "%s: " % title
        if is_update:
            subject += "password changed"
            template = "update_credentials.html"
        else:
            subject += "new credentials"
            template = "new_credentials.html"

        replaces = {
            "username": user.email,
            "password": unhashed_password
        }

        html = get_html_template(template, replaces)

        body = """
            Username: %s"
            Password: %s"
        """ % (user.email, unhashed_password)

        if html is None:
            send_mail(body, subject, user.email)
        else:
            send_mail(html, subject, user.email, plain_body=body)
Пример #2
0
    def send_notification(self, user, unhashed_password, is_update=False):

        title = get_project_configuration("project.title",
                                          default='Unkown title')

        if is_update:
            subject = "{}: password changed".format(title)
            template = "update_credentials.html"
        else:
            subject = "{}: new credentials".format(title)
            template = "new_credentials.html"

        replaces = {"username": user.email, "password": unhashed_password}

        html = get_html_template(template, replaces)

        body = """
Username: "******"
Password: "******"
        """.format(
            user.email,
            unhashed_password,
        )

        if html is None:
            send_mail(body, subject, user.email)
        else:
            send_mail(html, subject, user.email, plain_body=body)
Пример #3
0
    def wrapper(self, *args, **kwargs):

        try:
            return func(self, *args, **kwargs)

        except BaseException:

            task_id = self.request.id
            task_name = self.request.task

            log.error("Celery task {} failed ({})", task_id, task_name)
            arguments = str(self.request.args)
            log.error("Failed task arguments: {}", arguments[0:256])
            log.error("Task error: {}", traceback.format_exc())

            if send_mail_is_active():
                log.info("Sending error report by email", task_id, task_name)

                body = """
Celery task {} failed

Name: {}

Arguments: {}

Error: {}
""".format(task_id, task_name, str(self.request.args), traceback.format_exc())

                project = get_project_configuration(
                    "project.title",
                    default='Unkown title',
                )
                subject = "{}: task {} failed".format(project, task_name)
                send_mail(body, subject)
Пример #4
0
def notify_registration(user):
    var = "REGISTRATION_NOTIFICATIONS"
    if detector.get_bool_from_os(var):
        # Sending an email to the administrator
        title = get_project_configuration(
            "project.title", default='Unkown title'
        )
        subject = "{} New credentials requested".format(title)
        body = "New credentials request from {}".format(user.email)

        send_mail(body, subject)
Пример #5
0
def send_activation_link(auth, user):

    title = get_project_configuration(
        "project.title", default='Unkown title'
    )

    activation_token, jti = auth.create_reset_token(user, auth.ACTIVATE_ACCOUNT)

    domain = os.environ.get("DOMAIN")
    if PRODUCTION:
        protocol = "https"
    else:
        protocol = "http"

    rt = activation_token.replace(".", "+")
    log.debug("Activation token: {}", rt)
    url = "{}://{}/public/register/{}".format(protocol, domain, rt)
    body = "Follow this link to activate your account: {}".format(url)

    obj = meta.get_customizer_class('apis.profile', 'CustomActivation')

    # NORMAL ACTIVATION
    if obj is None:

        # customized template
        template_file = "activate_account.html"
        html_body = get_html_template(template_file, {"url": url})
        if html_body is None:
            html_body = body
            body = None

        # NOTE: possibility to define a different subject
        default_subject = "{} account activation".format(title)
        subject = os.environ.get('EMAIL_ACTIVATION_SUBJECT', default_subject)

        sent = send_mail(html_body, subject, user.email, plain_body=body)
        if not sent:
            raise BaseException("Error sending email, please retry")

    # EXTERNAL SMTP/EMAIL SENDER
    else:
        try:
            obj.request_activation(name=user.name, email=user.email, url=url)
        except BaseException as e:
            log.error(
                "Could not send email with custom service:\n{}: {}",
                e.__class__.__name__,
                e,
            )
            raise

    auth.save_token(user, activation_token, jti, token_type=auth.ACTIVATE_ACCOUNT)
Пример #6
0
def send_notification(self,
                      recipient,
                      subject,
                      template,
                      replaces,
                      task_id,
                      failure=None):
    """
    Send a notification email to a given recipient. If the failure is passed,
    an email is also sent to the system administrator with some more details
    about failure.
    """
    body = get_html_template(template, replaces)
    plain = "Sorry User, your job ID {task} is failed".format(task=task_id)
    send_mail(body, subject, recipient, plain_body=plain)

    if failure is not None:
        replaces['task_id'] = task_id
        replaces['failure'] = failure
        body = get_html_template(template, replaces)
        plain = "Job ID {task} is failed".format(task=task_id)
        send_mail(body, subject, plain_body=plain)
Пример #7
0
def send_internal_password_reset(uri, title, reset_email):
    # Internal templating
    body = "Follow this link to reset password: {}".format(uri)
    html_body = get_html_template("reset_password.html", {"url": uri})
    if html_body is None:
        log.warning("Unable to find email template")
        html_body = body
        body = None
    subject = "{} Password Reset".format(title)

    # Internal email sending
    c = send_mail(html_body, subject, reset_email, plain_body=body)
    if not c:
        raise RestApiException("Error sending email, please retry")
Пример #8
0
    def post(self):

        if not send_mail_is_active():
            raise RestApiException(
                'Server misconfiguration, unable to reset password. ' +
                'Please report this error to adminstrators',
                status_code=hcodes.HTTP_BAD_REQUEST)

        reset_email = self.get_input(single_parameter='reset_email')

        if reset_email is None:
            raise RestApiException(
                'Invalid reset email',
                status_code=hcodes.HTTP_BAD_FORBIDDEN)

        reset_email = reset_email.lower()

        user = self.auth.get_user_object(username=reset_email)

        if user is None:
            raise RestApiException(
                'Sorry, %s ' % reset_email +
                'is not recognized as a valid username or email address',
                status_code=hcodes.HTTP_BAD_FORBIDDEN)

        title = mem.customizer._configurations \
            .get('project', {}) \
            .get('title', "Unkown title")

        # invalidate previous reset tokens
        tokens = self.auth.get_tokens(user=user)
        for t in tokens:
            token_type = t.get("token_type")
            if token_type is None:
                continue
            if token_type != self.auth.PWD_RESET:
                continue

            tok = t.get("token")
            if self.auth.invalidate_token(tok):
                log.info("Previous reset token invalidated: %s", tok)

        # Generate a new reset token
        reset_token, jti = self.auth.create_temporary_token(
            user,
            duration=86400,
            token_type=self.auth.PWD_RESET
        )

        domain = os.environ.get("DOMAIN")
        if PRODUCTION:
            protocol = "https"
        else:
            protocol = "http"

        u = "%s://%s/public/reset/%s" % (protocol, domain, reset_token)
        body = "link to reset password: %s" % u

        replaces = {
            "url": u
        }
        html_body = get_html_template("reset_password.html", replaces)
        # html_body = "link to reset password: <a href='%s'>click here</a>" % u
        subject = "%s Password Reset" % title
        send_mail(html_body, subject, reset_email, plain_body=body)

        self.auth.save_token(
            user, reset_token, jti, token_type=self.auth.PWD_RESET)

        msg = "We are sending an email to your email address where " + \
            "you will find the link to enter a new password"
        return msg