From 7393c7ba592dff6b4b6bf66eee883e34c1c3357c Mon Sep 17 00:00:00 2001
From: Stefano Alberto Russo <stefano.russo@gmail.com>
Date: Wed, 31 Mar 2021 15:10:22 +0200
Subject: [PATCH] Improved handling of the invitation code. Minor fixes.

---
 services/webapp/code/rosetta/context_processors.py   | 11 ++++++++++-
 .../code/rosetta/core_app/templates/register.html    | 12 ++++++++++--
 services/webapp/code/rosetta/core_app/views.py       |  5 +++--
 services/webapp/code/rosetta/settings.py             |  3 +++
 4 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/services/webapp/code/rosetta/context_processors.py b/services/webapp/code/rosetta/context_processors.py
index 7e6e361..67c4435 100644
--- a/services/webapp/code/rosetta/context_processors.py
+++ b/services/webapp/code/rosetta/context_processors.py
@@ -2,8 +2,17 @@ import os
 from django.conf import settings
 def export_vars(request):
     data = {}
+    
+    # Set open id connect enabled or not
     if settings.OIDC_RP_CLIENT_ID:
         data['OPENID_ENABLED'] = True
     else:
-        data['OPENID_ENABLED'] = False        
+        data['OPENID_ENABLED'] = False
+    
+    # Set invitation code required or not
+    if settings.INVITATION_CODE:
+        data['INVITATION_CODE_ENABLED'] = True
+    else:
+        data['INVITATION_CODE_ENABLED'] = False   
+              
     return data
\ No newline at end of file
diff --git a/services/webapp/code/rosetta/core_app/templates/register.html b/services/webapp/code/rosetta/core_app/templates/register.html
index 26d8bd4..8a7ca29 100644
--- a/services/webapp/code/rosetta/core_app/templates/register.html
+++ b/services/webapp/code/rosetta/core_app/templates/register.html
@@ -7,7 +7,7 @@
 
 <div class="container">
   <div class="dashboard">
-    <div class="span8 offset2" style="font-size:18px">
+    <div class="span8 offset2">
     
       <h1>Register</h1>
       <hr>
@@ -41,7 +41,13 @@
 
       <p><b>Welcome to Rosetta!</b></p>
       
-      <p>We are now in closed beta testing, which means that we can only accept invite-based new users. If you have an invitation code, you can sign up right now. Otherwise, please contact the support ot get one.
+      <p>
+      {% if INVITATION_CODE_ENABLED %}
+      We are now in closed beta testing, which means that we can only accept invite-based new users. If you have an invitation code, you can sign up right now. Otherwise, please contact the support to get one.
+      {% else %}
+      Enter your email address and choose a password to register on the platform.
+      {% endif %}
+      </p>
       
       <br/>
       <br/>
@@ -51,7 +57,9 @@
           {% csrf_token %}
           <input type="email" class="form-control" placeholder="Email" name='email' required autofocus>
           <input type="password" class="form-control" placeholder="Password" name='password' required>
+          {% if INVITATION_CODE_ENABLED %}
           <input type="text" class="form-control" placeholder="Invitation code" name='invitation' value='' required autofocus>
+          {% endif %}
           <input type='submit' class="btn btn-lg ha-btn-lg" value='Go' />
           </form>
       </div>
diff --git a/services/webapp/code/rosetta/core_app/views.py b/services/webapp/code/rosetta/core_app/views.py
index 617dec5..3de2dd6 100644
--- a/services/webapp/code/rosetta/core_app/views.py
+++ b/services/webapp/code/rosetta/core_app/views.py
@@ -152,8 +152,9 @@ def register_view(request):
             password = request.POST.get('password')
             invitation = request.POST.get('invitation')
             
-            if invitation != os.environ.get('INVITATION_CODE', 'Rosetta'):
-                raise ErrorMessage('Wrong invitation code')
+            if settings.INVITATION_CODE:
+                if invitation != settings.INVITATION_CODE:
+                    raise ErrorMessage('Wrong invitation code')
 
             if '@' not in email:
                 raise ErrorMessage('Detected invalid email address')
diff --git a/services/webapp/code/rosetta/settings.py b/services/webapp/code/rosetta/settings.py
index 39b06b3..e3badd8 100644
--- a/services/webapp/code/rosetta/settings.py
+++ b/services/webapp/code/rosetta/settings.py
@@ -226,6 +226,9 @@ LOGGING = {
 # Local user data dir
 LOCAL_USER_DATA_DIR = os.environ.get('LOCAL_USER_DATA_DIR', '/data')
 
+# Invitation code if any
+INVITATION_CODE = os.environ.get('INVITATION_CODE', None)
+
 
 #===============================
 #  Auth
-- 
GitLab