From 683f7133190aa350cdd1ea2608c90fe5f47b35cd Mon Sep 17 00:00:00 2001
From: David Lu <david.lu@hotmail.com>
Date: Wed, 6 Jul 2016 18:54:54 -0400
Subject: PLT-1465 Added password requirements (#3489)

* Added password requirements

* added tweaks

* fixed error code

* removed http.StatusNotAcceptable
---
 webapp/utils/utils.jsx | 68 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 67 insertions(+), 1 deletion(-)

(limited to 'webapp/utils/utils.jsx')

diff --git a/webapp/utils/utils.jsx b/webapp/utils/utils.jsx
index 1b4b504f2..5f88f5b73 100644
--- a/webapp/utils/utils.jsx
+++ b/webapp/utils/utils.jsx
@@ -14,10 +14,13 @@ import * as AsyncClient from './async_client.jsx';
 import Client from './web_client.jsx';
 
 import {browserHistory} from 'react-router/es6';
+import {FormattedMessage} from 'react-intl';
 
 import icon50 from 'images/icon50x50.png';
 import bing from 'images/bing.mp3';
 
+import React from 'react';
+
 export function isEmail(email) {
     // writing a regex to match all valid email addresses is really, really hard (see http://stackoverflow.com/a/201378)
     // so we just do a simple check and rely on a verification email to tell if it's a real address
@@ -1366,4 +1369,67 @@ export function canCreateCustomEmoji(user) {
     }
 
     return true;
-}
\ No newline at end of file
+}
+
+export function isValidPassword(password) {
+    let errorMsg = '';
+    let errorId = 'user.settings.security.passwordError';
+    let error = false;
+    let minimumLength = Constants.MIN_PASSWORD_LENGTH;
+
+    if (global.window.mm_config.BuildEnterpriseReady === 'true' && global.window.mm_license.IsLicensed === 'true' && global.window.mm_license.PasswordRequirements === 'true') {
+        if (password.length < parseInt(global.window.mm_config.PasswordMinimumLength, 10) || password.length > Constants.MAX_PASSWORD_LENGTH) {
+            error = true;
+        }
+
+        if (global.window.mm_config.PasswordRequireLowercase === 'true') {
+            if (!password.match(/[a-z]/)) {
+                error = true;
+            }
+
+            errorId = errorId + 'Lowercase';
+        }
+
+        if (global.window.mm_config.PasswordRequireUppercase === 'true') {
+            if (!password.match(/[0-9]/)) {
+                error = true;
+            }
+
+            errorId = errorId + 'Uppercase';
+        }
+
+        if (global.window.mm_config.PasswordRequireNumber === 'true') {
+            if (!password.match(/[A-Z]/)) {
+                error = true;
+            }
+
+            errorId = errorId + 'Number';
+        }
+
+        if (global.window.mm_config.PasswordRequireSymbol === 'true') {
+            if (!password.match(/[ !"\\#$%&'()*+,-./:;<=>?@[\]^_`|~]/)) {
+                error = true;
+            }
+
+            errorId = errorId + 'Symbol';
+        }
+
+        minimumLength = global.window.mm_config.PasswordMinimumLength;
+    } else if (password.length < Constants.MIN_PASSWORD_LENGTH) {
+        error = true;
+    }
+
+    if (error) {
+        errorMsg = (
+            <FormattedMessage
+                id={errorId}
+                default='Your password must be at least {min} characters.'
+                values={{
+                    min: minimumLength
+                }}
+            />
+        );
+    }
+
+    return errorMsg;
+}
-- 
cgit v1.2.3-1-g7c22

