|
1 | | -from ckan.views.user import RegisterView, EditView, PerformResetView |
| 1 | +from ckan.views.user import RegisterView, EditView, PerformResetView, rotate_token, next_page_or_default |
2 | 2 | # from ckan.lib.repoze_plugins.friendly_form import FriendlyFormPlugin |
3 | 3 | import ckan.logic as logic |
4 | 4 | import ckan.plugins as plugins |
5 | 5 | import ckan.lib.base as base |
| 6 | +import ckan.lib.authenticator as authenticator |
6 | 7 | from flask import Blueprint |
7 | 8 | import ckan.model as model |
8 | 9 | import ckan.plugins.toolkit as tk |
9 | | -from ckan.common import _, config, g, request |
10 | 10 | import ckan.lib.helpers as h |
11 | 11 | import ckanext.password_policy.helpers as helper |
12 | 12 | # from webob import Request |
13 | 13 | # from webob.exc import HTTPFound, HTTPUnauthorized |
| 14 | +from typing import Any, Optional, Union |
14 | 15 | from six import text_type |
15 | 16 | from six.moves.urllib.parse import urlencode |
| 17 | +from ckan.common import ( |
| 18 | + _, config, g, current_user, login_user, logout_user, |
| 19 | + session, config, g, request, repr_untrusted |
| 20 | +) |
| 21 | +from ckan.types import Context, Schema, Response |
16 | 22 | # try: |
17 | 23 | # from webob.multidict import MultiDict |
18 | 24 | # except ImportError: |
@@ -218,23 +224,52 @@ def _get_repoze_handler(handler_name): |
218 | 224 | handler_name) |
219 | 225 |
|
220 | 226 |
|
221 | | -def custom_login(): |
222 | | - # Do any plugin login stuff |
| 227 | +def custom_login() -> Union[Response, str]: |
223 | 228 | for item in plugins.PluginImplementations(plugins.IAuthenticator): |
224 | 229 | response = item.login() |
225 | 230 | if response: |
226 | 231 | return response |
227 | 232 |
|
228 | | - extra_vars = {} |
229 | | - if g.user: |
230 | | - return base.render(u'user/logout_first.html', extra_vars) |
231 | | - |
232 | | - came_from = request.params.get(u'came_from') |
233 | | - if not came_from: |
234 | | - came_from = h.url_for(u'user.logged_in') |
235 | | - g.login_handler = h.url_for( |
236 | | - _get_repoze_handler(u'login_handler_path'), came_from=came_from) |
237 | | - return base.render(u'user/login.html', extra_vars) |
| 233 | + print("================================") |
| 234 | + print("custom login") |
| 235 | + print("================================") |
| 236 | + extra_vars: dict[str, Any] = {} |
| 237 | + |
| 238 | + if current_user.is_authenticated: |
| 239 | + return base.render("user/logout_first.html", extra_vars) |
| 240 | + |
| 241 | + if request.method == "POST": |
| 242 | + username_or_email = request.form.get("login") |
| 243 | + password = request.form.get("password") |
| 244 | + _remember = request.form.get("remember") |
| 245 | + |
| 246 | + identity = { |
| 247 | + u"login": username_or_email, |
| 248 | + u"password": password |
| 249 | + } |
| 250 | + |
| 251 | + user_obj = authenticator.ckan_authenticator(identity) |
| 252 | + if user_obj: |
| 253 | + next = request.args.get('next', request.args.get('came_from')) |
| 254 | + if _remember: |
| 255 | + from datetime import timedelta |
| 256 | + duration_time = timedelta(milliseconds=int(_remember)) |
| 257 | + login_user(user_obj, remember=True, duration=duration_time) |
| 258 | + rotate_token() |
| 259 | + return next_page_or_default(next) |
| 260 | + else: |
| 261 | + login_user(user_obj) |
| 262 | + rotate_token() |
| 263 | + return next_page_or_default(next) |
| 264 | + else: |
| 265 | + if config.get('ckan.recaptcha.privatekey'): |
| 266 | + err = _(u"Login failed. Bad username or password or CAPTCHA.") |
| 267 | + else: |
| 268 | + err = _(u"Login failed. Bad username or password.") |
| 269 | + h.flash_error(err) |
| 270 | + return base.render("user/login.html", extra_vars) |
| 271 | + |
| 272 | + return base.render("user/login.html", extra_vars) |
238 | 273 |
|
239 | 274 |
|
240 | 275 | def logged_in(): |
|
0 commit comments