better support for mailchimp emails
[roojs1] / Roo / bootstrap / form / SecurePass.js
1 /*
2  * - LGPL
3  *
4  * Input
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.form.SecurePass
10  * @extends Roo.bootstrap.form.Input
11  * Bootstrap SecurePass class
12  *
13  * 
14  * @constructor
15  * Create a new SecurePass
16  * @param {Object} config The config object
17  */
18  
19 Roo.bootstrap.form.SecurePass = function (config) {
20     // these go here, so the translation tool can replace them..
21     this.errors = {
22         PwdEmpty: "Please type a password, and then retype it to confirm.",
23         PwdShort: "Your password must be at least 6 characters long. Please type a different password.",
24         PwdLong: "Your password can't contain more than 16 characters. Please type a different password.",
25         PwdBadChar: "The password contains characters that aren't allowed. Please type a different password.",
26         IDInPwd: "Your password can't include the part of your ID. Please type a different password.",
27         FNInPwd: "Your password can't contain your first name. Please type a different password.",
28         LNInPwd: "Your password can't contain your last name. Please type a different password.",
29         TooWeak: "Your password is Too Weak."
30     },
31     this.meterLabel = "Password strength:";
32     this.pwdStrengths = ["Too Weak", "Weak", "Medium", "Strong"];
33     this.meterClass = [
34         "roo-password-meter-tooweak", 
35         "roo-password-meter-weak", 
36         "roo-password-meter-medium", 
37         "roo-password-meter-strong", 
38         "roo-password-meter-grey"
39     ];
40     
41     this.errors = {};
42     
43     Roo.bootstrap.form.SecurePass.superclass.constructor.call(this, config);
44 }
45
46 Roo.extend(Roo.bootstrap.form.SecurePass, Roo.bootstrap.form.Input, {
47     /**
48      * @cfg {String/Object} errors A Error spec, or true for a default spec (defaults to
49      * {
50      *  PwdEmpty: "Please type a password, and then retype it to confirm.",
51      *  PwdShort: "Your password must be at least 6 characters long. Please type a different password.",
52      *  PwdLong: "Your password can't contain more than 16 characters. Please type a different password.",
53      *  PwdBadChar: "The password contains characters that aren't allowed. Please type a different password.",
54      *  IDInPwd: "Your password can't include the part of your ID. Please type a different password.",
55      *  FNInPwd: "Your password can't contain your first name. Please type a different password.",
56      *  LNInPwd: "Your password can't contain your last name. Please type a different password."
57      * })
58      */
59     // private
60     
61     meterWidth: 300,
62     errorMsg :'',    
63     errors: false,
64     imageRoot: '/',
65     /**
66      * @cfg {String/Object} Label for the strength meter (defaults to
67      * 'Password strength:')
68      */
69     // private
70     meterLabel: '',
71     /**
72      * @cfg {String/Object} pwdStrengths A pwdStrengths spec, or true for a default spec (defaults to
73      * ['Weak', 'Medium', 'Strong'])
74      */
75     // private    
76     pwdStrengths: false,    
77     // private
78     strength: 0,
79     // private
80     _lastPwd: null,
81     // private
82     kCapitalLetter: 0,
83     kSmallLetter: 1,
84     kDigit: 2,
85     kPunctuation: 3,
86     
87     insecure: false,
88     // private
89     initEvents: function ()
90     {
91         Roo.bootstrap.form.SecurePass.superclass.initEvents.call(this);
92
93         if (this.el.is('input[type=password]') && Roo.isSafari) {
94             this.el.on('keydown', this.SafariOnKeyDown, this);
95         }
96
97         this.el.on('keyup', this.checkStrength, this, {buffer: 50});
98     },
99     // private
100     onRender: function (ct, position)
101     {
102         Roo.bootstrap.form.SecurePass.superclass.onRender.call(this, ct, position);
103         this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
104         this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter ' + this.triggerClass});
105
106         this.trigger.createChild({
107                    cn: [
108                     {
109                     //id: 'PwdMeter',
110                     tag: 'div',
111                     cls: 'roo-password-meter-grey col-xs-12',
112                     style: {
113                         //width: 0,
114                         //width: this.meterWidth + 'px'                                                
115                         }
116                     },
117                     {                            
118                          cls: 'roo-password-meter-text'                          
119                     }
120                 ]            
121         });
122
123          
124         if (this.hideTrigger) {
125             this.trigger.setDisplayed(false);
126         }
127         this.setSize(this.width || '', this.height || '');
128     },
129     // private
130     onDestroy: function ()
131     {
132         if (this.trigger) {
133             this.trigger.removeAllListeners();
134             this.trigger.remove();
135         }
136         if (this.wrap) {
137             this.wrap.remove();
138         }
139         Roo.bootstrap.form.TriggerField.superclass.onDestroy.call(this);
140     },
141     // private
142     checkStrength: function ()
143     {
144         var pwd = this.inputEl().getValue();
145         if (pwd == this._lastPwd) {
146             return;
147         }
148
149         var strength;
150         if (this.ClientSideStrongPassword(pwd)) {
151             strength = 3;
152         } else if (this.ClientSideMediumPassword(pwd)) {
153             strength = 2;
154         } else if (this.ClientSideWeakPassword(pwd)) {
155             strength = 1;
156         } else {
157             strength = 0;
158         }
159         
160         Roo.log('strength1: ' + strength);
161         
162         //var pm = this.trigger.child('div/div/div').dom;
163         var pm = this.trigger.child('div/div');
164         pm.removeClass(this.meterClass);
165         pm.addClass(this.meterClass[strength]);
166                 
167         
168         var pt = this.trigger.child('/div').child('>*[class=roo-password-meter-text]').dom;        
169                 
170         pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
171         
172         this._lastPwd = pwd;
173     },
174     reset: function ()
175     {
176         Roo.bootstrap.form.SecurePass.superclass.reset.call(this);
177         
178         this._lastPwd = '';
179         
180         var pm = this.trigger.child('div/div');
181         pm.removeClass(this.meterClass);
182         pm.addClass('roo-password-meter-grey');        
183         
184         
185         var pt = this.trigger.child('/div').child('>*[class=roo-password-meter-text]').dom;        
186         
187         pt.innerHTML = '';
188         this.inputEl().dom.type='password';
189     },
190     // private
191     validateValue: function (value)
192     {
193         if (!Roo.bootstrap.form.SecurePass.superclass.validateValue.call(this, value)) {
194             return false;
195         }
196         if (value.length == 0) {
197             if (this.allowBlank) {
198                 this.clearInvalid();
199                 return true;
200             }
201
202             this.markInvalid(this.errors.PwdEmpty);
203             this.errorMsg = this.errors.PwdEmpty;
204             return false;
205         }
206         
207         if(this.insecure){
208             return true;
209         }
210         
211         if (!value.match(/[\x21-\x7e]+/)) {
212             this.markInvalid(this.errors.PwdBadChar);
213             this.errorMsg = this.errors.PwdBadChar;
214             return false;
215         }
216         if (value.length < 6) {
217             this.markInvalid(this.errors.PwdShort);
218             this.errorMsg = this.errors.PwdShort;
219             return false;
220         }
221         if (value.length > 16) {
222             this.markInvalid(this.errors.PwdLong);
223             this.errorMsg = this.errors.PwdLong;
224             return false;
225         }
226         var strength;
227         if (this.ClientSideStrongPassword(value)) {
228             strength = 3;
229         } else if (this.ClientSideMediumPassword(value)) {
230             strength = 2;
231         } else if (this.ClientSideWeakPassword(value)) {
232             strength = 1;
233         } else {
234             strength = 0;
235         }
236
237         
238         if (strength < 2) {
239             //this.markInvalid(this.errors.TooWeak);
240             this.errorMsg = this.errors.TooWeak;
241             //return false;
242         }
243         
244         
245         console.log('strength2: ' + strength);
246         
247         //var pm = this.trigger.child('div/div/div').dom;
248         
249         var pm = this.trigger.child('div/div');
250         pm.removeClass(this.meterClass);
251         pm.addClass(this.meterClass[strength]);
252                 
253         var pt = this.trigger.child('/div').child('>*[class=roo-password-meter-text]').dom;        
254                 
255         pt.innerHTML = this.meterLabel + '&nbsp;' + this.pwdStrengths[strength];
256         
257         this.errorMsg = ''; 
258         return true;
259     },
260     // private
261     CharacterSetChecks: function (type)
262     {
263         this.type = type;
264         this.fResult = false;
265     },
266     // private
267     isctype: function (character, type)
268     {
269         switch (type) {  
270             case this.kCapitalLetter:
271                 if (character >= 'A' && character <= 'Z') {
272                     return true;
273                 }
274                 break;
275             
276             case this.kSmallLetter:
277                 if (character >= 'a' && character <= 'z') {
278                     return true;
279                 }
280                 break;
281             
282             case this.kDigit:
283                 if (character >= '0' && character <= '9') {
284                     return true;
285                 }
286                 break;
287             
288             case this.kPunctuation:
289                 if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) {
290                     return true;
291                 }
292                 break;
293             
294             default:
295                 return false;
296         }
297
298     },
299     // private
300     IsLongEnough: function (pwd, size)
301     {
302         return !(pwd == null || isNaN(size) || pwd.length < size);
303     },
304     // private
305     SpansEnoughCharacterSets: function (word, nb)
306     {
307         if (!this.IsLongEnough(word, nb))
308         {
309             return false;
310         }
311
312         var characterSetChecks = new Array(
313             new this.CharacterSetChecks(this.kCapitalLetter), new this.CharacterSetChecks(this.kSmallLetter),
314             new this.CharacterSetChecks(this.kDigit), new this.CharacterSetChecks(this.kPunctuation)
315         );
316         
317         for (var index = 0; index < word.length; ++index) {
318             for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
319                 if (!characterSetChecks[nCharSet].fResult && this.isctype(word.charAt(index), characterSetChecks[nCharSet].type)) {
320                     characterSetChecks[nCharSet].fResult = true;
321                     break;
322                 }
323             }
324         }
325
326         var nCharSets = 0;
327         for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
328             if (characterSetChecks[nCharSet].fResult) {
329                 ++nCharSets;
330             }
331         }
332
333         if (nCharSets < nb) {
334             return false;
335         }
336         return true;
337     },
338     // private
339     ClientSideStrongPassword: function (pwd)
340     {
341         return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
342     },
343     // private
344     ClientSideMediumPassword: function (pwd)
345     {
346         return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
347     },
348     // private
349     ClientSideWeakPassword: function (pwd)
350     {
351         return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);
352     }
353           
354 })