5 var crypto = require( 'crypto' );
7 var mongoose = require( 'mongoose' );
9 var Schema = mongoose.Schema;
10 var ObjectId = mongoose.SchemaTypes.ObjectId;
15 return Math.round( ( new Date().valueOf() * Math.random() ) ).toString();
22 var UserSchema = new Schema( {
23 email : { type : String, require: true, index : { unique : true } },
27 created : { type : Date, default : Date.now },
30 activateCode : String,
31 resetPassCode : String,
35 showName : { 'type' : Boolean, 'default' : true },
36 admin : { 'type' : Boolean, 'default' : false }
39 UserSchema.virtual( 'displayName' )
48 UserSchema.virtual( 'password' )
49 .set( function( password ) {
51 this.hashed = this.encrypt( password );
54 UserSchema.virtual( 'isComplete' )
56 // build on this as the schema develops
58 return ( this.name && this.affil && this.hashed );
61 UserSchema.method( 'encrypt', function( password ) {
62 var hmac = crypto.createHmac( 'sha1', this.salt );
64 return hmac.update( password ).digest( 'hex' );
67 UserSchema.method( 'authenticate', function( plaintext ) {
68 return ( this.encrypt( plaintext ) === this.hashed );
71 UserSchema.method('genRandomPassword', function () {
72 // this function generates the random password, it does not keep or save it.
76 var charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
77 for (var i = 0; i < len; i++) {
78 var randomPoz = Math.floor(Math.random() * charSet.length);
79 plaintext += charSet.substring(randomPoz, randomPoz + 1);
85 UserSchema.method( 'setResetPassCode', function ( code ) {
86 this.resetPassCode = code;
87 this.resetPassDate = new Date();
88 return this.resetPassCode;
91 UserSchema.method( 'canResetPassword', function ( code ) {
92 // ensure the passCode is valid, matches and the date has not yet expired, lets say 2 weeks for good measure.
95 var expDate = new Date();
96 expDate.setDate(expDate.getDate() - 14);
98 // we have a valid code and date
99 if (this.resetPassCode != null && this.resetPassDate != null && this.resetPassDate >= expDate && this.resetPassCode == code) {
106 UserSchema.method( 'resetPassword', function ( code, newPass1, newPass2) {
107 // ensure the date has not expired, lets say 2 weeks for good measure.
110 if (this.canResetPassword(code) && newPass1 != null && newPass1.length > 0 && newPass1 == newPass2) {
111 this.password = newPass1;
112 this.resetPassCode = null;
113 this.resetPassDate = null;
120 var User = mongoose.model( 'User', UserSchema );
124 var SchoolSchema = new Schema( {
125 name : { type : String, required : true },
126 description : String,
129 created : { type : Date, default : Date.now },
135 SchoolSchema.method( 'authorize', function( user, cb ) {
136 return cb(user.admin || ( this.users.indexOf( user._id ) !== -1 ));
139 var School = mongoose.model( 'School', SchoolSchema );
143 var CourseSchema = new Schema( {
144 name : { type : String, required : true },
146 description : String,
147 instructor : ObjectId,
150 // courses are tied to one school
153 // XXX: room for additional resources
154 created : { type : Date, default : Date.now },
158 // many users may subscribe to a course
162 CourseSchema.virtual( 'displayName' )
165 return this.number + ': ' + this.name;
171 CourseSchema.method( 'authorize', function( user, cb ) {
172 School.findById( this.school, function( err, school ) {
174 school.authorize( user, function( result ) {
181 CourseSchema.method( 'subscribed', function( user ) {
182 return ( this.users.indexOf( user ) > -1 ) ;
185 CourseSchema.method( 'subscribe', function( user, callback ) {
188 // mongoose issue #404
189 Course.collection.update( { '_id' : id }, { '$addToSet' : { 'users' : user } }, function( err ) {
194 CourseSchema.method( 'unsubscribe', function( user, callback ) {
197 // mongoose issue #404
198 Course.collection.update( { '_id' : id }, { '$pull' : { 'users' : user } }, function( err ) {
203 CourseSchema.method( 'delete', function( callback ) {
206 Course.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
207 if (callback) callback( err );
208 Lecture.find( { course: id }, function( err, lectures) {
209 if (lectures.length > 0) {
210 lectures.forEach(function(lecture) {
218 var Course = mongoose.model( 'Course', CourseSchema );
222 var LectureSchema = new Schema( {
223 name : { type : String, required : true },
224 date : { type : Date, default: Date.now },
232 LectureSchema.method( 'authorize', function( user, cb ) {
233 Course.findById( this.course, function( err, course ) {
235 course.authorize( user, function( res ) {
244 LectureSchema.method( 'delete', function( callback ) {
247 Lecture.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
248 if (callback) callback( err );
249 Note.find( { lecture : id }, function(err, notes) {
250 notes.forEach(function(note) {
254 Post.find( { lecture : id }, function(err, posts) {
255 posts.forEach(function(post) {
262 var Lecture = mongoose.model( 'Lecture', LectureSchema );
266 var NoteSchema = new Schema( {
267 name : { type : String, required : true },
272 created : { type : Date, default : Date.now },
278 collaborators : [String]
281 NoteSchema.method( 'authorize', function( user, cb ) {
282 Lecture.findById( this.lecture, function( err, lecture ) {
284 lecture.authorize( user, function( res ) {
293 NoteSchema.method( 'addVisit', function() {
296 Note.collection.update( { '_id' : id }, { '$inc' : { 'visits' : 1 } } );
299 NoteSchema.method( 'delete', function( callback ) {
302 Note.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
303 if (callback) callback( err );
307 var Note = mongoose.model( 'Note', NoteSchema );
311 var PostSchema = new Schema({
312 date : { type : Date, default : Date.now },
318 userid : String, // ObjectId,
324 lecture : String, // ObjectId
328 PostSchema.method( 'delete', function( callback ) {
331 Post.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
332 if (callback) callback( err );
336 mongoose.model( 'Post', PostSchema );
338 var ArchivedCourse = new Schema({
347 mongoose.model( 'ArchivedCourse', ArchivedCourse )
349 var ArchivedNote = new Schema({
355 mongoose.model( 'ArchivedNote', ArchivedNote )
357 var ArchivedSubject = new Schema({
362 mongoose.model( 'ArchivedSubject', ArchivedSubject )
364 module.exports.mongoose = mongoose;