forked from EC-CUBE/TwoFactorAuthCustomer42
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginManager.php
186 lines (163 loc) · 5.58 KB
/
PluginManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\TwoFactorAuthCustomer42;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Entity\Layout;
use Eccube\Entity\Page;
use Eccube\Entity\PageLayout;
use Eccube\Plugin\AbstractPluginManager;
use Plugin\TwoFactorAuthCustomer42\Entity\TwoFactorAuthConfig;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem;
/**
* Class PluginManager.
*/
class PluginManager extends AbstractPluginManager
{
// 設定対象ページ情報
private $pages = [
['plg_customer_2fa_device_auth_send_onetime', 'デバイス認証送信先入力', 'TwoFactorAuthCustomer42/Resource/template/default/device_auth/send'],
['plg_customer_2fa_device_auth_input_onetime', 'デバイス認証トークン入力', 'TwoFactorAuthCustomer42/Resource/template/default/device_auth/input'],
['plg_customer_2fa_auth_type_select', '多要素認証方式選択', 'TwoFactorAuthCustomer42/Resource/template/default/tfa/select_type'],
];
/**
* @param array $meta
* @param ContainerInterface $container
*/
public function enable(array $meta, ContainerInterface $container)
{
$em = $container->get('doctrine')->getManager();
$this->createConfig($em);
// twigファイルを追加
$this->copyTwigFiles($container);
// ページ登録
$this->createPages($em);
}
/**
* 設定の登録.
*
* @param EntityManagerInterface $em
*/
protected function createConfig(EntityManagerInterface $em)
{
$TwoFactorAuthConfig = $em->find(TwoFactorAuthConfig::class, 1);
if ($TwoFactorAuthConfig) {
return;
}
// 初期値を保存
$TwoFactorAuthConfig = new TwoFactorAuthConfig();
$em->persist($TwoFactorAuthConfig);
$em->flush();
}
/**
* Twigファイルの登録
*
* @param ContainerInterface $container
*/
protected function copyTwigFiles(ContainerInterface $container)
{
// テンプレートファイルコピー
$templatePath = $container->getParameter('eccube_theme_front_dir')
. '/TwoFactorAuthCustomer42/Resource/template/default';
$fs = new Filesystem();
if ($fs->exists($templatePath)) {
return;
}
$fs->mkdir($templatePath);
$fs->mirror(__DIR__ . '/Resource/template/default', $templatePath);
}
/**
* ページ情報の登録
*
* @param EntityManagerInterface $em
*/
protected function createPages(EntityManagerInterface $em)
{
foreach ($this->pages as $p) {
$Page = $em->getRepository(Page::class)->findOneBy(['url' => $p[0]]);
if (!$Page) {
/** @var Page $Page */
$Page = $em->getRepository(Page::class)->newPage();
$Page->setEditType(Page::EDIT_TYPE_DEFAULT);
$Page->setUrl($p[0]);
$Page->setName($p[1]);
$Page->setFileName($p[2]);
$Page->setMetaRobots('noindex');
$em->persist($Page);
$em->flush();
$Layout = $em->getRepository(Layout::class)->find(Layout::DEFAULT_LAYOUT_UNDERLAYER_PAGE);
$PageLayout = new PageLayout();
$PageLayout->setPage($Page)
->setPageId($Page->getId())
->setLayout($Layout)
->setLayoutId($Layout->getId())
->setSortNo(0);
$em->persist($PageLayout);
$em->flush();
}
}
}
/**
* @param array $meta
* @param ContainerInterface $container
*/
public function disable(array $meta, ContainerInterface $container)
{
$em = $container->get('doctrine')->getManager();
// twigファイルを削除
$this->removeTwigFiles($container);
// ページ削除
$this->removePages($em);
}
/**
* Twigファイルの削除
*
* @param ContainerInterface $container
*/
protected function removeTwigFiles(ContainerInterface $container)
{
$templatePath = $container->getParameter('eccube_theme_front_dir')
. '/TwoFactorAuthCustomer42';
$fs = new Filesystem();
$fs->remove($templatePath);
}
/**
* ページ情報の削除
*
* @param EntityManagerInterface $em
*/
protected function removePages(EntityManagerInterface $em)
{
foreach ($this->pages as $p) {
$Page = $em->getRepository(Page::class)->findOneBy(['url' => $p[0]]);
if ($Page !== null) {
$Layout = $em->getRepository(Layout::class)->find(Layout::DEFAULT_LAYOUT_UNDERLAYER_PAGE);
$PageLayout = $em->getRepository(PageLayout::class)->findOneBy(['Page' => $Page, 'Layout' => $Layout]);
$em->remove($PageLayout);
$em->remove($Page);
$em->flush();
}
}
}
/**
* @param array $meta
* @param ContainerInterface $container
*/
public function uninstall(array $meta, ContainerInterface $container)
{
$em = $container->get('doctrine')->getManager();
// twigファイルを削除
$this->removeTwigFiles($container);
// ページ削除
$this->removePages($em);
}
}