Repository: privacyidea/privacyidea

Issue:
Allow "-" in policy names
It is totally annoying to me personally. ;-)
We should allow "-" in policy names.


Discussion:

Resolving patch:
diff --git a/privacyidea/api/lib/utils.py b/privacyidea/api/lib/utils.py
--- a/privacyidea/api/lib/utils.py
+++ b/privacyidea/api/lib/utils.py
@@ -34,6 +34,7 @@
 import jwt
 import threading
 import six
+import re
 from flask import (jsonify,
                    current_app)
 
@@ -267,3 +268,23 @@ def verify_auth_token(auth_token, required_role=None):
                         "this resource!").format(required_role),
                         id=ERROR.AUTHENTICATE_MISSING_RIGHT)
     return r
+
+
+def check_policy_name(name):
+    """
+    This function checks, if the given name is a valid policy name.
+
+    :param name: The name of the policy
+    :return: Raises a ParameterError in case of an invalid name
+    """
+    disallowed_patterns = [("^check$", re.IGNORECASE),
+                           ("^pi-update-policy-", re.IGNORECASE)]
+    for disallowed_pattern in disallowed_patterns:
+        if re.search(disallowed_pattern[0], name, flags=disallowed_pattern[1]):
+            raise ParameterError(_(u"'{0!s}' is an invalid policy name.").format(name))
+
+    if not re.match('^[a-zA-Z0-9_.\- ]*$', name):
+        raise ParameterError(_("The name of the policy may only contain "
+                               "the characters a-zA-Z0-9_.- "))
+
+
diff --git a/privacyidea/api/policy.py b/privacyidea/api/policy.py
--- a/privacyidea/api/policy.py
+++ b/privacyidea/api/policy.py
@@ -40,7 +40,8 @@
                         getLowerParams,
                         optional,
                         required,
-                        send_result)
+                        send_result,
+                        check_policy_name)
 from ..lib.log import log_with
 from ..lib.policy import (set_policy,
                           PolicyClass, ACTION,
@@ -175,12 +176,7 @@ def set_policy_api(name=None):
     """
     res = {}
     param = request.all_data
-    if not re.match('^[a-zA-Z0-9_.]*$', name):
-        raise ParameterError(_("The name of the policy may only contain "
-                               "the characters a-zA-Z0-9_."))
-
-    if name.lower() == "check":
-        raise ParameterError(_("T'check' is an invalid policy name."))
+    check_policy_name(name)
 
     action = getParam(param, "action", required)
     scope = getParam(param, "scope", required)


Test patch:
diff --git a/tests/test_api_lib_utils.py b/tests/test_api_lib_utils.py
--- a/tests/test_api_lib_utils.py
+++ b/tests/test_api_lib_utils.py
@@ -4,7 +4,7 @@
 """
 from .base import MyApiTestCase
 
-from privacyidea.api.lib.utils import (getParam)
+from privacyidea.api.lib.utils import (getParam, check_policy_name)
 from privacyidea.lib.error import ParameterError
 
 
@@ -25,3 +25,20 @@ def test_01_getParam(self):
 
         v = getParam({}, "sslverify", allowed_values=["0", "1"], default="1")
         self.assertEqual("1", v)
+
+    def test_02_check_policy_name(self):
+        check_policy_name("This is a new valid Name")
+        check_policy_name("THis-is-a-valid-Name")
+        # The name "check" is reserved
+        self.assertRaises(ParameterError, check_policy_name, "check")
+        # This is an invalid name
+        self.assertRaises(ParameterError, check_policy_name, "~invalid name")
+
+        # some disallowed patterns:
+        self.assertRaises(ParameterError, check_policy_name, "Check")
+        self.assertRaises(ParameterError, check_policy_name, "pi-update-policy-something")
+        # Some patterns that work
+        check_policy_name("check this out.")
+        check_policy_name("my own pi-update-policy-something")
+        check_policy_name("pi-update-policysomething")
+