Repository: privacyidea/privacyidea

Issue:
Show welcome message if user has no token
The token list (https://github.com/privacyidea/privacyidea/blob/master/privacyidea/static/components/token/views/token.list.html) could show a welcome message for a normal user (not an admin), if this user does not have any tokens, yet.

This is the first view the user sees int the Web UI, so we could add a configurable template, that gets displayed.
Maybe we simply use a template like the *top* and *bottom* templates in the wizard.


Discussion:

Resolving patch:
diff --git a/privacyidea/api/lib/postpolicy.py b/privacyidea/api/lib/postpolicy.py
--- a/privacyidea/api/lib/postpolicy.py
+++ b/privacyidea/api/lib/postpolicy.py
@@ -563,11 +563,16 @@ def get_webui_settings(request, response):
                                        active=True,
                                        audit_data=g.audit_object.audit_data))
         token_wizard = False
+        dialog_no_token = False
         if role == ROLE.USER:
+            user_obj = User(loginname, realm)
+            user_token_num = get_tokens(user=user_obj, count=True)
             token_wizard_pol = policy_object.get_policies(
                 action=ACTION.TOKENWIZARD,
                 scope=SCOPE.WEBUI,
-                realm=realm,
+                realm=user_obj.realm,
+                resolver=user_obj.resolver,
+                user=user_obj.login,
                 client=client,
                 active=True,
                 audit_data=g.audit_object.audit_data
@@ -576,9 +581,19 @@ def get_webui_settings(request, response):
             # We also need to check, if the user has not tokens assigned.
             # If the user has no tokens, we run the wizard. If the user
             # already has tokens, we do not run the wizard.
-            if token_wizard_pol:
-                token_wizard = get_tokens(user=User(loginname, realm),
-                                          count=True) == 0
+            token_wizard = bool(token_wizard_pol) and (user_token_num == 0)
+
+            dialog_no_token_pol = policy_object.get_policies(
+                scope=SCOPE.WEBUI,
+                action=ACTION.DIALOG_NO_TOKEN,
+                client=client,
+                realm=user_obj.realm,
+                resolver=user_obj.resolver,
+                user=user_obj.login,
+                active=True,
+                audit_data=g.audit_object.audit_data
+            )
+            dialog_no_token = bool(dialog_no_token_pol) and (user_token_num == 0)
         user_details_pol = policy_object.get_policies(
             action=ACTION.USERDETAILS,
             scope=SCOPE.WEBUI,
@@ -667,6 +682,7 @@ def get_webui_settings(request, response):
         content["result"]["value"]["user_details"] = len(user_details_pol) > 0
         content["result"]["value"]["token_wizard"] = token_wizard
         content["result"]["value"]["token_wizard_2nd"] = token_wizard_2nd
+        content["result"]["value"]["dialog_no_token"] = dialog_no_token
         content["result"]["value"]["search_on_enter"] = len(search_on_enter) > 0
         content["result"]["value"]["timeout_action"] = timeout_action
         content["result"]["value"]["hide_welcome"] = hide_welcome
diff --git a/privacyidea/lib/policy.py b/privacyidea/lib/policy.py
--- a/privacyidea/lib/policy.py
+++ b/privacyidea/lib/policy.py
@@ -325,6 +325,7 @@ class ACTION(object):
     STATISTICSREAD = "statistics_read"
     STATISTICSDELETE = "statistics_delete"
     LOGIN_TEXT = "login_text"
+    DIALOG_NO_TOKEN = "dialog_no_token"
 
 
 class GROUP(object):
@@ -1794,6 +1795,10 @@ def get_static_policy_definitions(scope=None):
                 'desc': _("The tokenwizard will be displayed in the token "
                           "menu, even if the user already has a token.")
             },
+            ACTION.DIALOG_NO_TOKEN: {
+                'type': 'bool',
+                'desc': _("The welcome dialog will be displayed if the user has no tokens assigned.")
+            },
             ACTION.DEFAULT_TOKENTYPE: {
                 'type': 'str',
                 'desc': _("This is the default token type in the token "


Test patch:
diff --git a/tests/test_api_lib_policy.py b/tests/test_api_lib_policy.py
--- a/tests/test_api_lib_policy.py
+++ b/tests/test_api_lib_policy.py
@@ -2150,6 +2150,19 @@ def test_08_get_webui_settings(self):
 
         delete_policy("pol_wizard")
 
+        # check if the dialog_no_token will not be displayed
+        self.assertEqual(jresult.get("result").get("value").get(
+            "dialog_no_token"), False)
+
+        # Now set a policy and check again
+        set_policy(name="pol_dialog", scope=SCOPE.WEBUI, action=ACTION.DIALOG_NO_TOKEN)
+        g.policy_object = PolicyClass()
+        new_response = get_webui_settings(req, resp)
+        jresult = json.loads(new_response.data)
+        self.assertEqual(jresult.get("result").get("value").get(
+            ACTION.DIALOG_NO_TOKEN), True)
+        delete_policy("pol_dialog")
+
     def test_16_init_token_defaults(self):
         g.logged_in_user = {"username": "cornelius",
                             "role": "user"}