-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodels.coffee
More file actions
113 lines (95 loc) · 3 KB
/
models.coffee
File metadata and controls
113 lines (95 loc) · 3 KB
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
crypto = require('crypto')
mongoose = require("mongoose")
Schema = require("mongoose").Schema
bcrypt = require('bcrypt')
# Self returning
module.exports = -> module.exports
###*
* Just ensure that the given value exists
* @param {Mixed} value Value to check
* @return {Boolean} True if the value exists
###
validatePresenceOf = (value)-> value && value.length
###*
* User Schema
* @var {Object} The object describing the User Schema
###
userSchema = module.exports.userSchema = Schema(
username:
type: String
validate: [validatePresenceOf, 'An username is required.']
email:
type: String
validate: [validatePresenceOf, 'An email is required.']
index:
unique: true
hashed_password: String
salt: String
access_token: String
)
# Creates a virtual attribute that contains the id of the user
userSchema.virtual("id").get -> @_id.toHexString()
# Creates a virtual attribute that contains the email hashed (for gravatar)
userSchema.virtual("email_hash").get -> crypto.createHash('md5').update(@email).digest("hex");
# Creates a virtual attribute that contains the password of the user (before encryption)
userSchema.virtual("password").set((password) ->
@_password = password
@salt = @makeSalt()
@hashed_password = @encryptPassword(password)
).get -> @_password
# Checks that the username isn't taken yet
userSchema.pre "save", (next) ->
# Checks the username only if we change it
return next() unless @isModified 'username'
# Looks for users with the same username
User.findOne username: @username, (err, user) =>
# Username exists!
if user
@invalidate "username", "Username already taken."
err = new Error("Username already taken.")
# Callback function
next err
###*
* Authenticate checking method
* @param {String} plainText Password not hashed
* @return {Boolean} True if the passwords match
###
userSchema.method "authenticate", (plainText)-> @encryptPassword(plainText) is @hashed_password
###*
* Create a random salt for the password hash
* @return {String} Random salt
###
userSchema.method "makeSalt", (len=15)-> "" + bcrypt.genSaltSync(len)
###*
* Encrypt the given password
* @param {String} password Password to encrypt
* @return {String} Passwird encrypted
###
userSchema.method "encryptPassword", (password) -> crypto.createHmac("sha1", @salt).update(password).digest "hex"
###*
* User model
* @var {Object} The class creating from the User model
###
User = module.exports.User = mongoose.model('User', userSchema)
###*
* Screen Schema
* @var {Object} The object describing the Screen Schema
###
screenSchema = module.exports.screenSchema = Schema(
slug:
type: String
index:
unique: true
token: String
content: Schema.Types.Mixed
draft: Schema.Types.Mixed
created_at: Date
author:
type: Schema.Types.ObjectId
ref: 'User'
)
###*
* Screen model
* @var {Object} The class creating from the Screen model
###
Screen = module.exports.Screen = mongoose.model('Screen', screenSchema)