Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.

Commit 6f4df63

Browse files
authored
Merge pull request #10 from cleaniquecoders/develop
User Management
2 parents ed6969b + b33bc0f commit 6f4df63

File tree

86 files changed

+1511
-303
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+1511
-303
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Login details for default users:
6161

6262
1. E-mail : **[email protected]** Password: `developer`
6363
2. E-mail : **[email protected]** Password: `administrator`
64-
3. 1. E-mail : **[email protected]** Password: `user`
64+
3. E-mail : **[email protected]** Password: `user`
6565

6666
By default, all newly registered user will be assign role as `user`.
6767

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Datatable\Manage;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Models\User;
7+
use Illuminate\Http\Request;
8+
9+
class UserController extends Controller
10+
{
11+
public function __invoke(Request $request)
12+
{
13+
return app('datatables')->eloquent(User::datatable())->toJson();
14+
}
15+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Manage;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Models\User;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Auth\Events\Registered;
9+
10+
class UserController extends Controller
11+
{
12+
/**
13+
* Display a listing of the resource.
14+
*
15+
* @return \Illuminate\Http\Response
16+
*/
17+
public function index()
18+
{
19+
}
20+
21+
/**
22+
* Store a newly created resource in storage.
23+
*
24+
* @param \Illuminate\Http\Request $request
25+
*
26+
* @return \Illuminate\Http\Response
27+
*/
28+
public function store(Request $request)
29+
{
30+
$this->validate($request, [
31+
'name' => 'required|string|max:255',
32+
'email' => 'required|string|email|max:255|unique:users',
33+
'password' => 'required|string|min:6|confirmed',
34+
]);
35+
$data = $request->only('name', 'email', 'password');
36+
$user = User::create([
37+
'name' => $data['name'],
38+
'email' => $data['email'],
39+
'password' => bcrypt($data['password']),
40+
]);
41+
event(new Registered($user));
42+
$user->syncRoles([$request->role]);
43+
return response()->api([], __('User successfully stored.'), true, 201);
44+
}
45+
46+
/**
47+
* Display the specified resource.
48+
*
49+
* @param string $id
50+
*
51+
* @return \Illuminate\Http\Response
52+
*/
53+
public function show($id)
54+
{
55+
$user = User::details()->findByHashSlug($id);
56+
57+
/**
58+
* @todo should have a transformer to do this.
59+
*/
60+
$user = collect($user->only('name', 'email', 'roles_to_string', 'roles'));
61+
$roles = $user->get('roles')->mapWithKeys(function($role){
62+
return [$role->id => $role->name];
63+
});
64+
$user->put('roles', $roles);
65+
66+
return response()->api($user);
67+
}
68+
69+
/**
70+
* Update the specified resource in storage.
71+
*
72+
* @param \Illuminate\Http\Request $request
73+
* @param string $id
74+
*
75+
* @return \Illuminate\Http\Response
76+
*/
77+
public function update(Request $request, $id)
78+
{
79+
$this->validate($request, [
80+
'name' => 'required|string|max:255',
81+
]);
82+
83+
$fields = $request->only('name');
84+
85+
if(!empty($request->input('password'))) {
86+
$this->validate($request, [
87+
'password' => 'required|string|min:6|confirmed',
88+
]);
89+
$fields['password'] = bcrypt($request->input('password'));
90+
}
91+
92+
$user = User::findByHashSlug($id);
93+
$user->update($fields);
94+
$user->syncRoles($request->input('roles'));
95+
96+
return response()->api([], __('User successfully updated.'), true, 201);
97+
}
98+
99+
/**
100+
* Remove the specified resource from storage.
101+
*
102+
* @param string $id
103+
*
104+
* @return \Illuminate\Http\Response
105+
*/
106+
public function destroy($id)
107+
{
108+
if ($id == user()->hashslug) {
109+
return response()->api([], __('You cannot delete yourself!'), false, 401);
110+
}
111+
$user = User::findByHashSlug($id);
112+
if ($user->hasRole('developer')) {
113+
return response()->api([], __('Trust me, don\'t kill your developer!'), false, 401);
114+
}
115+
$user->delete();
116+
117+
return response()->api([], __('You have successfully delete a user.'));
118+
}
119+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Manage;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Models\User;
7+
8+
class UserController extends Controller
9+
{
10+
/**
11+
* Display a listing of the resource.
12+
*
13+
* @return \Illuminate\Http\Response
14+
*/
15+
public function index()
16+
{
17+
return view('manage.users.index');
18+
}
19+
20+
/**
21+
* Show the form for creating a new resource.
22+
*
23+
* @return \Illuminate\Http\Response
24+
*/
25+
public function create()
26+
{
27+
return view('manage.users.create');
28+
}
29+
30+
/**
31+
* Display the specified resource.
32+
*
33+
* @param string $id
34+
*
35+
* @return \Illuminate\Http\Response
36+
*/
37+
public function show($id)
38+
{
39+
$user = User::findByHashSlug($id);
40+
41+
return view('manage.users.show', compact('user'));
42+
}
43+
44+
/**
45+
* Show the form for editing the specified resource.
46+
*
47+
* @param string $id
48+
*
49+
* @return \Illuminate\Http\Response
50+
*/
51+
public function edit($id)
52+
{
53+
$user = User::findByHashSlug($id);
54+
55+
return view('manage.users.edit');
56+
}
57+
}

app/Http/Kernel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,16 @@ class Kernel extends HttpKernel
3636
\App\Http\Middleware\VerifyCsrfToken::class,
3737
\Illuminate\Routing\Middleware\SubstituteBindings::class,
3838
\Spatie\Referer\CaptureReferer::class,
39+
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
3940
],
4041

4142
'api' => [
43+
\App\Http\Middleware\ApiHeader::class,
44+
'throttle:60,1',
45+
'bindings',
46+
],
47+
48+
'datatable' => [
4249
'throttle:60,1',
4350
'bindings',
4451
],
@@ -60,5 +67,6 @@ class Kernel extends HttpKernel
6067
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
6168
'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
6269
'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
70+
'minify' => \App\Http\Middleware\MinifyHtml::class,
6371
];
6472
}

