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( 'sanitized' ).get(function() {
44 showName: this.showName,
51 UserSchema.virtual( 'displayName' )
60 UserSchema.virtual( 'password' )
61 .set( function( password ) {
63 this.hashed = this.encrypt( password );
66 UserSchema.virtual( 'isComplete' )
68 // build on this as the schema develops
70 return ( this.name && this.affil && this.hashed );
73 UserSchema.method( 'encrypt', function( password ) {
74 var hmac = crypto.createHmac( 'sha1', this.salt );
76 return hmac.update( password ).digest( 'hex' );
79 UserSchema.method( 'authenticate', function( plaintext ) {
80 return ( this.encrypt( plaintext ) === this.hashed );
83 UserSchema.method('genRandomPassword', function () {
84 // this function generates the random password, it does not keep or save it.
88 var charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
89 for (var i = 0; i < len; i++) {
90 var randomPoz = Math.floor(Math.random() * charSet.length);
91 plaintext += charSet.substring(randomPoz, randomPoz + 1);
97 UserSchema.method( 'setResetPassCode', function ( code ) {
98 this.resetPassCode = code;
99 this.resetPassDate = new Date();
100 return this.resetPassCode;
103 UserSchema.method( 'canResetPassword', function ( code ) {
104 // ensure the passCode is valid, matches and the date has not yet expired, lets say 2 weeks for good measure.
107 var expDate = new Date();
108 expDate.setDate(expDate.getDate() - 14);
110 // we have a valid code and date
111 if (this.resetPassCode != null && this.resetPassDate != null && this.resetPassDate >= expDate && this.resetPassCode == code) {
118 UserSchema.method( 'resetPassword', function ( code, newPass1, newPass2) {
119 // ensure the date has not expired, lets say 2 weeks for good measure.
122 if (this.canResetPassword(code) && newPass1 != null && newPass1.length > 0 && newPass1 == newPass2) {
123 this.password = newPass1;
124 this.resetPassCode = null;
125 this.resetPassDate = null;
132 var User = mongoose.model( 'User', UserSchema );
136 var SchoolSchema = new Schema( {
137 name : { type : String, required : true },
138 description : String,
141 created : { type : Date, default : Date.now },
147 SchoolSchema.virtual( 'sanitized' ).get(function() {
151 description: this.description,
158 SchoolSchema.method( 'authorize', function( user, cb ) {
159 return cb(user.admin || ( this.users.indexOf( user._id ) !== -1 ));
162 var School = mongoose.model( 'School', SchoolSchema );
166 var CourseSchema = new Schema( {
167 name : { type : String, required : true },
169 description : String,
170 instructor : ObjectId,
173 // courses are tied to one school
176 // XXX: room for additional resources
177 created : { type : Date, default : Date.now },
181 // many users may subscribe to a course
185 CourseSchema.virtual( 'sanitized' ).get(function() {
190 description: this.description,
191 subject: this.subject,
192 department: this.department
198 CourseSchema.virtual( 'displayName' )
201 return this.number + ': ' + this.name;
207 CourseSchema.method( 'authorize', function( user, cb ) {
208 School.findById( this.school, function( err, school ) {
210 school.authorize( user, function( result ) {
217 CourseSchema.method( 'subscribed', function( user ) {
218 return ( this.users.indexOf( user ) > -1 ) ;
221 CourseSchema.method( 'subscribe', function( user, callback ) {
224 // mongoose issue #404
225 Course.collection.update( { '_id' : id }, { '$addToSet' : { 'users' : user } }, function( err ) {
230 CourseSchema.method( 'unsubscribe', function( user, callback ) {
233 // mongoose issue #404
234 Course.collection.update( { '_id' : id }, { '$pull' : { 'users' : user } }, function( err ) {
239 CourseSchema.method( 'delete', function( callback ) {
242 Course.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
243 if (callback) callback( err );
244 Lecture.find( { course: id }, function( err, lectures) {
245 if (lectures.length > 0) {
246 lectures.forEach(function(lecture) {
254 var Course = mongoose.model( 'Course', CourseSchema );
258 var LectureSchema = new Schema( {
259 name : { type : String, required : true },
260 date : { type : Date, default: Date.now },
268 LectureSchema.virtual( 'sanitized' ).get(function() {
279 LectureSchema.method( 'authorize', function( user, cb ) {
280 Course.findById( this.course, function( err, course ) {
282 course.authorize( user, function( res ) {
291 LectureSchema.method( 'delete', function( callback ) {
294 Lecture.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
295 if (callback) callback( err );
296 Note.find( { lecture : id }, function(err, notes) {
297 notes.forEach(function(note) {
301 Post.find( { lecture : id }, function(err, posts) {
302 posts.forEach(function(post) {
309 var Lecture = mongoose.model( 'Lecture', LectureSchema );
313 var NoteSchema = new Schema( {
314 name : { type : String, required : true },
319 created : { type : Date, default : Date.now },
325 collaborators : [String]
328 NoteSchema.virtual( 'sanitized').get(function() {
341 NoteSchema.method( 'authorize', function( user, cb ) {
342 Lecture.findById( this.lecture, function( err, lecture ) {
344 lecture.authorize( user, function( res ) {
353 NoteSchema.method( 'addVisit', function() {
356 Note.collection.update( { '_id' : id }, { '$inc' : { 'visits' : 1 } } );
359 NoteSchema.method( 'delete', function( callback ) {
362 Note.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
363 if (callback) callback( err );
367 var Note = mongoose.model( 'Note', NoteSchema );
371 var PostSchema = new Schema({
372 date : { type : Date, default : Date.now },
378 userid : String, // ObjectId,
384 lecture : String, // ObjectId
388 PostSchema.method( 'delete', function( callback ) {
391 Post.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
392 if (callback) callback( err );
396 mongoose.model( 'Post', PostSchema );
398 var ArchivedCourse = new Schema({
407 mongoose.model( 'ArchivedCourse', ArchivedCourse )
409 var ArchivedNote = new Schema({
415 ArchivedNote.virtual( 'sanitized' ).get(function() {
423 mongoose.model( 'ArchivedNote', ArchivedNote )
425 var ArchivedSubject = new Schema({
430 mongoose.model( 'ArchivedSubject', ArchivedSubject )
432 module.exports.mongoose = mongoose;