Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
enniel committed May 31, 2017
0 parents commit 2721012
Show file tree
Hide file tree
Showing 24 changed files with 752 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
67 changes: 67 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Created by https://www.gitignore.io/api/node

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# Yarn lock file
yarn.lock

# dotenv environment variables file
.env


# End of https://www.gitignore.io/api/node
7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
npm-debug.log
yarn-debug.log
yarn-error.log
yarn.lock
.travis.yml
.editorconfig
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2017 Evgeny Razumov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Adonis ACL

Adonis ACL adds role based permissions to built in Auth System of Adonis Framework.

## Documentation

Follow along the [Wiki](https://github.com/enniel/adonis-acl/wiki) to find out more.

## Credits

- [Evgeni Razumov](https://github.com/enniel)

## Support

Having trouble? [Open an issue](https://github.com/enniel/adonis-acl/issues/new)!

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
51 changes: 51 additions & 0 deletions commands/Permission.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'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')

class PermissionCommand extends Command {
/**
* signature defines the requirements and name
* of command.
*
* @return {String}
*/
get signature () {
return 'acl:permission {slug} {name?} {description?}'
}

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

/**
* handle method is invoked automatically by ace, once your
* command has been executed.
*
* @param {Object} args [description]
* @param {Object} options [description]
*/
* handle ({ slug, name, description }, { permissions }) {
name = name || slug
let permission = yield Permission.query().where('slug', slug).first()
if (!permission) {
permission = new Permission({ slug })
}
permission.fill({
name, description
})
yield permission.save()
this.success(`${this.icon('success')} permission ${name} is updated.`)
Database.close()
}
}

module.exports = PermissionCommand
73 changes: 73 additions & 0 deletions commands/Role.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'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')
const _ = require('lodash')

class RoleCommand extends Command {
/**
* signature defines the requirements and name
* of command.
*
* @return {String}
*/
get signature () {
return 'acl:role {slug} {name?} {description?} {--permissions=@value}'
}

/**
* description is the little helpful information displayed
* on the console.
*
* @return {String}
*/
get description () {
return 'Create or update role'
}

/**
* handle method is invoked automatically by ace, once your
* command has been executed.
*
* @param {Object} args [description]
* @param {Object} options [description]
*/
* handle ({ slug, name, description }, { permissions }) {
name = name || slug
let role = yield Role.query().where('slug', slug).first()
if (!role) {
role = new Role({ slug })
}
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()
if (!entry) {
entry = yield Permission.create({
slug: permission, name: permission
})
}
return entry.id
}))
if (permissions.length) {
yield role.permissions().sync(permissions)
}
this.success(`${this.icon('success')} role ${name} is updated.`)
Database.close()
}
}

module.exports = RoleCommand
42 changes: 42 additions & 0 deletions commands/Setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

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

const Ace = require('adonis-ace')
const Ioc = require('adonis-fold').Ioc
const path = require('path')
const Command = Ioc.use('Adonis/Src/Command')

class SetupCommand extends Command {
get signature () {
return 'acl:setup'
}

get description () {
return 'Setup migration for ACL'
}

* handle () {
yield Ace.call('make:migration', ['create_roles_table'], {
template: path.join(__dirname, './templates/roles_schema.mustache')
})
yield Ace.call('make:migration', ['create_permissions_table'], {
template: path.join(__dirname, './templates/permissions_schema.mustache')
})
yield Ace.call('make:migration', ['create_role_user_table'], {
template: path.join(__dirname, './templates/role_user_schema.mustache')
})
yield Ace.call('make:migration', ['create_permission_role_table'], {
template: path.join(__dirname, './templates/permission_role_schema.mustache')
})
yield Ace.call('make:migration', ['create_permission_user_table'], {
template: path.join(__dirname, './templates/permission_user_schema.mustache')
})
}
}

module.exports = SetupCommand
24 changes: 24 additions & 0 deletions commands/templates/permission_role_schema.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const Schema = use('Schema')

class PermissionRoleTableSchema extends Schema {
up () {
this.create('permission_role', table => {
table.increments()
table.integer('permission_id').unsigned().index()
table.foreign('permission_id').references('id').on('permissions').onDelete('cascade')
table.integer('role_id').unsigned().index()
table.foreign('role_id').references('id').on('roles').onDelete('cascade')
table.timestamps()
})
}

down () {
this.drop('permission_role')
}

}

module.exports = PermissionRoleTableSchema
24 changes: 24 additions & 0 deletions commands/templates/permission_user_schema.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const Schema = use('Schema')

class PermissionUserTableSchema extends Schema {
up () {
this.create('permission_user', table => {
table.increments()
table.integer('permission_id').unsigned().index()
table.foreign('permission_id').references('id').on('permissions').onDelete('cascade')
table.integer('user_id').unsigned().index()
table.foreign('user_id').references('id').on('users').onDelete('cascade')
table.timestamps()
})
}

down () {
this.drop('permission_user')
}

}

module.exports = PermissionUserTableSchema
23 changes: 23 additions & 0 deletions commands/templates/permissions_schema.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const Schema = use('Schema')

class PermissionsTableSchema extends Schema {
up () {
this.create('permissions', table => {
table.increments()
table.string('slug').notNullable().unique()
table.string('name').notNullable().unique()
table.text('description').nullable()
table.timestamps()
})
}

down () {
this.drop('permissions')
}

}

module.exports = PermissionsTableSchema
24 changes: 24 additions & 0 deletions commands/templates/role_user_schema.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const Schema = use('Schema')

class RoleUserTableSchema extends Schema {
up () {
this.create('role_user', table => {
table.increments()
table.integer('role_id').unsigned().index()
table.foreign('role_id').references('id').on('roles').onDelete('cascade')
table.integer('user_id').unsigned().index()
table.foreign('user_id').references('id').on('users').onDelete('cascade')
table.timestamps()
})
}

down () {
this.drop('role_user')
}

}

module.exports = RoleUserTableSchema
Loading

0 comments on commit 2721012

Please sign in to comment.