99
1010use OCP \Cache \CappedMemoryCache ;
1111use OCP \DB \QueryBuilder \IQueryBuilder ;
12- use OCP \IConfig ;
12+ use OCP \IAppConfig ;
1313use OCP \IDBConnection ;
1414use OCP \Server ;
1515
@@ -18,7 +18,7 @@ class Helper {
1818 protected CappedMemoryCache $ sanitizeDnCache ;
1919
2020 public function __construct (
21- private IConfig $ config ,
21+ private IAppConfig $ appConfig ,
2222 private IDBConnection $ connection ,
2323 ) {
2424 $ this ->sanitizeDnCache = new CappedMemoryCache (10000 );
@@ -45,21 +45,37 @@ public function __construct(
4545 * except the default (first) server shall be connected to.
4646 *
4747 */
48- public function getServerConfigurationPrefixes ($ activeConfigurations = false ): array {
48+ public function getServerConfigurationPrefixes (bool $ activeConfigurations = false ): array {
49+ $ all = $ this ->getAllServerConfigurationPrefixes ();
50+ if (!$ activeConfigurations ) {
51+ return $ all ;
52+ }
53+ return array_values (array_filter (
54+ $ all ,
55+ fn (string $ prefix ): bool => ($ this ->appConfig ->getValueString ('user_ldap ' , $ prefix . 'ldap_configuration_active ' ) === '1 ' )
56+ ));
57+ }
58+
59+ protected function getAllServerConfigurationPrefixes (): array {
60+ $ unfilled = ['UNFILLED ' ];
61+ $ prefixes = $ this ->appConfig ->getValueArray ('user_ldap ' , 'configuration_prefixes ' , $ unfilled );
62+ if ($ prefixes !== $ unfilled ) {
63+ return $ prefixes ;
64+ }
65+
66+ /* Fallback to browsing key for migration from Nextcloud<32 */
4967 $ referenceConfigkey = 'ldap_configuration_active ' ;
5068
5169 $ keys = $ this ->getServersConfig ($ referenceConfigkey );
5270
5371 $ prefixes = [];
5472 foreach ($ keys as $ key ) {
55- if ($ activeConfigurations && $ this ->config ->getAppValue ('user_ldap ' , $ key , '0 ' ) !== '1 ' ) {
56- continue ;
57- }
58-
5973 $ len = strlen ($ key ) - strlen ($ referenceConfigkey );
6074 $ prefixes [] = substr ($ key , 0 , $ len );
6175 }
62- asort ($ prefixes );
76+ sort ($ prefixes );
77+
78+ $ this ->appConfig ->setValueArray ('user_ldap ' , 'configuration_prefixes ' , $ prefixes );
6379
6480 return $ prefixes ;
6581 }
@@ -68,46 +84,45 @@ public function getServerConfigurationPrefixes($activeConfigurations = false): a
6884 *
6985 * determines the host for every configured connection
7086 *
71- * @return array an array with configprefix as keys
87+ * @return array<string,string> an array with configprefix as keys
7288 *
7389 */
74- public function getServerConfigurationHosts () {
75- $ referenceConfigkey = 'ldap_host ' ;
76-
77- $ keys = $ this ->getServersConfig ($ referenceConfigkey );
90+ public function getServerConfigurationHosts (): array {
91+ $ prefixes = $ this ->getServerConfigurationPrefixes ();
7892
93+ $ referenceConfigkey = 'ldap_host ' ;
7994 $ result = [];
80- foreach ($ keys as $ key ) {
81- $ len = strlen ($ key ) - strlen ($ referenceConfigkey );
82- $ prefix = substr ($ key , 0 , $ len );
83- $ result [$ prefix ] = $ this ->config ->getAppValue ('user_ldap ' , $ key );
95+ foreach ($ prefixes as $ prefix ) {
96+ $ result [$ prefix ] = $ this ->appConfig ->getValueString ('user_ldap ' , $ prefix . $ referenceConfigkey );
8497 }
8598
8699 return $ result ;
87100 }
88101
89102 /**
90- * return the next available configuration prefix
91- *
92- * @return string
103+ * return the next available configuration prefix and register it as used
93104 */
94- public function getNextServerConfigurationPrefix () {
95- $ serverConnections = $ this ->getServerConfigurationPrefixes ();
96-
97- if (count ($ serverConnections ) === 0 ) {
98- return 's01 ' ;
105+ public function getNextServerConfigurationPrefix (): string {
106+ $ prefixes = $ this ->getServerConfigurationPrefixes ();
107+
108+ if (count ($ prefixes ) === 0 ) {
109+ $ prefix = 's01 ' ;
110+ } else {
111+ sort ($ prefixes );
112+ $ lastKey = array_pop ($ prefixes );
113+ $ lastNumber = (int )str_replace ('s ' , '' , $ lastKey );
114+ $ prefix = 's ' . str_pad ((string )($ lastNumber + 1 ), 2 , '0 ' , STR_PAD_LEFT );
99115 }
100116
101- sort ($ serverConnections );
102- $ lastKey = array_pop ($ serverConnections );
103- $ lastNumber = (int )str_replace ('s ' , '' , $ lastKey );
104- return 's ' . str_pad ((string )($ lastNumber + 1 ), 2 , '0 ' , STR_PAD_LEFT );
117+ $ prefixes [] = $ prefix ;
118+ $ this ->appConfig ->setValueArray ('user_ldap ' , 'configuration_prefixes ' , $ prefixes );
119+ return $ prefix ;
105120 }
106121
107122 private function getServersConfig (string $ value ): array {
108123 $ regex = '/ ' . $ value . '$/S ' ;
109124
110- $ keys = $ this ->config -> getAppKeys ('user_ldap ' );
125+ $ keys = $ this ->appConfig -> getKeys ('user_ldap ' );
111126 $ result = [];
112127 foreach ($ keys as $ key ) {
113128 if (preg_match ($ regex , $ key ) === 1 ) {
@@ -125,7 +140,9 @@ private function getServersConfig(string $value): array {
125140 * @return bool true on success, false otherwise
126141 */
127142 public function deleteServerConfiguration ($ prefix ) {
128- if (!in_array ($ prefix , self ::getServerConfigurationPrefixes ())) {
143+ $ prefixes = $ this ->getServerConfigurationPrefixes ();
144+ $ index = array_search ($ prefix , $ prefixes );
145+ if ($ index === false ) {
129146 return false ;
130147 }
131148
@@ -144,18 +161,25 @@ public function deleteServerConfiguration($prefix) {
144161 $ query ->andWhere ($ query ->expr ()->notLike ('configkey ' , $ query ->createNamedParameter ('s% ' )));
145162 }
146163
147- $ deletedRows = $ query ->execute ();
164+ $ deletedRows = $ query ->executeStatement ();
165+
166+ unset($ prefixes [$ index ]);
167+ $ this ->appConfig ->setValueArray ('user_ldap ' , 'configuration_prefixes ' , array_values ($ prefixes ));
168+
148169 return $ deletedRows !== 0 ;
149170 }
150171
151172 /**
152173 * checks whether there is one or more disabled LDAP configurations
153174 */
154175 public function haveDisabledConfigurations (): bool {
155- $ all = $ this ->getServerConfigurationPrefixes (false );
156- $ active = $ this ->getServerConfigurationPrefixes (true );
157-
158- return count ($ all ) !== count ($ active ) || count ($ all ) === 0 ;
176+ $ all = $ this ->getServerConfigurationPrefixes ();
177+ foreach ($ all as $ prefix ) {
178+ if ($ this ->appConfig ->getValueString ('user_ldap ' , $ prefix . 'ldap_configuration_active ' ) !== '1 ' ) {
179+ return true ;
180+ }
181+ }
182+ return false ;
159183 }
160184
161185 /**
0 commit comments