1212use OCP \AppFramework \Http ;
1313use OCP \AppFramework \Http \Attribute \NoAdminRequired ;
1414use OCP \AppFramework \Http \DataResponse ;
15- use OCP \IConfig ;
15+ use OCP \Encryption \ Exceptions \ GenericEncryptionException ;
1616use OCP \IL10N ;
1717use OCP \IRequest ;
18+ use Psr \Log \LoggerInterface ;
1819
1920class RecoveryController extends Controller {
20- /**
21- * @param string $AppName
22- * @param IRequest $request
23- * @param IConfig $config
24- * @param IL10N $l
25- * @param Recovery $recovery
26- */
2721 public function __construct (
28- $ appName ,
22+ string $ appName ,
2923 IRequest $ request ,
30- private IConfig $ config ,
3124 private IL10N $ l ,
3225 private Recovery $ recovery ,
26+ private LoggerInterface $ logger ,
3327 ) {
3428 parent ::__construct ($ appName , $ request );
3529 }
3630
37- /**
38- * @param string $recoveryPassword
39- * @param string $confirmPassword
40- * @param string $adminEnableRecovery
41- * @return DataResponse
42- */
43- public function adminRecovery ($ recoveryPassword , $ confirmPassword , $ adminEnableRecovery ) {
31+ public function adminRecovery (string $ recoveryPassword , string $ confirmPassword , bool $ adminEnableRecovery ): DataResponse {
4432 // Check if both passwords are the same
4533 if (empty ($ recoveryPassword )) {
4634 $ errorMessage = $ this ->l ->t ('Missing recovery key password ' );
@@ -60,28 +48,28 @@ public function adminRecovery($recoveryPassword, $confirmPassword, $adminEnableR
6048 Http::STATUS_BAD_REQUEST );
6149 }
6250
63- if (isset ($ adminEnableRecovery ) && $ adminEnableRecovery === '1 ' ) {
64- if ($ this ->recovery ->enableAdminRecovery ($ recoveryPassword )) {
65- return new DataResponse (['data ' => ['message ' => $ this ->l ->t ('Recovery key successfully enabled ' )]]);
51+ try {
52+ if ($ adminEnableRecovery ) {
53+ if ($ this ->recovery ->enableAdminRecovery ($ recoveryPassword )) {
54+ return new DataResponse (['data ' => ['message ' => $ this ->l ->t ('Recovery key successfully enabled ' )]]);
55+ }
56+ return new DataResponse (['data ' => ['message ' => $ this ->l ->t ('Could not enable recovery key. Please check your recovery key password! ' )]], Http::STATUS_BAD_REQUEST );
57+ } else {
58+ if ($ this ->recovery ->disableAdminRecovery ($ recoveryPassword )) {
59+ return new DataResponse (['data ' => ['message ' => $ this ->l ->t ('Recovery key successfully disabled ' )]]);
60+ }
61+ return new DataResponse (['data ' => ['message ' => $ this ->l ->t ('Could not disable recovery key. Please check your recovery key password! ' )]], Http::STATUS_BAD_REQUEST );
6662 }
67- return new DataResponse ([ ' data ' => [ ' message ' => $ this -> l -> t ( ' Could not enable recovery key. Please check your recovery key password! ' )]], Http:: STATUS_BAD_REQUEST );
68- } elseif ( isset ( $ adminEnableRecovery ) && $ adminEnableRecovery === ' 0 ' ) {
69- if ($ this -> recovery -> disableAdminRecovery ( $ recoveryPassword ) ) {
70- return new DataResponse (['data ' => ['message ' => $ this -> l -> t ( ' Recovery key successfully disabled ' )]]);
63+ } catch ( \ Exception $ e ) {
64+ $ this -> logger -> error ( ' Error enabling or disabling recovery key ' , [ ' exception ' => $ e ]);
65+ if ($ e instanceof GenericEncryptionException ) {
66+ return new DataResponse (['data ' => ['message ' => $ e -> getMessage ( )]], Http:: STATUS_INTERNAL_SERVER_ERROR );
7167 }
72- return new DataResponse ([' data ' => [ ' message ' => $ this -> l -> t ( ' Could not disable recovery key. Please check your recovery key password! ' )]] , Http::STATUS_BAD_REQUEST );
68+ return new DataResponse ([] , Http::STATUS_INTERNAL_SERVER_ERROR );
7369 }
74- // this response should never be sent but just in case.
75- return new DataResponse (['data ' => ['message ' => $ this ->l ->t ('Missing parameters ' )]], Http::STATUS_BAD_REQUEST );
7670 }
7771
78- /**
79- * @param string $newPassword
80- * @param string $oldPassword
81- * @param string $confirmPassword
82- * @return DataResponse
83- */
84- public function changeRecoveryPassword ($ newPassword , $ oldPassword , $ confirmPassword ) {
72+ public function changeRecoveryPassword (string $ newPassword , string $ oldPassword , string $ confirmPassword ): DataResponse {
8573 //check if both passwords are the same
8674 if (empty ($ oldPassword )) {
8775 $ errorMessage = $ this ->l ->t ('Please provide the old recovery password ' );
@@ -103,23 +91,30 @@ public function changeRecoveryPassword($newPassword, $oldPassword, $confirmPassw
10391 return new DataResponse (['data ' => ['message ' => $ errorMessage ]], Http::STATUS_BAD_REQUEST );
10492 }
10593
106- $ result = $ this ->recovery ->changeRecoveryKeyPassword ($ newPassword ,
107- $ oldPassword );
94+ try {
95+ $ result = $ this ->recovery ->changeRecoveryKeyPassword ($ newPassword ,
96+ $ oldPassword );
10897
109- if ($ result ) {
110- return new DataResponse (
111- [
112- 'data ' => [
113- 'message ' => $ this ->l ->t ('Password successfully changed. ' )]
114- ]
115- );
116- }
117- return new DataResponse (
118- [
98+ if ($ result ) {
99+ return new DataResponse (
100+ [
101+ 'data ' => [
102+ 'message ' => $ this ->l ->t ('Password successfully changed. ' )]
103+ ]
104+ );
105+ }
106+ return new DataResponse ([
119107 'data ' => [
120108 'message ' => $ this ->l ->t ('Could not change the password. Maybe the old password was not correct. ' )
121109 ]
122110 ], Http::STATUS_BAD_REQUEST );
111+ } catch (\Exception $ e ) {
112+ $ this ->logger ->error ('Error changing recovery password ' , ['exception ' => $ e ]);
113+ if ($ e instanceof GenericEncryptionException) {
114+ return new DataResponse (['data ' => ['message ' => $ e ->getMessage ()]], Http::STATUS_INTERNAL_SERVER_ERROR );
115+ }
116+ return new DataResponse ([], Http::STATUS_INTERNAL_SERVER_ERROR );
117+ }
123118 }
124119
125120 /**
0 commit comments