1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Socializer plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* @link https://enupal.com/ |
6
|
|
|
* @copyright Copyright (c) 2019 Enupal LLC |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace enupal\socializer\services; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\elements\User; |
13
|
|
|
use enupal\socializer\elements\Provider; |
14
|
|
|
use enupal\socializer\elements\Token; |
15
|
|
|
use enupal\socializer\Socializer; |
16
|
|
|
use yii\base\Component; |
17
|
|
|
|
18
|
|
|
class Tokens extends Component |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param int $providerId |
22
|
|
|
* @param int $userId |
23
|
|
|
* @return array|\craft\base\ElementInterface|null |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
public function getToken(int $providerId, int $userId) |
26
|
|
|
{ |
27
|
|
|
$query = Token::find(); |
28
|
|
|
$query->providerId = $providerId; |
29
|
|
|
$query->userId = $userId; |
30
|
|
|
|
31
|
|
|
return $query->one(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param User $user |
36
|
|
|
* @param Provider $provider |
37
|
|
|
* @return bool |
38
|
|
|
* @throws \Throwable |
39
|
|
|
* @throws \craft\errors\ElementNotFoundException |
40
|
|
|
* @throws \yii\base\Exception |
41
|
|
|
*/ |
42
|
|
|
public function registerToken(User $user, Provider $provider) |
43
|
|
|
{ |
44
|
|
|
$adapter = $provider->getAdapter(); |
45
|
|
|
$token = Socializer::$app->tokens->getToken($provider->id, $user->id); |
46
|
|
|
|
47
|
|
|
if (is_null($token)){ |
48
|
|
|
$token = new Token(); |
49
|
|
|
$token->providerId = $provider->id; |
50
|
|
|
$token->userId = $user->id; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$token->accessToken = $adapter->getAccessToken(); |
|
|
|
|
54
|
|
|
|
55
|
|
|
if (!Craft::$app->elements->saveElement($token)) { |
|
|
|
|
56
|
|
|
Craft::error("Unable to save token: ".json_encode($token->getErrors()), __METHOD__); |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param int $providerId |
65
|
|
|
* @return array|\craft\base\ElementInterface|null |
66
|
|
|
*/ |
67
|
|
|
public function getCurrentUserToken(int $providerId) |
68
|
|
|
{ |
69
|
|
|
$user = Craft::$app->getUser()->getIdentity(); |
70
|
|
|
|
71
|
|
|
if ($user){ |
|
|
|
|
72
|
|
|
/** @var Token $token */ |
73
|
|
|
$token = $this->getToken($providerId, $user->id); |
|
|
|
|
74
|
|
|
if ($token){ |
|
|
|
|
75
|
|
|
return $token->accessToken; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths