├── .gitignore
├── README.md
├── example
├── acl-config.js
├── index.bundle.js
├── index.html
├── index.js
└── webpack.config.js
├── gulpfile.js
├── package.json
└── src
├── authorize.js
├── authorize
├── permission.js
└── role.js
├── component.js
├── directive.js
├── index.js
├── options.js
├── promise.js
└── utils.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # vue-authorize
2 |
3 | [](https://gitter.im/vuejs-auth/vue-authorize?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4 |
5 | **vue-authorize** is a simple authorization library for [Vue.js](https://vuejs.org/) and is an extension of [vue-authenticate](https://github.com/dgrubelic/vue-authenticate) library.
6 |
7 | **DISCLAIMER**
8 |
9 | For now, this package only supports ES6 import usage, but soon will have standalone ES5 build.
10 |
11 |
12 | *DEMO app comming soon...*
13 |
14 |
15 | ## Instalation
16 | ```bash
17 | npm install vue-authorize
18 | ```
19 |
20 | ## Usage
21 | ```javascript
22 | import Vue from 'vue'
23 | import VueResource from 'vue-resource'
24 | import VueAuthenticate from 'vue-authenticate'
25 | import VueAuthorize from 'vue-authorize'
26 |
27 | Vue.use(VueResource)
28 | Vue.use(VueAuthenticate, {
29 | // Your authentication config
30 | }
31 |
32 | Vue.use(VueAuthorize, {
33 | roles: {
34 | user: {
35 | handler: function () {
36 | // You have $auth instance directly in your role or permission handlers
37 | return this.$auth.isAuthorized()
38 | },
39 | permissions: ['can_read', 'can_create', 'can_update', 'can_delete']
40 | },
41 | guest: {
42 | handler: function () {
43 | return !this.$auth.isAuthorized()
44 | },
45 | permissions: ['can_read']
46 | }
47 | },
48 |
49 | permissions: {
50 | // You can have simple logic for checking if current user has write rights
51 | can_read: function () { return true },
52 |
53 | // ... or you can even perform AJAX request and return Promise instance
54 | can_create: function () { return Promise.resolve() },
55 | can_update: function () { return false },
56 | can_delete: function () { return Promise.reject() }
57 | }
58 | })
59 | ```
60 |
61 | For internal Vue component usage, you have access to `$authorize` service. To check if user has access to some resource, you can simply call `$authorize.isAuthorized(roles, permissions)` method.
62 |
63 | ```javascript
64 | new Vue({
65 | data() {
66 | return {
67 | showCreateButton: false,
68 | showUpdateButton: false,
69 | showDeleteButton: false
70 | }
71 | },
72 |
73 | inserted () {
74 | /**
75 | * This will resolve since user role (if we asume that use is authenticated)
76 | * has 'can_create' permission.
77 | */
78 | this.$authorize.isAuthorized(['user'], ['can_create']).then(() => {
79 | // User is allowed to add more resources
80 | this.showCreateButton = true
81 | }).catch(() => {
82 | // User is not allowed to add any more resources
83 | this.showCreateButton = false
84 | })
85 |
86 | /**
87 | * This will reject since user role (if we asume that use is authenticated)
88 | * has 'can_update' permission, but that permission returns `false`.
89 | */
90 | this.$authorize.isAuthorized(['guest'], ['can_update']).then(() => {
91 | // User is allowed to add more resources
92 | this.showUpdateButton = true
93 | }).catch(() => {
94 | // User is not allowed to add any more resources
95 | this.showUpdateButton = false
96 | })
97 |
98 | /**
99 | * This will reject since user role (if we asume that use is authenticated)
100 | * has 'can_delete' permission, but that permission returns `Promise.reject()`.
101 | */
102 | this.$authorize.isAuthorized(['guest'], ['can_delete']).then(() => {
103 | // User is allowed to add more resources
104 | this.showUpdateButton = true
105 | }).catch(() => {
106 | // User is not allowed to add any more resources
107 | this.showUpdateButton = false
108 | })
109 |
110 | /**
111 | * This will reject since guest role (if we asume that use is authenticated)
112 | * does not have 'can_create' permission.
113 | */
114 | this.$authorize.isAuthorized(['guest'], ['can_create']).then(() => {
115 | // User is allowed to add more resources
116 | this.showCreateButton = true
117 | }).catch(() => {
118 | // User is not allowed to add any more resources
119 | this.showCreateButton = false
120 | })
121 | }
122 | })
123 | ```
124 |
125 | ### $authorize.isAuthorized() use cases
126 |
127 | ```javascript
128 | // Check if user with guest role has access to resources.
129 | this.$authorize.isAuthorized(['guest'])
130 | ```
131 |
132 | ```javascript
133 | // Check if user with guest role has permission to access and read resources.
134 | this.$authorize.isAuthorized(['guest'], ['can_read'])
135 | ```
136 |
137 | ```javascript
138 | // Check if user with any role has permission to edit resources.
139 | this.$authorize.isAuthorized(null, ['can_update'])
140 | ```
141 |
142 | ### vue-router
143 |
144 | You can easily use this library with [vue-router]()
145 |
146 | Route example:
147 | ```javascript
148 | new VueRouter({
149 | routes: [
150 | {
151 | path: '/protected-route',
152 | meta: {
153 | permissions: {
154 | roles: ['user'],
155 | redirectTo: '/login'
156 | }
157 | }
158 | }
159 | ]
160 | })
161 | ```
162 |
163 | Once you have setup your route meta data, write simple route change handler to check if user has access to targeted route.
164 |
165 | ```javascript
166 | router.beforeEach(function (to, from, next) {
167 | if (to.meta && to.meta.permissions) {
168 | let roles = to.meta.permissions.roles
169 | let permissions = to.meta.permissions.permissions
170 |
171 | router.app.$authorize.isAuthorized(roles, permissions).then(function () {
172 | next()
173 | }).catch(function () {
174 | next(to.meta.permissions.redirectTo || '/login')
175 | })
176 | } else {
177 | next()
178 | }
179 | })
180 | ```
181 |
182 |
183 | Also, you can use already built-in directive and component to show/hide elements in the template.
184 |
185 | ### Directive (WIP) - need help!
186 | Directive only does show/hide of elements
187 |
188 | ```html
189 |
190 |
191 |
192 | ```
193 |
194 | ### Component
195 | Component internaly removes elements from the DOM if user does not have access to them.
196 |
197 | ```html
198 |
199 |
200 |
201 | ```
202 |
203 |
204 | ## License
205 |
206 | The MIT License (MIT)
207 |
208 | Copyright (c) 2017 Davor Grubelić
209 |
210 | Permission is hereby granted, free of charge, to any person obtaining a copy of
211 | this software and associated documentation files (the "Software"), to deal in
212 | the Software without restriction, including without limitation the rights to
213 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
214 | the Software, and to permit persons to whom the Software is furnished to do so,
215 | subject to the following conditions:
216 |
217 | The above copyright notice and this permission notice shall be included in all
218 | copies or substantial portions of the Software.
219 |
220 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
221 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
222 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
223 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
224 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
225 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
226 |
--------------------------------------------------------------------------------
/example/acl-config.js:
--------------------------------------------------------------------------------
1 | export default {
2 | roles: {
3 | admin: {
4 | handler: function () {
5 | // return this.$auth.isAuthenticated()
6 | return true
7 | },
8 | permissions: ['can_read', 'can_create', 'can_update', 'can_moderate', 'can_delete']
9 | },
10 |
11 | moderator: {
12 | handler: function () {
13 | return this.$auth.isAuthenticated()
14 | },
15 | permissions: ['can_moderate']
16 | },
17 |
18 | user: {
19 | handler: function () {
20 | return this.$auth.isAuthenticated()
21 | },
22 | permissions: ['can_read', 'can_create', 'can_update']
23 | },
24 |
25 | guest: {
26 | handler: function () {
27 | return !this.$auth.isAuthenticated()
28 | },
29 | permissions: ['can_read']
30 | }
31 | },
32 |
33 | permissions: {
34 | can_read: function () {
35 | return true
36 | },
37 |
38 | can_create: function () {
39 | return true
40 | },
41 |
42 | can_update: function () {
43 | return false
44 | },
45 |
46 | can_moderate: function () {
47 | return true
48 | },
49 |
50 | can_delete: function () {
51 | return true
52 | }
53 | }
54 | }
--------------------------------------------------------------------------------
/example/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Vue.Authorize
6 |
7 |
92 |
93 |
94 |