Issues (3099)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

Service/EntityVersionLockService.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminListBundle\Service;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use FOS\UserBundle\Model\User;
7
use Kunstmaan\AdminListBundle\Entity\LockableEntity;
8
use Kunstmaan\AdminListBundle\Entity\EntityVersionLock;
9
use Kunstmaan\AdminListBundle\Entity\LockableEntityInterface;
10
use Kunstmaan\AdminListBundle\Repository\EntityVersionLockRepository;
11
12
class EntityVersionLockService
13
{
14
    /**
15
     * @var ObjectManager
16
     */
17
    private $objectManager;
18
19
    /**
20
     * @var int
21
     */
22
    private $threshold;
23
24
    /**
25
     * @var bool
26
     */
27
    private $lockEnabled;
28
29 6
    public function __construct(ObjectManager $em, $threshold, $lockEnabled)
30
    {
31 6
        $this->setObjectManager($em);
32 6
        $this->setThreshold($threshold);
33 6
        $this->setLockEnabled($lockEnabled);
34 6
    }
35
36
    /**
37
     * @param LockableEntityInterface $entity
38
     *
39
     * @return bool
40
     */
41 3
    public function isEntityBelowThreshold(LockableEntityInterface $entity)
42
    {
43
        /** @var LockableEntity $lockable */
44 3
        $lockable = $this->getLockableEntity($entity, false);
45
46 3
        if ($this->lockEnabled && $lockable->getId() !== null) {
47 2
            $now = new \DateTime();
48 2
            $thresholdDate = clone $lockable->getUpdated();
49 2
            $thresholdDate->add(new \DateInterval('PT'.$this->threshold.'S'));
50
51 2
            return $thresholdDate > $now;
52
        }
53
54 1
        return false;
55
    }
56
57
    /**
58
     * @param User                    $user
59
     * @param LockableEntityInterface $entity
60
     *
61
     * @return bool
62
     */
63 2
    public function isEntityLocked(User $user, LockableEntityInterface $entity)
64
    {
65
        /** @var LockableEntity $lockable */
66 2
        $lockable = $this->getLockableEntity($entity);
67
68 2
        if ($this->lockEnabled) {
69 2
            $this->removeExpiredLocks($lockable);
70 2
            $locks = $this->getEntityVersionLocksByLockableEntity($lockable, $user);
71
72 2
            if ($locks === null || !\count($locks)) {
73 1
                $this->createEntityVersionLock($user, $lockable);
74
75 1
                $lockable->setUpdated(new \DateTime());
76 1
                $this->objectManager->flush();
77
78 1
                return false;
79
            }
80
81 1
            return true;
82
        }
83
84
        return false;
85
    }
86
87
    /**
88
     * When editing the entity, create a new entity translation lock.
89
     *
90
     * @param User           $user
91
     * @param LockableEntity $entity
92
     */
93 1
    protected function createEntityVersionLock(User $user, LockableEntity $entity)
94
    {
95
        /** @var EntityVersionLock $lock */
96 1
        $lock = $this->objectManager->getRepository(EntityVersionLock::class)->findOneBy([
97 1
            'owner' => $user->getUsername(),
98 1
            'lockableEntity' => $entity,
99
        ]);
100 1
        if (!$lock) {
101
            $lock = new EntityVersionLock();
102
        }
103 1
        $lock->setOwner($user->getUsername());
104 1
        $lock->setLockableEntity($entity);
105 1
        $lock->setCreatedAt(new \DateTime());
106 1
        $this->objectManager->persist($lock);
107 1
        $this->objectManager->flush();
108 1
    }
109
110
    /**
111
     * @param LockableEntityInterface $entity
112
     * @param User                    $userToExclude
0 ignored issues
show
Should the type for parameter $userToExclude not be null|User?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
113
     *
114
     * @return array
115
     */
116 1
    public function getUsersWithEntityVersionLock(LockableEntityInterface $entity, User $userToExclude = null)
117
    {
118
        /** @var LockableEntity $lockable */
119 1
        $lockable = $this->getLockableEntity($entity);
120
121 1
        return  array_reduce(
122 1
            $this->getEntityVersionLocksByLockableEntity($lockable, $userToExclude),
123
            function ($return, EntityVersionLock $item) {
124 1
                $return[] = $item->getOwner();
125
126 1
                return $return;
127 1
            },
128 1
            []
129
        );
130
    }
131
132
    /**
133
     * @param LockableEntity $entity
134
     */
135 2
    protected function removeExpiredLocks(LockableEntity $entity)
136
    {
137 2
        $locks = $this->objectManager->getRepository(EntityVersionLock::class)->getExpiredLocks($entity, $this->threshold);
138 2
        foreach ($locks as $lock) {
139 2
            $this->objectManager->remove($lock);
140
        }
141 2
    }
142
143
    /**
144
     * When editing an entity, check if there is a lock for this entity.
145
     *
146
     * @param LockableEntity $entity
147
     * @param User           $userToExclude
148
     *
149
     * @return EntityVersionLock[]
0 ignored issues
show
Should the return type not be \Kunstmaan\AdminListBund...ory\EntityVersionLock[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
150
     */
151 3
    protected function getEntityVersionLocksByLockableEntity(LockableEntity $entity, User $userToExclude = null)
152
    {
153
        /** @var EntityVersionLockRepository $objectRepository */
154 3
        $objectRepository = $this->objectManager->getRepository(EntityVersionLock::class);
155
156 3
        return $objectRepository->getLocksForLockableEntity($entity, $this->threshold, $userToExclude);
157
    }
158
159
    /**
160
     * Get or create a LockableEntity for an entity with LockableEntityInterface
161
     *
162
     * @param LockableEntityInterface $entity
163
     *
164
     * @return LockableEntity
165
     */
166 6
    protected function getLockableEntity(LockableEntityInterface $entity, $create = true)
167
    {
168
        /** @var LockableEntity $lockable */
169 6
        $lockable = $this->objectManager->getRepository(LockableEntity::class)->getOrCreate($entity->getId(), \get_class($entity));
170
171 6
        if ($create === true && $lockable->getId() === null) {
172
            $this->objectManager->persist($lockable);
173
            $this->objectManager->flush();
174
        }
175
176 6
        return $lockable;
177
    }
178
179
    /**
180
     * @param ObjectManager $objectManager
181
     */
182 6
    public function setObjectManager($objectManager)
183
    {
184 6
        $this->objectManager = $objectManager;
185 6
    }
186
187
    /**
188
     * @param int $threshold
189
     */
190 6
    public function setThreshold($threshold)
191
    {
192 6
        $this->threshold = $threshold;
193 6
    }
194
195
    /**
196
     * @param bool lockEnabled
197
     */
198 6
    public function setLockEnabled($lockEnabled)
199
    {
200 6
        $this->lockEnabled = $lockEnabled;
201 6
    }
202
}
203