2014-08-31 23:22:56 +02:00
|
|
|
var App = App || {};
|
|
|
|
|
2014-09-08 22:02:28 +02:00
|
|
|
App.Auth = function(_, jQuery, util, api, appState, promise) {
|
2014-08-31 23:22:56 +02:00
|
|
|
|
2014-09-06 10:00:26 +02:00
|
|
|
var privileges = {
|
|
|
|
register: 'register',
|
|
|
|
listUsers: 'listUsers',
|
2014-09-07 00:33:46 +02:00
|
|
|
viewAllEmailAddresses: 'viewAllEmailAddresses',
|
|
|
|
changeAccessRank: 'changeAccessRank',
|
|
|
|
changeOwnAvatarStyle: 'changeOwnAvatarStyle',
|
|
|
|
changeOwnEmailAddress: 'changeOwnEmailAddress',
|
|
|
|
changeOwnName: 'changeOwnName',
|
|
|
|
changeOwnPassword: 'changeOwnPassword',
|
|
|
|
changeAllAvatarStyles: 'changeAllAvatarStyles',
|
|
|
|
changeAllEmailAddresses: 'changeAllEmailAddresses',
|
|
|
|
changeAllNames: 'changeAllNames',
|
|
|
|
changeAllPasswords: 'changeAllPasswords',
|
2014-09-06 10:00:26 +02:00
|
|
|
deleteOwnAccount: 'deleteOwnAccount',
|
|
|
|
deleteAllAccounts: 'deleteAllAccounts',
|
2014-09-07 19:49:11 +02:00
|
|
|
|
|
|
|
listSafePosts: 'listSafePosts',
|
|
|
|
listSketchyPosts: 'listSketchyPosts',
|
|
|
|
listUnsafePosts: 'listUnsafePosts',
|
|
|
|
uploadPosts: 'uploadPosts',
|
|
|
|
|
|
|
|
listTags: 'listTags',
|
2014-09-06 10:00:26 +02:00
|
|
|
};
|
|
|
|
|
2014-09-09 19:38:16 +02:00
|
|
|
function loginFromCredentials(userNameOrEmail, password, remember) {
|
2014-09-04 18:06:25 +02:00
|
|
|
return promise.make(function(resolve, reject) {
|
2014-09-09 19:38:16 +02:00
|
|
|
promise.wait(api.post('/login', {userNameOrEmail: userNameOrEmail, password: password}))
|
2014-08-31 23:22:56 +02:00
|
|
|
.then(function(response) {
|
2014-09-01 08:07:51 +02:00
|
|
|
updateAppState(response);
|
2014-08-31 23:22:56 +02:00
|
|
|
jQuery.cookie(
|
|
|
|
'auth',
|
|
|
|
response.json.token.name,
|
|
|
|
remember ? { expires: 365 } : {});
|
|
|
|
resolve(response);
|
2014-09-04 18:06:25 +02:00
|
|
|
}).fail(function(response) {
|
2014-08-31 23:22:56 +02:00
|
|
|
reject(response);
|
|
|
|
});
|
|
|
|
});
|
2014-09-05 13:50:19 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
function loginFromToken(token) {
|
2014-09-04 18:06:25 +02:00
|
|
|
return promise.make(function(resolve, reject) {
|
|
|
|
promise.wait(api.post('/login', {token: token}))
|
2014-08-31 23:22:56 +02:00
|
|
|
.then(function(response) {
|
2014-09-01 08:07:51 +02:00
|
|
|
updateAppState(response);
|
2014-08-31 23:22:56 +02:00
|
|
|
resolve(response);
|
2014-09-04 18:06:25 +02:00
|
|
|
}).fail(function(response) {
|
2014-08-31 23:22:56 +02:00
|
|
|
reject(response);
|
|
|
|
});
|
|
|
|
});
|
2014-09-05 13:50:19 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
function loginAnonymous() {
|
2014-09-04 18:06:25 +02:00
|
|
|
return promise.make(function(resolve, reject) {
|
|
|
|
promise.wait(api.post('/login'))
|
2014-08-31 23:22:56 +02:00
|
|
|
.then(function(response) {
|
2014-09-01 08:07:51 +02:00
|
|
|
updateAppState(response);
|
2014-08-31 23:22:56 +02:00
|
|
|
resolve(response);
|
2014-09-04 18:06:25 +02:00
|
|
|
}).fail(function(response) {
|
2014-08-31 23:22:56 +02:00
|
|
|
reject(response);
|
|
|
|
});
|
|
|
|
});
|
2014-09-05 13:50:19 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
function logout() {
|
2014-09-04 18:06:25 +02:00
|
|
|
return promise.make(function(resolve, reject) {
|
2014-08-31 23:22:56 +02:00
|
|
|
jQuery.removeCookie('auth');
|
2014-09-07 08:47:06 +02:00
|
|
|
appState.set('loginToken', null);
|
2014-09-04 19:57:06 +02:00
|
|
|
return loginAnonymous().then(resolve).fail(reject);
|
2014-08-31 23:22:56 +02:00
|
|
|
});
|
2014-09-05 13:50:19 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
function tryLoginFromCookie() {
|
2014-09-04 18:06:25 +02:00
|
|
|
return promise.make(function(resolve, reject) {
|
2014-09-05 13:50:19 +02:00
|
|
|
if (isLoggedIn()) {
|
2014-08-31 23:22:56 +02:00
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var authCookie = jQuery.cookie('auth');
|
|
|
|
if (!authCookie) {
|
|
|
|
reject();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-04 18:06:25 +02:00
|
|
|
promise.wait(loginFromToken(authCookie))
|
|
|
|
.then(function(response) {
|
|
|
|
resolve();
|
|
|
|
}).fail(function(response) {
|
|
|
|
jQuery.removeCookie('auth');
|
|
|
|
reject();
|
|
|
|
});
|
2014-08-31 23:22:56 +02:00
|
|
|
});
|
2014-09-05 13:50:19 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
2014-09-01 08:07:51 +02:00
|
|
|
function updateAppState(response) {
|
2014-09-04 19:57:06 +02:00
|
|
|
appState.set('privileges', response.json.privileges || []);
|
2014-09-01 08:07:51 +02:00
|
|
|
appState.set('loginToken', response.json.token && response.json.token.name);
|
2014-09-03 09:10:26 +02:00
|
|
|
appState.set('loggedIn', response.json.user && !!response.json.user.id);
|
2014-09-10 19:19:30 +02:00
|
|
|
appState.set('loggedInUser', response.json.user);
|
2014-09-01 08:07:51 +02:00
|
|
|
}
|
|
|
|
|
2014-09-06 10:00:26 +02:00
|
|
|
function isLoggedIn(userName) {
|
2014-09-08 22:02:28 +02:00
|
|
|
if (!appState.get('loggedIn')) {
|
2014-09-06 10:00:26 +02:00
|
|
|
return false;
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
|
|
|
if (typeof(userName) !== 'undefined') {
|
|
|
|
if (getCurrentUser().name !== userName) {
|
2014-09-06 10:00:26 +02:00
|
|
|
return false;
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-09-06 10:00:26 +02:00
|
|
|
}
|
|
|
|
return true;
|
2014-09-05 13:50:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentUser() {
|
|
|
|
return appState.get('loggedInUser');
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentPrivileges() {
|
|
|
|
return appState.get('privileges');
|
|
|
|
}
|
|
|
|
|
2014-09-10 19:19:30 +02:00
|
|
|
function updateCurrentUser(user) {
|
|
|
|
if (user.id !== getCurrentUser().id) {
|
|
|
|
throw new Error('Cannot set current user to other user this way.');
|
|
|
|
}
|
|
|
|
appState.set('loggedInUser', user);
|
|
|
|
}
|
|
|
|
|
2014-09-05 13:50:19 +02:00
|
|
|
function hasPrivilege(privilege) {
|
|
|
|
return _.contains(getCurrentPrivileges(), privilege);
|
|
|
|
}
|
|
|
|
|
|
|
|
function startObservingLoginChanges(listenerName, callback) {
|
2014-09-10 19:19:30 +02:00
|
|
|
appState.startObserving('loggedInUser', listenerName, callback);
|
2014-09-05 13:50:19 +02:00
|
|
|
}
|
|
|
|
|
2014-08-31 23:22:56 +02:00
|
|
|
return {
|
|
|
|
loginFromCredentials: loginFromCredentials,
|
|
|
|
loginFromToken: loginFromToken,
|
|
|
|
loginAnonymous: loginAnonymous,
|
|
|
|
tryLoginFromCookie: tryLoginFromCookie,
|
|
|
|
logout: logout,
|
2014-09-06 10:00:26 +02:00
|
|
|
|
|
|
|
startObservingLoginChanges: startObservingLoginChanges,
|
2014-09-05 13:50:19 +02:00
|
|
|
isLoggedIn: isLoggedIn,
|
|
|
|
getCurrentUser: getCurrentUser,
|
2014-09-10 19:19:30 +02:00
|
|
|
updateCurrentUser: updateCurrentUser,
|
2014-09-05 13:50:19 +02:00
|
|
|
getCurrentPrivileges: getCurrentPrivileges,
|
|
|
|
hasPrivilege: hasPrivilege,
|
2014-09-06 10:00:26 +02:00
|
|
|
|
|
|
|
privileges: privileges,
|
2014-08-31 23:22:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
App.DI.registerSingleton('auth', App.Auth);
|