Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect to the login view when login is required #1733

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions umap/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def wrapper(request, *args, **kwargs):
not getattr(settings, "UMAP_ALLOW_ANONYMOUS", False)
and not request.user.is_authenticated
):
return simple_json_response(login_required=str(LOGIN_URL))
response = simple_json_response(login_required=str(LOGIN_URL))
response.status_code = 401
return response
return view_func(request, *args, **kwargs)

return wrapper
Expand All @@ -39,7 +41,9 @@ def wrapper(request, *args, **kwargs):
can_edit = map_inst.can_edit(user=user, request=request)
if not can_edit:
if map_inst.owner and not user.is_authenticated:
return simple_json_response(login_required=str(LOGIN_URL))
response = simple_json_response(login_required=str(LOGIN_URL))
response.status_code = 401
return response
return HttpResponseForbidden()
return view_func(request, *args, **kwargs)

Expand Down
17 changes: 9 additions & 8 deletions umap/static/umap/js/umap.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ U.Map = L.Map.extend({
.split(',')
}


let editedFeature = null
const self = this
try {
Expand Down Expand Up @@ -345,7 +344,7 @@ U.Map = L.Map.extend({
document.body,
'umap-caption-bar-enabled',
this.options.captionBar ||
(this.options.slideshow && this.options.slideshow.active)
(this.options.slideshow && this.options.slideshow.active)
)
L.DomUtil.classIf(
document.body,
Expand Down Expand Up @@ -962,8 +961,10 @@ U.Map = L.Map.extend({
formData.append('settings', JSON.stringify(geojson))
const uri = this.urls.get('map_save', { map_id: this.options.umap_id })
const [data, response, error] = await this.server.post(uri, {}, formData)
// FIXME: login_required response will not be an error, so it will not
// stop code while it should
if (error && response.status === 401) {
const data = await response.json()
window.location = data.login_required
}
if (!error) {
let duration = 3000,
alert = { content: L._('Map has been saved!'), level: 'info' }
Expand Down Expand Up @@ -1573,10 +1574,10 @@ U.Map = L.Map.extend({

initCaptionBar: function () {
const container = L.DomUtil.create(
'div',
'umap-caption-bar',
this._controlContainer
),
'div',
'umap-caption-bar',
this._controlContainer
),
name = L.DomUtil.create('h3', '', container)
L.DomEvent.disableClickPropagation(container)
this.permissions.addOwnerLink('span', container)
Expand Down
2 changes: 1 addition & 1 deletion umap/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class Meta:


def login_required(response):
assert response.status_code == 200
assert response.status_code == 401
j = json.loads(response.content.decode())
assert "login_required" in j
redirect_url = reverse("login")
Expand Down
2 changes: 1 addition & 1 deletion umap/tests/test_map_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ def test_cannot_send_link_on_owned_map(client, map):
assert len(mail.outbox) == 0
url = reverse("map_send_edit_link", args=(map.pk,))
resp = client.post(url, {"email": "[email protected]"})
assert resp.status_code == 200
assert resp.status_code == 401
assert json.loads(resp.content.decode()) == {"login_required": "/en/login/"}
assert len(mail.outbox) == 0

Expand Down
Loading