2828use OC \DB \ConnectionFactory ;
2929use OCA \UserSQL \Cache ;
3030use OCA \UserSQL \Constant \App ;
31+ use OCA \UserSQL \Constant \Opt ;
32+ use OCA \UserSQL \Crypto \IPasswordAlgorithm ;
3133use OCA \UserSQL \Platform \PlatformFactory ;
3234use OCA \UserSQL \Properties ;
3335use OCP \AppFramework \Controller ;
3436use OCP \IL10N ;
3537use OCP \ILogger ;
3638use OCP \IRequest ;
39+ use ReflectionClass ;
40+ use ReflectionException ;
3741
3842/**
3943 * The settings controller.
@@ -72,7 +76,8 @@ class SettingsController extends Controller
7276 public function __construct (
7377 $ appName , IRequest $ request , ILogger $ logger , IL10N $ localization ,
7478 Properties $ properties , Cache $ cache
75- ) {
79+ )
80+ {
7681 parent ::__construct ($ appName , $ request );
7782 $ this ->appName = $ appName ;
7883 $ this ->logger = $ logger ;
@@ -193,6 +198,16 @@ public function saveProperties()
193198 ];
194199 }
195200
201+ if (!$ this ->validateCryptoParams ()) {
202+ return [
203+ "status " => "error " , "data " => [
204+ "message " => $ this ->localization ->t (
205+ "Hash algorithm parameter is out of range. "
206+ )
207+ ]
208+ ];
209+ }
210+
196211 foreach ($ properties as $ key => $ value ) {
197212 $ reqValue = $ this ->request ->getParam (str_replace (". " , "- " , $ key ));
198213 $ appValue = $ this ->properties [$ key ];
@@ -208,6 +223,9 @@ public function saveProperties()
208223 "Property ' $ key' has been set to: " . $ value ,
209224 ["app " => $ this ->appName ]
210225 );
226+ } elseif (!is_bool ($ appValue ) && !isset ($ reqValue )) {
227+ unset($ this ->properties [$ key ]);
228+
211229 }
212230 }
213231
@@ -225,6 +243,48 @@ public function saveProperties()
225243 ];
226244 }
227245
246+ /**
247+ * Validate request crypto params.
248+ *
249+ * @return bool TRUE if crypto params are correct FALSE otherwise.
250+ */
251+ private function validateCryptoParams ()
252+ {
253+ $ cryptoClass = $ this ->request ->getParam ("opt-crypto_class " );
254+ $ configuration = $ this ->cryptoClassConfiguration ($ cryptoClass );
255+
256+ for ($ i = 0 ; $ i < count ($ configuration ); ++$ i ) {
257+ $ reqParam = $ this ->request ->getParam (
258+ "opt-crypto_param_ " . $ i , null
259+ );
260+ $ cryptoParam = $ configuration [$ i ];
261+
262+ if (is_null ($ reqParam ) || $ reqParam < $ cryptoParam ->min
263+ || $ reqParam > $ cryptoParam ->max
264+ ) {
265+ return false ;
266+ }
267+ }
268+
269+ return true ;
270+ }
271+
272+ /**
273+ * Get a crypto class configuration from request.
274+ *
275+ * @param $cryptoClass string Crypto class name.
276+ *
277+ * @return array A crypto class configuration.
278+ */
279+ private function cryptoClassConfiguration ($ cryptoClass )
280+ {
281+ /**
282+ * @var $passwordAlgorithm IPasswordAlgorithm
283+ */
284+ $ passwordAlgorithm = new $ cryptoClass ($ this ->localization );
285+ return $ passwordAlgorithm ->configuration ();
286+ }
287+
228288 /**
229289 * Clear the application cache memory.
230290 *
@@ -367,4 +427,40 @@ public function groupTableAutocomplete()
367427
368428 return $ columns ;
369429 }
430+
431+ /**
432+ * Get parameters for a password algorithm.
433+ *
434+ * @return array Password algorithm parameters.
435+ * @throws ReflectionException Whenever Opt class cannot be initiated.
436+ */
437+ public function cryptoParams ()
438+ {
439+ $ this ->logger ->debug (
440+ "Entering cryptoParams() " , ["app " => $ this ->appName ]
441+ );
442+
443+ $ cryptoClass = $ this ->request ->getParam ("cryptoClass " );
444+ $ configuration = $ this ->cryptoClassConfiguration ($ cryptoClass );
445+
446+ if ($ cryptoClass === $ this ->properties [Opt::CRYPTO_CLASS ]) {
447+ foreach ($ configuration as $ key => $ value ) {
448+ $ opt = new ReflectionClass ("OCA\UserSQL\Constant\Opt " );
449+ $ param = $ this ->properties [$ opt ->getConstant (
450+ "CRYPTO_PARAM_ " . $ key
451+ )];
452+
453+ if (!is_null ($ param )) {
454+ $ value ->value = $ param ;
455+ }
456+ }
457+ }
458+
459+ $ this ->logger ->debug (
460+ "Returning cryptoParams(): count( " . count ($ configuration ) . ") " ,
461+ ["app " => $ this ->appName ]
462+ );
463+
464+ return ["status " => "success " , "data " => (array )$ configuration ];
465+ }
370466}
0 commit comments