ACLs functions moved to Xo.

This commit is contained in:
Julien Fontanet 2015-05-13 12:00:01 +02:00
parent 7412d97bf3
commit 4dd3be1568
2 changed files with 54 additions and 46 deletions

View File

@ -3,36 +3,19 @@ import {ModelAlreadyExists} from '../collection'
// ===================================================================
export const get = coroutine(function * ({subject, object}) {
const sieve = {}
try {
if (subject !== undefined) {
sieve.subject = (yield this.users.first(subject)).get('id')
}
if (object !== undefined) {
sieve.object = this.getObject(object).id
}
} catch (error) {
this.throw('NO_SUCH_OBJECT')
}
return this.acls.get(sieve)
})
export async function get () {
return await this.getAllAcls()
}
get.permission = 'admin'
get.params = {
subject: { type: 'string', optional: true },
object: { type: 'string', optional: true }
}
get.description = 'get existing ACLs'
// -------------------------------------------------------------------
export const getCurrent = coroutine(function * () {
return this.acls.get({ subject: this.session.get('user_id') })
})
export async function getCurrent () {
return await this.getAclsForSubject(this.session.get('user_id'))
}
getCurrent.permission = ''
@ -40,22 +23,9 @@ getCurrent.description = 'get existing ACLs concerning current user'
// -------------------------------------------------------------------
export const add = coroutine(function * ({subject, object}) {
try {
subject = (yield this.users.first(subject)).get('id')
object = this.getObject(object).id
} catch (error) {
this.throw('NO_SUCH_OBJECT')
}
try {
yield this.acls.create(subject, object)
} catch (error) {
if (!(error instanceof ModelAlreadyExists)) {
throw error
}
}
})
export async function add ({subject, object}) {
await this.addAcl(subject, object)
}
add.permission = 'admin'
@ -68,9 +38,9 @@ add.description = 'add a new ACL entry'
// -------------------------------------------------------------------
export const remove = coroutine(function * ({subject, object}) {
yield this.acls.delete(subject, object)
})
export async function remove ({subject, object}) {
await this.removeAcl(subject, object)
}
remove.permission = 'admin'

View File

@ -74,11 +74,11 @@ export default class Xo extends EventEmitter {
// These will be initialized in start()
//
// TODO: remove and put everything in the `_objects` collection.
this._acls = null
this._servers = null
this._tokens = null
this._users = null
this._UUIDsToKeys = null
this.acls = null
// Connections to Xen servers.
this._xapis = Object.create(null)
@ -130,7 +130,7 @@ export default class Xo extends EventEmitter {
const redis = createRedisClient(config.redis && config.redis.uri)
// Creates persistent collections.
this.acls = new Acls({
this._acls = new Acls({
connection: redis,
prefix: 'xo:acl',
indexes: ['subject', 'object']
@ -184,6 +184,34 @@ export default class Xo extends EventEmitter {
// -----------------------------------------------------------------
async addAcl (subject, object) {
subject = (await this.getUser(subject)).id
object = this.getObject(object).id
try {
await this._acls.create(subject, object)
} catch (error) {
if (!(error instanceof ModelAlreadyExists)) {
throw error
}
}
}
async removeAcl (subject, object) {
await this._acls.delete(subject, object)
}
async getAclsForSubject (subject) {
return this._acls.get({ subject })
}
// TODO: remove when new collection.
async getAllAcls () {
return this._acls.get()
}
// -----------------------------------------------------------------
async createUser ({email, password, permission}) {
// TODO: use plain objects
const user = await this._users.create(email, password, permission)
@ -207,8 +235,7 @@ export default class Xo extends EventEmitter {
await this._users.update(user)
}
// TODO: this method will no longer be async when users are
// integrated to the main collection.
// Merge this method in getUser() when plain objects.
async _getUser (id) {
const user = await this._users.first(id)
if (!user) {
@ -218,6 +245,12 @@ export default class Xo extends EventEmitter {
return user
}
// TODO: this method will no longer be async when users are
// integrated to the main collection.
async getUser (id) {
return (await this._getUser(id)).properties
}
// -----------------------------------------------------------------
async createAuthenticationToken ({userId}) {
@ -370,6 +403,8 @@ export default class Xo extends EventEmitter {
// -----------------------------------------------------------------
// Returns an object from its key or UUID.
//
// TODO: should throw a NoSuchObject error on failure.
getObject (key, type) {
// Gracefully handles UUIDs.
if (key in this._UUIDsToKeys) {
@ -539,6 +574,9 @@ export default class Xo extends EventEmitter {
// -----------------------------------------------------------------
// TODO: should be removed when no longer used.
//
// Replaced internally by Xapi.
watchTask (ref) {
let watcher = this._taskWatchers[ref]
if (!watcher) {