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