szurubooru/public_html/js/Auth.js

145 lines
3.6 KiB
JavaScript
Raw Normal View History

2014-08-31 23:22:56 +02:00
var App = App || {};
2014-09-04 18:06:25 +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',
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-08-31 23:22:56 +02:00
function loginFromCredentials(userName, password, remember) {
2014-09-04 18:06:25 +02:00
return promise.make(function(resolve, reject) {
promise.wait(api.post('/login', {userName: userName, password: password}))
2014-08-31 23:22:56 +02:00
.then(function(response) {
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-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) {
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-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) {
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-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-08-31 23:22:56 +02:00
function tryLoginFromCookie() {
2014-09-04 18:06:25 +02:00
return promise.make(function(resolve, reject) {
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-08-31 23:22:56 +02:00
function updateAppState(response) {
2014-09-04 19:57:06 +02:00
appState.set('privileges', response.json.privileges || []);
appState.set('loginToken', response.json.token && response.json.token.name);
appState.set('loggedInUser', response.json.user);
appState.set('loggedIn', response.json.user && !!response.json.user.id);
}
2014-09-06 10:00:26 +02:00
function isLoggedIn(userName) {
if (!appState.get('loggedIn'))
return false;
if (typeof(userName) != 'undefined') {
if (getCurrentUser().name != userName)
return false;
}
return true;
}
function getCurrentUser() {
return appState.get('loggedInUser');
}
function getCurrentPrivileges() {
return appState.get('privileges');
}
function hasPrivilege(privilege) {
return _.contains(getCurrentPrivileges(), privilege);
}
function startObservingLoginChanges(listenerName, callback) {
appState.startObserving('loggedIn', listenerName, callback);
}
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,
isLoggedIn: isLoggedIn,
getCurrentUser: getCurrentUser,
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);