factoring in backbone user obj

This commit is contained in:
Paul Sawaya 2013-01-14 13:59:53 -08:00
Родитель 14d8e8ee31
Коммит f216965a98
5 изменённых файлов: 63 добавлений и 124 удалений

Просмотреть файл

@ -1,56 +1,59 @@
var CommandHandler = function(Messaging, CapturedCredentialStorage, Realms) {
function addLogin(message, sender) {
var currentUser = Gombot.getCurrentUser();
var notificationObj = message,
tabID = sender.tab.id;
// User.NeverAsk.checkForHostname(notificationObj.hostname, function(shouldAsk) {
// Check to see if the user disabled password saving on this site
if (neverSaveOnSites.indexOf(notificationObj.hostname) != -1) return;
if (currentUser.get('disabledSites')[notificationObj.hostname]) return;
notificationObj.type = 'password_observed';
// Look for passwords in use on the current site
getLoginsForSite(notificationObj.hostname,function(logins) {
if (logins === undefined) logins = [];
var loginsForSameUsername = logins.filter(
function(l) { return l.username == notificationObj.username; }
);
if (loginsForSameUsername.length == 1) {
// User already has a login saved for this site.
if (loginsForSameUsername[0].password == notificationObj.password) {
// We're just logging into a site with an existing login. Bail.
return;
}
else {
// Prompt user to update password
notificationObj.type = 'update_password';
// If the existing login stored for this site was PIN locked,
// make sure this new one will be also.
notificationObj.pinLocked = logins[0].pinLocked;
}
}
// Has the user signed up for a Gombot account?
checkIfDidFirstRun(function(didFirstRun) {
if (didFirstRun) {
// Prompt the user to save the login
displayInfobar({
notify: true,
tabID: tabID,
notification: notificationObj
});
}
else {
// Browser not associated with Gombot account, offer
// to create one/log in.
displayInfobar({
notify: true,
tabID: tabID,
notification: {
type: 'signup_nag'
}
});
}
});
// TODO: Send new login to server
var logins = currentUser.get('logins').filter(function(login) {
return login.get('hostname') === notificationObj.hostname;
});
if (logins === undefined) logins = [];
var loginsForSameUsername = logins.filter(
function(l) { return l.username == notificationObj.username; }
);
if (loginsForSameUsername.length == 1) {
// User already has a login saved for this site.
if (loginsForSameUsername[0].password == notificationObj.password) {
// We're just logging into a site with an existing login. Bail.
return;
}
else {
// Prompt user to update password
notificationObj.type = 'update_password';
// If the existing login stored for this site was PIN locked,
// make sure this new one will be also.
notificationObj.pinLocked = logins[0].pinLocked;
}
}
// Has the user signed up for a Gombot account?
checkIfDidFirstRun(function(didFirstRun) {
if (didFirstRun) {
// Prompt the user to save the login
displayInfobar({
notify: true,
tabID: tabID,
notification: notificationObj
});
}
else {
// Browser not associated with Gombot account, offer
// to create one/log in.
displayInfobar({
notify: true,
tabID: tabID,
notification: {
type: 'signup_nag'
}
});
}
});
// TODO: Send new login to server
}
function observingPage(message, sender) {

Просмотреть файл

@ -33,6 +33,15 @@ Gombot.LoginCredential = LoginCredential(Backbone, _);
Gombot.LoginCredentialCollection = LoginCredentialCollection(Backbone, _, Gombot.LoginCredential);
Gombot.User = User(Backbone, _, Gombot.LoginCredentialCollection);
Gombot.getCurrentUser = function() {
return this.currentUser;
};
Gombot.setCurrentUser = function(user) {
this.currentUser = user;
}
var usersStore = new Gombot.Storage("users", function() {
Gombot.UserCollection = UserCollection(Backbone, _, Gombot.User, usersStore);
initGombot();
@ -49,82 +58,6 @@ function initGombot() {
var showSignInPage = Gombot.users.size() > 0;
startFirstRunFlow(showSignInPage);
}});
// Users.fetch({ success: function(collection, response, options) {
// currentUser = collection
// }});
// Load blacklisted sites from localStorage
// loadNeverSaveOnSites();
// Load PIN lock state from localStorage
// loadLoginsLock();
// if (!User.firstRun.wasCompleted()) {
// startFirstRunFlow();
// }
}
//
// Content-to-chrome message handlers
//
// TODO: merge into command handlers
var messageHandlers = {
'add_login': function(message,tabID) {
var notificationObj = message;
// Check to see if the user disabled password saving on this site
User.NeverAsk.checkForHostname(notificationObj.hostname, function(shouldAsk) {
if (!shouldAsk) return;
notificationObj.type = 'password_observed';
notificationObj.hash = SHA1(notificationObj.password);
// Look for passwords in use on the current site
getLoginsForSite(notificationObj.hostname,function(logins) {
if (logins === undefined) logins = [];
var loginsForSameUsername = logins.filter(
function(l) { return l.username == notificationObj.username; }
);
if (loginsForSameUsername.length == 1) {
// User already has a login saved for this site.
if (loginsForSameUsername[0].password == notificationObj.password) {
// We're just logging into a site with an existing login. Bail.
return;
}
else {
// Prompt user to update password
notificationObj.type = 'update_password';
// If the existing login stored for this site was PIN locked,
// make sure this new one will be also.
notificationObj.pinLocked = logins[0].pinLocked;
}
}
// Has the user signed up for a Gombot account?
checkIfDidFirstRun(function(didFirstRun) {
if (didFirstRun) {
// Prompt the user to save the login
displayInfobar({
notify: true,
tabID: tabID,
notification: notificationObj
});
}
else {
// Browser not associated with Gombot account, offer
// to create one/log in.
displayInfobar({
notify: true,
tabID: tabID,
notification: {
type: 'signup_nag'
}
});
}
});
});
});
},
'validate_pin': function(message,tabID,sendResponse) {
sendResponse({
'is_valid': validatePIN(message.pin)
});
}
}
//

Просмотреть файл

@ -26,7 +26,8 @@ var User = function(Backbone, _, LoginCredentialCollection) {
version: USER_DATA_VERSIONS[USER_DATA_VERSIONS.length-1],
pin: null,
logins: null,
email: ""
email: "",
disabledSites: {}
},
initialize: function() {

Просмотреть файл

@ -42,11 +42,12 @@ $(document).ready(function() {
'email': email,
'pin': pin
});
// Copy authKey/cryptKey to user object
user.keys = client.keys;
userCollection.add(user);
user.save(null,{
success: function() {
// Copy authKey/cryptKey to user object
user.keys = client.keys;
Gombot.setCurrentUser(user);
window.location = '/pages/first_run/success.html';
}
});

Просмотреть файл

@ -30,6 +30,7 @@ $(document).ready(function() {
});
if (user) {
user.keys = client.keys;
Gombot.setCurrentUser(user);
window.location = 'success.html';
}
else {