Repository: learningequality/kolibri

Issue:
add redirect from '/user' to '/auth'
## Observed behavior

I had http://kolibri-beta.learningequality.org in my browser history and went there. However it turned out that the page was `/en/user/#/` which returns a `404` now.

## Expected behavior

`/user` should redirect to `/auth` using a `301` (permanently moved) code.

## User-facing consequences

Existing bookmarks and browser history will break.

## Steps to reproduce

Navigate to http://kolibri-beta.learningequality.org/en/user/#/

## Context

0.15.0-beta1


Discussion:
I would like to work on this.

Can you guide me how should I solve this issue :)
Hi @ozer619 –

I believe this will be added as the second-to-last item in the `lang_prefixed_patterns` array in the core urls.py file:

https://github.com/learningequality/kolibri/blob/d63bb74b99b744541d86a01db90d3f90b476213d/kolibri/core/urls.py#L52-L59

The easiest solution would probably be to use `RootURLRedirectView`:

```python
lang_prefixed_patterns = [
    url(r"^i18n/setlang/$", set_language, name="set_language"),
    url(r"^logout/$", logout_view, name="logout"),
    url(r"^redirectuser/$", RootURLRedirectView.as_view(), name="redirect_user"),
    url(r"^guestaccess/$", GuestRedirectView.as_view(), name="guest"),
    url(r"^unsupported/$", UnsupportedBrowserView.as_view(), name="unsupported"),
    url(r"^user/$", RootURLRedirectView.as_view(), name="deprecated_user_url"),
    url(r"^$", RootURLRedirectView.as_view(), name="root_redirect"),
]
```

However `RootURLRedirectView` uses a `302` rather than a `301` which isn't quite as useful e.g. for caches.

You could return a 301 using [`HttpResponsePermanentRedirect`](https://docs.djangoproject.com/en/3.2/ref/request-response/#django.http.HttpResponsePermanentRedirect). However then we'd need to hard-code the `/auth` URL which has its own disadvantages with regard to coupling between core and the Kolibri user auth plugin.

@rtibbles care to make a final call?






Hi, any updates on this?
hi @ozer619 –

I spoke with @rtibbles today and he pointed out that the solutions I proposed above would cause conflicts for users who have a custom `user` plugin.

Instead, we'll need to update the [`user_auth` plugin](https://github.com/learningequality/kolibri/blob/release-v0.15.x/kolibri/plugins/user_auth/kolibri_plugin.py) to modify the root-level URLs with a new lang_prefixed URL. This way, `/[LANG]/user` will only be a registered URL if the server has `user_auth` enabled and not otherwise.

For an example of how to add a new root-level URL from a plugin, see:

* specify that the plugin provides a `root_urls` module: https://github.com/learningequality/kolibri-oidc-provider-plugin/blob/master/kolibri_oidc_provider_plugin/kolibri_plugin.py#L11
* define a plugin-specific `root_urls` module: https://github.com/learningequality/kolibri-oidc-provider-plugin/blob/master/kolibri_oidc_provider_plugin/root_urls.py

Note that you'll need to use the `i18n_patterns` function to prefix the `/user` URL with the supported languages, like is done in the core urls.py:

https://github.com/learningequality/kolibri/blob/d63bb74b99b744541d86a01db90d3f90b476213d/kolibri/core/urls.py#L63







Resolving patch:
diff --git a/kolibri/plugins/user_auth/kolibri_plugin.py b/kolibri/plugins/user_auth/kolibri_plugin.py
--- a/kolibri/plugins/user_auth/kolibri_plugin.py
+++ b/kolibri/plugins/user_auth/kolibri_plugin.py
@@ -16,6 +16,7 @@
 
 class UserAuth(KolibriPluginBase):
     translated_view_urls = "urls"
+    root_view_urls = "root_urls"
 
     @property
     def url_slug(self):
diff --git a/kolibri/plugins/user_auth/root_urls.py b/kolibri/plugins/user_auth/root_urls.py
new file mode 100644
--- /dev/null
+++ b/kolibri/plugins/user_auth/root_urls.py
@@ -0,0 +1,20 @@
+"""
+This is here to enable redirects from the old /user endpoint to /auth
+"""
+from django.conf.urls import include
+from django.conf.urls import url
+from django.views.generic.base import RedirectView
+
+from kolibri.core.device.translation import i18n_patterns
+
+redirect_patterns = [
+    url(
+        r"^user/$",
+        RedirectView.as_view(
+            pattern_name="kolibri:kolibri.plugins.user_auth:user_auth", permanent=True
+        ),
+        name="redirect_user",
+    ),
+]
+
+urlpatterns = [url(r"", include(i18n_patterns(redirect_patterns)))]


Test patch:
diff --git a/kolibri/core/test/test_key_urls.py b/kolibri/core/test/test_key_urls.py
--- a/kolibri/core/test/test_key_urls.py
+++ b/kolibri/core/test/test_key_urls.py
@@ -80,7 +80,7 @@ class AllUrlsTest(APITransactionTestCase):
 
     # Allow codes that may indicate a poorly formed response
     # 412 is returned from endpoints that have required GET params when these are not supplied
-    allowed_http_codes = [200, 302, 400, 401, 403, 404, 405, 412]
+    allowed_http_codes = [200, 301, 302, 400, 401, 403, 404, 405, 412]
 
     def setUp(self):
         provision_device()