2019-05-24 17:36:06 +02:00
|
|
|
export const UserEvents = {
|
|
|
|
USER_LOADED: 'user-loaded',
|
|
|
|
}
|
|
|
|
let currentUserEventBus = new Vue();
|
|
|
|
|
2018-12-12 11:45:47 +01:00
|
|
|
class User{
|
|
|
|
constructor(kwargs) {
|
|
|
|
this.user_id = kwargs['user_id'] || '';
|
|
|
|
this.username = kwargs['username'] || '';
|
|
|
|
this.full_name = kwargs['full_name'] || '';
|
2019-05-24 17:36:06 +02:00
|
|
|
this.avatar_url = kwargs['avatar_url'] || '';
|
2018-12-12 11:45:47 +01:00
|
|
|
this.email = kwargs['email'] || '';
|
|
|
|
this.capabilities = kwargs['capabilities'] || [];
|
|
|
|
this.badges_html = kwargs['badges_html'] || '';
|
|
|
|
this.is_authenticated = kwargs['is_authenticated'] || false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* """Returns True iff the user has one or more of the given capabilities."""
|
2019-05-24 17:36:06 +02:00
|
|
|
* @param {...String} args
|
2018-12-12 11:45:47 +01:00
|
|
|
*/
|
|
|
|
hasCap(...args) {
|
|
|
|
for(let cap of args) {
|
|
|
|
if (this.capabilities.indexOf(cap) != -1) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let currentUser;
|
|
|
|
function initCurrentUser(kwargs){
|
|
|
|
currentUser = new User(kwargs);
|
2019-05-24 17:36:06 +02:00
|
|
|
currentUserEventBus.$emit(UserEvents.USER_LOADED, currentUser);
|
2018-12-12 11:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentUser() {
|
|
|
|
return currentUser;
|
|
|
|
}
|
|
|
|
|
2019-05-24 17:36:06 +02:00
|
|
|
function updateCurrentUser(user) {
|
|
|
|
currentUser = user;
|
|
|
|
currentUserEventBus.$emit(UserEvents.USER_LOADED, currentUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
export { getCurrentUser, initCurrentUser, updateCurrentUser, currentUserEventBus }
|