usr
Version:
User and groups management rest webservice
83 lines (79 loc) • 2.79 kB
text/coffeescript
Component = require '../component'
module.exports = class Access extends Component
constructor : (app)->
= app
.access = @
= app.stores.group
= {}
= app.stores.token
= app.event
= app.access
#!TODO this function can be improve with some local cache ? Array ?
_isUserMemberOfGroup: (userId, groupName)->
_ = @
return (cb)->
_.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)
)
)
#!TODO improve this function with cache
_checkToken: (token, cb)->
_ = @
.getToken(token, (err,datas)->
_.checkErr(err)
#!TODO Token read only ?
#!TODO Token time expiration
if datas.userId?
cb(null,datas.userId)
)
check : (token, groups, cb, action = "undefined") ->
_ = @
_add : (groupName, token, cb)->
_ = @
#!TODO check group existence
.addGroup(groupName, (res)->
cb(res)
_.event.emit('group:new',groupId, token)
)
#This function should be in group....
add : (groupName, token, cb)->
_ = @
.check(token, ['_group_add','root'],()->
_._add(groupName,cb)
,'group/add')
###