@@ -28,6 +28,7 @@ import axios from '@nextcloud/axios'
2828import { generateUrl } from '@nextcloud/router'
2929import { showError , showInfo } from '@nextcloud/dialogs'
3030import { loadState } from '@nextcloud/initial-state'
31+ import { PwdConfirmationMode } from '@nextcloud/password-confirmation'
3132
3233const state = {
3334 apps : [ ] ,
@@ -186,58 +187,82 @@ const actions = {
186187 } else {
187188 apps = [ appId ]
188189 }
189- return api . requireAdmin ( ) . then ( ( response ) => {
190- context . commit ( 'startLoading' , apps )
191- context . commit ( 'startLoading' , 'install' )
192- return api . post ( generateUrl ( 'settings/apps/enable' ) , { appIds : apps , groups } )
193- . then ( ( response ) => {
194- context . commit ( 'stopLoading' , apps )
195- context . commit ( 'stopLoading' , 'install' )
196- apps . forEach ( _appId => {
197- context . commit ( 'enableApp' , { appId : _appId , groups } )
190+ context . commit ( 'startLoading' , apps )
191+ context . commit ( 'startLoading' , 'install' )
192+
193+ const previousState = { }
194+ apps . forEach ( ( _appId ) => {
195+ const app = context . state . apps . find ( ( app ) => app . id === _appId )
196+ if ( app ) {
197+ previousState [ _appId ] = {
198+ active : app . active ,
199+ groups : [ ...( app . groups || [ ] ) ] ,
200+ }
201+ context . commit ( 'enableApp' , { appId : _appId , groups } )
202+ }
203+ } )
204+
205+ return api . post ( generateUrl ( 'settings/apps/enable' ) , { appIds : apps , groups } , { confirmPassword : PwdConfirmationMode . Strict } )
206+ . then ( ( response ) => {
207+ context . commit ( 'stopLoading' , apps )
208+ context . commit ( 'stopLoading' , 'install' )
209+
210+ // check for server health
211+ return axios . get ( generateUrl ( 'apps/files/' ) )
212+ . then ( ( ) => {
213+ if ( response . data . update_required ) {
214+ showInfo (
215+ t (
216+ 'settings' ,
217+ 'The app has been enabled but needs to be updated. You will be redirected to the update page in 5 seconds.' ,
218+ ) ,
219+ {
220+ onClick : ( ) => window . location . reload ( ) ,
221+ close : false ,
222+
223+ } ,
224+ )
225+ setTimeout ( function ( ) {
226+ location . reload ( )
227+ } , 5000 )
228+ }
229+ } )
230+ . catch ( ( ) => {
231+ if ( ! Array . isArray ( appId ) ) {
232+ showError ( t ( 'settings' , 'Error: This app cannot be enabled because it makes the server unstable' ) )
233+ context . commit ( 'setError' , {
234+ appId : apps ,
235+ error : t ( 'settings' , 'Error: This app cannot be enabled because it makes the server unstable' ) ,
236+ } )
237+ context . dispatch ( 'disableApp' , { appId } )
238+ }
198239 } )
240+ } )
241+ . catch ( ( error ) => {
242+ context . commit ( 'stopLoading' , apps )
243+ context . commit ( 'stopLoading' , 'install' )
199244
200- // check for server health
201- return axios . get ( generateUrl ( 'apps/files/' ) )
202- . then ( ( ) => {
203- if ( response . data . update_required ) {
204- showInfo (
205- t (
206- 'settings' ,
207- 'The app has been enabled but needs to be updated. You will be redirected to the update page in 5 seconds.' ,
208- ) ,
209- {
210- onClick : ( ) => window . location . reload ( ) ,
211- close : false ,
212-
213- } ,
214- )
215- setTimeout ( function ( ) {
216- location . reload ( )
217- } , 5000 )
218- }
219- } )
220- . catch ( ( ) => {
221- if ( ! Array . isArray ( appId ) ) {
222- showError ( t ( 'settings' , 'Error: This app cannot be enabled because it makes the server unstable' ) )
223- context . commit ( 'setError' , {
224- appId : apps ,
225- error : t ( 'settings' , 'Error: This app cannot be enabled because it makes the server unstable' ) ,
226- } )
227- context . dispatch ( 'disableApp' , { appId } )
228- }
245+ apps . forEach ( ( _appId ) => {
246+ if ( previousState [ _appId ] ) {
247+ context . commit ( 'enableApp' , {
248+ appId : _appId ,
249+ groups : previousState [ _appId ] . groups ,
229250 } )
251+ if ( ! previousState [ _appId ] . active ) {
252+ context . commit ( 'disableApp' , _appId )
253+ }
254+ }
230255 } )
231- . catch ( ( error ) => {
232- context . commit ( 'stopLoading' , apps )
233- context . commit ( 'stopLoading' , 'install' )
256+
257+ const message = error . response ?. data ?. data ?. message
258+ if ( message ) {
234259 context . commit ( 'setError' , {
235260 appId : apps ,
236- error : error . response . data . data . message ,
261+ error : message ,
237262 } )
238263 context . commit ( 'APPS_API_FAILURE' , { appId, error } )
239- } )
240- } ) . catch ( ( error ) => context . commit ( 'API_FAILURE' , { appId , error } ) )
264+ }
265+ } )
241266 } ,
242267 forceEnableApp ( context , { appId, groups } ) {
243268 let apps
0 commit comments