2012-10-27 76 views
3

我需要維護一個集合'用戶'[用戶]文檔,用於實現我的網站的身份驗證機制。文檔使用Node.js/MongooseJs的MongoDb集合中的多態性 -

[用戶]〜{顏色:字符串,用戶名:串,密碼哈希:串,密碼鹽:串}

此外,每個[用戶]可以是3種顏色之一 - [RedUser],[BlueUser][GreenUser]並且將根據顏色具有不同的架構。所有從[用戶]架構延伸:

[RedUser] ~ Union([User], {redfield:string}) 
[GreenUser] ~ Union([User], {greenfield:boolean}) 
[BlueUser] ~ Union([User], {bluefield:number}) 

例如一個綠色的用戶 - {顏色:'綠色',用戶名:'鮑勃',密碼哈希:'1313a ...',密碼鹽:'...'}

對於身份驗證我想查詢'用戶使用一般的[用戶]模式收集,因爲我需要知道的只有密碼哈希。

不過,我想一個函數如下(僞代碼):

Authenticate(username, password) 
    userDoc <- users.findOne({username:username}) 
    if badPassword(usersDoc.password-hash,password) throw error 
    else 
     if userDoc.color=='red' 
      return recast(userDoc, RedUser) 
     if userDoc.color=='green' 
      return recast(userDoc, BlueUser) 
     if userDoc.color=='blue' 
      return recast(userDoc, GreenUser) 

所以,我怎麼能做到從[用戶]文檔上鑄造到[RedUser]例如?

我如何使用Node.js/MongooseJs/CoffeeScript的

的感謝!

回答

0

很簡單真的,我們只是做從https://github.com/briankircho/mongoose-schema-extend

### 
    Dependencies. 
### 

mongoose = require('mongoose') 
extend = require('mongoose-schema-extend') 

### 
    Main 
### 

class Demo 
    constructor: (dbUrl)-> 
     @db = mongoose.createConnection(dbUrl) 

     userSchema = new mongoose.Schema 
      username     : { type: String, default: "" }     
      password_hash    : { type: String, default: "" }  
      , 
      {collection : 'users', discriminatorKey : 'color'} 

     userSchema.methods.verifyPassword = (pass)-> 
      console.log("is it a good password? " + (this.password_hash==pass)) 

     redSchema = userSchema.extend 
      redfield    : { type: String, default: "red" }   
     redSchema.methods.printColor = -> 
      console.log("RED!") 

     blueSchema = userSchema.extend 
      bluefield   : { type: String, default: "blue" }    
     blueSchema.methods.printColor = -> 
      console.log("BLUE!")   

     greenSchema = userSchema.extend 
      greenfield   : { type: String, default: "green" }     
     greenSchema.methods.printColor = -> 
      console.log("GREEN!")    

     # create the mongoose db models 
     @UserModel = @db.model('User', userSchema) 
     @RedModel = @db.model('Red', redSchema) 
     @BlueModel = @db.model('Blue', blueSchema)  
     @GreenModel = @db.model('Green', greenSchema) 

    populate: -> 
     user1 = new @RedModel({username:'user1',password_hash:'pass1',redfield:'blabla'}) 
     user1.save() 
     user2 = new @GreenModel({username:'user2',password_hash:'pass2',greenfield:'blabla2'}) 
     user2.save()  
     user3 = new @RedModel({username:'user3',password_hash:'pass3',redfield:'blabla3'}) 
     user3.save()   
     user4 = new @GreenModel({username:'user4',password_hash:'pass1',greenfield:'dflabla'}) 
     user4.save() 
     user5 = new @BlueModel({username:'user5',password_hash:'pass2',bluefield:'blabla2'}) 
     user5.save()  
     user6 = new @BlueModel({username:'user6',password_hash:'pass3',bluefield:'blabla3'}) 
     user6.save()     

    auth:(username,callback)-> 
     @UserModel.findOne({ username: username }, callback) 

### 
    Start the demo 
### 

demo = new Demo('mongodb://localhost/testpolymorph') 
demo.populate() 
demo.auth 'user5', (err,userDoc)-> 
    userDoc.verifyPassword("pass") 
    userDoc.printColor() 
demo.auth 'user3', (err,userDoc)-> 
    userDoc.verifyPassword("pass3") 
    userDoc.printColor() 
demo.auth 'user4', (err,userDoc)-> 
    userDoc.verifyPassword("pass3") 
    userDoc.printColor()  
使用微小但有用 mongoose-schema-extend
相關問題