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.virtual( 'sanitized' ).get(function() {
138 description: this.description,
145 SchoolSchema.method( 'authorize', function( user, cb ) {
146 return cb(user.admin || ( this.users.indexOf( user._id ) !== -1 ));
149 var School = mongoose.model( 'School', SchoolSchema );
153 var CourseSchema = new Schema( {
154 name : { type : String, required : true },
156 description : String,
157 instructor : ObjectId,
160 // courses are tied to one school
163 // XXX: room for additional resources
164 created : { type : Date, default : Date.now },
168 // many users may subscribe to a course
172 CourseSchema.virtual( 'displayName' )
175 return this.number + ': ' + this.name;
181 CourseSchema.method( 'authorize', function( user, cb ) {
182 School.findById( this.school, function( err, school ) {
184 school.authorize( user, function( result ) {
191 CourseSchema.method( 'subscribed', function( user ) {
192 return ( this.users.indexOf( user ) > -1 ) ;
195 CourseSchema.method( 'subscribe', function( user, callback ) {
198 // mongoose issue #404
199 Course.collection.update( { '_id' : id }, { '$addToSet' : { 'users' : user } }, function( err ) {
204 CourseSchema.method( 'unsubscribe', function( user, callback ) {
207 // mongoose issue #404
208 Course.collection.update( { '_id' : id }, { '$pull' : { 'users' : user } }, function( err ) {
213 CourseSchema.method( 'delete', function( callback ) {
216 Course.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
217 if (callback) callback( err );
218 Lecture.find( { course: id }, function( err, lectures) {
219 if (lectures.length > 0) {
220 lectures.forEach(function(lecture) {
228 var Course = mongoose.model( 'Course', CourseSchema );
232 var LectureSchema = new Schema( {
233 name : { type : String, required : true },
234 date : { type : Date, default: Date.now },
242 LectureSchema.method( 'authorize', function( user, cb ) {
243 Course.findById( this.course, function( err, course ) {
245 course.authorize( user, function( res ) {
254 LectureSchema.method( 'delete', function( callback ) {
257 Lecture.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
258 if (callback) callback( err );
259 Note.find( { lecture : id }, function(err, notes) {
260 notes.forEach(function(note) {
264 Post.find( { lecture : id }, function(err, posts) {
265 posts.forEach(function(post) {
272 var Lecture = mongoose.model( 'Lecture', LectureSchema );
276 var NoteSchema = new Schema( {
277 name : { type : String, required : true },
282 created : { type : Date, default : Date.now },
288 collaborators : [String]
291 NoteSchema.method( 'authorize', function( user, cb ) {
292 Lecture.findById( this.lecture, function( err, lecture ) {
294 lecture.authorize( user, function( res ) {
303 NoteSchema.method( 'addVisit', function() {
306 Note.collection.update( { '_id' : id }, { '$inc' : { 'visits' : 1 } } );
309 NoteSchema.method( 'delete', function( callback ) {
312 Note.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
313 if (callback) callback( err );
317 var Note = mongoose.model( 'Note', NoteSchema );
321 var PostSchema = new Schema({
322 date : { type : Date, default : Date.now },
328 userid : String, // ObjectId,
334 lecture : String, // ObjectId
338 PostSchema.method( 'delete', function( callback ) {
341 Post.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
342 if (callback) callback( err );
346 mongoose.model( 'Post', PostSchema );
348 var ArchivedCourse = new Schema({
357 mongoose.model( 'ArchivedCourse', ArchivedCourse )
359 var ArchivedNote = new Schema({
365 mongoose.model( 'ArchivedNote', ArchivedNote )
367 var ArchivedSubject = new Schema({
372 mongoose.model( 'ArchivedSubject', ArchivedSubject )
374 module.exports.mongoose = mongoose;