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
, render_404
24 from mediagoblin
.util
import pass_to_ugettext
as _
25 from mediagoblin
.db
.util
import ObjectId
26 from mediagoblin
.auth
import lib
as auth_lib
27 from mediagoblin
.auth
import forms
as auth_forms
28 from mediagoblin
.auth
.lib
import send_verification_email
31 def register(request
):
33 Your classic registration view!
35 # Redirects to indexpage if registrations are disabled
36 if not mg_globals
.app_config
["allow_registration"]:
40 _('Sorry, registration is disabled on this instance.'))
41 return redirect(request
, "index")
43 register_form
= auth_forms
.RegistrationForm(request
.POST
)
45 if request
.method
== 'POST' and register_form
.validate():
46 # TODO: Make sure the user doesn't exist already
47 username
= unicode(request
.POST
['username'].lower())
48 email
= unicode(request
.POST
['email'].lower())
49 users_with_username
= request
.db
.User
.find(
50 {'username': username
}).count()
51 users_with_email
= request
.db
.User
.find(
52 {'email': email
}).count()
54 extra_validation_passes
= True
56 if users_with_username
:
57 register_form
.username
.errors
.append(
58 _(u
'Sorry, a user with that name already exists.'))
59 extra_validation_passes
= False
61 register_form
.email
.errors
.append(
62 _(u
'Sorry, that email address has already been taken.'))
63 extra_validation_passes
= False
65 if extra_validation_passes
:
67 user
= request
.db
.User()
68 user
['username'] = username
70 user
['pw_hash'] = auth_lib
.bcrypt_gen_password_hash(
71 request
.POST
['password'])
72 user
.save(validate
=True)
75 request
.session
['user_id'] = unicode(user
['_id'])
76 request
.session
.save()
78 # send verification email
79 send_verification_email(user
, request
)
81 # redirect the user to their homepage... there will be a
82 # message waiting for them to verify their email
84 request
, 'mediagoblin.user_pages.user_home',
85 user
=user
['username'])
87 return render_to_response(
89 'mediagoblin/auth/register.html',
90 {'register_form': register_form
})
95 MediaGoblin login view.
97 If you provide the POST with 'next', it'll redirect to that view.
99 login_form
= auth_forms
.LoginForm(request
.POST
)
103 if request
.method
== 'POST' and login_form
.validate():
104 user
= request
.db
.User
.one(
105 {'username': request
.POST
['username'].lower()})
107 if user
and user
.check_login(request
.POST
['password']):
108 # set up login in session
109 request
.session
['user_id'] = unicode(user
['_id'])
110 request
.session
.save()
112 if request
.POST
.get('next'):
113 return exc
.HTTPFound(location
=request
.POST
['next'])
115 return redirect(request
, "index")
118 # Prevent detecting who's on this system by testing login
120 auth_lib
.fake_login_attempt()
123 return render_to_response(
125 'mediagoblin/auth/login.html',
126 {'login_form': login_form
,
127 'next': request
.GET
.get('next') or request
.POST
.get('next'),
128 'login_failed': login_failed
,
129 'allow_registration': mg_globals
.app_config
["allow_registration"]})
133 # Maybe deleting the user_id parameter would be enough?
134 request
.session
.delete()
136 return redirect(request
, "index")
139 def verify_email(request
):
141 Email verification view
143 validates GET parameters against database and unlocks the user account, if
146 # If we don't have userid and token parameters, we can't do anything; 404
147 if not request
.GET
.has_key('userid') or not request
.GET
.has_key('token'):
148 return render_404(request
)
150 user
= request
.db
.User
.find_one(
151 {'_id': ObjectId(unicode(request
.GET
['userid']))})
153 if user
and user
['verification_key'] == unicode(request
.GET
['token']):
154 user
['status'] = u
'active'
155 user
['email_verified'] = True
157 messages
.add_message(
160 _("Your email address has been verified. "
161 "You may now login, edit your profile, and submit images!"))
163 messages
.add_message(
166 _('The verification key or user id is incorrect'))
169 request
, 'mediagoblin.user_pages.user_home',
170 user
=user
['username'])
173 def resend_activation(request
):
175 The reactivation view
177 Resend the activation email.
179 request
.user
['verification_key'] = unicode(uuid
.uuid4())
182 send_verification_email(request
.user
, request
)
184 messages
.add_message(
187 _('Resent your verification email.'))
189 request
, 'mediagoblin.user_pages.user_home',
190 user
=request
.user
['username'])