This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
user.js
126 lines (115 loc) · 2.9 KB
/
user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* eslint-disable id-match */ /* eslint-disable no-unused-vars */
import bcrypt from 'bcryptjs';
import { Model } from 'objection';
import BaseModel from './base';
// Related Models
import Role from './role';
import Attachment from './attachment';
import Token from './token';
import Post from './post';
import UserRole from './join/userRole';
const debug = require('debug')('boldr:user-model');
class User extends BaseModel {
static get tableName() {
return 'user';
}
static addTimestamps = true;
/**
* An array of attribute names that will be excluded from being returned.
*
* @type {array}
*/
static hidden = [];
static get relationMappings() {
return {
roles: {
relation: Model.ManyToManyRelation,
modelClass: Role,
join: {
from: 'user.id',
through: {
from: 'user_role.user_id',
to: 'user_role.role_id',
},
to: 'role.id',
},
},
posts: {
relation: Model.HasManyRelation,
modelClass: Post,
join: {
from: 'user.id',
to: 'post.user_id',
},
},
uploads: {
relation: Model.HasManyRelation,
modelClass: Attachment,
join: {
from: 'user.id',
to: 'attachment.user_id',
},
},
tokens: {
relation: Model.HasOneRelation,
modelClass: Token,
join: {
from: 'user.id',
to: 'token.user_id',
},
},
};
}
stripPassword() {
delete this['password']; // eslint-disable-line
return this;
}
/**
* Before inserting make sure we hash the password if provided.
*
* @param {object} queryContext
*/
$beforeInsert(queryContext) {
super.$beforeInsert(queryContext);
if (this.hasOwnProperty('password')) {
this.password = bcrypt.hashSync(this.password, 10);
}
if (this.firstName) this.firstName = this.firstName.trim();
if (this.lastName) this.lastName = this.lastName.trim();
this.email = this.email.trim();
}
/**
* authenticate is specific to the user instance. compares the hashed password
* with the password from the request.
* @param plainText
* @returns {*}
*/
authenticate(plainText) {
return bcrypt.compareSync(plainText, this.password);
}
/**
* Before updating make sure we hash the password if provided.
*
* @param {object} queryContext
*/
$beforeUpdate(queryContext) {
super.$beforeUpdate(queryContext);
if (this.hasOwnProperty('password')) {
this.password = bcrypt.hashSync(this.password, 10);
}
}
/**
* Checks to see if this user has the provided role or not.
*
* @param {string} role
* @returns {boolean}
*/
hasRole(role) {
if (!this.roles) {
return false;
}
const validRoles = this.roles.filter(({ name }) => name === role);
return validRoles.length;
}
}
export default User;