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