From 16b845c0d77535ea306339f7a8bd22fc72f8a3c5 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Wed, 25 Oct 2017 08:17:17 -0400 Subject: Differentiate between installed and activated states for plugins (#7706) --- api4/plugin.go | 57 ++++++++++++++++++++++++++++++++++++++--- api4/plugin_test.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 120 insertions(+), 10 deletions(-) (limited to 'api4') diff --git a/api4/plugin.go b/api4/plugin.go index 2045a35a8..c1ee986da 100644 --- a/api4/plugin.go +++ b/api4/plugin.go @@ -24,6 +24,9 @@ func (api *API) InitPlugin() { api.BaseRoutes.Plugins.Handle("", api.ApiSessionRequired(getPlugins)).Methods("GET") api.BaseRoutes.Plugin.Handle("", api.ApiSessionRequired(removePlugin)).Methods("DELETE") + api.BaseRoutes.Plugin.Handle("/activate", api.ApiSessionRequired(activatePlugin)).Methods("POST") + api.BaseRoutes.Plugin.Handle("/deactivate", api.ApiSessionRequired(deactivatePlugin)).Methods("POST") + api.BaseRoutes.Plugins.Handle("/webapp", api.ApiHandler(getWebappPlugins)).Methods("GET") } @@ -64,7 +67,7 @@ func uploadPlugin(c *Context, w http.ResponseWriter, r *http.Request) { } defer file.Close() - manifest, unpackErr := c.App.UnpackAndActivatePlugin(file) + manifest, unpackErr := c.App.InstallPlugin(file) if unpackErr != nil { c.Err = unpackErr @@ -86,13 +89,13 @@ func getPlugins(c *Context, w http.ResponseWriter, r *http.Request) { return } - manifests, err := c.App.GetActivePluginManifests() + response, err := c.App.GetPluginManifests() if err != nil { c.Err = err return } - w.Write([]byte(model.ManifestListToJson(manifests))) + w.Write([]byte(response.ToJson())) } func removePlugin(c *Context, w http.ResponseWriter, r *http.Request) { @@ -141,3 +144,51 @@ func getWebappPlugins(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(model.ManifestListToJson(clientManifests))) } + +func activatePlugin(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequirePluginId() + if c.Err != nil { + return + } + + if !*c.App.Config().PluginSettings.Enable { + c.Err = model.NewAppError("activatePlugin", "app.plugin.disabled.app_error", nil, "", http.StatusNotImplemented) + return + } + + if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) { + c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) + return + } + + if err := c.App.EnablePlugin(c.Params.PluginId); err != nil { + c.Err = err + return + } + + ReturnStatusOK(w) +} + +func deactivatePlugin(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequirePluginId() + if c.Err != nil { + return + } + + if !*c.App.Config().PluginSettings.Enable { + c.Err = model.NewAppError("deactivatePlugin", "app.plugin.disabled.app_error", nil, "", http.StatusNotImplemented) + return + } + + if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) { + c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) + return + } + + if err := c.App.DisablePlugin(c.Params.PluginId); err != nil { + c.Err = err + return + } + + ReturnStatusOK(w) +} diff --git a/api4/plugin_test.go b/api4/plugin_test.go index 1feb1b06a..a3f3bd49a 100644 --- a/api4/plugin_test.go +++ b/api4/plugin_test.go @@ -65,12 +65,43 @@ func TestPlugin(t *testing.T) { _, resp = th.Client.UploadPlugin(file) CheckForbiddenStatus(t, resp) - // Successful get - manifests, resp := th.SystemAdminClient.GetPlugins() + // Successful gets + pluginsResp, resp := th.SystemAdminClient.GetPlugins() CheckNoError(t, resp) found := false - for _, m := range manifests { + for _, m := range pluginsResp.Inactive { + if m.Id == manifest.Id { + found = true + } + } + + assert.True(t, found) + + found = false + for _, m := range pluginsResp.Active { + if m.Id == manifest.Id { + found = true + } + } + + assert.False(t, found) + + states := th.App.Config().PluginSettings.PluginStates + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.PluginSettings.PluginStates = states }) + }() + + // Successful activate + ok, resp := th.SystemAdminClient.ActivatePlugin(manifest.Id) + CheckNoError(t, resp) + assert.True(t, ok) + + pluginsResp, resp = th.SystemAdminClient.GetPlugins() + CheckNoError(t, resp) + + found = false + for _, m := range pluginsResp.Active { if m.Id == manifest.Id { found = true } @@ -78,6 +109,33 @@ func TestPlugin(t *testing.T) { assert.True(t, found) + // Activate error case + ok, resp = th.SystemAdminClient.ActivatePlugin("junk") + CheckBadRequestStatus(t, resp) + assert.False(t, ok) + + // Successful deactivate + ok, resp = th.SystemAdminClient.DeactivatePlugin(manifest.Id) + CheckNoError(t, resp) + assert.True(t, ok) + + pluginsResp, resp = th.SystemAdminClient.GetPlugins() + CheckNoError(t, resp) + + found = false + for _, m := range pluginsResp.Inactive { + if m.Id == manifest.Id { + found = true + } + } + + assert.True(t, found) + + // Deactivate error case + ok, resp = th.SystemAdminClient.DeactivatePlugin("junk") + CheckBadRequestStatus(t, resp) + assert.False(t, ok) + // Get error cases th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = false }) _, resp = th.SystemAdminClient.GetPlugins() @@ -88,7 +146,10 @@ func TestPlugin(t *testing.T) { CheckForbiddenStatus(t, resp) // Successful webapp get - manifests, resp = th.Client.GetWebappPlugins() + _, resp = th.SystemAdminClient.ActivatePlugin(manifest.Id) + CheckNoError(t, resp) + + manifests, resp := th.Client.GetWebappPlugins() CheckNoError(t, resp) found = false @@ -101,15 +162,13 @@ func TestPlugin(t *testing.T) { assert.True(t, found) // Successful remove - ok, resp := th.SystemAdminClient.RemovePlugin(manifest.Id) + ok, resp = th.SystemAdminClient.RemovePlugin(manifest.Id) CheckNoError(t, resp) - assert.True(t, ok) // Remove error cases ok, resp = th.SystemAdminClient.RemovePlugin(manifest.Id) CheckBadRequestStatus(t, resp) - assert.False(t, ok) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = false }) -- cgit v1.2.3-1-g7c22