app/Http/Middleware/ApiHeader.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Closure;
6+
7+
class ApiHeader
8+
{
9+
/**
10+
* Handle an incoming request.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @param \Closure $next
14+
*
15+
* @return mixed
16+
*/
17+
public function handle($request, Closure $next)
18+
{
19+
if (config('api.header.accept') != $request->header('Accept')) {
20+
return response()->json(['message' => 'Invalid Accept Header'], 400);
21+
}
22+
23+
return $next($request);
24+
}
25+
}

app/Http/Middleware/MinifyHtml.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Closure;
6+
7+
class MinifyHtml
8+
{
9+
/**
10+
* Handle an incoming request.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @param \Closure $next
14+
*
15+
* @return mixed
16+
*/
17+
public function handle($request, Closure $next)
18+
{
19+
$response = $next($request);
20+
$response->setContent(minify($response->getContent()));
21+
22+
return $response;
23+
}
24+
}

app/Macros/Http/Response.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Macros\Http;
4+
5+
use Illuminate\Support\Facades\Response as HttpResponse;
6+
7+
class Response
8+
{
9+
public static function registerMacros()
10+
{
11+
HttpResponse::macro('api', function ($data, $message = null, $status = true, $code = 200) {
12+
return response()->json([
13+
'data' => $data,
14+
'message' => $message,
15+
'status' => $status,
16+
'code' => $code,
17+
], $code);
18+
});
19+
20+
HttpResponse::macro('datatable', function ($data, $countRecordsFiltered, $countRecordsTotal) {
21+
return response()->json([
22+
'recordsFiltered' => $countRecordsFiltered,
23+
'recordsTotal' => $countRecordsTotal,
24+
'data' => $data,
25+
]);
26+
});
27+
}
28+
}

app/Macros/Models/Model.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Macros\Models;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
7+
class Model
8+
{
9+
public static function registerMacros()
10+
{
11+
Builder::macro('hashslug', function ($hashslug) {
12+
return $this->where('hashslug', $hashslug);
13+
});
14+
15+
Builder::macro('findByHashSlug', function ($hashslug) {
16+
return $this->hashslug($hashslug)->firstOrFail();
17+
});
18+
}
19+
}

app/Macros/Routing/Route.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Macros\Routing;
4+
5+
// use Illuminate\Support\Facades\Route as DefaultRoute;
6+
7+
class Route
8+
{
9+
public static function registerMacros()
10+
{
11+
// if (! DefaultRoute::hasMacro('name')) {
12+
// DefaultRoute::macro('name', function ($modules) {
13+
14+
// });
15+
// }
16+
}
17+
}

0 commit comments

Comments
 (0)