项目作者: mikield

项目描述 :
AdonisJS框架的用户策略特征(支持可以并且可以不是用户模型的方法)
高级语言: JavaScript
项目地址: git://github.com/mikield/adonis-user-policy.git
创建时间: 2017-10-17T16:13:00Z
项目社区:https://github.com/mikield/adonis-user-policy

开源协议:MIT License

下载


Welcome to Adonis User Policy 👋


Version

Documentation


Maintenance


License: MIT


Twitter: AdmiralMiki

Adonis User Policy (Authorization).
This is a trait wich adds can and canNot methods to User Model (or other class :smile:)

🏠 Homepage

Install

  1. adonis install @mikield/adonis-user-policy

This package relies on https://github.com/mikield/adonis-true-traits which is included when installing the user policy package. You need to register its provider in start/app.js

  1. const providers = [
  2. ...,
  3. '@mikield/adonis-true-traits'
  4. ]

Usage

Mix User model class with a PolicyTrait class

  1. 'use strict'
  2. const Model = use('Model')
  3. const PolicyTrait = use('@mikield/adonis-user-policy')
  4. class User extends Model {
  5. ...
  6. }
  7. module.exports = mix(User).with(PolicyTrait)
Create a Policy User.js under app\Policies folder.
  1. 'use strcit'
  2. class User {
  3. static createPosts(user) {
  4. return !user.roles.contains('create-post') //We can check a user role for example
  5. }
  6. }
  7. module.exports = Post
And attach it to User model.
  1. 'use strict'
  2. const Model = use('Model')
  3. const PolicyTrait = use('@mikield/adonis-user-policy')
  4. class User extends Model {
  5. //User policy
  6. static get policy(){
  7. return 'App/Policies/User'
  8. }
  9. }
  10. module.exports = mix(User).with(PolicyTrait)
Or if you want to check access based on another model — then, for example:
Create a Policy Post.js under app\Policies folder.
  1. 'use strcit'
  2. class Post {
  3. static add(user, post) { //In this case the post will be a class and not a instance
  4. return !user.banned //User can create a Post if it is not banned
  5. }
  6. static edit(user, post){ //In this case the post will be a instance and not a class
  7. return post.owner.id === user.id //Only the owner of the post can edit the post
  8. }
  9. }
  10. module.exports = Post
Next register a Policy in other Post model.
  1. 'use strict'
  2. const Model = use('Model')
  3. class Post extends Model {
  4. //Post policy
  5. static get policy(){
  6. return 'App/Policies/Post'
  7. }
  8. }
  9. module.exports = Post
On a User instance call can or canNot function.
Without Post model.
  1. const User = use('App/Models/User')
  2. Route.get('/test-user-policy', async () => {
  3. const user = await User.find(1)
  4. if(user.can('createPost')) {
  5. ...
  6. }
  7. if(user.canNot('createPost')) {
  8. ...
  9. }
  10. })
With Post model.
  1. const User = use('App/Models/User')
  2. const Post = use('App/Models/Post')
  3. Route.get('/test-user-policy', async () => {
  4. const user = await User.find(1)
  5. if(user.can('add', Post)){
  6. ...
  7. }
  8. if(user.canNot('add', Post)){
  9. ...
  10. }
  11. })
With Post instance (existing Post object).
  1. const User = use('App/Models/User')
  2. const Post = use('App/Models/Post')
  3. Route.get('/test-user-policy', async () => {
  4. const user = await User.find(1)
  5. const post = await Post.find(1)
  6. if(user.can('edit', post)) {
  7. ...
  8. }
  9. if(user.canNot('edit', post)) {
  10. ...
  11. }
  12. })

Author

👤 Vladyslav Gaysyuk

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.

Show your support

Give a ⭐️ if this project helped you!



📝 License

Copyright © 2020 Vladyslav Gaysyuk.

This project is MIT licensed.


This README was generated with ❤️ by readme-md-generator