Completed
Push — staging ( d71a8f...4368fa )
by Woeler
41s queued 32s
created

app/bundles/ApiBundle/Entity/oAuth2/Client.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\ApiBundle\Entity\oAuth2;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\ORM\Mapping as ORM;
16
use FOS\OAuthServerBundle\Model\Client as BaseClient;
17
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
18
use Mautic\UserBundle\Entity\User;
19
use OAuth2\OAuth2;
20
use Symfony\Component\Validator\Constraints as Assert;
21
use Symfony\Component\Validator\Mapping\ClassMetadata;
22
23
/**
24
 * Class Client.
25
 */
26
class Client extends BaseClient
27
{
28
    /**
29
     * @var int
30
     */
31
    protected $id;
32
33
    /**
34
     * @var string
35
     */
36
    protected $name;
37
38
    /**
39
     * @var ArrayCollection
40
     */
41
    protected $users;
42
43
    /**
44
     * @var string
45
     */
46
    protected $randomId;
47
48
    /**
49
     * @var string
50
     */
51
    protected $secret;
52
53
    /**
54
     * @var array
55
     */
56
    protected $redirectUris = [];
57
58
    /**
59
     * @var array
60
     */
61
    protected $allowedGrantTypes;
62
63
    /**
64
     *  Construct.
65
     */
66
    public function __construct()
67
    {
68
        parent::__construct();
69
70
        $this->allowedGrantTypes = [
71
            OAuth2::GRANT_TYPE_AUTH_CODE,
72
            OAuth2::GRANT_TYPE_REFRESH_TOKEN,
73
        ];
74
75
        $this->users = new ArrayCollection();
76
    }
77
78
    /**
79
     * @param ORM\ClassMetadata $metadata
80
     */
81
    public static function loadMetadata(ORM\ClassMetadata $metadata)
82
    {
83
        $builder = new ClassMetadataBuilder($metadata);
84
85
        $builder->setTable('oauth2_clients')
86
            ->setCustomRepositoryClass('Mautic\ApiBundle\Entity\oAuth2\ClientRepository')
87
            ->addIndex(['random_id'], 'client_id_search');
88
89
        $builder->addIdColumns('name', false);
90
91
        $builder->createManyToMany('users', 'Mautic\UserBundle\Entity\User')
92
            ->setJoinTable('oauth2_user_client_xref')
93
            ->addInverseJoinColumn('user_id', 'id', false, false, 'CASCADE')
94
            ->addJoinColumn('client_id', 'id', false, false, 'CASCADE')
95
            ->fetchExtraLazy()
96
            ->build();
97
98
        $builder->createField('randomId', 'string')
99
            ->columnName('random_id')
100
            ->build();
101
102
        $builder->addField('secret', 'string');
103
104
        $builder->createField('redirectUris', 'array')
105
            ->columnName('redirect_uris')
106
            ->build();
107
108
        $builder->createField('allowedGrantTypes', 'array')
109
            ->columnName('allowed_grant_types')
110
            ->build();
111
    }
112
113
    /**
114
     * @param ClassMetadata $metadata
115
     */
116
    public static function loadValidatorMetadata(ClassMetadata $metadata)
117
    {
118
        $metadata->addPropertyConstraint('name', new Assert\NotBlank(
119
            ['message' => 'mautic.core.name.required']
120
        ));
121
122
        $metadata->addPropertyConstraint('redirectUris', new Assert\NotBlank(
123
            ['message' => 'mautic.api.client.redirecturis.notblank']
124
        ));
125
    }
126
127
    /**
128
     * @var array
129
     */
130
    protected $changes;
131
132
    /**
133
     * @param $prop
134
     * @param $val
135
     */
136
    protected function isChanged($prop, $val)
137
    {
138
        $getter  = 'get'.ucfirst($prop);
139
        $current = $this->$getter();
140
        if ($current != $val) {
141
            $this->changes[$prop] = [$current, $val];
142
        }
143
    }
144
145
    /**
146
     * @return array
147
     */
148
    public function getChanges()
149
    {
150
        return $this->changes;
151
    }
152
153
    /**
154
     * Get id.
155
     *
156
     * @return int
157
     */
158
    public function getId()
159
    {
160
        return $this->id;
161
    }
162
163
    /**
164
     * Set name.
165
     *
166
     * @param string $name
167
     *
168
     * @return Client
169
     */
170
    public function setName($name)
171
    {
172
        $this->isChanged('name', $name);
173
174
        $this->name = $name;
175
176
        return $this;
177
    }
178
179
    /**
180
     * Get name.
181
     *
182
     * @return string
183
     */
184
    public function getName()
185
    {
186
        return $this->name;
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function setRedirectUris(array $redirectUris)
193
    {
194
        $this->isChanged('redirectUris', $redirectUris);
195
196
        $this->redirectUris = $redirectUris;
197
    }
198
199
    /**
200
     * Add authCodes.
201
     *
202
     * @param AuthCode $authCodes
203
     *
204
     * @return Client
205
     */
206
    public function addAuthCode(AuthCode $authCodes)
207
    {
208
        $this->authCodes[] = $authCodes;
0 ignored issues
show
Bug Best Practice introduced by
The property authCodes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
209
210
        return $this;
211
    }
212
213
    /**
214
     * Remove authCodes.
215
     *
216
     * @param AuthCode $authCodes
217
     */
218
    public function removeAuthCode(AuthCode $authCodes)
219
    {
220
        $this->authCodes->removeElement($authCodes);
221
    }
222
223
    /**
224
     * Get authCodes.
225
     *
226
     * @return \Doctrine\Common\Collections\Collection
227
     */
228
    public function getAuthCodes()
229
    {
230
        return $this->authCodes;
231
    }
232
233
    /**
234
     * Determines if a client attempting API access is already authorized by the user.
235
     *
236
     * @param User $user
237
     *
238
     * @return bool
239
     */
240
    public function isAuthorizedClient(User $user)
241
    {
242
        $users = $this->getUsers();
243
244
        return $users->contains($user);
245
    }
246
247
    /**
248
     * Add users.
249
     *
250
     * @param \Mautic\UserBundle\Entity\User $users
251
     *
252
     * @return Client
253
     */
254
    public function addUser(\Mautic\UserBundle\Entity\User $users)
255
    {
256
        $this->users[] = $users;
257
258
        return $this;
259
    }
260
261
    /**
262
     * Remove users.
263
     *
264
     * @param \Mautic\UserBundle\Entity\User $users
265
     */
266
    public function removeUser(\Mautic\UserBundle\Entity\User $users)
267
    {
268
        $this->users->removeElement($users);
269
    }
270
271
    /**
272
     * Get users.
273
     *
274
     * @return \Doctrine\Common\Collections\Collection
275
     */
276
    public function getUsers()
277
    {
278
        return $this->users;
279
    }
280
}
281