32 lines
1.2 KiB
TypeScript
32 lines
1.2 KiB
TypeScript
import Model from "wms-core/db/Model";
|
|
import Validator from "wms-core/db/Validator";
|
|
import User from "wms-core/auth/models/User";
|
|
|
|
export const USERNAME_REGEXP = /^[0-9a-z_-]+$/;
|
|
|
|
export default class Username extends Model {
|
|
public static async fromUser(userID: number): Promise<Username | null> {
|
|
const models = await this.models<Username>(this.select().where('user_id', userID));
|
|
return models && models.length > 0 ? models[0] : null;
|
|
}
|
|
|
|
public static async getUserFromUsername(username: string): Promise<User | null> {
|
|
const models = await this.models<Username>(this.select().where('username', username.toLowerCase()));
|
|
if (!models || models.length === 0) return null;
|
|
return await User.getById<User>(models[0].user_id!);
|
|
}
|
|
|
|
private user_id?: number;
|
|
public username?: string;
|
|
|
|
|
|
constructor(data: any) {
|
|
super(data);
|
|
}
|
|
|
|
protected defineProperties(): void {
|
|
this.defineProperty<number>('user_id', new Validator().defined().exists(User, 'id').unique(this));
|
|
this.defineProperty<number>('username', new Validator().defined().between(3, 64).regexp(USERNAME_REGEXP).unique(this));
|
|
}
|
|
|
|
} |