Skip to content
Snippets Groups Projects
Select Git revision
  • 81a65c5227009c556fb4bf1c95f235269bd64577
  • main default protected
  • jw_sonar
  • v6.0.0 protected
  • bedran_exercise-list
  • ask-user-to-delete-exercises-on-duplicates
  • update-dependencies
  • jw_sonar_backup
  • add_route_assignments
  • 6.0.0-dev
  • 5.0.1
  • 5.0.0
  • 4.1.0
  • 4.0.0
  • 3.5.3
  • 3.5.2
  • 3.5.1
  • 3.5.0
  • 3.4.2
  • 3.4.1
  • 3.4.0
  • 3.3.0
  • 3.2.0
  • 3.1.3
  • 3.1.2
  • 3.1.1
  • 3.1.0
  • 3.0.1
  • 3.0.0
29 results

User.ts

Blame
  • User.ts 3.00 KiB
    import Session     from '../controllers/Session';
    import Config      from '../config/Config';
    import Toolbox     from '../shared/Toolbox';
    import * as bcrypt from 'bcryptjs';
    import Model       from './Model';
    import db          from '../helpers/DatabaseHelper';
    
    
    class User extends Model {
        static tableName: string = 'Users';
    
        userID: number = null;
        userFirstName: string = '';
        userLastName: string = '';
        userMail: string = null;
        userRole: string = null;
        userDeleted: boolean = null;
    
        userPassword: string = null;
    
        unencryptedPassword: string = null; // This value is not set from the db. It's a value that is not null only if we have called createPassword function
    
        currentSession: Session = null;
    
        public async toJsonObject(lightVersion: boolean): Promise<Object> {
            const result = {
                'id'             : this.userID,
                'firstName'      : this.userFirstName,
                'lastName'       : this.userLastName,
                'mail'           : this.userMail,
                'role'           : this.userRole,
                'isTeachingStaff': this.isTeachingStaff,
                'deleted'        : this.userDeleted
            };
    
            return result;
        };
    
        get fullName(): string {
            return this.userLastName.toUpperCase() + ' ' + this.userFirstName;
        }
    
        get isTeachingStaff(): boolean {
            return Config.permissions.teachingStaff.includes(this.userRole);
        }
    
        public importFromJsonObject(jsonObject: any) {
            this.userID = jsonObject.id;
            this.userFirstName = jsonObject.firstName;
            this.userLastName = jsonObject.lastName;
            this.userMail = jsonObject.mail;
            this.userRole = jsonObject.role;
            this.userDeleted = jsonObject.deleted;
        }
    
        public generateHashedPassword() {
            this.userPassword = bcrypt.hashSync(this.unencryptedPassword, Config.userPasswordSaltRounds);
        }
    
        public replacePassword(password: string) {
            this.unencryptedPassword = password;
            this.generateHashedPassword();
        }
    
        public createPassword() {
            this.unencryptedPassword = Toolbox.randomString(Config.userPasswordLength);
            this.generateHashedPassword();
        }
    
        public toDb(): any {
            return {
                userFirstName: Toolbox.capitalizeName(this.userFirstName),
                userLastName : Toolbox.capitalizeName(this.userLastName),
                userRole     : this.userRole,
                userMail     : this.userMail,
                userPassword : this.userPassword
            };
        }
    
        create(): Promise<void> {
            return db(User.tableName).insert(this.toDb());
        }
    
        update(): Promise<void> {
            return db(User.tableName).where('userID', this.userID).update(this.toDb());
        }
    
        updatePassword(): Promise<void> {
            return db(User.tableName).where('userID', this.userID).update({ 'userPassword': this.userPassword });
        }
    
        del(): Promise<void> {
            return db(User.tableName).where('userID', this.userID).update({ 'userDeleted': true });
        }
    }
    
    
    export default User;