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