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