szurubooru/public_html/js/Auth.js

188 lines
4.8 KiB
JavaScript
Raw Normal View History

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-30 22:09:43 +02:00
viewUsers: 'viewUsers',
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-30 13:22:11 +02:00
ban: 'ban',
2014-09-07 19:49:11 +02:00
2014-09-30 22:09:43 +02:00
listPosts: 'listPosts',
viewPosts: 'viewPosts',
2014-09-07 19:49:11 +02:00
uploadPosts: 'uploadPosts',
2014-09-15 11:38:24 +02:00
uploadPostsAnonymously: 'uploadPostsAnonymously',
2014-09-23 20:18:12 +02:00
deletePosts: 'deletePosts',
2014-09-24 23:24:51 +02:00
featurePosts: 'featurePosts',
2014-09-25 19:11:41 +02:00
changePostSafety: 'changePostSafety',
changePostSource: 'changePostSource',
changePostTags: 'changePostTags',
changePostContent: 'changePostContent',
changePostThumbnail: 'changePostThumbnail',
2014-09-25 23:53:47 +02:00
changePostRelations: 'changePostRelations',
2014-10-09 09:45:06 +02:00
changePostFlags: 'changePostFlags',
2014-09-07 19:49:11 +02:00
2014-10-04 13:56:38 +02:00
listComments: 'listComments',
addComments: 'addComments',
editOwnComments: 'editOwnComments',
editAllComments: 'editAllComments',
deleteOwnComments: 'deleteOwnComments',
deleteAllComments: 'deleteAllComments',
2014-09-07 19:49:11 +02:00
listTags: 'listTags',
2014-09-26 20:41:28 +02:00
viewHistory: 'viewHistory',
2014-09-06 10:00:26 +02:00
};
function loginFromCredentials(userNameOrEmail, password, remember) {
2014-09-04 18:06:25 +02:00
return promise.make(function(resolve, reject) {
promise.wait(api.post('/login', {userNameOrEmail: userNameOrEmail, 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, isFromCookie) {
2014-09-04 18:06:25 +02:00
return promise.make(function(resolve, reject) {
var fd = {
token: token,
isFromCookie: isFromCookie
};
promise.wait(api.post('/login', fd))
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-10-02 00:30:25 +02:00
return promise.wait(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;
}
promise.wait(loginFromToken(authCookie, true))
2014-09-04 18:06:25 +02:00
.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('loggedIn', response.json.user && !!response.json.user.id);
appState.set('loggedInUser', response.json.user);
}
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;
}
function getCurrentUser() {
return appState.get('loggedInUser');
}
function getCurrentPrivileges() {
return appState.get('privileges');
}
function updateCurrentUser(user) {
if (user.id !== getCurrentUser().id) {
throw new Error('Cannot set current user to other user this way.');
}
appState.set('loggedInUser', user);
}
function hasPrivilege(privilege) {
return _.contains(getCurrentPrivileges(), privilege);
}
function startObservingLoginChanges(listenerName, callback) {
appState.startObserving('loggedInUser', 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,
updateCurrentUser: updateCurrentUser,
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', ['_', 'jQuery', 'util', 'api', 'appState', 'promise'], App.Auth);