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