Skip to content

Commit

Permalink
All Test working well with callback
Browse files Browse the repository at this point in the history
  • Loading branch information
ombr committed Aug 2, 2012
1 parent ff620f1 commit 996a688
Show file tree
Hide file tree
Showing 16 changed files with 290 additions and 311 deletions.
69 changes: 32 additions & 37 deletions lib/access/access.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,57 @@ module.exports = class Access extends Component
_isUserMemberOfGroup: (userId, groupName)->
_ = @
return (cb)->
try
_.app.stores.group.findGroupByName(groupName, (group)->
_.app.stores.group.isUserMemberOfGroupCache(userId, group.id,(res)->
_.app.stores.group.findGroupByName(groupName, (err,group)->
if err?
if err[0] == 'Not found'
cb(null,false)
return
else
cb(err,null)
return
_.app.stores.group.isUserMemberOfGroupCache(userId, group.id,(err,res)->
cb(null, res)
)
)
catch e
if e == 'Not found'
cb(null, false)
return
throw e

#!TODO improve this function with cache
_checkToken: (token, callbackOK)->
try
@token.getToken(token, (datas)->
#!TODO Token read only ?
#!TODO Token time expiration
if datas.userId?
console.log "TOKEN OK"
callbackOK(datas.userId)
)
catch e
throw e
_ = @
@token.getToken(token, (err,datas)->
_.checkErr(err)
#!TODO Token read only ?
#!TODO Token time expiration
if datas.userId?
callbackOK(null,datas.userId)
)

check : (token, groups, cb, action = "undefined") ->
_ = @
@_checkToken(token,
(userId)->
(err,userId)->
_.checkErr(err)
groupCheck = []
for g in groups
groupCheck.push(_._isUserMemberOfGroup(userId, g))
try
require('async').parallel(groupCheck,(err,res)->
if true in res
cb(userId)
_.emit('access/'+action,
granted : true
token : token
userId : userId
groups : groups
)
return
require('async').parallel(groupCheck,(err,res)->
_.checkErr(err)
if true in res
cb(null,userId)
_.emit('access/'+action,
granted : false
granted : true
token : token
userId : userId
groups : groups
)
throw 'Access denied'
return
_.emit('access/'+action,
granted : false
token : token
userId : userId
groups : groups
)
catch e
console.log "ERROR IN CHECK ?#{e}?"
console.log e
throw e
cb(['Access denied'],null)
return
)
)
_add : (groupName, token, cb)->
_ = @
Expand Down
42 changes: 18 additions & 24 deletions lib/app.coffee
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@

module.exports = class App
constructor : (app,@configs)->
constructor : (express,@configs)->
_ = @
Log = require 'log'
@log = new Log()
EventEmitter = require('eventemitter2').EventEmitter2
@_event = new EventEmitter(
wildcard:true
)
@_event.on('*',(infos)->
console.log this.event
console.log infos
_.log.debug "EVENT #{@event} : #{JSON.stringify(infos)}"
)

#!TODO move this function to access...
@_event.once('token/new',(datas)->
console.log "Check INIT ROOT ?"
try
_.stores.group.findGroupByName('root',()->)
catch e
if e == 'Not found'
try
_.stores.group.addGroup('_root',(groupId)->
_.stores.group.addUserToGroup(datas.userId,groupId,(res)->
_.stores.group.findGroupByName('root',(err,group)->
if group == null
console.log "ROOT NULL ?"
_.stores.group.addGroup('_root',(err,groupId)->
_.stores.group.addUserToGroup(datas.userId,groupId,(err,res)->
if !res
throw "Error root access granted..."
_.stores.group.addUserToGroupCache(datas.userId,groupId,(err,res)->
if !res
throw "Error root access granted..."
_.stores.group.addUserToGroupCache(datas.userId,groupId,(res)->
if !res
throw "Error root access granted..."
_.emit('root/new',datas)
)
_.emit('root/new',datas)
)
)
catch e
console.log "ROOT INIT ERROR"
console.log e
else
throw e

)
)
)
#@log = @app.log
@app = app
@app[ "auth" or @configs.bind] = @ #App binding
@express = express
@express[ "auth" or @configs.bind] = @ #express binding

#Init stores :
@stores = {}
Expand Down Expand Up @@ -82,7 +77,6 @@ module.exports = class App
# Events
###
emit : (args...)->
console.log "EMIT ?"
@event.emit(args...)
on : (args...)->
@event.on(args...)
137 changes: 66 additions & 71 deletions lib/auth/auth.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,58 @@ module.exports = class Auth extends Component
@._everyAuth()
@._routes()

addUser : (source='', id='', datas={},cb)->
_ = @
store = @app.stores.user
store.addUser(source,id,datas,(err,userId)->
_.checkErr(err)
cb(null,userId)
_.emit('user/new',
userId:userId
)
)


login : (source, id, datas, user, cb)->
_ = @
store = @app.stores.user
#Check on user : Does the user is already logged in ?
if user != null
# Is it a new way of connection ?
#@log.debug "User Is already in, let's add a way of login !"
else
store.findUserBySourceAndId(source,id,(err,user)->
if err?
if err[0] == 'Not found'
store.addUser(source,id,datas,(err,userId)->
#find a better way ?
store.findUserById(userId,(err,user)->
cb(null,user)
_.app.event.emit('user/login',
userId:user.id
)
return
)
)
else
cb(err,null)
return
else
if source == 'local'
#!TODO password hash with a good method....
if datas.password != user[source].password
cb(['Wrong Password'],null)
return
_.app.event.emit('user/login',
userId:user.id
)
cb(null,user)
return
)

_routes : ()->
_ = @
@app.get('/login/*', (req, res)->
@routeGet('/login/*', (req, res)->
if req.params? and req.params[0]? and req.params[0] != ''
req.session.url = req.params[0]
if req.user?
Expand All @@ -16,13 +65,11 @@ module.exports = class Auth extends Component
res.redirect('/auth/local')#!TODO RENDER LOGIN PAGE
)

@app.get('/redirect', (req, res)->
@routeGet('/redirect', (req, res)->
if not req.loggedIn
res.json({})
return
res.redirect('/login/')
return
tokenCallback = (token)->
tokenCallback = (err,token)->
req.session.token = token
if req.session.url?
#_.app.log.debug "Redirect "+url
Expand All @@ -37,68 +84,17 @@ module.exports = class Auth extends Component
)
return
if req.session.token?
tokenCallback(req.session.token)
tokenCallback(null,req.session.token)
else
_.app.token.add(req.user.id, {}, tokenCallback)
)
addUser : (source='', id='', datas={},cb)->
_ = @
store = @app.stores.user
store.addUser(source,id,datas,(userId)->
cb(userId)
_.emit('user/new',
userId:userId
)
)


login : (source, id, datas, user, cb)->
_ = @
store = @app.stores.user
#Check on user : Does the user is already logged in ?
if user != null
# Is it a new way of connection ?
#@log.debug "User Is already in, let's add a way of login !"
else
try
store.findUserBySourceAndId(source,id,(user)->
if source == 'local'
#!TODO password hash with a good method....
if datas.password != user[source].password
throw 'Wrong Password'
return
_.app.event.emit('user/login',
userId:user.id
)
cb(user)
)
catch e
if e == 'Not found'
store.addUser(source,id,datas,(userId)->
#find a better way ?
store.findUserById(userId,(user)->
cb(user)
_.app.event.emit('user/login',
userId:user.id
)
return
)
)
else
throw e

_everyAuth : ()->
_ = @
store = @app.stores.user
@everyAuth = require 'everyauth'
#@everyAuth.debug = true
@everyAuth.everymodule.findUserById((id, cb)->
try
store.findUserById(id,(user)->
cb(null,user)
)
catch e
cb([e],null)
store.findUserById(id,cb)
)
#PASSWORD :
@everyAuth
Expand All @@ -109,17 +105,16 @@ module.exports = class Auth extends Component
.loginView('login')
.authenticate((login, password)->
promise = this.Promise()
try
_.login('local',login,
login:login
password:password
,null#!TODO put here user from session
,(user)->
promise.fulfill(user)
)
catch e
promise.fulfill([e])

_.login('local',login,
login:login
password:password
,null#!TODO put here user from session
,(err,user)->
if err != null
promise.fulfill(err)
return
promise.fulfill(user)
)
return promise
)
.getRegisterPath('/register')
Expand All @@ -136,8 +131,8 @@ module.exports = class Auth extends Component
for providerName, providerConfigs of _.app.configs.everyAuth
@._everyAuth_Providers(providerName, providerConfigs)
#Register EveryAuth
@app.app.use(@everyAuth.middleware())
@everyAuth.helpExpress(@app.app)
@express().use(@everyAuth.middleware())
@everyAuth.helpExpress(@express())

_everyAuth_Providers:(providerName, providerConfigs)->
_ = @
Expand Down
26 changes: 18 additions & 8 deletions lib/component.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@ module.exports = class Component
res.json(
error:true
)
#!TODO Make a better check error
checkErr : (err)->
if err != null
throw err

###
# Express route encapsulation
###
route_get : (args...)->
@app.app.get(args...)
route_post : (args...)->
@app.app.post(args...)
route_update : (args...)->
@app.app.update(args...)
route_delete : (args...)->
@app.app.delete(args...)
routeGet : (args...)->
@express().get(args...)
routePost : (args...)->
@express().post(args...)
routeUpdate : (args...)->
@express().update(args...)
routeDelete : (args...)->
@express().delete(args...)

###
# Events
Expand All @@ -36,3 +41,8 @@ module.exports = class Component
@app._event.emit(args...)
on : (args...)->
@app._event.on(args...)

log : ()->
return @app.log
express : ()->
return @app.express
Loading

0 comments on commit 996a688

Please sign in to comment.