Skip to content

Commit

Permalink
Added user permission on frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
mshossain110 committed May 27, 2019
1 parent 7057edd commit 049f99c
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"globals": {
"axios": true,
"Vue": true,
"Bus": true
"Bus": true,
"LD": true
},
"rules": {
"indent": ["error", 4],
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/API/V1/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ protected function getPolicyMethods($model, $policy) {

if ( is_array( $methods ) ) {
foreach ( $methods as $method) {
if ($method == 'before') {
continue;
}
$data[] = strtolower($name) . '.' . $method;
}
}
Expand Down
1 change: 0 additions & 1 deletion app/Http/Controllers/API/V1/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ public function destroy ( $id ) {
public function deleteMultiple(Request $request )
{
$this->authorize('delete', User::class);
$this->authorize('destroy', User::class);

$this->validate($request, [
'ids' => 'required|array|min:1'
Expand Down
4 changes: 3 additions & 1 deletion app/Policies/UserPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class UserPolicy

public function before(User $user)
{
return $user->hasPermission('administrator');
if ($user->hasPermission('administrator')) {
return true;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function getPermissionsAttribute( $value ) {
$permissions = array_merge($permissions, $role->permissions);
}

return empty($permissions) ? null : $permissions;
return empty($permissions) ? [] : $permissions;
}

public function hasPermission( $permission ) {
Expand Down
27 changes: 13 additions & 14 deletions resources/assets/admin/src/components/Users/Profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@
<h1>This is user profile page</h1>
</template>


<script>
export default {
data () {
return {
}
},
computed: {
},
methods: {
export default {
data () {
return {
}
},
computed: {
},
methods: {
}
}
</script>

<style>
</style>
</style>
5 changes: 2 additions & 3 deletions resources/assets/admin/src/components/Users/Roles.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ import Role from './Role.vue'
export default {
beforeRouteEnter (to, from, next) {
next(vm => {
vm.hasPermission('role.view')
})
var p = LD.hasPermission('role.view')
next(p)
},
components: {
RoleForm,
Expand Down
6 changes: 6 additions & 0 deletions resources/assets/admin/src/components/Users/Users.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<VSpacer />

<VBtn
v-if="hasPermission('user.create')"
icon
@click="openform = true"
>
Expand All @@ -45,6 +46,7 @@
</VFlex>

<VDialog
v-if="hasPermission('user.create')"
v-model="openform"
max-width="500px"
>
Expand All @@ -58,6 +60,10 @@ import UsersTable from './UsersTable.vue'
import UserForm from './UserForm.vue'
export default {
beforeRouteEnter (to, from, next) {
var p = LD.hasPermission('user.view')
next(p)
},
components: {
UsersTable,
UserForm
Expand Down
6 changes: 6 additions & 0 deletions resources/assets/admin/src/components/Users/UsersTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@
</td>
<td class="justify-center layout px-0">
<VIcon
v-if="hasPermission('user.update')"
small
class="mr-2"
@click="editUserMethod(props.item)"
>
edit
</VIcon>
<VIcon
v-if="hasPermission('user.delete')"
small
@click="deleteUser(props.item)"
>
Expand All @@ -101,6 +103,7 @@
/>
</div>
<VDialog
v-if="hasPermission('user.update')"
v-model="openEditUserForm"
max-width="500px"
>
Expand Down Expand Up @@ -176,6 +179,9 @@ export default {
}
},
activation (user) {
if (!this.hasPermission('user.updata')) {
return
}
let status = user.status.toLowerCase() === 'inactive' ? 'active' : 'inactive'
Vue.set(user, 'status', status)
this.$store.dispatch('Users/updateUser', user)
Expand Down
3 changes: 3 additions & 0 deletions resources/assets/admin/src/mixin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export default {
return LD.user.permissions
},
hasPermission (p) {
if (!LD.user.permissions || !LD.user.permissions.length) {
return false
}
if (LD.user.permissions.indexOf('administrator') !== -1) {
return true
}
Expand Down
14 changes: 14 additions & 0 deletions resources/assets/common/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,17 @@ if (bearer) {
// broadcaster: 'pusher',
// key: 'your-pusher-key'
// });

window.LD.getUserPermissions = function getUserPermissions () {
return LD.user.permissions
}
window.LD.hasPermission = function hasPermission (p) {
if (!LD.user.permissions || !LD.user.permissions.length) {
return false
}
if (LD.user.permissions.indexOf('administrator') !== -1) {
return true
}

return LD.user.permissions.indexOf(p) !== -1
}

0 comments on commit 049f99c

Please sign in to comment.