Skip to content

Commit

Permalink
support for adonis 4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
enniel committed Nov 9, 2017
1 parent 902dfa7 commit d655b41
Show file tree
Hide file tree
Showing 31 changed files with 692 additions and 471 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ package-lock.json
# dotenv environment variables file
.env

# sqlite storage
test/unit/storage
# tmp dirs
test/tmp
test/database


# End of https://www.gitignore.io/api/node
6 changes: 3 additions & 3 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ yarn.lock
package-lock.json
.editorconfig
.env
test/unit/storage
.travis.yaml
.coveralls.yml
test/tmp
test/database
coverage
.nyc_output
18 changes: 0 additions & 18 deletions bin/coverage.js

This file was deleted.

37 changes: 37 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

/**
* adonis-acl
* Copyright(c) 2017 Evgeny Razumov
* MIT Licensed
*/

const semver = require('semver')
const { spawn } = require('child_process')
const spawnArgs = []

if (semver.lt(process.version, '8.0.0')) {
spawnArgs.push('--harmony-async-await')
}

function local () {
spawnArgs.push('./node_modules/.bin/japa')
const tests = spawn('node', spawnArgs)
tests.stdout.on('data', (data) => process.stdout.write(data))
tests.stderr.on('data', (data) => process.stderr.write(data))
tests.on('close', (code) => process.exit(code))
}

function win () {
spawnArgs.push('./node_modules/japa-cli/index.js')
const tests = spawn('node', spawnArgs)
tests.stdout.on('data', (data) => process.stdout.write(data))
tests.stderr.on('data', (data) => process.stderr.write(data))
tests.on('close', (code) => process.exit(code))
}

if (process.argv.indexOf('--local') > -1) {
local()
} else if (process.argv.indexOf('--win') > -1) {
win()
}
18 changes: 0 additions & 18 deletions bin/test.js

This file was deleted.

59 changes: 37 additions & 22 deletions commands/Permission.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,63 @@
'use strict'

const Ioc = require('adonis-fold').Ioc
const Command = Ioc.use('Adonis/Src/Command')
const Permission = Ioc.use('Adonis/Acl/Permission')
const Database = Ioc.use('Adonis/Src/Database')
/**
* adonis-acl
* Copyright(c) 2017 Evgeny Razumov
* MIT Licensed
*/

const { Command } = require('@adonisjs/ace')
const Permission = use('Adonis/Acl/Permission')
const Database = use('Adonis/Src/Database')

class PermissionCommand extends Command {
/**
* signature defines the requirements and name
* of command.
* The command signature getter to define the
* command name, arguments and options.
*
* @attribute signature
* @static
*
* @return {String}
*/
get signature () {
static get signature () {
return 'acl:permission {slug} {name?} {description?}'
}

/**
* description is the little helpful information displayed
* on the console.
* The command description getter.
*
* @attribute description
* @static
*
* @return {String}
*/
get description () {
static get description () {
return 'Create or update permission'
}

/**
* handle method is invoked automatically by ace, once your
* command has been executed.
* The handle method to be executed
* when running command
*
* @method handle
*
* @param {Object} args
* @param {Object} options
*
* @param {Object} args [description]
* @param {Object} options [description]
* @return {void}
*/
* handle ({ slug, name, description }, { permissions }) {
async handle ({ slug, name, description }, { permissions }) {
name = name || slug
let permission = yield Permission.query().where('slug', slug).first()
if (!permission) {
permission = new Permission({ slug })
let permission = await Permission.findBy('slug', slug)
if (permission) {
permission.fill({
name, description
})
await permission.save()
} else {
permission = await Permission.create({ slug, name, description })
}
permission.fill({
name, description
})
yield permission.save()
this.success(`${this.icon('success')} permission ${name} is updated.`)
Database.close()
}
Expand Down
76 changes: 47 additions & 29 deletions commands/Role.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
'use strict'

const Ioc = require('adonis-fold').Ioc
const Command = Ioc.use('Adonis/Src/Command')
const Role = Ioc.use('Adonis/Acl/Role')
const Permission = Ioc.use('Adonis/Acl/Permission')
const Database = Ioc.use('Adonis/Src/Database')
const series = require('co-series')
/**
* adonis-acl
* Copyright(c) 2017 Evgeny Razumov
* MIT Licensed
*/

const { Command } = require('@adonisjs/ace')
const Role = use('Adonis/Acl/Role')
const Permission = use('Adonis/Acl/Permission')
const Database = use('Adonis/Src/Database')
const _ = require('lodash')

class RoleCommand extends Command {
/**
* signature defines the requirements and name
* of command.
* The command signature getter to define the
* command name, arguments and options.
*
* @attribute signature
* @static
*
* @return {String}
*/
get signature () {
static get signature () {
return 'acl:role {slug} {name?} {description?} {--permissions=@value}'
}

/**
* description is the little helpful information displayed
* on the console.
* The command description getter.
*
* @attribute description
* @static
*
* @return {String}
*/
Expand All @@ -30,43 +39,52 @@ class RoleCommand extends Command {
}

/**
* handle method is invoked automatically by ace, once your
* command has been executed.
* The handle method to be executed
* when running command
*
* @param {Object} args [description]
* @param {Object} options [description]
* @method handle
*
* @param {Object} args
* @param {Object} options
*
* @return {void}
*/
* handle ({ slug, name, description }, { permissions }) {
async handle ({ slug, name, description }, { permissions }) {
name = name || slug
let role = yield Role.query().where('slug', slug).first()
if (!role) {
role = new Role({ slug })
let role = await Role.findBy('slug', slug)
if (role) {
role.fill({
name, description
})
await role.save()
} else {
role = await Role.create({
slug, name, description
})
}
role.fill({
name, description
})
yield role.save()
permissions = _.reduce(_.split(permissions, ','), (result, permission) => {
permission = _.trim(permission)
if (permission.length) {
result.push(permission)
}
return result
}, [])
permissions = yield _.map(permissions, series(function * (permission) {
let entry = yield Permission.query().where('slug', permission).first()
for (let i in permissions) {
const permission = permissions[i]
let entry = await Permission.findBy('slug', permission)
if (!entry) {
entry = yield Permission.create({
entry = await Permission.create({
slug: permission, name: permission
})
}
return entry.id
}))
permissions[i] = entry.id
}
if (permissions.length) {
yield role.permissions().sync(permissions)
await role.permissions().attach(permissions)
}
this.success(`${this.icon('success')} role ${name} is updated.`)
Database.close()
return role
}
}

Expand Down
Loading

0 comments on commit d655b41

Please sign in to comment.