aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/models/Access.js
blob: ee62911aed276b483057e75712618f93f8c5a794 (plain) (blame)
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

/**
 * Module dependencies
 */

var mongoose = require('mongoose')
  , Schema = mongoose.Schema;

var AccessSchema = new Schema({
    user: { type: Schema.ObjectId, ref: 'User' },
    creator: { type: Schema.ObjectId, ref: 'User' },
    project: { type: Schema.ObjectId, ref: 'Project' },
    permissions: { type: Number, default: '3' },
    created: { type: Date, default: Date.now },
    updated: { type: Date, default: Date.now }
});

// the four validations below only apply if you are signing up traditionally

AccessSchema.statics = {

   /**
    * Load ALL accesses for a single user
    *
    * @param {ObjectId} id
    * @param {Function} callback
    * @api private
    */

    loadUser: function(id, callback) {
        this.find({ user: id })
          .populate('project')
          .sort({ 'created': -1 }) // sort by date
          .exec(callback);
    },


   /**
    * Check to see if user has access to a particular project
    *
    * @param {ObjectId} user
    * @param {ObjectId} project
    * @return {Boolean}
    * @api public
    */

    checkAccess: function(user, project) {
        console.log('inni checkAccess!')
        this.findOne({ user: user }).where('project').equals(project).exec(function(err, access) {
            if (err || !access) return false;
            else return true;
        });
    }
}

mongoose.model('Access', AccessSchema);