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