##// END OF EJS Templates
index: remove unused variables
ergo -
Show More
@@ -1,255 +1,242 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2016 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # App Enlight Enterprise Edition, including its added features, Support
19 19 # services, and proprietary license terms, please see
20 20 # https://rhodecode.com/licenses/
21 21
22 22 import datetime
23 23 import logging
24 24 import uuid
25 25
26 26 import pyramid.security as security
27 27
28 28 from pyramid.view import view_config
29 29 from pyramid.httpexceptions import HTTPFound
30 30 from pyramid.response import Response
31 31 from pyramid.security import NO_PERMISSION_REQUIRED
32 32 from ziggurat_foundations.ext.pyramid.sign_in import ZigguratSignInSuccess
33 33 from ziggurat_foundations.ext.pyramid.sign_in import ZigguratSignInBadAuth
34 34 from ziggurat_foundations.ext.pyramid.sign_in import ZigguratSignOut
35 35
36 36 from appenlight.lib.social import handle_social_data
37 37 from appenlight.models import DBSession
38 38 from appenlight.models.user import User
39 39 from appenlight.models.services.user import UserService
40 40 from appenlight.subscribers import _
41 41 from appenlight import forms
42 42 from webob.multidict import MultiDict
43 43
44 44 log = logging.getLogger(__name__)
45 45
46 46
47 47 @view_config(context=ZigguratSignInSuccess, permission=NO_PERMISSION_REQUIRED)
48 48 def sign_in(request):
49 49 """
50 50 Performs sign in by sending proper user identification headers
51 51 Regenerates CSRF token
52 52 """
53 53 user = request.context.user
54 54 if user.status == 1:
55 55 request.session.new_csrf_token()
56 56 user.last_login_date = datetime.datetime.utcnow()
57 57 social_data = request.session.get('zigg.social_auth')
58 58 if social_data:
59 59 handle_social_data(request, user, social_data)
60 60 else:
61 61 request.session.flash(_('Account got disabled'))
62 62
63 63 if request.context.came_from != '/':
64 64 return HTTPFound(location=request.context.came_from,
65 65 headers=request.context.headers)
66 66 else:
67 67 return HTTPFound(location=request.route_url('/'),
68 68 headers=request.context.headers)
69 69
70 70
71 71 @view_config(context=ZigguratSignInBadAuth, permission=NO_PERMISSION_REQUIRED)
72 72 def bad_auth(request):
73 73 """
74 74 Handles incorrect login flow
75 75 """
76 76 request.session.flash(_('Incorrect username or password'), 'warning')
77 77 return HTTPFound(location=request.route_url('register'),
78 78 headers=request.context.headers)
79 79
80 80
81 81 @view_config(context=ZigguratSignOut, permission=NO_PERMISSION_REQUIRED)
82 82 def sign_out(request):
83 83 """
84 84 Removes user identification cookie
85 85 """
86 86 return HTTPFound(location=request.route_url('register'),
87 87 headers=request.context.headers)
88 88
89 89
90 90 @view_config(route_name='lost_password',
91 91 renderer='appenlight:templates/user/lost_password.jinja2',
92 92 permission=NO_PERMISSION_REQUIRED)
93 93 def lost_password(request):
94 94 """
95 95 Presents lost password page - sends password reset link to
96 96 specified email address.
97 97 This link is valid only for 10 minutes
98 98 """
99 99 form = forms.LostPasswordForm(request.POST, csrf_context=request)
100 100 if request.method == 'POST' and form.validate():
101 101 user = User.by_email(form.email.data)
102 102 if user:
103 103 user.regenerate_security_code()
104 104 user.security_code_date = datetime.datetime.utcnow()
105 105 email_vars = {
106 106 'user': user,
107 107 'request': request,
108 108 'email_title': "App Enlight :: New password request"
109 109 }
110 110 UserService.send_email(
111 111 request, recipients=[user.email],
112 112 variables=email_vars,
113 113 template='/email_templates/lost_password.jinja2')
114 114 msg = 'Password reset email had been sent. ' \
115 115 'Please check your mailbox for further instructions.'
116 116 request.session.flash(_(msg))
117 117 return HTTPFound(location=request.route_url('lost_password'))
118 118 return {"form": form}
119 119
120 120
121 121 @view_config(route_name='lost_password_generate',
122 122 permission=NO_PERMISSION_REQUIRED,
123 123 renderer='appenlight:templates/user/lost_password_generate.jinja2')
124 124 def lost_password_generate(request):
125 125 """
126 126 Shows new password form - perform time check and set new password for user
127 127 """
128 128 user = User.by_user_name_and_security_code(
129 129 request.GET.get('user_name'), request.GET.get('security_code'))
130 130 if user:
131 131 delta = datetime.datetime.utcnow() - user.security_code_date
132 132
133 133 if user and delta.total_seconds() < 600:
134 134 form = forms.NewPasswordForm(request.POST, csrf_context=request)
135 135 if request.method == "POST" and form.validate():
136 136 user.set_password(form.new_password.data)
137 137 request.session.flash(_('You can sign in with your new password.'))
138 138 return HTTPFound(location=request.route_url('register'))
139 139 else:
140 140 return {"form": form}
141 141 else:
142 142 return Response('Security code expired')
143 143
144 144
145 145 @view_config(route_name='register',
146 146 renderer='appenlight:templates/user/register.jinja2',
147 147 permission=NO_PERMISSION_REQUIRED)
148 148 def register(request):
149 149 """
150 150 Render register page with form
151 151 Also handles oAuth flow for registration
152 152 """
153 153 login_url = request.route_url('ziggurat.routes.sign_in')
154 154 if request.query_string:
155 155 query_string = '?%s' % request.query_string
156 156 else:
157 157 query_string = ''
158 158 referrer = '%s%s' % (request.path, query_string)
159 159
160 160 if referrer in [login_url, '/register', '/register?sign_in=1']:
161 161 referrer = '/' # never use the login form itself as came_from
162 162 sign_in_form = forms.SignInForm(
163 163 came_from=request.params.get('came_from', referrer),
164 164 csrf_context=request)
165 165
166 166 # populate form from oAuth session data returned by authomatic
167 167 social_data = request.session.get('zigg.social_auth')
168 168 if request.method != 'POST' and social_data:
169 169 log.debug(social_data)
170 170 user_name = social_data['user'].get('user_name', '').split('@')[0]
171 171 form_data = {
172 172 'user_name': user_name,
173 173 'email': social_data['user'].get('email')
174 174 }
175 175 form_data['user_password'] = str(uuid.uuid4())
176 176 form = forms.UserRegisterForm(MultiDict(form_data),
177 177 csrf_context=request)
178 178 form.user_password.widget.hide_value = False
179 179 else:
180 180 form = forms.UserRegisterForm(request.POST, csrf_context=request)
181 181 if request.method == 'POST' and form.validate():
182 182 log.info('registering user')
183 183 # insert new user here
184 184 new_user = User()
185 185 DBSession.add(new_user)
186 186 form.populate_obj(new_user)
187 187 new_user.regenerate_security_code()
188 188 new_user.status = 1
189 189 new_user.set_password(new_user.user_password)
190 190 new_user.registration_ip = request.environ.get('REMOTE_ADDR')
191 191
192 192 if social_data:
193 193 handle_social_data(request, new_user, social_data)
194 194
195 195 email_vars = {'user': new_user,
196 196 'request': request,
197 197 'email_title': "App Enlight :: Start information"}
198 198 UserService.send_email(
199 199 request, recipients=[new_user.email], variables=email_vars,
200 200 template='/email_templates/registered.jinja2')
201 201 request.session.flash(_('You have successfully registered.'))
202 202 DBSession.flush()
203 203 headers = security.remember(request, new_user.id)
204 204 return HTTPFound(location=request.route_url('/'),
205 205 headers=headers)
206 206 settings = request.registry.settings
207 207 social_plugins = {}
208 208 if settings.get('authomatic.pr.twitter.key', ''):
209 209 social_plugins['twitter'] = True
210 210 if settings.get('authomatic.pr.google.key', ''):
211 211 social_plugins['google'] = True
212 212 if settings.get('authomatic.pr.github.key', ''):
213 213 social_plugins['github'] = True
214 214 if settings.get('authomatic.pr.bitbucket.key', ''):
215 215 social_plugins['bitbucket'] = True
216 216
217 217 return {
218 218 "form": form,
219 219 "sign_in_form": sign_in_form,
220 220 "social_plugins": social_plugins
221 221 }
222 222
223 223
224 224 @view_config(route_name='/',
225 225 renderer='appenlight:templates/dashboard/index.jinja2',
226 226 permission=NO_PERMISSION_REQUIRED)
227 227 @view_config(route_name='angular_app_ui',
228 228 renderer='appenlight:templates/dashboard/index.jinja2',
229 229 permission=NO_PERMISSION_REQUIRED)
230 230 @view_config(route_name='angular_app_ui_ix',
231 231 renderer='appenlight:templates/dashboard/index.jinja2',
232 232 permission=NO_PERMISSION_REQUIRED)
233 233 def app_main_index(request):
234 234 """
235 235 Render dashoard/report browser page page along with:
236 236 - flash messages
237 237 - application list
238 238 - assigned reports
239 239 - latest events
240 240 (those last two come from subscribers.py that sets global renderer variables)
241 241 """
242
243 if request.user:
244 request.user.last_login_date = datetime.datetime.utcnow()
245 applications = request.user.resources_with_perms(
246 ['view'], resource_types=['application'])
247 # convert for angular
248 applications = dict(
249 [(a.resource_id, a.resource_name) for a in applications.all()]
250 )
251 else:
252 applications = {}
253 return {
254 'applications': applications
255 }
242 return {}
General Comments 0
You need to be logged in to leave comments. Login now