Issues (55)

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.

OrganizationsAssociatedToUserBehavior.php (1 issue)

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 flipbox\organizations\behaviors;
4
5
use Craft;
6
use craft\elements\User;
7
use craft\events\ModelEvent;
8
use flipbox\craft\ember\helpers\QueryHelper;
9
use flipbox\organizations\elements\Organization;
10
use flipbox\organizations\relationships\RelationshipInterface;
11
use flipbox\organizations\relationships\OrganizationRelationship;
12
use flipbox\organizations\Organizations as OrganizationPlugin;
13
use flipbox\organizations\queries\OrganizationQuery;
14
use flipbox\organizations\validators\OrganizationsValidator;
15
use Tightenco\Collect\Support\Collection;
16
use yii\base\Behavior;
17
use yii\base\Event;
18
use yii\base\Exception;
19
20
/**
21
 * Class UserOrganizationsBehavior
22
 * @package flipbox\organizations\behaviors
23
 *
24
 * @property User $owner;
25
 */
26
class OrganizationsAssociatedToUserBehavior extends Behavior
27
{
28
    /**
29
     * @var RelationshipInterface
30
     */
31
    private $manager;
32
33
    /**
34
     * Whether associated organizations should be saved
35
     *
36
     * @var bool
37
     */
38
    private $saveOrganizations = true;
39
40
    /**
41
     * @return static
42
     */
43
    public function withOrganizations(): self
44
    {
45
        $this->saveOrganizations = true;
46
        return $this;
47
    }
48
49
    /**
50
     * @return static
51
     */
52
    public function withoutUsers(): self
53
    {
54
        $this->saveOrganizations = false;
55
        return $this;
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function init()
62
    {
63
        parent::init();
64
65
        // Validate organizations
66
        Event::on(
67
            User::class,
68
            User::EVENT_AFTER_VALIDATE,
69
            function (Event $e) {
70
                /** @var User $user */
71
                $user = $e->sender;
72
                $this->onAfterValidate($user);
73
            }
74
        );
75
76
        // Associate
77
        Event::on(
78
            User::class,
79
            User::EVENT_AFTER_SAVE,
80
            function (ModelEvent $e) {
81
                /** @var User $user */
82
                $user = $e->sender;
83
                $this->onAfterSave($user);
84
            }
85
        );
86
87
        // Dissociate
88
        Event::on(
89
            User::class,
90
            User::EVENT_AFTER_DELETE,
91
            function (Event $e) {
92
                /** @var User $user */
93
                $user = $e->sender;
94
                $this->onAfterDelete($user);
95
            }
96
        );
97
    }
98
99
    /**
100
     * @param User $user
101
     * @return void
102
     * @throws \Throwable
103
     */
104
    private function onAfterDelete(User $user)
105
    {
106
        /** @var static $user */
107
        $user->getOrganizations()
108
            ->clear()
109
            ->save();
110
    }
111
112
    /**
113
     * @param User|self $user
114
     * @throws Exception
115
     * @throws \Exception
116
     * @throws \Throwable
117
     * @throws \craft\errors\ElementNotFoundException
118
     */
119
    private function onAfterSave(User $user)
120
    {
121
        // Check cache for explicitly set (and possibly not saved) organizations
122
        if (true === $this->saveOrganizations && $user->getOrganizations()->isMutated()) {
123
124
            /** @var Organization $organization */
125
            foreach ($user->getOrganizations()->getCollection() as $organization) {
126
                if (null === $organization->getId()) {
127
                    if (!Craft::$app->getElements()->saveElement($organization)) {
128
                        $user->addError(
129
                            'organizations',
130
                            OrganizationPlugin::t('Unable to save organization.')
131
                        );
132
133
                        throw new Exception('Unable to save organization.');
134
                    }
135
                }
136
            }
137
138
            $user->getOrganizations()->save();
139
        }
140
    }
141
142
    /**
143
     * @param User|self $user
144
     * @return void
145
     */
146
    private function onAfterValidate(User $user)
147
    {
148
        (new OrganizationsValidator())->validateAttribute($user, 'organizations');
149
    }
150
151
    /**
152
     * Get an array of associated organizations
153
     *
154
     * @return OrganizationRelationship|RelationshipInterface
155
     */
156
    public function getOrganizations(): RelationshipInterface
157
    {
158
        if (null === $this->manager) {
159
            $this->manager = new OrganizationRelationship($this->owner);
0 ignored issues
show
$this->owner is of type object<yii\base\Component>|null, but the function expects a object<craft\elements\User>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
160
        }
161
162
        return $this->manager;
163
    }
164
165
    /**
166
     * Set an array or query of organizations to a user
167
     *
168
     * @param $organizations
169
     * @return $this
170
     */
171
    public function setOrganizations($organizations)
172
    {
173
        $this->getOrganizations()->clear()->add($organizations);
174
        return $this;
175
    }
176
}
177