1
|
|
|
<?php namespace Comodojo\Installer\Actions; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Exception\InstallerException; |
4
|
|
|
use \Exception; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* |
8
|
|
|
* |
9
|
|
|
* @package Comodojo Framework |
10
|
|
|
* @author Marco Giovinazzi <[email protected]> |
11
|
|
|
* @author Marco Castiello <[email protected]> |
12
|
|
|
* @license GPL-3.0+ |
13
|
|
|
* |
14
|
|
|
* LICENSE: |
15
|
|
|
* |
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
17
|
|
|
* it under the terms of the GNU Affero General Public License as |
18
|
|
|
* published by the Free Software Foundation, either version 3 of the |
19
|
|
|
* License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU Affero General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU Affero General Public License |
27
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
class Authentication extends AbstractAction { |
31
|
|
|
|
32
|
|
View Code Duplication |
public function install($package_name, $package_extra) { |
|
|
|
|
33
|
|
|
|
34
|
|
|
$io = $this->getIO(); |
35
|
|
|
|
36
|
|
|
$io->write("<info>>>> Installing authentication providers from ".$package_name."</info>"); |
37
|
|
|
|
38
|
|
|
foreach ($package_extra as $provider => $configuration) { |
39
|
|
|
|
40
|
|
|
$this->addAuthProvider($io, $package_name, $provider, $configuration); |
41
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
public function update($package_name, $initial_extra, $target_extra) { |
|
|
|
|
47
|
|
|
|
48
|
|
|
$io = $this->getIO(); |
49
|
|
|
|
50
|
|
|
$io->write("<info>>>> Updating authentication providers from ".$package_name."</info>"); |
51
|
|
|
|
52
|
|
|
$old_providers = array_keys($initial_extra); |
53
|
|
|
|
54
|
|
|
$new_providers = array_keys($target_extra); |
55
|
|
|
|
56
|
|
|
$uninstall = array_diff($old_providers, $new_providers); |
57
|
|
|
|
58
|
|
|
$install = array_diff($new_providers, $old_providers); |
59
|
|
|
|
60
|
|
|
$update = array_intersect($old_providers, $new_providers); |
61
|
|
|
|
62
|
|
|
foreach ( $uninstall as $provider ) { |
63
|
|
|
|
64
|
|
|
$this->removeAuthProvider($io, $package_name, $provider, $initial_extra[$provider]); |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
foreach ( $install as $provider ) { |
69
|
|
|
|
70
|
|
|
$this->addAuthProvider($io, $package_name, $provider, $target_extra[$provider]); |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
foreach ( $update as $provider ) { |
75
|
|
|
|
76
|
|
|
$this->updateAuthProvider($io, $package_name, $provider, $target_extra[$provider]); |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function uninstall($package_name, $package_extra) { |
|
|
|
|
83
|
|
|
|
84
|
|
|
$io = $this->getIO(); |
85
|
|
|
|
86
|
|
|
$io->write("<info>>>> Removing authentication providers from ".$package_name."</info>"); |
87
|
|
|
|
88
|
|
|
foreach ($package_extra as $provider => $configuration) { |
89
|
|
|
|
90
|
|
|
$this->removeAuthProvider($io, $package_name, $provider, $configuration); |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function addAuthProvider($io, $package_name, $provider, $configuration) { |
97
|
|
|
|
98
|
|
|
try { |
99
|
|
|
|
100
|
|
|
if ( !self::validateProvider($configuration) ) throw new InstallerException('Skipping invalid authentication provider '.$provider.' in '.$package_name); |
101
|
|
|
|
102
|
|
|
$class = $configuration['class']; |
103
|
|
|
|
104
|
|
|
$description = empty($configuration['description']) ? null : $configuration['description']; |
105
|
|
|
|
106
|
|
|
$this->getPackageInstaller()->authentication()->add($package_name, $provider, $class, $description); |
107
|
|
|
|
108
|
|
|
$io->write(" <info>+</info> added authentication provider ".$provider); |
109
|
|
|
|
110
|
|
|
} catch (Exception $e) { |
111
|
|
|
|
112
|
|
|
$io->write('<error>Error processing authentication provider: '.$e->getMessage().'</error>'); |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
View Code Duplication |
private function removeAuthProvider($io, $package_name, $provider, $configuration) { |
|
|
|
|
119
|
|
|
|
120
|
|
|
try { |
121
|
|
|
|
122
|
|
|
if ( !self::validateProvider($configuration) ) throw new InstallerException('Skipping invalid authentication provider '.$provider.' in '.$package_name); |
123
|
|
|
|
124
|
|
|
$id = $this->getPackageInstaller()->authentication()->getByName($provider)->getId(); |
125
|
|
|
|
126
|
|
|
$this->getPackageInstaller()->authentication()->delete($id); |
127
|
|
|
|
128
|
|
|
$io->write(" <comment>-</comment> removed authentication provider ".$provider); |
129
|
|
|
|
130
|
|
|
} catch (Exception $e) { |
131
|
|
|
|
132
|
|
|
$io->write('<error>Error processing authentication provider: '.$e->getMessage().'</error>'); |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private function updateAuthProvider($io, $package_name, $provider, $configuration) { |
139
|
|
|
|
140
|
|
|
try { |
141
|
|
|
|
142
|
|
|
if ( !self::validateProvider($configuration) ) throw new InstallerException('Skipping invalid authentication provider '.$provider.' in '.$package_name); |
143
|
|
|
|
144
|
|
|
$class = $configuration['class']; |
145
|
|
|
|
146
|
|
|
$description = empty($configuration['description']) ? null : $configuration['description']; |
147
|
|
|
|
148
|
|
|
$id = $this->getPackageInstaller()->authentication()->getByName($provider)->getId(); |
149
|
|
|
|
150
|
|
|
$this->getPackageInstaller()->authentication()->update($id, $package_name, $provider, $class, $description); |
151
|
|
|
|
152
|
|
|
$io->write(" <comment>~</comment> updated authentication provider ".$provider); |
153
|
|
|
|
154
|
|
|
} catch (Exception $e) { |
155
|
|
|
|
156
|
|
|
$io->write('<error>Error processing authentication provider: '.$e->getMessage().'</error>'); |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
private static function validateProvider($auth) { |
163
|
|
|
|
164
|
|
|
return !( empty($auth["class"]) ); |
165
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.