-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSchoolController.php
293 lines (244 loc) · 8.73 KB
/
SchoolController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* Class SchoolController
* This controller handles the CRUD of schools, and associated default calendars that are generated alongside of a school.
*/
class SchoolController extends \BaseController
{
// TODO: Allow school to update their own information
protected $layout = 'layout.master';
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$user = Sentry::getUser();
// If user is logged in, redirect to calendar index
if (!Sentry::check()) {
return Redirect::route('landing');
}
// Check if user is a superAdmin (other users are not allowed on this page)
if ($user->hasAccess('superadmin')) {
$schools = School::get();
return View::make('school.index')->with('schools', $schools);
} else {
// If no permissions, redirect the user to the calendar index page
return Redirect::route('calendar.redirect');
}
}
/**
* Store a newly created school in storage.
* Create default calendars.
* Store new user (schooladmin) as well.
* @return Response
*/
// TODO: Get rid of short (reoccuring)
public function store()
{
// Validation rules for input fields
$validator = Validator::make(
[
'Voornaam' => Input::get('user-firstname'),
'Achternaam' => Input::get('user-lastname'),
'Schoolnaam' => Input::get('school-name'),
'E-mail adres' => Input::get('user-email'),
'Stad' => Input::get('school-city'),
'Wachtwoord' => Input::get('user-password'),
'Wachtwoord_confirmation' => Input::get('user-password-confirm'),
],
[
'Voornaam' => 'required',
'Achternaam' => 'required',
'Schoolnaam' => 'required|unique:schools,name',
'Stad' => 'required',
'E-mail adres' => 'required|email|unique:users,email',
'Wachtwoord' => 'required|min:8|confirmed',
]
);
// If validator fails, go back and show errors
if ($validator->fails()) {
$validator->getMessageBag()->add('errorMessage',
'Je school kon niet geregistreerd worden. Corrigeer de aangegeven fouten en probeer opnieuw.');
return Redirect::route('school.register')->withInput()
->withErrors($validator);
}
// If there are no errors, prepare a new School object to be inserted in the database
$school = new School();
$school->name = e(Input::get("school-name"));
$school->city = e(Input::get("school-city"));
$school->slug = preg_replace('/[^a-zA-Z0-9]/', '-', e(strtolower(Input::get("school-name"))));
$school->save();
// Create the default calendars "global" and "admin"
// TODO: create new "calendars" instead of the calendars
// Store the newly created user along with the school
$user = Sentry::createUser(
[
'email' => e(Input::get("user-email")),
'password' => Input::get("user-password"),
'activated' => true,
'school_id' => $school->id,
'first_name' => e(Input::get("user-firstname")),
'last_name' => e(Input::get("user-lastname")),
]
);
// make sure the roles exist
UserController::checkCreateRoles();
// Find the role using the calendar id
$adminRole = Sentry::findGroupByName('admin');
// Assign the calendar to the user
$user->addGroup($adminRole);
$calendar = new Calendar();
$calendar->name = "global";
$calendar->description = "events for everyone";
$calendar->school_id = $school->id;
$calendar->save();
// link to global calendar
$user->calendars()->attach($calendar);
// Add the user to the admin calendar
// $user->addcalendar($calendar);
// Log the user in
Sentry::login($user, false);
return Redirect::route('calendar.redirect');
}
/**
* Show the form for editing the specified resource.
*
* @param string $slug the slug of the school for which we want to show the dashboard
* @return Response
*/
public function dashboard($slug)
{
if (!Sentry::check()) {
return Redirect::route('landing')->withErrors("Je moet ingelogd zijn om van deze functie gebruikt te maken!");
}
$user = Sentry::getUser();
$school = SchoolController::getSchoolBySlug($slug);
// Check if user is superAdmin (only they can edit schools)
if ($user->hasAccess('admin') && $school->id == $user->school_id) {
return View::make('admin.dashboard')->with('org', $school);
} else {
// If no permissions, redirect the user to the calendar index page
return Redirect::route('calendar.redirect')->withErrors("Je moet ingelogd zijn om van deze functie gebruikt te maken!");
}
}
/**
* Update the specified school in storage.
*
* @param string $slug the slug of the school to update
* @return Response
*/
public function updateSchoolBySlug($slug)
{
$this->updateSchool(SchoolController::getSchoolBySlug($slug));
}
/**
* Update the specified school in storage.
*
* @param int $id the Id of the school to update
* @return Response
*/
public function updateSchoolById($id)
{
$this->updateSchool(School::find($id));
}
/**
* Update the specified school in storage.
*
* @param School $school the school to update
* @return Response
*/
public function updateSchool($school)
{
if (!Sentry::check()) {
return Redirect::route('landing');
}
$user = Sentry::getUser();
// Check if user is superAdmin (only they can update schools)
if (!$user->hasAccess('admin') || $user->school_id != $school->id) { // If no permissions, redirect the user to the calendar index page
return Redirect::route('calendar.redirect');
}
if (Input::has("name")) {
$school->name = e(Input::get("name"));
}
if (Input::has("city")) {
$school->city = e(Input::get("city"));
}
if (Input::has("opening")) {
$school->opening = e(Input::get("opening"));
}
$school->save();
return Redirect::route('school.index');
}
/**
* Remove the specified school from storage.
*
* @param string $slug the school slug
* @return Response
*/
public function destroySchoolBySlug($slug)
{
$this->destroySchool(SchoolController::getSchoolBySlug($slug));
}
/**
* Remove the specified school from storage.
*
* @param int $id the school ID
* @return Response
*/
public function destroySchoolById($id)
{
$this->destroySchool(School::find($id));
}
// TODO: Authenticate in route?
/**
* Destroy a school after checking for permissions
* @param $school \School the school to destroy
* @return \Illuminate\Http\RedirectResponse
*/
public function destroySchool($school)
{
if (!Sentry::check()) {
return Redirect::route('landing');
}
$user = Sentry::getUser();
// Check if user is superAdmin (only they can remove schools)
if (!$user->hasAccess('superadmin')) {
// If no permissions, redirect the user to the calendar index page
return Redirect::route('calendar.redirect');
}
$school->delete();
return Redirect::route('school.index');
}
/**
* Get all admins for a school
* @param $school_id int the ID of the school
* @return array the admins
*/
public static function getSchoolAdmins($school_id)
{
$adminrole = Sentry::findGroupByName('admin'); // all admins
$users = Sentry::findAllUsersInGroup($adminrole);
// we got all admins, but we only need the admins for this school
$filtered = array();
foreach ($users as $usr) {
if ($users->school_id == $school_id) {
array_push($filtered, $usr);
}
}
return $filtered;
}
/**
* @param $school_slug
* @return \School |static
*/
public static function getSchoolBySlug($school_slug)
{
return School::where('slug', $school_slug)->firstOrFail();
}
public static function showRegisterForm()
{
return View::make('user.register');
}
}