| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview | 6 * @fileoverview |
| 7 * 'settings-users-add-user-dialog' is the dialog shown for adding new allowed | 7 * 'settings-users-add-user-dialog' is the dialog shown for adding new allowed |
| 8 * users to a ChromeOS device. | 8 * users to a ChromeOS device. |
| 9 */ | 9 */ |
| 10 (function() { | 10 (function() { |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * Regular expression for adding a user where the string provided is just | 13 * Regular expression for adding a user where the string provided is just |
| 14 * the part before the "@". | 14 * the part before the "@". |
| 15 * Email alias only, assuming it's a gmail address. | 15 * Email alias only, assuming it's a gmail address. |
| 16 * e.g. 'john' | 16 * e.g. 'john' |
| 17 * @const {!RegExp} | 17 * @const {!RegExp} |
| 18 */ | 18 */ |
| 19 var NAME_ONLY_REGEX = new RegExp( | 19 var NAME_ONLY_REGEX = |
| 20 '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)\\s*$'); | 20 new RegExp('^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)\\s*$'); |
| 21 | |
| 22 /** | |
| 23 * Regular expression for adding a user where the string provided is a full | |
| 24 * email address. | |
| 25 * e.g. 'john@chromium.org' | |
| 26 * @const {!RegExp} | |
| 27 */ | |
| 28 var EMAIL_REGEX = new RegExp( | |
| 29 '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)@' + | |
| 30 '([A-Za-z0-9\-]{2,63}\\..+)\\s*$'); | |
| 31 | |
| 32 Polymer({ | |
| 33 is: 'settings-users-add-user-dialog', | |
| 34 | |
| 35 open: function() { | |
| 36 this.$.dialog.showModal(); | |
| 37 }, | |
| 38 | |
| 39 /** @private */ | |
| 40 onCancelTap_: function() { | |
| 41 this.$.dialog.cancel(); | |
| 42 }, | |
| 43 | 21 |
| 44 /** | 22 /** |
| 23 * Regular expression for adding a user where the string provided is a full |
| 24 * email address. |
| 25 * e.g. 'john@chromium.org' |
| 26 * @const {!RegExp} |
| 27 */ |
| 28 var EMAIL_REGEX = new RegExp( |
| 29 '^\\s*([\\w\\.!#\\$%&\'\\*\\+-\\/=\\?\\^`\\{\\|\\}~]+)@' + |
| 30 '([A-Za-z0-9\-]{2,63}\\..+)\\s*$'); |
| 31 |
| 32 Polymer({ |
| 33 is: 'settings-users-add-user-dialog', |
| 34 |
| 35 open: function() { |
| 36 this.$.dialog.showModal(); |
| 37 }, |
| 38 |
| 39 /** @private */ |
| 40 onCancelTap_: function() { |
| 41 this.$.dialog.cancel(); |
| 42 }, |
| 43 |
| 44 /** |
| 45 * Validates that the new user entered is valid. | 45 * Validates that the new user entered is valid. |
| 46 * @private | 46 * @private |
| 47 * @return {boolean} | 47 * @return {boolean} |
| 48 */ | 48 */ |
| 49 validate_: function() { | 49 validate_: function() { |
| 50 var input = this.$.addUserInput.value; | 50 var input = this.$.addUserInput.value; |
| 51 var valid = NAME_ONLY_REGEX.test(input) || EMAIL_REGEX.test(input); | 51 var valid = NAME_ONLY_REGEX.test(input) || EMAIL_REGEX.test(input); |
| 52 | 52 |
| 53 this.$.add.disabled = !valid; | 53 this.$.add.disabled = !valid; |
| 54 this.$.addUserInput.invalid = !valid; | 54 this.$.addUserInput.invalid = !valid; |
| 55 return valid; | 55 return valid; |
| 56 }, | 56 }, |
| 57 | 57 |
| 58 /** @private */ | 58 /** @private */ |
| 59 addUser_: function() { | 59 addUser_: function() { |
| 60 // May be submitted by the Enter key even if the input value is invalid. | 60 // May be submitted by the Enter key even if the input value is invalid. |
| 61 if (!this.validate_()) | 61 if (!this.validate_()) |
| 62 return; | 62 return; |
| 63 | 63 |
| 64 var input = this.$.addUserInput.value; | 64 var input = this.$.addUserInput.value; |
| 65 | 65 |
| 66 var nameOnlyMatches = NAME_ONLY_REGEX.exec(input); | 66 var nameOnlyMatches = NAME_ONLY_REGEX.exec(input); |
| 67 var userEmail; | 67 var userEmail; |
| 68 if (nameOnlyMatches) { | 68 if (nameOnlyMatches) { |
| 69 userEmail = nameOnlyMatches[1] + '@gmail.com'; | 69 userEmail = nameOnlyMatches[1] + '@gmail.com'; |
| 70 } else { | 70 } else { |
| 71 var emailMatches = EMAIL_REGEX.exec(input); | 71 var emailMatches = EMAIL_REGEX.exec(input); |
| 72 // Assuming the input validated, one of these two must match. | 72 // Assuming the input validated, one of these two must match. |
| 73 assert(emailMatches); | 73 assert(emailMatches); |
| 74 userEmail = emailMatches[1] + '@' + emailMatches[2]; | 74 userEmail = emailMatches[1] + '@' + emailMatches[2]; |
| 75 } | 75 } |
| 76 | 76 |
| 77 chrome.usersPrivate.addWhitelistedUser( | 77 chrome.usersPrivate.addWhitelistedUser( |
| 78 userEmail, | 78 userEmail, |
| 79 /* callback */ function(success) {}); | 79 /* callback */ function(success) {}); |
| 80 this.$.addUserInput.value = ''; | 80 this.$.addUserInput.value = ''; |
| 81 this.$.dialog.close(); | 81 this.$.dialog.close(); |
| 82 }, | 82 }, |
| 83 }); | 83 }); |
| 84 | 84 |
| 85 })(); | 85 })(); |
| OLD | NEW |