Skip to content

Commit

Permalink
initial support for confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Jun 7, 2016
1 parent 6e80d42 commit c3a51ca
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
36 changes: 28 additions & 8 deletions src/appier_extras/parts/admin/models/account.py
Expand Up @@ -326,7 +326,7 @@ def login_key(cls, key):
return account

@classmethod
def recover(cls, identifier):
def recover(cls, identifier, send_email = False):
# verifies if a valid identifier has been provided because that value
# is required for the proper account recover process to be executed
if not identifier:
Expand Down Expand Up @@ -360,7 +360,7 @@ def recover(cls, identifier):

# runs the recover instance method that should generate a new reset token
# for the account and send the proper notifications
return account.recover_s()
return account.recover_s(send_email = send_email)

@classmethod
def reset(cls, reset_token, password, password_confirm):
Expand All @@ -369,9 +369,9 @@ def reset(cls, reset_token, password, password_confirm):
return account

@classmethod
def confirm(cls, confirmation_token):
def confirm(cls, confirmation_token, send_email = False):
account = cls.validate_confirmation(confirmation_token)
account.confirm_s()
account.confirm_s(send_email = send_email)
return account

@classmethod
Expand Down Expand Up @@ -498,15 +498,16 @@ def touch_s(self):
self.last_login = time.time()
self.save()

def confirm_s(self):
def confirm_s(self, send_email = False):
self.confirmation_token = None
self.enabled = True
if send_email: self.email_confirm()
self.save()

def recover_s(self):
def recover_s(self, send_email = False):
self.reset_token = self.secret()
self.save()
self.email_recover()
if send_email: self.email_recover()
return self.reset_token

def reset_s(self, password, password_confirm):
Expand Down Expand Up @@ -554,10 +555,16 @@ def generate_key_s(self, force = False):
self.key = self.secret()
self.save()

@appier.operation(name = "Mark Unconfirmed")
def mark_unconfirmed_s(self, owner = None):
self.enabled = False
self.confirmation_token = self.secret()
self.save()

@appier.operation(name = "Email New")
def email_new(self, password = None, owner = None):
owner = owner or appier.get_app()
account = self.reload(meta = True)
account = self.reload(rules = False, meta = True)
base.Base.send_email_g(
owner,
"admin/email/account/new.html.tpl",
Expand All @@ -568,6 +575,19 @@ def email_new(self, password = None, owner = None):
account_password = password
)

@appier.operation(name = "Email Confirm")
def email_confirm(self, owner = None):
owner = owner or appier.get_app()
account = self.reload(rules = False, meta = True)
base.Base.send_email_g(
owner,
"admin/email/account/confirm.html.tpl",
receivers = [self.email_f],
subject = "Confirm account",
title = "Confirm account",
account = account
)

@appier.operation(name = "Email Recover")
def email_recover(self, owner = None):
owner = owner or appier.get_app()
Expand Down
19 changes: 17 additions & 2 deletions src/appier_extras/parts/admin/part.py
Expand Up @@ -118,6 +118,7 @@ def routes(self):
(("POST"), "/admin/recover", self.recover_do),
(("GET"), "/admin/reset", self.reset),
(("POST"), "/admin/reset", self.reset_do),
(("GET"), "/admin/confirm", self.confirm),
(("GET",), "/admin/options", self.options),
(("POST",), "/admin/options", self.options_action),
(("GET",), "/admin/status", self.status),
Expand Down Expand Up @@ -277,7 +278,8 @@ def recover(self):

def recover_do(self):
identifier = self.field("identifier")
try: self.account_c.recover(identifier)
send_email = self.field("send_email", True, cast = bool)
try: self.account_c.recover(identifier, send_email = send_email)
except appier.AppierException as error:
return self.template(
"recover.html.tpl",
Expand All @@ -294,7 +296,7 @@ def reset(self):
mandatory = True,
not_empty = True
)
self.account_c.validate_token(reset_token)
self.account_c.validate_reset(reset_token)
return self.template(
"reset.html.tpl",
next = next,
Expand Down Expand Up @@ -325,6 +327,19 @@ def reset_do(self):
next or self.url_for(self.login_route_admin)
)

def confirm(self):
next = self.field("next")
confirmation_token = self.field(
"confirmation_token",
mandatory = True,
not_empty = True
)
send_email = self.field("send_email", True, cast = bool)
self.account_c.confirm(confirmation_token, send_email = send_email)
return self.redirect(
next or self.url_for(self.login_route_admin)
)

def new_account(self):
if not self.owner.admin_open: raise appier.SecurityError(
message = "signup not allowed"
Expand Down
@@ -0,0 +1,19 @@
{% extends "admin/email/layout.html.tpl" %}
{% block title %}{{ title|default(subject, True)|default("Confirm account", True) }}{% endblock %}
{% block content %}
<p>Hello <strong>{{ account.username }}</strong>,</p>
<p>
You're receiving this email because you account has just
been confirmed.
</p>
{{ h2("Support") }}
{% set support_email = config.conf("SUPPORT_EMAIL")|default("no-reply@appier.hive.pt", True) %}
<p>
Are you having any trouble? We are here to help.<br/>
Feel free to send us an email to {{ link("mailto:" + support_email, support_email, False) }} and we'll get in touch.
</p>
<p>
Thank you,<br/>
The {{ owner.description }} team
</p>
{% endblock %}
Expand Up @@ -19,6 +19,13 @@
<br/><strong>Password:</strong> <span>{{ account_password }}</span>
{% endif %}
</p>
{% if not account.enabled and account.confirmation_token %}
{{ h2("Confirmation") }}
<p>
Please visit this address to activate your account:
{{ link(url_for("admin.confirm", confirmation_token = account.confirmation_token, absolute = True), "Confirm Account", False) }}.
</p>
{% endif %}
{{ h2("Support") }}
{% set support_email = config.conf("SUPPORT_EMAIL")|default("no-reply@appier.hive.pt", True) %}
<p>
Expand Down

0 comments on commit c3a51ca

Please sign in to comment.