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