1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 from mediagoblin
import messages
22 from mediagoblin
import mg_globals
23 from mediagoblin
.util
import render_to_response
, redirect
24 from mediagoblin
.db
.util
import ObjectId
25 from mediagoblin
.auth
import lib
as auth_lib
26 from mediagoblin
.auth
import forms
as auth_forms
27 from mediagoblin
.auth
.lib
import send_verification_email
30 def register(request
):
32 Your classic registration view!
34 # Redirects to indexpage if registrations are disabled
35 if not mg_globals
.app_config
["allow_registration"]:
39 ('Sorry, registration is disabled on this instance.'))
40 return redirect(request
, "index")
42 register_form
= auth_forms
.RegistrationForm(request
.POST
)
44 if request
.method
== 'POST' and register_form
.validate():
45 # TODO: Make sure the user doesn't exist already
47 users_with_username
= \
48 request
.db
.User
.find({
49 'username': request
.POST
['username'].lower()
52 if users_with_username
:
53 register_form
.username
.errors
.append(
54 u
'Sorry, a user with that name already exists.')
58 entry
= request
.db
.User()
59 entry
['username'] = request
.POST
['username'].lower()
60 entry
['email'] = request
.POST
['email']
61 entry
['pw_hash'] = auth_lib
.bcrypt_gen_password_hash(
62 request
.POST
['password'])
63 entry
.save(validate
=True)
65 send_verification_email(entry
, request
)
67 return redirect(request
, "mediagoblin.auth.register_success")
69 return render_to_response(
71 'mediagoblin/auth/register.html',
72 {'register_form': register_form
})
77 MediaGoblin login view.
79 If you provide the POST with 'next', it'll redirect to that view.
81 login_form
= auth_forms
.LoginForm(request
.POST
)
85 if request
.method
== 'POST' and login_form
.validate():
86 user
= request
.db
.User
.one(
87 {'username': request
.POST
['username'].lower()})
89 if user
and user
.check_login(request
.POST
['password']):
90 # set up login in session
91 request
.session
['user_id'] = unicode(user
['_id'])
92 request
.session
.save()
94 if request
.POST
.get('next'):
95 return exc
.HTTPFound(location
=request
.POST
['next'])
97 return redirect(request
, "index")
100 # Prevent detecting who's on this system by testing login
102 auth_lib
.fake_login_attempt()
105 return render_to_response(
107 'mediagoblin/auth/login.html',
108 {'login_form': login_form
,
109 'next': request
.GET
.get('next') or request
.POST
.get('next'),
110 'login_failed': login_failed
,
111 'allow_registration': mg_globals
.app_config
["allow_registration"]})
115 # Maybe deleting the user_id parameter would be enough?
116 request
.session
.delete()
118 return redirect(request
, "index")
121 def verify_email(request
):
123 Email verification view
125 validates GET parameters against database and unlocks the user account, if
128 # If we don't have userid and token parameters, we can't do anything; 404
129 if not request
.GET
.has_key('userid') or not request
.GET
.has_key('token'):
130 return exc
.HTTPNotFound()
132 user
= request
.db
.User
.find_one(
133 {'_id': ObjectId(unicode(request
.GET
['userid']))})
135 if user
and user
['verification_key'] == unicode(request
.GET
['token']):
136 user
['status'] = u
'active'
137 user
['email_verified'] = True
139 verification_successful
= True
140 messages
.add_message(
143 ('Your email address has been verified. '
144 'You may now login, edit your profile, and submit images!'))
146 verification_successful
= False
147 messages
.add_message(request
,
149 'The verification key or user id is incorrect')
151 return render_to_response(
153 'mediagoblin/user_pages/user.html',
155 'verification_successful' : verification_successful
})
158 def resend_activation(request
):
160 The reactivation view
162 Resend the activation email.
164 request
.user
['verification_key'] = unicode(uuid
.uuid4())
167 send_verification_email(request
.user
, request
)
169 return redirect(request
, 'mediagoblin.auth.resend_verification_success')