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