71f5c4383c64d2660c6c9cc6e48a004b2734849d
[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                     if((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1){ // backspace and delete key
77                         event.preventDefault();
78                         this.setValue('');
79                     };
80                 },this);
81             }
82             Ext.form.SecurePass.superclass.initEvents.call(this);
83             this.el.on('keyup', this.checkStrength, this, {buffer:50});
84         },
85
86         // private
87         onRender : function(ct, position){
88                 Ext.form.SecurePass.superclass.onRender.call(this, ct, position);
89                 this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
90                 this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter '+this.triggerClass});
91                  
92                 this.trigger.createChild({
93             tag: 'div',  
94             style: {
95                 'margin-bottom': '10px',
96                 width: this.width + 'px'
97             },
98             cn: {
99                 tag: 'div', 
100                 style: {
101                     width: this.width + 'px',
102                     height: '9px',
103                     'background-image' : 'url(\''+this.imageRoot+'/password_meter_grey.gif\')',
104                     'background-position' : 'center center',
105                     'background-repeat': 'no-repeat'
106                 },
107                 cn : {
108                     //id: 'PwdMeter',
109                     tag: 'div',
110                     style:  {
111                         width: 0,
112                         height: '9px',
113                         'background-image': 'url(\''+this.imageRoot+'/password_meter.gif\')',
114                         'background-position': 'center center',
115                         'background-repeat': 'no-repeat',
116                         'font-size': '9px'
117                     }
118                 }
119             }
120         });
121                 if(this.hideTrigger){
122                         this.trigger.setDisplayed(false);
123                 }
124                 this.setSize(this.width||'', this.height||'');
125         },
126
127         // private
128         onDestroy : function(){
129                 if(this.trigger){
130                         this.trigger.removeAllListeners();
131                         this.trigger.remove();
132                 }
133                 if(this.wrap){
134                         this.wrap.remove();
135                 }
136                 Ext.form.TriggerField.superclass.onDestroy.call(this);
137         },
138     
139         // private
140         checkStrength : function(){
141                 var pwd = this.el.getValue();
142                 if (pwd == this._lastPwd) {
143                         return;
144                 }
145
146                 var strength;
147                 if (this.ClientSideStrongPassword(pwd)) {
148                         strength = 3;
149                 } else if(this.ClientSideMediumPassword(pwd)) {
150                         strength = 2;
151                 } else if(this.ClientSideWeakPassword(pwd)) {
152                         strength = 1;
153                 } else {
154                         strength = 0;
155                 }
156         var pm = this.trigger.child('div/div/div').dom;
157         
158                 pm.style.width = (this.width/3) * strength +'px';
159                 //if(this.pwdStrengths != null && strength > 0) {
160         pm.innerHTML = this.meterLabel + '&nbsp;'+ this.pwdStrengths[strength];
161                 //} else {
162                 //      pm.innerHTML = '';
163                 //}
164
165                 this._lastPwd = pwd;
166         },
167     reset : function(){
168         Ext.form.SecurePass.superclass.reset.call(this);
169         this._lastPwd = '';
170         var pm = this.trigger.child('div/div/div').dom;
171         pm.style.width = 0;
172         pm.innerHTML = '';
173     },
174     // private
175         validateValue : function(value){
176                 if (!Ext.form.TextField.superclass.validateValue.call(this, value)){
177             return false;
178         }
179                 if (value.length == 0) {
180             if (this.allowBlank) {
181                 this.clearInvalid();
182                 return true;
183             }
184             
185                         this.markInvalid(this.errors.PwdEmpty);
186             return false;
187                 }
188                 if ('[\x21-\x7e]*'.match(value)) {
189                         this.markInvalid(this.errors.PwdBadChar);
190             return false;
191                 }
192                 if (value.length < 6) {
193                         this.markInvalid(this.errors.PwdShort);
194             return false;
195                 }
196                 if (value.length > 16) {
197                         this.markInvalid(this.errors.PwdLong);
198             return false;
199                 }
200         var strength;
201                 if (this.ClientSideStrongPassword(value)) {
202                         strength = 3;
203                 } else if(this.ClientSideMediumPassword(value)) {
204                         strength = 2;
205                 } else if(this.ClientSideWeakPassword(value)) {
206                         strength = 1;
207                 } else {
208                         strength = 0;
209                 }
210         if (strength < 2) {
211                         this.markInvalid(this.errors.TooWeak);
212             return false;
213                 }
214         /*
215                 for (var index = 0; index < this.fieldsFilter.length; ++index) {
216                         filter = document.getElementById(this.fieldsFilter[index][0]).value;
217                         if (filter != '')
218                         {
219                                 re = new RegExp(filter);
220                                 if (re.test(value)) {
221                                         this.markInvalid(eval('this.errors.'+ this.fieldsFilter[index][1]));
222                                         return false;
223                                 }
224                         }
225                 }
226         */
227                 return true;
228         },
229
230     // private
231         CharacterSetChecks : function(type){
232                 this.type = type;
233                 this.fResult = false;
234         },
235
236     // private
237         isctype : function(character, type){
238                 switch (type) { //why needed break after return in js ? very odd bug
239                         case this.kCapitalLetter: if (character >= 'A' && character <= 'Z') { return true; } break;
240                         case this.kSmallLetter: if (character >= 'a' && character <= 'z') { return true; } break;
241                         case this.kDigit: if (character >= '0' && character <= '9') { return true; } break;
242                         case this.kPunctuation: if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) { return true; } break;
243                         default: return false;
244                 }
245         
246         },
247
248     // private
249         IsLongEnough : function(pwd, size){
250                 return !(pwd == null || isNaN(size) || pwd.length < size);
251         },
252
253     // private
254         SpansEnoughCharacterSets : function(word, nb){
255                 if (!this.IsLongEnough(word, nb))
256                 {
257                         return false;
258                 }
259
260                 var characterSetChecks = new Array(
261                         new this.CharacterSetChecks(this.kCapitalLetter), new this.CharacterSetChecks(this.kSmallLetter),
262                         new this.CharacterSetChecks(this.kDigit), new this.CharacterSetChecks(this.kPunctuation));
263                 for (var index = 0; index < word.length; ++index) {
264                         for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
265                                 if (!characterSetChecks[nCharSet].fResult && this.isctype(word.charAt(index), characterSetChecks[nCharSet].type)) {
266                                         characterSetChecks[nCharSet].fResult = true;
267                                         break;
268                                 }
269                         }
270                 }
271
272                 var nCharSets = 0;
273                 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
274                         if (characterSetChecks[nCharSet].fResult) {
275                                 ++nCharSets;
276                         }
277                 }
278
279                 if (nCharSets < nb) {
280                         return false;
281                 }
282                 return true;
283         },
284
285     // private
286         ClientSideStrongPassword : function(pwd){
287                 return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
288         },
289
290     // private
291         ClientSideMediumPassword : function(pwd){
292                 return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
293         },
294
295     // private
296         ClientSideWeakPassword : function(pwd){
297                 return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);
298         }
299 })