Skip to content

Commit

Permalink
Admin roles support in API
Browse files Browse the repository at this point in the history
  • Loading branch information
usp-npe committed Apr 21, 2022
1 parent 0660573 commit 02ab2b3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
16 changes: 13 additions & 3 deletions app/Http/Controllers/UserManagement/API/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ public function index()
$this->validatePagination();

return new UserCollection(User::filtered($this->getFilter())
->when(in_array('roles', $this->getIncludes()), fn ($qry) => $qry->with(['roles' => fn ($qry) => $qry->orderBy('name')]))
->when(in_array('roles', $this->getIncludes()), fn ($qry) => $qry->with([
'roles' => fn ($qry) => $qry->orderBy('name'),
]))
->when(in_array('administeredRoles', $this->getIncludes()), fn ($qry) => $qry->with([
'administeredRoles' => fn ($qry) => $qry->orderBy('name'),
]))
->orderBy($this->getSortBy('name'), $this->getSortDirection())
->paginate($this->getPageSize()));
}
Expand Down Expand Up @@ -78,6 +83,13 @@ public function store(StoreUser $request)
*/
public function show(User $user)
{
if (in_array('roles', $this->getIncludes())) {
$user->load(['roles' => fn ($q) => $q->orderBy('name')]);
}
if (in_array('administeredRoles', $this->getIncludes())) {
$user->load(['administeredRoles' => fn ($q) => $q->orderBy('name')]);
}

$current_permissions = $user->permissions();
$permissions = [];
foreach (getCategorizedPermissions() as $title => $elements) {
Expand All @@ -90,8 +102,6 @@ public function show(User $user)

return (new UserResource($user))->additional([
'permissions' => $permissions,
'roles' => $user->roles->sortBy('name'),
'administeredRoles' => $user->administeredRoles->sortBy('name'),
]);
}

Expand Down
7 changes: 7 additions & 0 deletions app/Http/Resources/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ private function relationships()
'related' => route('api.users.roles.index', $this->resource),
],
],
'administeredRoles' => [
'data' => RoleResource::collection($this->whenLoaded('administeredRoles')),
'links' => [
'self' => route('api.users.relationships.roles.index', $this->resource),
'related' => route('api.users.roles.index', $this->resource),
],
],
];
}
}
18 changes: 11 additions & 7 deletions resources/js/api/user_management/users.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import { api, route } from '@/api/baseApi'
export default {
async list (params) {
async list(params) {
const url = route('api.users.index', params)
return await api.get(url)
},
async listWithRoles (params) {
async listWithRoles(params) {
if (!params) {
params = {}
}
params.include = 'roles'
const url = route('api.users.index', params)
return await api.get(url)
},
async store (data) {
async store(data) {
const url = route('api.users.store')
return await api.post(url, data)
},
async find (id) {
const url = route('api.users.show', id)
async find(id) {
let params = {
user: id,
include: 'roles,administeredRoles'
}
const url = route('api.users.show', params)
return await api.get(url)
},
async update (id, data) {
async update(id, data) {
const url = route('api.users.update', id)
return await api.put(url, data)
},
async delete (id) {
async delete(id) {
const url = route('api.users.destroy', id)
return await api.delete(url)
}
Expand Down
4 changes: 2 additions & 2 deletions resources/js/pages/users/UserShowPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ export default {
try {
let data = await usersApi.find(this.id)
this.user = data.data
this.roles = data.data.relationships.roles?.data ?? []
this.administeredRoles = data.data.relationships.administeredRoles?.data ?? []
this.permissions = data.permissions
this.roles = data.roles
this.administeredRoles = data.administeredRoles
} catch (err) {
this.error = err
}
Expand Down

0 comments on commit 02ab2b3

Please sign in to comment.