@@ -34,12 +34,7 @@ public function updateForUserClass(string $yamlSource, UserClassConfiguration $u
3434 {
3535 $ this ->manipulator = new YamlSourceManipulator ($ yamlSource );
3636
37- // normalize the top level, just in case
38- if (!isset ($ this ->manipulator ->getData ()['security ' ])) {
39- $ newData = $ this ->manipulator ->getData ();
40- $ newData ['security ' ] = [];
41- $ this ->manipulator ->setData ($ newData );
42- }
37+ $ this ->normalizeSecurityYamlFile ();
4338
4439 $ this ->updateProviders ($ userConfig , $ userClass );
4540
@@ -53,6 +48,57 @@ public function updateForUserClass(string $yamlSource, UserClassConfiguration $u
5348 return $ contents ;
5449 }
5550
51+ public function updateForAuthenticator (string $ yamlSource , string $ authenticatorFQCN )
52+ {
53+ $ this ->manipulator = new YamlSourceManipulator ($ yamlSource );
54+
55+ $ this ->normalizeSecurityYamlFile ();
56+
57+ $ newData = $ this ->manipulator ->getData ();
58+
59+ if (!isset ($ newData ['security ' ]['firewalls ' ])) {
60+ $ newData ['security ' ]['firewalls ' ] = [];
61+ }
62+
63+ $ firewalls = array_filter (
64+ $ newData ['security ' ]['firewalls ' ],
65+ function ($ item ) {
66+ return !isset ($ item ['security ' ]) || true === $ item ['security ' ];
67+ }
68+ );
69+
70+ if (!$ firewalls ) {
71+ $ firewalls ['main ' ] = ['anonymous ' => true ];
72+ }
73+
74+ $ firewall = $ firewalls ['main ' ];
75+ if (!isset ($ firewall ['guard ' ])) {
76+ $ firewall ['guard ' ] = [];
77+ }
78+
79+ if (!isset ($ firewall ['guard ' ]['authenticators ' ])) {
80+ $ firewall ['guard ' ]['authenticators ' ] = [];
81+ }
82+
83+ $ firewall ['guard ' ]['authenticators ' ][] = $ authenticatorFQCN ;
84+
85+ $ newData ['security ' ]['firewalls ' ]['main ' ] = $ firewall ;
86+ $ this ->manipulator ->setData ($ newData );
87+
88+ $ contents = $ this ->manipulator ->getContents ();
89+
90+ return $ contents ;
91+ }
92+
93+ private function normalizeSecurityYamlFile ()
94+ {
95+ if (!isset ($ this ->manipulator ->getData ()['security ' ])) {
96+ $ newData = $ this ->manipulator ->getData ();
97+ $ newData ['security ' ] = [];
98+ $ this ->manipulator ->setData ($ newData );
99+ }
100+ }
101+
56102 private function updateProviders (UserClassConfiguration $ userConfig , string $ userClass )
57103 {
58104 if ($ this ->isSingleInMemoryProviderConfigured ()) {
0 commit comments