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