Skip to content

Commit 7894328

Browse files
last step: add scheduling
1 parent c0a889f commit 7894328

File tree

16 files changed

+297
-15
lines changed

16 files changed

+297
-15
lines changed

app/Console/Kernel.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Scheduling\Schedule;
66
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
7+
use Illuminate\Support\Facades\DB;
78

89
class Kernel extends ConsoleKernel
910
{
@@ -12,7 +13,12 @@ class Kernel extends ConsoleKernel
1213
*/
1314
protected function schedule(Schedule $schedule): void
1415
{
15-
// $schedule->command('inspire')->hourly();
16+
// $schedule->command('php artisan passport:purge')->hourly();
17+
// $schedule->call(function() {
18+
// DB::table('recent_users')->delete();
19+
// })->daily();
20+
// $schedule->exec('sudo /etc/init.d/apache2 stop')->daily();
21+
// $schedule->exec('sudo /opt/lampp/lampp start')->daily();
1622
}
1723

1824
/**

app/Http/Controllers/API/LessonController.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ class LessonController extends Controller
1212
/**
1313
* Display a listing of the resource.
1414
*/
15-
public function index()
15+
public function __construct() {
16+
$this->middleware('auth:api')->except(['index', 'show']);
17+
}
18+
public function index(Request $request)
1619
{
17-
$lesson = LessonResource::collection(Lesson::all());
20+
$limit = $request->input('limit') <= 50 ? $request->input('limit') : 15;
21+
$lesson = LessonResource::collection(Lesson::paginate($limit));
1822
return $lesson->response()->setStatusCode(200);
1923
}
2024

@@ -41,6 +45,8 @@ public function show($id)
4145
*/
4246
public function update(Request $request, $id)
4347
{
48+
$idLesson = Lesson::findOrFail($id);
49+
$this->authorize('delete', $idLesson);
4450
$lesson = new LessonResource(Lesson::findOrFail($id));
4551
$lesson->update($request->all());
4652

@@ -53,6 +59,8 @@ public function update(Request $request, $id)
5359
*/
5460
public function destroy($id)
5561
{
62+
$lesson = Lesson::findOrFail($id);
63+
$this->authorize('delete', $lesson);
5664
Lesson::findOrFail($id)->delete();
5765

5866
return 204;

app/Http/Controllers/API/LoginController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ public function __construct() {
1515

1616
public function login() {
1717
$AccessToken = Auth::user()->createToken('Access Token')->accessToken;
18-
18+
1919
return Response(['User' => new UserResource(Auth::user()), 'Access Token' => $AccessToken]);
2020
}
21-
22-
2321
}

app/Http/Controllers/API/RelationsController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function userLessons($id) {
2121
$fields = array();
2222
$filtered = array();
2323
foreach($user as $lesson) {
24+
$fields['Ref'] = $lesson->id;
2425
$fields['Title'] = $lesson->title;
2526
$fields['Content'] = $lesson->body;
2627
$filtered[] = $fields;

app/Http/Controllers/API/TagController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ class TagController extends Controller
1212
/**
1313
* Display a listing of the resource.
1414
*/
15-
public function index()
15+
public function __construct() {
16+
$this->middleware('auth:api')->except(['index', 'show']);
17+
}
18+
public function index(Request $request)
1619
{
17-
$tag = TagResource::collection(Tag::all());
20+
$limit = $request->input('limit') <= 50 ? $request->input('limit') : 15;
21+
$tag = TagResource::collection(Tag::paginate($limit));
1822
return $tag->response()->setStatusCode(200);
1923
}
2024

app/Http/Controllers/API/UserController.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ class UserController extends Controller
1313
/**
1414
* Display a listing of the resource.
1515
*/
16-
public function index()
16+
public function __construct() {
17+
$this->middleware('auth:api')->except(['index', 'show']);
18+
}
19+
public function index(Request $request)
1720
{
18-
$user = UserResource::collection(User::all());
21+
$limit = $request->input('limit') <= 50 ? $request->input('limit') : 15;
22+
$user = UserResource::collection(User::paginate($limit));
1923
return $user->response()->setStatusCode(200);
2024
}
2125

@@ -24,6 +28,7 @@ public function index()
2428
*/
2529
public function store(Request $request)
2630
{
31+
$this->authorize('create', User::class);
2732
$user = new UserResource(User::create([
2833
'name' => $request->name,
2934
'email' => $request->email,
@@ -46,6 +51,8 @@ public function show($id)
4651
*/
4752
public function update(Request $request, $id)
4853
{
54+
$idUser = User::findOrFail($id);
55+
$this->authorize('update', $idUser);
4956
$user = new UserResource(User::findOrFail($id));
5057

5158
$user->update($request->all());
@@ -58,6 +65,8 @@ public function update(Request $request, $id)
5865
*/
5966
public function destroy($id)
6067
{
68+
$idUser = User::findOrFail($id);
69+
$this->authorize('delete', $idUser);
6170
User::findOrFail($id)->delete();
6271

6372
return 204;

app/Http/Resources/Lesson.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ class Lesson extends JsonResource
1616
public function toArray(Request $request): array
1717
{
1818
return [
19+
// 'Ref' => $this->id,
1920
'Author' => $this->user->name,
2021
'Title' => $this->title,
2122
'Content' => $this->body,
22-
'Tags' => TagResource::collection($this->tags)
23+
// 'Tags' => TagResource::collection($this->tags)
2324
];
2425
}
2526
}

app/Http/Resources/User.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function toArray(Request $request): array
1919
'id' => $this->id,
2020
'Full Name' => $this->name,
2121
'E-Mail' => $this->email,
22+
'Role' => $this->role
2223
// 'Pass' => $this->password
2324
// 'Lessons' => LessonResource::collection($this->lessons),
2425
];

app/Models/User.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class User extends Authenticatable
2121
'name',
2222
'email',
2323
'password',
24+
'role'
2425
];
2526

2627
/**

app/Policies/LessonPolicy.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace App\Policies;
4+
5+
use App\Models\Lesson;
6+
use App\Models\User;
7+
use Illuminate\Auth\Access\Response;
8+
9+
class LessonPolicy
10+
{
11+
/**
12+
* Determine whether the user can view any models.
13+
*/
14+
public function viewAny(User $user)
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Determine whether the user can view the model.
21+
*/
22+
public function view(User $user, Lesson $lesson)
23+
{
24+
//
25+
}
26+
27+
/**
28+
* Determine whether the user can create models.
29+
*/
30+
public function create(User $user)
31+
{
32+
//
33+
}
34+
35+
/**
36+
* Determine whether the user can update the model.
37+
*/
38+
public function update(User $user, Lesson $lesson)
39+
{
40+
return $user->id === $lesson->user_id || $user->role === 'admin'
41+
? Response::allow()
42+
: Response::deny('You do not have permission to perform this action.');
43+
}
44+
45+
/**
46+
* Determine whether the user can delete the model.
47+
*/
48+
public function delete(User $user, Lesson $lesson)
49+
{
50+
return $user->id === $lesson->user_id || $user->role === 'admin'
51+
? Response::allow()
52+
: Response::deny('You do not have permission to perform this action.');
53+
}
54+
55+
/**
56+
* Determine whether the user can restore the model.
57+
*/
58+
public function restore(User $user, Lesson $lesson)
59+
{
60+
//
61+
}
62+
63+
/**
64+
* Determine whether the user can permanently delete the model.
65+
*/
66+
public function forceDelete(User $user, Lesson $lesson)
67+
{
68+
//
69+
}
70+
}

app/Policies/TagPolicy.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace App\Policies;
4+
5+
use App\Models\Tag;
6+
use App\Models\User;
7+
use Illuminate\Auth\Access\Response;
8+
9+
class TagPolicy
10+
{
11+
/**
12+
* Determine whether the user can view any models.
13+
*/
14+
public function viewAny(User $user)
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Determine whether the user can view the model.
21+
*/
22+
public function view(User $user, Tag $tag)
23+
{
24+
//
25+
}
26+
27+
/**
28+
* Determine whether the user can create models.
29+
*/
30+
public function create(User $user)
31+
{
32+
return $user->role == 'admin'
33+
? Response::allow()
34+
: Response::deny('You do not have permission to create new users.');
35+
}
36+
37+
/**
38+
* Determine whether the user can update the model.
39+
*/
40+
public function update(User $user, Tag $tag)
41+
{
42+
return $user->role === 'admin'
43+
? Response::allow()
44+
: Response::deny('You do not have permission to perform this action.');
45+
}
46+
47+
/**
48+
* Determine whether the user can delete the model.
49+
*/
50+
public function delete(User $user, Tag $tag)
51+
{
52+
return $user->role === 'admin'
53+
? Response::allow()
54+
: Response::deny('You do not have permission to perform this action.');
55+
}
56+
57+
/**
58+
* Determine whether the user can restore the model.
59+
*/
60+
public function restore(User $user, Tag $tag)
61+
{
62+
//
63+
}
64+
65+
/**
66+
* Determine whether the user can permanently delete the model.
67+
*/
68+
public function forceDelete(User $user, Tag $tag)
69+
{
70+
//
71+
}
72+
}

app/Policies/UserPolicy.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace App\Policies;
4+
5+
use App\Models\User;
6+
use Illuminate\Auth\Access\Response;
7+
8+
class UserPolicy
9+
{
10+
/**
11+
* Determine whether the user can view any models.
12+
*/
13+
public function viewAny(User $user)
14+
{
15+
//
16+
}
17+
18+
/**
19+
* Determine whether the user can view the model.
20+
*/
21+
public function view(User $user, User $model)
22+
{
23+
//
24+
}
25+
26+
/**
27+
* Determine whether the user can create models.
28+
*/
29+
public function create(User $user)
30+
{
31+
return $user->role == 'admin'
32+
? Response::allow()
33+
: Response::deny('You do not have permission to create new users.');
34+
}
35+
36+
/**
37+
* Determine whether the user can update the model.
38+
*/
39+
public function update(User $user, User $model)
40+
{
41+
return $user->id === $model->id || $user->role === 'admin'
42+
? Response::allow()
43+
: Response::deny('You do not have permission to perform this action.');
44+
}
45+
46+
/**
47+
* Determine whether the user can delete the model.
48+
*/
49+
public function delete(User $user, User $model)
50+
{
51+
return $user->id === $model->id || $user->role === 'admin'
52+
? Response::allow()
53+
: Response::deny('You do not have permission to perform this action.');
54+
}
55+
56+
/**
57+
* Determine whether the user can restore the model.
58+
*/
59+
public function restore(User $user, User $model)
60+
{
61+
//
62+
}
63+
64+
/**
65+
* Determine whether the user can permanently delete the model.
66+
*/
67+
public function forceDelete(User $user, User $model)
68+
{
69+
//
70+
}
71+
}

0 commit comments

Comments
 (0)