Issues (60)

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.

src/services/Field.php (1 issue)

Labels
Severity

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
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organization\services;
10
11
use Craft;
12
use craft\elements\User as UserElement;
13
use craft\helpers\ArrayHelper;
14
use flipbox\organization\elements\db\Organization as OrganizationQuery;
15
use flipbox\organization\elements\Organization as OrganizationElement;
16
use flipbox\organization\fields\User as OrganizationUserField;
17
use flipbox\organization\Organization as OrganizationPlugin;
18
use flipbox\organization\records\User;
0 ignored issues
show
This use statement conflicts with another class in this namespace, flipbox\organization\services\User.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
19
use flipbox\spark\helpers\RecordHelper;
20
use yii\base\Component;
21
use yii\base\Exception;
22
23
/**
24
 * @author Flipbox Factory <[email protected]>
25
 * @since 1.0.0
26
 */
27
class Field extends Component
28
{
29
30
    /**
31
     * @param OrganizationUserField $field
32
     * @param UserElement $source
33
     * @param OrganizationQuery $target
34
     * @return bool
35
     */
36
    public function beforeSaveUserRelations(
37
        OrganizationUserField $field,
38
        UserElement $source,
39
        OrganizationQuery $target
40
    ) {
41
42
        // Check cache for explicitly set (and possibly not saved) organizations
43
        if (null !== ($targets = $target->getCachedResult())) {
44
45
            /** @var OrganizationElement $target */
46
            foreach ($targets as $target) {
47
                // New organization?
48
                if (!$target->id) {
49
                    if (!Craft::$app->getElements()->saveElement($target)) {
50
                        $source->addError(
51
                            $field->handle,
52
                            Craft::t('organization', 'Unable to save organization.')
53
                        );
54
55
                        return false;
56
                    }
57
                }
58
            }
59
        }
60
61
        return true;
62
    }
63
64
    /**
65
     * @param OrganizationUserField $field
66
     * @param UserElement $user
67
     * @param OrganizationQuery $organizationQuery
68
     * @throws \Exception
69
     * @return void
70
     */
71
    public function afterSaveUserRelations(
72
        OrganizationUserField $field,
73
        UserElement $user,
74
        OrganizationQuery $organizationQuery
75
    ) {
76
77
        /** @var OrganizationElement[]|null $targets */
78
        if (null === ($targets = $organizationQuery->getCachedResult())) {
79
            if (null !== $organizationQuery->id) {
80
                $targets = OrganizationPlugin::getInstance()->getOrganization()->getQuery([
81
                    'status' => null,
82
                    'id' => $organizationQuery->id
83
                ])->all();
84
            }
85
        }
86
87
        // Nothing to change
88
        if (null === $targets) {
89
            return;
90
        }
91
92
        $transaction = RecordHelper::beginTransaction();
93
94
        try {
95
            // Delete the existing relations
96
            $oldRelationConditions = [
97
                'and',
98
                [
99
                    'userId' => $user->id
100
                ]
101
            ];
102
103
            if ($field->localizeRelations) {
104
                $oldRelationConditions[] = [
105
                    'or',
106
                    ['siteId' => null],
107
                    ['siteId' => $user->siteId]
108
                ];
109
            }
110
111
            $oldTargetIds = User::find()
112
                ->select(['organizationId'])
113
                ->where($oldRelationConditions)
114
                ->column();
115
116
            // Find organization ids that we need to remove
117
            foreach ($targets as $organizationQuery) {
118
                ArrayHelper::remove($oldTargetIds, $organizationQuery->id);
119
            }
120
121
            /** @var OrganizationElement[] $organizations */
122
            $organizations = OrganizationPlugin::getInstance()->getOrganization()->getQuery()
123
                ->id($oldTargetIds)
124
                ->status(null)
125
                ->all();
126
127
            foreach ($organizations as $organization) {
128
                OrganizationPlugin::getInstance()->getUser()->dissociate($user, $organization);
129
            }
130
131
            // Add the new ones
132
            if (!empty($targets)) {
133
                $siteId = null;
134
135
                if ($field->localizeRelations) {
136
                    $siteId = $user->siteId;
137
                }
138
139
                foreach ($targets as $organizationQuery) {
140
                    if (!OrganizationPlugin::getInstance()->getUser()->associate(
141
                        $user,
142
                        $organizationQuery,
143
                        $siteId,
144
                        0
145
                    )) {
146
                        throw new Exception('Unable to associate user to organization');
147
                    }
148
                }
149
            }
150
151
            $transaction->commit();
152
        } catch (\Exception $e) {
153
            $transaction->rollBack();
154
155
            throw $e;
156
        }
157
    }
158
}
159