1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Providers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; |
6
|
|
|
use Illuminate\Support\Facades\Gate; |
7
|
|
|
|
8
|
|
|
class AuthServiceProvider extends ServiceProvider |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The policy mappings for the application. |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $policies = [ |
16
|
|
|
'App\\Model' => 'App\\Policies\\ModelPolicy', |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Register any authentication / authorization services. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function boot() |
25
|
|
|
{ |
26
|
|
|
$this->registerPolicies(); |
27
|
|
|
|
28
|
|
|
/* |
29
|
|
|
* a user is allowed to edit the course grades if they are the teacher for this course, |
30
|
|
|
* or if they have explicit permission to do so |
31
|
|
|
*/ |
32
|
|
|
Gate::define('edit-course-grades', function ($user, $course) { |
33
|
|
|
return $user->isTeacher() && $user->teacher_id == $course->teacher_id || $user->can('evaluation.edit'); |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
/* |
37
|
|
|
* a user is allowed to view the course attendance sheet if they are the teacher for this course, |
38
|
|
|
* or if they have explicit permission to view all course attendance sheets |
39
|
|
|
*/ |
40
|
|
|
Gate::define('view-course-attendance', function ($user, $course) { |
41
|
|
|
return $user->isTeacher() && $user->teacher_id == $course->teacher_id || $user->can('attendance.view'); |
42
|
|
|
}); |
43
|
|
|
|
44
|
|
|
/* |
45
|
|
|
* a user is allowed to view the event attendance sheet if they are the teacher for this event, |
46
|
|
|
* if they are the teacher for this course, |
47
|
|
|
* or if they have explicit permission to view all event attendance sheets |
48
|
|
|
*/ |
49
|
|
|
Gate::define('view-event-attendance', function ($user, $event) { |
50
|
|
|
return ($event->teacher_id == $user->teacher_id) || ($event->course->teacher_id == $user->teacher_id) || $user->can('attendance.view'); |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
/* |
54
|
|
|
* a user is allowed to edit an attendance sheet if they are the teacher for the event, |
55
|
|
|
* if they are the teacher for the course, |
56
|
|
|
* or if they have explicit permission to edit any attendance sheets |
57
|
|
|
*/ |
58
|
|
|
Gate::define('edit-attendance', function ($user, $event) { |
59
|
|
|
return ($event->teacher_id == $user->teacher_id) || ($event->course->teacher_id == $user->teacher_id) || $user->can('attendance.edit'); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
/* |
63
|
|
|
* teachers are allowed to view their own calendar, |
64
|
|
|
* and users with explicit permission can view all calendars |
65
|
|
|
*/ |
66
|
|
|
Gate::define('view-teacher-calendar', function ($user, $teacher) { |
67
|
|
|
return ($user->isTeacher() && $user->teacher_id == $teacher->id) || $user->can('calendars.view'); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
/* |
71
|
|
|
* teachers are allowed to view their own courses, |
72
|
|
|
* and users with explicit permission can view all courses |
73
|
|
|
*/ |
74
|
|
|
Gate::define('view-course', function ($user, $course) { |
75
|
|
|
return ($user->isTeacher() && $user->teacher_id == $course->teacher_id) || $user->can('courses.view'); |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
/* |
79
|
|
|
* the user is allowed to view the result if they are the student, |
80
|
|
|
* if they are a teacher |
81
|
|
|
* of if they have explicit permission to view any result |
82
|
|
|
*/ |
83
|
|
|
Gate::define('view-enrollment', function ($user, $enrollment) { |
84
|
|
|
return ($user->isStudent() && $user->student_id == $enrollment->student_id) || $user->isTeacher() || $user->can('evaluation.view'); |
85
|
|
|
}); |
86
|
|
|
|
87
|
|
|
/* |
88
|
|
|
* if the user is the teacher of the course |
89
|
|
|
* of if they have explicit permission to enroll students |
90
|
|
|
*/ |
91
|
|
|
Gate::define('enroll-in-course', function ($user, $course) { |
92
|
|
|
return $course->teacher_id == $user->teacher_id || $user->can('enrollments.edit'); |
93
|
|
|
}); |
94
|
|
|
|
95
|
|
|
/* |
96
|
|
|
* if the user is a teacher |
97
|
|
|
* of if they have explicit permission to enroll students |
98
|
|
|
*/ |
99
|
|
|
Gate::define('enroll-students', function ($user) { |
100
|
|
|
return $user->isTeacher() || $user->can('enrollments.edit'); |
101
|
|
|
}); |
102
|
|
|
|
103
|
|
|
/* |
104
|
|
|
* teachers are allowed to view their own hours, |
105
|
|
|
* and users with explicit permission can view all hours |
106
|
|
|
*/ |
107
|
|
|
Gate::define('view-teacher-hours', function ($user, $teacher) { |
108
|
|
|
return ($user->isTeacher() && $user->teacher_id == $teacher->id) || $user->can('hr.view'); |
109
|
|
|
}); |
110
|
|
|
|
111
|
|
|
/* |
112
|
|
|
* teachers are allowed to edit results for their own students |
113
|
|
|
* as well as users with explicit permission to edit any result |
114
|
|
|
*/ |
115
|
|
|
Gate::define('edit-result', function ($user, $enrollment) { |
116
|
|
|
return ($user->isTeacher() && $user->teacher_id == $enrollment->course->teacher_id) || $user->can('evaluation.edit'); |
117
|
|
|
}); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|