@webda/core
Version:
Expose API with Lambda
157 lines • 3.5 kB
JavaScript
import { CoreModel } from "./coremodel.js";
/**
* First basic model for User
* @class
* @WebdaModel
*/
export class User extends CoreModel {
constructor() {
super(...arguments);
/**
* Last time the password was recovered
*/
this._lastPasswordRecovery = 0;
}
/**
* Return user email if known or guessable
* @returns
*/
getEmail() {
this.email ?? (this.email = this.getIdents().find(i => {
return i.email;
})?.email);
return this.email;
}
/**
* Return displayable public entry
* @returns
*/
toPublicEntry() {
const res = {
displayName: this.displayName,
uuid: this.getUuid(),
avatar: this._avatar,
email: this.getEmail()
};
return res;
}
getGroups() {
return [];
}
getRoles() {
return [];
}
getDisplayName() {
return this.displayName;
}
getIdents() {
return [];
}
lastPasswordRecoveryBefore(timestamp) {
return this._lastPasswordRecovery < timestamp;
}
getPassword() {
return this.__password;
}
setPassword(password) {
this.__password = password;
}
async canAct(ctx, _action) {
if (!ctx.getCurrentUserId() || ctx.getCurrentUserId() !== this.getUuid()) {
return "You can't act on this user";
}
return true;
}
/**
* Add a login/logout event
* @returns
*/
static getClientEvents() {
return [...CoreModel.getClientEvents(), "login", "logout"];
}
/**
* Only current user can see its own events
* @param _event
* @param context
* @param model
* @returns
*/
static authorizeClientEvent(_event, context, model) {
if (model && model.getUuid() === context.getCurrentUserId()) {
return true;
}
return false;
}
}
/**
* Simple user offers groups and roles management
*
* Groups and roles are defined just as string, no
* models
*/
export class SimpleUser extends User {
constructor() {
super(...arguments);
/**
* Groups for a user
*/
this._groups = [];
/**
* Roles of the user
*/
this._roles = [];
/**
* Normal ident
*/
this._idents = [];
}
/**
* Return idents
* @returns
*/
getIdents() {
return this._idents;
}
addGroup(group) {
if (this.inGroup(group)) {
return;
}
this._groups.push(group);
}
inGroup(group) {
if (group === "all" || group === this.getUuid()) {
return true;
}
return this._groups.includes(group);
}
removeGroup(group) {
let ind = this._groups.indexOf(group);
if (ind < 0) {
return;
}
this._groups.splice(ind, 1);
}
getGroups() {
return this._groups;
}
getRoles() {
return this._roles;
}
addRole(role) {
if (this.hasRole(role)) {
return;
}
this._roles.push(role);
}
hasRole(role) {
return this._roles.indexOf(role) >= 0;
}
removeRole(role) {
let ind = this._roles.indexOf(role);
if (ind < 0) {
return;
}
this._roles.splice(ind, 1);
}
}
//# sourceMappingURL=user.js.map