|
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\db\Query; |
|
13
|
|
|
use yii\base\Component; |
|
14
|
|
|
use enupal\socializer\models\Settings as SettingsModel; |
|
15
|
|
|
use enupal\socializer\Socializer; |
|
16
|
|
|
|
|
17
|
|
|
class Settings extends Component |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Saves Settings |
|
21
|
|
|
* |
|
22
|
|
|
* @param $scenario |
|
23
|
|
|
* @param $settings SettingsModel |
|
24
|
|
|
* |
|
25
|
|
|
* @return bool |
|
26
|
|
|
*/ |
|
27
|
|
|
public function saveSettings(SettingsModel $settings, $scenario = null): bool |
|
28
|
|
|
{ |
|
29
|
|
|
$plugin = $this->getPlugin(); |
|
30
|
|
|
|
|
31
|
|
|
if (!is_null($scenario)) { |
|
32
|
|
|
$settings->setScenario($scenario); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// Validate them, now that it's a model |
|
36
|
|
|
if ($settings->validate() === false) { |
|
37
|
|
|
return false; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$success = Craft::$app->getPlugins()->savePluginSettings($plugin, $settings->getAttributes()); |
|
41
|
|
|
|
|
42
|
|
|
return $success; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return bool|string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getPrimarySiteUrl() |
|
49
|
|
|
{ |
|
50
|
|
|
$primarySite = (new Query()) |
|
51
|
|
|
->select(['baseUrl']) |
|
52
|
|
|
->from(['{{%sites}}']) |
|
53
|
|
|
->where(['primary' => 1]) |
|
54
|
|
|
->one(); |
|
55
|
|
|
|
|
56
|
|
|
$primarySiteUrl = Craft::getAlias($primarySite['baseUrl']); |
|
57
|
|
|
|
|
58
|
|
|
return Craft::parseEnv(Craft::getAlias(rtrim(trim($primarySiteUrl), "/"))); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getCallbackUrl() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->getPrimarySiteUrl()."/socializer/login/callback"; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return SettingsModel |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getSettings() |
|
73
|
|
|
{ |
|
74
|
|
|
/** @var SettingsModel $settings */ |
|
75
|
|
|
$settings = $this->getPlugin()->getSettings(); |
|
76
|
|
|
|
|
77
|
|
|
return $settings; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return \craft\base\PluginInterface|null |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getPlugin() |
|
84
|
|
|
{ |
|
85
|
|
|
return Craft::$app->getPlugins()->getPlugin('enupal-socializer'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return string|null |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getPluginUid() |
|
92
|
|
|
{ |
|
93
|
|
|
$plugin = (new Query()) |
|
94
|
|
|
->select(['uid']) |
|
95
|
|
|
->from('{{%plugins}}') |
|
96
|
|
|
->where(["handle" => 'enupal-socializer']) |
|
97
|
|
|
->one(); |
|
98
|
|
|
|
|
99
|
|
|
return $plugin['uid'] ?? null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getGlobalFieldMapping() |
|
106
|
|
|
{ |
|
107
|
|
|
$settings = $this->getSettings(); |
|
108
|
|
|
|
|
109
|
|
|
return $settings->fieldMapping ?? Socializer::$app->providers->getDefaultFieldMapping(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return array|\craft\models\UserGroup[] |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getUserGroups() |
|
116
|
|
|
{ |
|
117
|
|
|
$userGroups = [ |
|
118
|
|
|
["name" => "None", "id" => ""] |
|
119
|
|
|
]; |
|
120
|
|
|
$craftUserGroups = Craft::$app->getUserGroups()->getAllGroups(); |
|
121
|
|
|
|
|
122
|
|
|
$userGroups = array_merge($userGroups, $craftUserGroups); |
|
123
|
|
|
|
|
124
|
|
|
return $userGroups; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return array|null |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getConfigSettings() |
|
131
|
|
|
{ |
|
132
|
|
|
return Craft::$app->config->getGeneral()->socializer ?? null; |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function validateAppleSettings() |
|
136
|
|
|
{ |
|
137
|
|
|
$config = $this->getConfigSettings(); |
|
138
|
|
|
|
|
139
|
|
|
if (!isset($config['apple'])) { |
|
140
|
|
|
Craft::error('Apple config is not set', __METHOD__); |
|
141
|
|
|
return false; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$apple = $config['apple']; |
|
145
|
|
|
|
|
146
|
|
|
if (!isset($apple['keys']['id']) || !isset($apple['keys']['team_id']) || |
|
147
|
|
|
!isset($apple['keys']['key_id']) || |
|
148
|
|
|
!isset($apple['keys']['key_file']) || !isset($apple['scope']) || |
|
149
|
|
|
!isset($apple['verifyTokenSignature'])) { |
|
150
|
|
|
Craft::error('Missing a required Apple config, please check our docs.', __METHOD__); |
|
151
|
|
|
return false; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
if (!file_exists($apple['keys']['key_file'])) { |
|
155
|
|
|
Craft::error('Unable to find Apple key file: '.$apple['keys']['key_file'], __METHOD__); |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return true; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|