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