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     imageRoot: '/',
38     /**
39      * @cfg {String/Object} Label for the strength meter (defaults to
40      * 'Password strength:')
41      */
42     // private
43     meterLabel: '',
44     /**
45      * @cfg {String/Object} pwdStrengths A pwdStrengths spec, or true for a default spec (defaults to
46      * ['Weak', 'Medium', 'Strong'])
47      */
48     // private
49     pwdStrengths: [],
50     /**
51      * @cfg {String/Object} fieldsFilter A fieldsFilter spec, as [['field_name', 'error_id'], ...]
52      */
53     // private
54     fieldsFilter: [],
55     // private
56     strength: 0,
57     // private
58     _lastPwd: null,
59     // private
60     kCapitalLetter: 0,
61     kSmallLetter: 1,
62     kDigit: 2,
63     kPunctuation: 3,
64     
65     insecure: false,
66     // private
67     initEvents: function () {
68         Ext.form.SecurePass.superclass.initEvents.call(this);
69
70         if (this.el.is('input[type=password]') && Roo.isSafari) {
71             this.el.on('keydown', this.SafariOnKeyDown, this);
72         }
73
74         this.el.on('keyup', this.checkStrength, this, {buffer: 50});
75     },
76     // private
77     onRender: function (ct, position) {
78         Ext.form.SecurePass.superclass.onRender.call(this, ct, position);
79         this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
80         this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter ' + this.triggerClass});
81
82         this.trigger.createChild({
83             tag: 'div',
84             style: {
85                 'margin-bottom': '10px',
86                 width: this.width + 'px'
87             },
88             cn: {
89                 tag: 'div',
90                 style: {
91                     width: this.width + 'px',
92                     height: '9px',
93                     'background-image': 'url(\'' + this.imageRoot + '/password_meter_grey.gif\')',
94                     'background-position': 'center center',
95                     'background-repeat': 'no-repeat'
96                 },
97                 cn: {
98                     //id: 'PwdMeter',
99                     tag: 'div',
100                     style: {
101                         width: 0,
102                         height: '9px',
103                         'background-image': 'url(\'' + this.imageRoot + '/password_meter.gif\')',
104                         'background-position': 'center center',
105                         'background-repeat': 'no-repeat',
106                         'font-size': '9px'
107                     }
108                 }
109             }
110         });
111         if (this.hideTrigger) {
112             this.trigger.setDisplayed(false);
113         }
114         this.setSize(this.width || '', this.height || '');
115     },
116     // private
117     onDestroy: function () {
118         if (this.trigger) {
119             this.trigger.removeAllListeners();
120             this.trigger.remove();
121         }
122         if (this.wrap) {
123             this.wrap.remove();
124         }
125         Ext.form.TriggerField.superclass.onDestroy.call(this);
126     },
127     // private
128     checkStrength: function () {
129         var pwd = this.el.getValue();
130         if (pwd == this._lastPwd) {
131             return;
132         }
133
134         var strength;
135         if (this.ClientSideStrongPassword(pwd)) {
136             strength = 3;
137         } else if (this.ClientSideMediumPassword(pwd)) {
138             strength = 2;
139         } else if (this.ClientSideWeakPassword(pwd)) {
140             strength = 1;
141         } else {
142             strength = 0;
143         }
144         var pm = this.trigger.child('div/div/div').dom;
145
146         pm.style.width = (this.width / 3) * strength + 'px';
147         //if(this.pwdStrengths != null && strength > 0) {
148         pm.innerHTML = this.meterLabel + '&nbsp;' + this.pwdStrengths[strength];
149         //} else {
150         //      pm.innerHTML = '';
151         //}
152
153         this._lastPwd = pwd;
154     },
155     reset: function () {
156         Ext.form.SecurePass.superclass.reset.call(this);
157         this._lastPwd = '';
158         var pm = this.trigger.child('div/div/div').dom;
159         pm.style.width = 0;
160         pm.innerHTML = '';
161     },
162     // private
163     validateValue: function (value) {
164         if(this.insecure){
165             return true;
166         }
167         if (!Ext.form.TextField.superclass.validateValue.call(this, value)) {
168             return false;
169         }
170         if (value.length == 0) {
171             if (this.allowBlank) {
172                 this.clearInvalid();
173                 return true;
174             }
175
176             this.markInvalid(this.errors.PwdEmpty);
177             return false;
178         }
179         if ('[\x21-\x7e]*'.match(value)) {
180             this.markInvalid(this.errors.PwdBadChar);
181             return false;
182         }
183         if (value.length < 6) {
184             this.markInvalid(this.errors.PwdShort);
185             return false;
186         }
187         if (value.length > 16) {
188             this.markInvalid(this.errors.PwdLong);
189             return false;
190         }
191         var strength;
192         if (this.ClientSideStrongPassword(value)) {
193             strength = 3;
194         } else if (this.ClientSideMediumPassword(value)) {
195             strength = 2;
196         } else if (this.ClientSideWeakPassword(value)) {
197             strength = 1;
198         } else {
199             strength = 0;
200         }
201         if (strength < 2) {
202             this.markInvalid(this.errors.TooWeak);
203             return false;
204         }
205         /*
206          for (var index = 0; index < this.fieldsFilter.length; ++index) {
207          filter = document.getElementById(this.fieldsFilter[index][0]).value;
208          if (filter != '')
209          {
210          re = new RegExp(filter);
211          if (re.test(value)) {
212          this.markInvalid(eval('this.errors.'+ this.fieldsFilter[index][1]));
213          return false;
214          }
215          }
216          }
217          */
218         return true;
219     },
220     // private
221     CharacterSetChecks: function (type) {
222         this.type = type;
223         this.fResult = false;
224     },
225     // private
226     isctype: function (character, type) {
227         switch (type) { //why needed break after return in js ? very odd bug
228             case this.kCapitalLetter:
229                 if (character >= 'A' && character <= 'Z') {
230                     return true;
231                 }
232                 break;
233             case this.kSmallLetter:
234                 if (character >= 'a' && character <= 'z') {
235                     return true;
236                 }
237                 break;
238             case this.kDigit:
239                 if (character >= '0' && character <= '9') {
240                     return true;
241                 }
242                 break;
243             case this.kPunctuation:
244                 if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) {
245                     return true;
246                 }
247                 break;
248             default:
249                 return false;
250         }
251
252     },
253     // private
254     IsLongEnough: function (pwd, size) {
255         return !(pwd == null || isNaN(size) || pwd.length < size);
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     // private
289     ClientSideStrongPassword: function (pwd) {
290         return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
291     },
292     // private
293     ClientSideMediumPassword: function (pwd) {
294         return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
295     },
296     // private
297     ClientSideWeakPassword: function (pwd) {
298         return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);
299     }/*,
300      
301      // private -- see TextFiedl... - not sure why we are duping the code?
302      SafariOnKeyDown : function(event){
303      
304      var isSelectAll = false;
305      if(this.el.dom.selectionEnd > 0){
306      isSelectAll = (this.el.dom.selectionEnd - this.el.dom.selectionStart - this.getValue().length == 0) ? true : false;
307      }
308      if(((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1)){ // backspace and delete key
309      event.preventDefault();
310      this.setValue('');
311      return;
312      };
313      if(isSelectAll){ // backspace and delete key
314      
315      event.preventDefault();
316      this.setValue(String.fromCharCode(
317      this.shiftKey ? event.getKey() : event.getKey().toLowerCase()
318      ));  
319      };
320      } */
321 })