]>
git.ipfire.org Git - people/jschlag/pbs.git/blob - src/web/handlers_auth.py
f28c1f13d65081e05b933f1e8e7f7d3dbf4c6903
5 from .handlers_base
import *
7 class LoginHandler(BaseHandler
):
9 # If the user is already logged in, we just send him back
15 self
.render("login.html", failed
=False)
18 name
= self
.get_argument("name", None)
19 passphrase
= self
.get_argument("pass", None)
22 user
= self
.pakfire
.users
.auth(name
, passphrase
)
24 # If the login was unsuccessful
26 self
.set_status(403, "Login failed")
27 return self
.render("login.html", failed
=True)
29 # Create a new session for the user.
30 with self
.db
.transaction():
31 self
.session
= self
.backend
.sessions
.create(user
,
32 self
.current_address
, user_agent
=self
.user_agent
)
34 # Set a cookie and update the current user.
35 self
.set_cookie("session_id", self
.session
.session_id
,
36 expires
=self
.session
.valid_until
)
38 # If there is "next" given, we redirect the user accordingly.
39 # Otherwise we redirect to the front page.
40 next
= self
.get_argument("next", "/")
44 class RegisterHandler(BaseHandler
):
46 # If the user is already logged in, we just send him back
52 self
.render("register.html")
55 _
= self
.locale
.translate
58 # Read all information from the request.
59 name
= self
.get_argument("name", None)
60 email
= self
.get_argument("email", None)
61 realname
= self
.get_argument("realname", None)
62 pass1
= self
.get_argument("pass1", None)
63 pass2
= self
.get_argument("pass2", None)
66 msgs
.append(_("No username provided."))
67 elif self
.pakfire
.users
.name_is_used(name
):
68 msgs
.append(_("The given username is already taken."))
71 msgs
.append(_("No email address provided."))
72 elif not "@" in email
:
73 msgs
.append(_("Email address is invalid."))
74 elif self
.pakfire
.users
.email_is_used(email
):
75 msgs
.append(_("The given email address is already used for another account."))
77 # Check if the passphrase is okay.
79 msgs
.append(_("No password provided."))
80 elif not pass1
== pass2
:
81 msgs
.append(_("Passwords do not match."))
83 accepted
, score
= self
.backend
.users
.check_password_strength(pass1
)
85 msgs
.append(_("Your password is too weak."))
88 self
.render("register-fail.html", messages
=msgs
)
91 # All provided data seems okay.
92 # Register the new user to the database.
93 user
= self
.pakfire
.users
.register(name
, pass1
, email
, realname
,
96 self
.render("register-success.html", user
=user
)
99 class ActivationHandler(BaseHandler
):
100 def get(self
, _user
):
101 user
= self
.pakfire
.users
.get_by_name(_user
)
103 raise tornado
.web
.HTTPError(404)
105 code
= self
.get_argument("code")
107 # Check if the activation code matches and then activate the account.
108 if user
.activation_code
== code
:
111 # If an admin activated another account, he impersonates it.
112 if self
.current_user
and self
.current_user
.is_admin():
113 self
.session
.start_impersonation(user
)
116 # Automatically login the user.
117 session
= sessions
.Session
.create(self
.pakfire
, user
)
119 # Set a cookie and update the current user.
120 self
.set_cookie("session_id", session
.id, expires
=session
.valid_until
)
121 self
._current
_user
= user
123 self
.render("register-activation-success.html", user
=user
)
126 # Otherwise, show an error message.
127 self
.render("register-activation-fail.html")
130 class PasswordRecoveryHandler(BaseHandler
):
132 return self
.render("user-forgot-password.html")
135 username
= self
.get_argument("name", None)
143 class LogoutHandler(BaseHandler
):
144 @tornado.web
.authenticated
146 # Destroy the user's session.
147 with self
.db
.transaction():
148 self
.session
.destroy()
150 # Remove the cookie, that identifies the user.
151 self
.clear_cookie("session_id")
153 # Redirect the user to the front page.