-
Notifications
You must be signed in to change notification settings - Fork 13
/
keys.js
66 lines (59 loc) · 1.64 KB
/
keys.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
Roles.keys = {};
/**
* Initialize the collection
*/
Roles.keys.collection = new Meteor.Collection('nicolaslopezj_roles_keys');
/**
* Set the permissions
* Users can request keys just for them
*/
Roles.keys.collection.allow({
insert: function(userId, doc) {
return userId === doc.userId;
},
remove: function(userId, doc) {
return userId === doc.userId;
}
});
/**
* Requests a new key
* @param {String} userId Id of the userId
* @param {Date} expiresAt Date of expiration
* @return {String} Id of the key
*/
Roles.keys.request = function(userId, expiresAt) {
check(userId, String);
var doc = {
userId: userId,
createdAt: new Date()
};
if (expiresAt) {
check(expiresAt, Date);
doc.expiresAt = expiresAt;
}
return this.collection.insert(doc);
};
/**
* Returns the userId of the specified key and deletes the key from the database
* @param {String} key
* @param {Boolean} dontDelete True to leave the key in the database
* @return {String} Id of the user
*/
Roles.keys.getUserId = function(key, dontDelete) {
check(key, String);
check(dontDelete, Match.Optional(Boolean));
var doc = this.collection.findOne({ _id: key, $or: [{ expiresAt: { $exists: false } }, { expiresAt: { $gte: new Date() } }] });
if (!doc) return;
if (!dontDelete) {
if (!doc.expiresAt) {
console.log('borrando por no tener expire at');
this.collection.remove({ _id: key });
} else {
if (moment(doc.expiresAt).isBefore(moment())) {
console.log('borrando por expire at ya pasó');
this.collection.remove({ _id: key });
}
}
}
return doc.userId;
};