Passed
Push — master ( 58b7d4...bb40b7 )
by Marcel
08:51
created

UserChecker::checkPreAuth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 1
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Security;
4
5
use App\Entity\IcsAccessToken;
6
use App\Entity\User;
7
use App\Entity\UserType;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
10
use Symfony\Component\Security\Core\Exception\AccountStatusException;
11
use Symfony\Component\Security\Core\User\UserCheckerInterface;
12
use Symfony\Component\Security\Core\User\UserInterface;
13
14
/**
15
 * Checks whether the user meets certain critiera or sends
16
 * an error page:
17
 *
18
 * - students must have exactly one associated student
19
 * - parents must have at least one associated student
20
 * - teachers must have one associated teacher
21
 */
22
class UserChecker implements UserCheckerInterface {
23
24 10
    public function checkPreAuth(UserInterface $user) {
25 10
        
26
    }
27 10
28 3
    public function checkPostAuth(UserInterface $user) {
29
        if($user instanceof IcsAccessToken) {
30
            $user = $user->getUser();
31 7
        }
32 1
33
        if(!$user instanceof User) {
34
            return;
35 6
        }
36 2
37
        if($user->isTeacher() && $user->getTeacher() === null) {
38
            throw new InvalidAccountException('invalid_account.teacher');
39 4
        }
40 1
41
        if($user->isStudent() && $user->getStudents()->count() !== 1) {
42 3
            throw new InvalidAccountException('invalid_account.student');
43
        }
44
45
        if($user->isParent() && $user->getStudents()->count() === 0) {
46
            throw new InvalidAccountException('invalid_account.parent');
47
        }
48
    }
49
}