Completed
Push — master ( 7db876...4bd76b )
by Jonathan
02:00
created

FileOwnerValidator::createViolation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
/**
4
 * Copyright 2014 Jonathan Bouzekri. All rights reserved.
5
 *
6
 * @copyright Copyright 2014 Jonathan Bouzekri <[email protected]>
7
 * @license https://github.com/jbouzekri/FileUploaderBundle/blob/master/LICENSE
8
 * @link https://github.com/jbouzekri/FileUploaderBundle
9
 */
10
11
namespace Jb\Bundle\FileUploaderBundle\Service\Validator\Constraints;
12
13
use Symfony\Component\Validator\Constraint;
14
use Symfony\Component\Validator\ConstraintValidator;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
17
18
/**
19
 * FileOwnerValidator
20
 *
21
 * @author jobou
22
 */
23
class FileOwnerValidator extends ConstraintValidator
24
{
25
    /**
26
     * @var \Doctrine\Common\Persistence\ObjectManager
27
     */
28
    protected $em;
29
30
    /**
31
     * @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
32
     */
33
    protected $tokenStorage;
34
35
    /**
36
     * Constructor
37
     *
38
     * @param \Doctrine\Common\Persistence\ObjectManager $em
39
     * @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface $tokenStorage
40
     */
41
    public function __construct(ObjectManager $em, TokenStorageInterface $tokenStorage)
42
    {
43
        $this->em = $em;
44
        $this->tokenStorage = $tokenStorage;
45
    }
46
47
    /**
48
     * Validate that the submitted file is owned by the authenticated user
49
     *
50
     * @param string $value
51
     * @param Constraint $constraint
52
     */
53
    public function validate($value, Constraint $constraint)
54
    {
55
        if (!$value) {
56
            return;
57
        }
58
59
        $fileHistory = $this->em->getRepository('JbFileUploaderBundle:FileHistory')->find($value);
60
        if (!$fileHistory) {
61
            return;
62
        }
63
64
        // No userid associated with file. Every one can use it.
65
        if (!$fileHistory->getUserId()) {
66
            return;
67
        }
68
69
        // No token. Violation as there is a user id associate with file.
70
        $token = $this->tokenStorage->getToken();
71
        if (!$token) {
72
            return $this->createViolation($value, $constraint);
73
        }
74
75
        // No user. Violation as there is a user id associate with file.
76
        $user = $token->getUser();
77
        if (!$user) {
78
            return $this->createViolation($value, $constraint);
79
        }
80
81
        if ($user->getId() !== $fileHistory->getUserId()) {
82
            return $this->createViolation($value, $constraint);
83
        }
84
85
        return;
86
    }
87
88
    /**
89
     * Create violation for validator
90
     *
91
     * @param string $value
92
     * @param Constraint $constraint
93
     */
94
    protected function createViolation($value, Constraint $constraint)
95
    {
96
        $this
97
            ->context
98
            ->buildViolation($constraint->message)
99
            ->setParameter('%filename%', $value)
100
            ->addViolation();
101
    }
102
}
103