github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/api4/plugin.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  // EXPERIMENTAL - SUBJECT TO CHANGE
     5  
     6  package api4
     7  
     8  import (
     9  	"net/http"
    10  
    11  	"github.com/mattermost/mattermost-server/mlog"
    12  	"github.com/mattermost/mattermost-server/model"
    13  )
    14  
    15  const (
    16  	MAXIMUM_PLUGIN_FILE_SIZE = 50 * 1024 * 1024
    17  )
    18  
    19  func (api *API) InitPlugin() {
    20  	mlog.Debug("EXPERIMENTAL: Initializing plugin api")
    21  
    22  	api.BaseRoutes.Plugins.Handle("", api.ApiSessionRequired(uploadPlugin)).Methods("POST")
    23  	api.BaseRoutes.Plugins.Handle("", api.ApiSessionRequired(getPlugins)).Methods("GET")
    24  	api.BaseRoutes.Plugin.Handle("", api.ApiSessionRequired(removePlugin)).Methods("DELETE")
    25  
    26  	api.BaseRoutes.Plugins.Handle("/statuses", api.ApiSessionRequired(getPluginStatuses)).Methods("GET")
    27  	api.BaseRoutes.Plugin.Handle("/enable", api.ApiSessionRequired(enablePlugin)).Methods("POST")
    28  	api.BaseRoutes.Plugin.Handle("/disable", api.ApiSessionRequired(disablePlugin)).Methods("POST")
    29  
    30  	api.BaseRoutes.Plugins.Handle("/webapp", api.ApiHandler(getWebappPlugins)).Methods("GET")
    31  }
    32  
    33  func uploadPlugin(c *Context, w http.ResponseWriter, r *http.Request) {
    34  	if !*c.App.Config().PluginSettings.Enable || !*c.App.Config().PluginSettings.EnableUploads {
    35  		c.Err = model.NewAppError("uploadPlugin", "app.plugin.upload_disabled.app_error", nil, "", http.StatusNotImplemented)
    36  		return
    37  	}
    38  
    39  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
    40  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    41  		return
    42  	}
    43  
    44  	if err := r.ParseMultipartForm(MAXIMUM_PLUGIN_FILE_SIZE); err != nil {
    45  		http.Error(w, err.Error(), http.StatusBadRequest)
    46  		return
    47  	}
    48  
    49  	m := r.MultipartForm
    50  
    51  	pluginArray, ok := m.File["plugin"]
    52  	if !ok {
    53  		c.Err = model.NewAppError("uploadPlugin", "api.plugin.upload.no_file.app_error", nil, "", http.StatusBadRequest)
    54  		return
    55  	}
    56  
    57  	if len(pluginArray) <= 0 {
    58  		c.Err = model.NewAppError("uploadPlugin", "api.plugin.upload.array.app_error", nil, "", http.StatusBadRequest)
    59  		return
    60  	}
    61  
    62  	file, err := pluginArray[0].Open()
    63  	if err != nil {
    64  		c.Err = model.NewAppError("uploadPlugin", "api.plugin.upload.file.app_error", nil, "", http.StatusBadRequest)
    65  		return
    66  	}
    67  	defer file.Close()
    68  
    69  	manifest, unpackErr := c.App.InstallPlugin(file, false)
    70  
    71  	if unpackErr != nil {
    72  		c.Err = unpackErr
    73  		return
    74  	}
    75  
    76  	w.WriteHeader(http.StatusCreated)
    77  	w.Write([]byte(manifest.ToJson()))
    78  }
    79  
    80  func getPlugins(c *Context, w http.ResponseWriter, r *http.Request) {
    81  	if !*c.App.Config().PluginSettings.Enable {
    82  		c.Err = model.NewAppError("getPlugins", "app.plugin.disabled.app_error", nil, "", http.StatusNotImplemented)
    83  		return
    84  	}
    85  
    86  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
    87  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    88  		return
    89  	}
    90  
    91  	response, err := c.App.GetPlugins()
    92  	if err != nil {
    93  		c.Err = err
    94  		return
    95  	}
    96  
    97  	w.Write([]byte(response.ToJson()))
    98  }
    99  
   100  func getPluginStatuses(c *Context, w http.ResponseWriter, r *http.Request) {
   101  	if !*c.App.Config().PluginSettings.Enable {
   102  		c.Err = model.NewAppError("getPluginStatuses", "app.plugin.disabled.app_error", nil, "", http.StatusNotImplemented)
   103  		return
   104  	}
   105  
   106  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
   107  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   108  		return
   109  	}
   110  
   111  	response, err := c.App.GetClusterPluginStatuses()
   112  	if err != nil {
   113  		c.Err = err
   114  		return
   115  	}
   116  
   117  	w.Write([]byte(response.ToJson()))
   118  }
   119  
   120  func removePlugin(c *Context, w http.ResponseWriter, r *http.Request) {
   121  	c.RequirePluginId()
   122  	if c.Err != nil {
   123  		return
   124  	}
   125  
   126  	if !*c.App.Config().PluginSettings.Enable {
   127  		c.Err = model.NewAppError("removePlugin", "app.plugin.disabled.app_error", nil, "", http.StatusNotImplemented)
   128  		return
   129  	}
   130  
   131  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
   132  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   133  		return
   134  	}
   135  
   136  	err := c.App.RemovePlugin(c.Params.PluginId)
   137  	if err != nil {
   138  		c.Err = err
   139  		return
   140  	}
   141  
   142  	ReturnStatusOK(w)
   143  }
   144  
   145  func getWebappPlugins(c *Context, w http.ResponseWriter, r *http.Request) {
   146  	if !*c.App.Config().PluginSettings.Enable {
   147  		c.Err = model.NewAppError("getWebappPlugins", "app.plugin.disabled.app_error", nil, "", http.StatusNotImplemented)
   148  		return
   149  	}
   150  
   151  	manifests, err := c.App.GetActivePluginManifests()
   152  	if err != nil {
   153  		c.Err = err
   154  		return
   155  	}
   156  
   157  	clientManifests := []*model.Manifest{}
   158  	for _, m := range manifests {
   159  		if m.HasClient() {
   160  			clientManifests = append(clientManifests, m.ClientManifest())
   161  		}
   162  	}
   163  
   164  	w.Write([]byte(model.ManifestListToJson(clientManifests)))
   165  }
   166  
   167  func enablePlugin(c *Context, w http.ResponseWriter, r *http.Request) {
   168  	c.RequirePluginId()
   169  	if c.Err != nil {
   170  		return
   171  	}
   172  
   173  	if !*c.App.Config().PluginSettings.Enable {
   174  		c.Err = model.NewAppError("activatePlugin", "app.plugin.disabled.app_error", nil, "", http.StatusNotImplemented)
   175  		return
   176  	}
   177  
   178  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
   179  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   180  		return
   181  	}
   182  
   183  	if err := c.App.EnablePlugin(c.Params.PluginId); err != nil {
   184  		c.Err = err
   185  		return
   186  	}
   187  
   188  	ReturnStatusOK(w)
   189  }
   190  
   191  func disablePlugin(c *Context, w http.ResponseWriter, r *http.Request) {
   192  	c.RequirePluginId()
   193  	if c.Err != nil {
   194  		return
   195  	}
   196  
   197  	if !*c.App.Config().PluginSettings.Enable {
   198  		c.Err = model.NewAppError("deactivatePlugin", "app.plugin.disabled.app_error", nil, "", http.StatusNotImplemented)
   199  		return
   200  	}
   201  
   202  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
   203  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   204  		return
   205  	}
   206  
   207  	if err := c.App.DisablePlugin(c.Params.PluginId); err != nil {
   208  		c.Err = err
   209  		return
   210  	}
   211  
   212  	ReturnStatusOK(w)
   213  }