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',
|
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-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-10-09 23:46:32 +02:00
|
|
|
massTag: 'massTag',
|
2014-10-10 09:53:05 +02:00
|
|
|
changeTagName: 'changeTagName',
|
2014-09-26 20:41:28 +02:00
|
|
|
|
|
|
|
viewHistory: 'viewHistory',
|
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
|
|
|
|
2014-09-23 20:35:41 +02:00
|
|
|
function loginFromToken(token, isFromCookie) {
|
2014-09-04 18:06:25 +02:00
|
|
|
return promise.make(function(resolve, reject) {
|
2014-09-23 20:35:41 +02:00
|
|
|
var fd = {
|
|
|
|
token: token,
|
|
|
|
isFromCookie: isFromCookie
|
|
|
|
};
|
|
|
|
promise.wait(api.post('/login', fd))
|
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-10-02 00:30:25 +02:00
|
|
|
return promise.wait(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-23 20:35:41 +02:00
|
|
|
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-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
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-09-11 12:33:44 +02:00
|
|
|
App.DI.registerSingleton('auth', ['_', 'jQuery', 'util', 'api', 'appState', 'promise'], App.Auth);
|